-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Try: Add metadata attribute to blocks allowing section naming and future semantic meta information #40393
Try: Add metadata attribute to blocks allowing section naming and future semantic meta information #40393
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* External dependencies | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
import { has } from 'lodash'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* WordPress dependencies | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
import { addFilter } from '@wordpress/hooks'; | ||||||||||||||||||||||||
import { hasBlockSupport } from '@wordpress/blocks'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* Filters registered block settings, extending attributes to include `metadata` in blocks declaring section support. | ||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* @param {Object} blockTypeSettings Original block settings. | ||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* @return {Object} Filtered block settings. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
export function addAttribute( blockTypeSettings ) { | ||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||
hasBlockSupport( blockTypeSettings, '__experimentalMetadata', false ) | ||||||||||||||||||||||||
) { | ||||||||||||||||||||||||
// Allow blocks to specify their own metadata attribute definition with default value if needed. | ||||||||||||||||||||||||
if ( ! has( blockTypeSettings.attributes, [ 'metadata' ] ) ) { | ||||||||||||||||||||||||
blockTypeSettings.attributes = { | ||||||||||||||||||||||||
...blockTypeSettings.attributes, | ||||||||||||||||||||||||
metadata: { | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the difference with the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The settings attribute is a global styles settings object like the presets of a block, and other settings you can define using theme.json. It is equivalent to the styles object. The metadata is for information specific to a block instance that is not part of global styles/theme.json like the name of a specific section. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is though a good question (whether these could merge or not). I'm not sure I have any preference right now. They do seem to have different purposes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm really worried that we overuse We use the block metadata phrase when talking about everything that For settings the best way to show the challenge is by showing the implementation: gutenberg/packages/block-editor/src/hooks/settings.js Lines 16 to 23 in fa456ec
In effect, There are also existing blocks that contain similar types of attributes that don't represent the content or visual aspects of the block and are included as regular attributes:
I agree that it doesn't have too much difference from the implementation perspective and it's perfectly fine to proceed as is with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just wanted to say that this has been on my mind since yesterday as well. And not only is
I agree, @gziolo, that - if ( ! settings?.attributes?.settings ) {
- settings.attributes = {
- ...settings.attributes,
+ if ( ! blockTypeSettings?.attributes?.settings ) {
+ blockTypeSettings.attributes = {
+ ...blockTypeSettings.attributes,
settings: {
type: 'object',
}, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The two certain things is "name" and somekind of "tag" or "area" or "keyword" or something like that to add "semantics" to the given section/block. (better surface related patterns...). I think "locking" could be moved in that same object. Potentially other things can come later. This comment is relevant here #34352 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's true that having all the existing block settings and the new Other than that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Theme.json has styles and settings it seems natural that blocks also have styles and settings attributes. We don't have any other attribute for settings. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should follow @mcsf and use blockTypeSettings as a name to make things clearer. This PR already does that, I will update the settings hook. |
||||||||||||||||||||||||
type: 'object', | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return blockTypeSettings; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
addFilter( | ||||||||||||||||||||||||
'blocks.registerBlockType', | ||||||||||||||||||||||||
'core/metadata/addAttribute', | ||||||||||||||||||||||||
addAttribute | ||||||||||||||||||||||||
); |
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.
With the change of intent in this PR (moving away from referring to "section"), should this also be reflected in the phrasing/naming in tests? I think leaving as is could be confusing for those reading tests to understand expected behaviour.