@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.
Files changed (40) hide show
  1. package/README.md +42 -0
  2. package/build/components/media-placeholder/index.js +17 -19
  3. package/build/components/media-placeholder/index.js.map +1 -1
  4. package/build/components/rich-text/index.js +4 -8
  5. package/build/components/rich-text/index.js.map +1 -1
  6. package/build/hooks/block-bindings.js +6 -13
  7. package/build/hooks/block-bindings.js.map +1 -1
  8. package/build/hooks/use-bindings-attributes.js +5 -4
  9. package/build/hooks/use-bindings-attributes.js.map +1 -1
  10. package/build/private-apis.js +0 -2
  11. package/build/private-apis.js.map +1 -1
  12. package/build/utils/block-bindings.js +48 -0
  13. package/build/utils/block-bindings.js.map +1 -1
  14. package/build/utils/index.js +7 -0
  15. package/build/utils/index.js.map +1 -1
  16. package/build-module/components/media-placeholder/index.js +18 -20
  17. package/build-module/components/media-placeholder/index.js.map +1 -1
  18. package/build-module/components/rich-text/index.js +4 -8
  19. package/build-module/components/rich-text/index.js.map +1 -1
  20. package/build-module/hooks/block-bindings.js +6 -13
  21. package/build-module/hooks/block-bindings.js.map +1 -1
  22. package/build-module/hooks/use-bindings-attributes.js +5 -4
  23. package/build-module/hooks/use-bindings-attributes.js.map +1 -1
  24. package/build-module/private-apis.js +0 -2
  25. package/build-module/private-apis.js.map +1 -1
  26. package/build-module/utils/block-bindings.js +48 -0
  27. package/build-module/utils/block-bindings.js.map +1 -1
  28. package/build-module/utils/index.js +1 -0
  29. package/build-module/utils/index.js.map +1 -1
  30. package/build-style/content-rtl.css +2 -14
  31. package/build-style/content.css +2 -14
  32. package/package.json +6 -6
  33. package/src/components/media-placeholder/content.scss +3 -19
  34. package/src/components/media-placeholder/index.js +17 -17
  35. package/src/components/rich-text/index.js +4 -13
  36. package/src/hooks/block-bindings.js +9 -10
  37. package/src/hooks/use-bindings-attributes.js +66 -63
  38. package/src/private-apis.js +0 -2
  39. package/src/utils/block-bindings.js +47 -0
  40. 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
- if ( ! blockBindings ) {
123
- return;
124
- }
121
+ const boundAttributes = useSelect(
122
+ ( select ) => {
123
+ if ( ! blockBindings ) {
124
+ return;
125
+ }
125
126
 
126
- const attributes = {};
127
+ const attributes = {};
127
128
 
128
- const blockBindingsBySource = new Map();
129
+ const blockBindingsBySource = new Map();
129
130
 
130
- for ( const [ attributeName, binding ] of Object.entries(
131
- blockBindings
132
- ) ) {
133
- const { source: sourceName, args: sourceArgs } = binding;
134
- const source = sources[ sourceName ];
135
- if ( ! source || ! canBindAttribute( name, attributeName ) ) {
136
- continue;
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
- // Populate context.
140
- for ( const key of source.usesContext || [] ) {
141
- updatedContext[ key ] = blockContext[ key ];
142
- }
143
+ // Populate context.
144
+ for ( const key of source.usesContext || [] ) {
145
+ updatedContext[ key ] = blockContext[ key ];
146
+ }
143
147
 
144
- blockBindingsBySource.set( source, {
145
- ...blockBindingsBySource.get( source ),
146
- [ attributeName ]: {
147
- args: sourceArgs,
148
- },
149
- } );
150
- }
148
+ blockBindingsBySource.set( source, {
149
+ ...blockBindingsBySource.get( source ),
150
+ [ attributeName ]: {
151
+ args: sourceArgs,
152
+ },
153
+ } );
154
+ }
151
155
 
152
- if ( blockBindingsBySource.size ) {
153
- for ( const [ source, bindings ] of blockBindingsBySource ) {
154
- // Get values in batch if the source supports it.
155
- let values = {};
156
- if ( ! source.getValues ) {
157
- Object.keys( bindings ).forEach( ( attr ) => {
158
- // Default to the the source label when `getValues` doesn't exist.
159
- values[ attr ] = source.label;
160
- } );
161
- } else {
162
- values = source.getValues( {
163
- registry,
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
- attributes[ attributeName ] = value;
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
- return attributes;
186
- }, [
187
- blockBindings,
188
- name,
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,
@@ -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 );
@@ -1,2 +1,3 @@
1
1
  export { default as transformStyles } from './transform-styles';
2
2
  export { default as getPxFromCssUnit } from './get-px-from-css-unit';
3
+ export { useBlockBindingsUtils } from './block-bindings';