-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blocks: Ensure that metadata registered on the server for core block is preserved on the client (try 2) #29302
Merged
gziolo
merged 2 commits into
master
from
revert-29279-revert-29213-update/register-block-server-metadata
Feb 26, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Gutenberg Test Register Block Type Hooks | ||
* Plugin URI: https://github.com/WordPress/gutenberg | ||
* Author: Gutenberg Team | ||
* | ||
* @package gutenberg-test-register-block-type-hooks | ||
*/ | ||
|
||
/** | ||
* Changes the category for the paragraph block. | ||
* | ||
* @param array $metadata Array of metadata for registering a block type. | ||
* | ||
* @return array Filtered metadata for registering a block type. | ||
*/ | ||
function gutenberg_test_block_type_metadata( $metadata ) { | ||
if ( 'core/paragraph' !== $metadata['name'] ) { | ||
return $metadata; | ||
} | ||
|
||
return array_merge( | ||
$metadata, | ||
array( 'category' => 'widgets' ) | ||
); | ||
} | ||
|
||
add_filter( 'block_type_metadata', 'gutenberg_test_block_type_metadata' ); |
32 changes: 32 additions & 0 deletions
32
packages/e2e-tests/specs/editor/plugins/register-block-type-hooks.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
activatePlugin, | ||
createNewPost, | ||
deactivatePlugin, | ||
openGlobalBlockInserter, | ||
} from '@wordpress/e2e-test-utils'; | ||
|
||
describe( 'Register block type hooks', () => { | ||
beforeEach( async () => { | ||
await activatePlugin( 'gutenberg-test-register-block-type-hooks' ); | ||
await createNewPost(); | ||
} ); | ||
|
||
afterEach( async () => { | ||
await deactivatePlugin( 'gutenberg-test-register-block-type-hooks' ); | ||
} ); | ||
|
||
it( 'has a custom category for Paragraph block', async () => { | ||
await openGlobalBlockInserter(); | ||
|
||
const widgetsCategory = await page.$( | ||
'.block-editor-block-types-list[aria-label="Widgets"]' | ||
); | ||
|
||
expect( | ||
await widgetsCategory.$( '.editor-block-list-item-paragraph' ) | ||
).toBeDefined(); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I spotted a few more differences on the server definition:
editorStyle
supports
, get expanded in blockattributes
(I think this is okay)style
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth setting up a test case with all block registration items set and then compare to see what's returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment it's used only on the server to enqueue CSS files, for example:
https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/block-api/block-metadata.md#editor-style
In the future, we might need it on the client to lazy load CSS or JS files for the block.
Edit: In fact, those fields were used in the past for installing blocks from Block Directory, but it was replaced with a different approach in #24117. However, in practice, Block Directory uses its own endpoint to get the block metadata which is further processed
block.json
file:gutenberg/packages/block-directory/src/store/resolvers.js
Lines 24 to 26 in 8ab2453
Yes, it's used quite extensively for features built-in in the block editor like alignment, custom class name, anchor, fonts, colors, and other stuff that Global Styles offer. Most (hopefully all) of the hooks that add those fields for
attributes
respect the version created by developers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, sounds like this should be relatively safe to try again. ✨