-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds block supports for metadata (#43986)
* Adds metadata supports * Correct spelling Co-authored-by: Ben Dwyer <ben@scruffian.com> * Add explanatory comment Co-authored-by: Ben Dwyer <ben@scruffian.com> Co-authored-by: Jorge Costa <jorge.costa@developer.pt>
- Loading branch information
1 parent
dff616f
commit 9e08674
Showing
4 changed files
with
139 additions
and
0 deletions.
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,48 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { hasBlockMetadataSupport } from './metadata'; | ||
|
||
/** | ||
* Filters registered block settings, adding an `__experimentalLabel` callback if one does not already exist. | ||
* | ||
* @param {Object} settings Original block settings. | ||
* | ||
* @return {Object} Filtered block settings. | ||
*/ | ||
export function addLabelCallback( settings ) { | ||
// If blocks provide their own label callback, do not override it. | ||
if ( settings.__experimentalLabel ) { | ||
return settings; | ||
} | ||
|
||
const supportsBlockNaming = hasBlockMetadataSupport( | ||
settings, | ||
'name', | ||
false // default value | ||
); | ||
|
||
// Check whether block metadata is supported before using it. | ||
if ( supportsBlockNaming ) { | ||
settings.__experimentalLabel = ( attributes, { context } ) => { | ||
const { metadata } = attributes; | ||
|
||
// In the list view, use the block's name attribute as the label. | ||
if ( context === 'list-view' && metadata?.name ) { | ||
return metadata.name; | ||
} | ||
}; | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
addFilter( | ||
'blocks.registerBlockType', | ||
'core/metadata/addLabelCallback', | ||
addLabelCallback | ||
); |
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,64 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { getBlockSupport } from '@wordpress/blocks'; | ||
|
||
const META_ATTRIBUTE_NAME = 'metadata'; | ||
|
||
export function hasBlockMetadataSupport( blockType, feature = '' ) { | ||
const support = getBlockSupport( blockType, '__experimentalMetadata' ); | ||
return !! ( true === support || support?.[ feature ] ); | ||
} | ||
|
||
/** | ||
* Filters registered block settings, extending attributes to include `metadata`. | ||
* | ||
* see: https://github.com/WordPress/gutenberg/pull/40393/files#r864632012 | ||
* | ||
* @param {Object} blockTypeSettings Original block settings. | ||
* @return {Object} Filtered block settings. | ||
*/ | ||
export function addMetaAttribute( blockTypeSettings ) { | ||
// Allow blocks to specify their own attribute definition with default values if needed. | ||
if ( blockTypeSettings?.attributes?.[ META_ATTRIBUTE_NAME ]?.type ) { | ||
return blockTypeSettings; | ||
} | ||
|
||
const supportsBlockNaming = hasBlockMetadataSupport( | ||
blockTypeSettings, | ||
'name', | ||
false | ||
); | ||
|
||
if ( supportsBlockNaming ) { | ||
blockTypeSettings.attributes = { | ||
...blockTypeSettings.attributes, | ||
[ META_ATTRIBUTE_NAME ]: { | ||
type: 'object', | ||
}, | ||
}; | ||
} | ||
|
||
return blockTypeSettings; | ||
} | ||
|
||
export function addSaveProps( extraProps, blockType, attributes ) { | ||
if ( hasBlockMetadataSupport( blockType ) ) { | ||
extraProps[ META_ATTRIBUTE_NAME ] = attributes[ META_ATTRIBUTE_NAME ]; | ||
} | ||
|
||
return extraProps; | ||
} | ||
|
||
addFilter( | ||
'blocks.registerBlockType', | ||
'core/metadata/addMetaAttribute', | ||
addMetaAttribute | ||
); | ||
|
||
addFilter( | ||
'blocks.getSaveContent.extraProps', | ||
'core/metadata/save-props', | ||
addSaveProps | ||
); |