-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
custom-sources-backwards-compatibility.js
125 lines (114 loc) · 3.54 KB
/
custom-sources-backwards-compatibility.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { useMemo } from '@wordpress/element';
import { createHigherOrderComponent } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { store as editorStore } from '../store';
/** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */
/** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */
/**
* Object whose keys are the names of block attributes, where each value
* represents the meta key to which the block attribute is intended to save.
*
* @see https://developer.wordpress.org/reference/functions/register_meta/
*
* @typedef {Object<string,string>} WPMetaAttributeMapping
*/
/**
* Given a mapping of attribute names (meta source attributes) to their
* associated meta key, returns a higher order component that overrides its
* `attributes` and `setAttributes` props to sync any changes with the edited
* post's meta keys.
*
* @param {WPMetaAttributeMapping} metaAttributes Meta attribute mapping.
*
* @return {WPHigherOrderComponent} Higher-order component.
*/
const createWithMetaAttributeSource = ( metaAttributes ) =>
createHigherOrderComponent(
( BlockEdit ) =>
( { attributes, setAttributes, ...props } ) => {
const postType = useSelect(
( select ) => select( editorStore ).getCurrentPostType(),
[]
);
const [ meta, setMeta ] = useEntityProp(
'postType',
postType,
'meta'
);
const mergedAttributes = useMemo(
() => ( {
...attributes,
...Object.fromEntries(
Object.entries( metaAttributes ).map(
( [ attributeKey, metaKey ] ) => [
attributeKey,
meta[ metaKey ],
]
)
),
} ),
[ attributes, meta ]
);
return (
<BlockEdit
attributes={ mergedAttributes }
setAttributes={ ( nextAttributes ) => {
const nextMeta = Object.fromEntries(
Object.entries( nextAttributes ?? {} )
.filter(
// Filter to intersection of keys between the updated
// attributes and those with an associated meta key.
( [ key ] ) => key in metaAttributes
)
.map( ( [ attributeKey, value ] ) => [
// Rename the keys to the expected meta key name.
metaAttributes[ attributeKey ],
value,
] )
);
if ( Object.entries( nextMeta ).length ) {
setMeta( nextMeta );
}
setAttributes( nextAttributes );
} }
{ ...props }
/>
);
},
'withMetaAttributeSource'
);
/**
* Filters a registered block's settings to enhance a block's `edit` component
* to upgrade meta-sourced attributes to use the post's meta entity property.
*
* @param {WPBlockSettings} settings Registered block settings.
*
* @return {WPBlockSettings} Filtered block settings.
*/
function shimAttributeSource( settings ) {
/** @type {WPMetaAttributeMapping} */
const metaAttributes = Object.fromEntries(
Object.entries( settings.attributes ?? {} )
.filter( ( [ , { source } ] ) => source === 'meta' )
.map( ( [ attributeKey, { meta } ] ) => [ attributeKey, meta ] )
);
if ( Object.entries( metaAttributes ).length ) {
settings.edit = createWithMetaAttributeSource( metaAttributes )(
settings.edit
);
}
return settings;
}
addFilter(
'blocks.registerBlockType',
'core/editor/custom-sources-backwards-compatibility/shim-attribute-source',
shimAttributeSource
);