-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
metadata.js
67 lines (57 loc) · 1.72 KB
/
metadata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { getBlockSupport } from '@wordpress/blocks';
const META_ATTRIBUTE_NAME = 'metadata';
export function hasBlockMetadataSupport( blockType, feature = '' ) {
// Only core blocks are allowed to use __experimentalMetadata until the fetaure is stablised.
if ( ! blockType.name.startsWith( 'core/' ) ) {
return false;
}
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'
);
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
);