@wordpress/block-editor 14.3.3 → 14.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/build/components/media-placeholder/index.js +17 -19
- package/build/components/media-placeholder/index.js.map +1 -1
- package/build/components/rich-text/index.js +4 -8
- package/build/components/rich-text/index.js.map +1 -1
- package/build/hooks/block-bindings.js +6 -13
- package/build/hooks/block-bindings.js.map +1 -1
- package/build/hooks/use-bindings-attributes.js +5 -4
- package/build/hooks/use-bindings-attributes.js.map +1 -1
- package/build/private-apis.js +0 -2
- package/build/private-apis.js.map +1 -1
- package/build/utils/block-bindings.js +48 -0
- package/build/utils/block-bindings.js.map +1 -1
- package/build/utils/index.js +7 -0
- package/build/utils/index.js.map +1 -1
- package/build-module/components/media-placeholder/index.js +18 -20
- package/build-module/components/media-placeholder/index.js.map +1 -1
- package/build-module/components/rich-text/index.js +4 -8
- package/build-module/components/rich-text/index.js.map +1 -1
- package/build-module/hooks/block-bindings.js +6 -13
- package/build-module/hooks/block-bindings.js.map +1 -1
- package/build-module/hooks/use-bindings-attributes.js +5 -4
- package/build-module/hooks/use-bindings-attributes.js.map +1 -1
- package/build-module/private-apis.js +0 -2
- package/build-module/private-apis.js.map +1 -1
- package/build-module/utils/block-bindings.js +48 -0
- package/build-module/utils/block-bindings.js.map +1 -1
- package/build-module/utils/index.js +1 -0
- package/build-module/utils/index.js.map +1 -1
- package/build-style/content-rtl.css +2 -14
- package/build-style/content.css +2 -14
- package/package.json +6 -6
- package/src/components/media-placeholder/content.scss +3 -19
- package/src/components/media-placeholder/index.js +17 -17
- package/src/components/rich-text/index.js +4 -13
- package/src/hooks/block-bindings.js +9 -10
- package/src/hooks/use-bindings-attributes.js +66 -63
- package/src/private-apis.js +0 -2
- package/src/utils/block-bindings.js +47 -0
- package/src/utils/index.js +1 -0
|
@@ -118,79 +118,81 @@ export const withBlockBindingSupport = createHigherOrderComponent(
|
|
|
118
118
|
// there are attribute updates.
|
|
119
119
|
// `source.getValues` may also call a selector via `registry.select`.
|
|
120
120
|
const updatedContext = {};
|
|
121
|
-
const boundAttributes = useSelect(
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
121
|
+
const boundAttributes = useSelect(
|
|
122
|
+
( select ) => {
|
|
123
|
+
if ( ! blockBindings ) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
125
126
|
|
|
126
|
-
|
|
127
|
+
const attributes = {};
|
|
127
128
|
|
|
128
|
-
|
|
129
|
+
const blockBindingsBySource = new Map();
|
|
129
130
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
131
|
+
for ( const [ attributeName, binding ] of Object.entries(
|
|
132
|
+
blockBindings
|
|
133
|
+
) ) {
|
|
134
|
+
const { source: sourceName, args: sourceArgs } = binding;
|
|
135
|
+
const source = sources[ sourceName ];
|
|
136
|
+
if (
|
|
137
|
+
! source ||
|
|
138
|
+
! canBindAttribute( name, attributeName )
|
|
139
|
+
) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
138
142
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
+
// Populate context.
|
|
144
|
+
for ( const key of source.usesContext || [] ) {
|
|
145
|
+
updatedContext[ key ] = blockContext[ key ];
|
|
146
|
+
}
|
|
143
147
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
148
|
+
blockBindingsBySource.set( source, {
|
|
149
|
+
...blockBindingsBySource.get( source ),
|
|
150
|
+
[ attributeName ]: {
|
|
151
|
+
args: sourceArgs,
|
|
152
|
+
},
|
|
153
|
+
} );
|
|
154
|
+
}
|
|
151
155
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
context: updatedContext,
|
|
165
|
-
clientId,
|
|
166
|
-
bindings,
|
|
167
|
-
} );
|
|
168
|
-
}
|
|
169
|
-
for ( const [ attributeName, value ] of Object.entries(
|
|
170
|
-
values
|
|
171
|
-
) ) {
|
|
172
|
-
if (
|
|
173
|
-
attributeName === 'url' &&
|
|
174
|
-
( ! value || ! isURLLike( value ) )
|
|
175
|
-
) {
|
|
176
|
-
// Return null if value is not a valid URL.
|
|
177
|
-
attributes[ attributeName ] = null;
|
|
156
|
+
if ( blockBindingsBySource.size ) {
|
|
157
|
+
for ( const [
|
|
158
|
+
source,
|
|
159
|
+
bindings,
|
|
160
|
+
] of blockBindingsBySource ) {
|
|
161
|
+
// Get values in batch if the source supports it.
|
|
162
|
+
let values = {};
|
|
163
|
+
if ( ! source.getValues ) {
|
|
164
|
+
Object.keys( bindings ).forEach( ( attr ) => {
|
|
165
|
+
// Default to the the source label when `getValues` doesn't exist.
|
|
166
|
+
values[ attr ] = source.label;
|
|
167
|
+
} );
|
|
178
168
|
} else {
|
|
179
|
-
|
|
169
|
+
values = source.getValues( {
|
|
170
|
+
select,
|
|
171
|
+
context: updatedContext,
|
|
172
|
+
clientId,
|
|
173
|
+
bindings,
|
|
174
|
+
} );
|
|
175
|
+
}
|
|
176
|
+
for ( const [ attributeName, value ] of Object.entries(
|
|
177
|
+
values
|
|
178
|
+
) ) {
|
|
179
|
+
if (
|
|
180
|
+
attributeName === 'url' &&
|
|
181
|
+
( ! value || ! isURLLike( value ) )
|
|
182
|
+
) {
|
|
183
|
+
// Return null if value is not a valid URL.
|
|
184
|
+
attributes[ attributeName ] = null;
|
|
185
|
+
} else {
|
|
186
|
+
attributes[ attributeName ] = value;
|
|
187
|
+
}
|
|
180
188
|
}
|
|
181
189
|
}
|
|
182
190
|
}
|
|
183
|
-
}
|
|
184
191
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
blockBindings,
|
|
188
|
-
|
|
189
|
-
clientId,
|
|
190
|
-
updatedContext,
|
|
191
|
-
registry,
|
|
192
|
-
sources,
|
|
193
|
-
] );
|
|
192
|
+
return attributes;
|
|
193
|
+
},
|
|
194
|
+
[ blockBindings, name, clientId, updatedContext, sources ]
|
|
195
|
+
);
|
|
194
196
|
|
|
195
197
|
const hasParentPattern = !! updatedContext[ 'pattern/overrides' ];
|
|
196
198
|
const hasPatternOverridesDefaultBinding =
|
|
@@ -240,7 +242,8 @@ export const withBlockBindingSupport = createHigherOrderComponent(
|
|
|
240
242
|
bindings,
|
|
241
243
|
] of blockBindingsBySource ) {
|
|
242
244
|
source.setValues( {
|
|
243
|
-
registry,
|
|
245
|
+
select: registry.select,
|
|
246
|
+
dispatch: registry.dispatch,
|
|
244
247
|
context: updatedContext,
|
|
245
248
|
clientId,
|
|
246
249
|
bindings,
|
package/src/private-apis.js
CHANGED
|
@@ -47,7 +47,6 @@ import { PrivatePublishDateTimePicker } from './components/publish-date-time-pic
|
|
|
47
47
|
import useSpacingSizes from './components/spacing-sizes-control/hooks/use-spacing-sizes';
|
|
48
48
|
import useBlockDisplayTitle from './components/block-title/use-block-display-title';
|
|
49
49
|
import TabbedSidebar from './components/tabbed-sidebar';
|
|
50
|
-
import { useBlockBindingsUtils } from './utils/block-bindings';
|
|
51
50
|
|
|
52
51
|
/**
|
|
53
52
|
* Private @wordpress/block-editor APIs.
|
|
@@ -92,6 +91,5 @@ lock( privateApis, {
|
|
|
92
91
|
useBlockDisplayTitle,
|
|
93
92
|
__unstableBlockStyleVariationOverridesWithConfig,
|
|
94
93
|
setBackgroundStyleDefaults,
|
|
95
|
-
useBlockBindingsUtils,
|
|
96
94
|
sectionRootClientIdKey,
|
|
97
95
|
} );
|
|
@@ -13,6 +13,53 @@ function isObjectEmpty( object ) {
|
|
|
13
13
|
return ! object || Object.keys( object ).length === 0;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Contains utils to update the block `bindings` metadata.
|
|
18
|
+
*
|
|
19
|
+
* @typedef {Object} WPBlockBindingsUtils
|
|
20
|
+
*
|
|
21
|
+
* @property {Function} updateBlockBindings Updates the value of the bindings connected to block attributes.
|
|
22
|
+
* @property {Function} removeAllBlockBindings Removes the bindings property of the `metadata` attribute.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Retrieves the existing utils needed to update the block `bindings` metadata.
|
|
27
|
+
* They can be used to create, modify, or remove connections from the existing block attributes.
|
|
28
|
+
*
|
|
29
|
+
* It contains the following utils:
|
|
30
|
+
* - `updateBlockBindings`: Updates the value of the bindings connected to block attributes. It can be used to remove a specific binding by setting the value to `undefined`.
|
|
31
|
+
* - `removeAllBlockBindings`: Removes the bindings property of the `metadata` attribute.
|
|
32
|
+
*
|
|
33
|
+
* @return {?WPBlockBindingsUtils} Object containing the block bindings utils.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```js
|
|
37
|
+
* import { useBlockBindingsUtils } from '@wordpress/block-editor'
|
|
38
|
+
* const { updateBlockBindings, removeAllBlockBindings } = useBlockBindingsUtils();
|
|
39
|
+
*
|
|
40
|
+
* // Update url and alt attributes.
|
|
41
|
+
* updateBlockBindings( {
|
|
42
|
+
* url: {
|
|
43
|
+
* source: 'core/post-meta',
|
|
44
|
+
* args: {
|
|
45
|
+
* key: 'url_custom_field',
|
|
46
|
+
* },
|
|
47
|
+
* },
|
|
48
|
+
* alt: {
|
|
49
|
+
* source: 'core/post-meta',
|
|
50
|
+
* args: {
|
|
51
|
+
* key: 'text_custom_field',
|
|
52
|
+
* },
|
|
53
|
+
* },
|
|
54
|
+
* } );
|
|
55
|
+
*
|
|
56
|
+
* // Remove binding from url attribute.
|
|
57
|
+
* updateBlockBindings( { url: undefined } );
|
|
58
|
+
*
|
|
59
|
+
* // Remove bindings from all attributes.
|
|
60
|
+
* removeAllBlockBindings();
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
16
63
|
export function useBlockBindingsUtils() {
|
|
17
64
|
const { clientId } = useBlockEditContext();
|
|
18
65
|
const { updateBlockAttributes } = useDispatch( blockEditorStore );
|
package/src/utils/index.js
CHANGED