From 9fcf841a434624b221c198047c3a1be13d9286fe Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Mon, 22 Jul 2024 10:36:31 +0200 Subject: [PATCH] Adapt code after rebase --- .../src/hooks/use-bindings-attributes.js | 2 +- packages/blocks/src/api/registration.js | 18 +------- packages/blocks/src/store/private-actions.js | 2 +- packages/blocks/src/store/reducer.js | 30 +++++-------- .../editor/src/bindings/pattern-overrides.js | 43 ++++++++++++------- packages/editor/src/bindings/post-meta.js | 36 ++++++++++------ 6 files changed, 67 insertions(+), 64 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index e8a55e9fb42f04..54b5425bfc44a5 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -188,7 +188,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( } return attributes; - }, [ bindings, name, clientId, blockContext, registry, sources ] ); + }, [ blockBindings, name, clientId, blockContext, registry, sources ] ); const { setAttributes } = props; diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index a7fc048d9ada06..9b2f0f8bf5a3cd 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -871,7 +871,7 @@ export const registerBlockBindingsSource = ( source ) => { // Check the `usesContext` property is correct. if ( usesContext && ! Array.isArray( usesContext ) ) { - console.error( 'Block bindings source usesContext must be an array.' ); + warning( 'Block bindings source usesContext must be an array.' ); return; } @@ -899,21 +899,7 @@ export const registerBlockBindingsSource = ( source ) => { return; } - // Merge context from server and client. - let mergedUsesContext = [ - ...( existingSource?.usesContext || [] ), - ...( usesContext || [] ), - ]; - // Remove duplicates. - mergedUsesContext = - mergedUsesContext.length > 0 - ? [ ...new Set( mergedUsesContext ) ] - : undefined; - - unlock( dispatch( blocksStore ) ).addBlockBindingsSource( { - ...source, - usesContext: mergedUsesContext, - } ); + unlock( dispatch( blocksStore ) ).addBlockBindingsSource( source ); }; /** diff --git a/packages/blocks/src/store/private-actions.js b/packages/blocks/src/store/private-actions.js index 5440a74f39d8bb..977270cf1d0c97 100644 --- a/packages/blocks/src/store/private-actions.js +++ b/packages/blocks/src/store/private-actions.js @@ -51,8 +51,8 @@ export function addBlockBindingsSource( source ) { type: 'ADD_BLOCK_BINDINGS_SOURCE', name: source.name, label: source.label, - getValues: source.getValues, usesContext: source.usesContext, + getValues: source.getValues, setValues: source.setValues, getPlaceholder: source.getPlaceholder, canUserEditValue: source.canUserEditValue, diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index 68b5fae37f4931..5e0714b6064fde 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -374,31 +374,23 @@ export function collections( state = {}, action ) { export function blockBindingsSources( state = {}, action ) { switch ( action.type ) { case 'ADD_BLOCK_BINDINGS_SOURCE': - // Filter the name property, the type property, and the undefined values. - const newProperties = Object.fromEntries( - Object.entries( action ).filter( - ( [ key, value ] ) => - value !== undefined && key !== 'name' && key !== 'type' - ) - ); + // Merge usesContext with existing values, potentially defined in the server registration. + let mergedUsesContext = [ + ...( state[ action.name ]?.usesContext || [] ), + ...( action.usesContext || [] ), + ]; + // Remove duplicates. + mergedUsesContext = + mergedUsesContext.length > 0 + ? [ ...new Set( mergedUsesContext ) ] + : undefined; - return { - ...state, - [ action.name ]: { - // Keep the existing properties if it has been bootstrapped. - ...state[ action.name ], - // Update with the new properties. - ...newProperties, - canUserEditValue: - action.canUserEditValue || ( () => false ), - }, - }; - case 'ADD_BOOTSTRAPPED_BLOCK_BINDINGS_SOURCE': return { ...state, [ action.name ]: { // Don't override the label if it's already set. label: state[ action.name ]?.label || action.label, + usesContext: mergedUsesContext, getValues: action.getValues, setValues: action.setValues, getPlaceholder: action.getPlaceholder, diff --git a/packages/editor/src/bindings/pattern-overrides.js b/packages/editor/src/bindings/pattern-overrides.js index 492406a9f8eb1a..88c6c73bdc61c1 100644 --- a/packages/editor/src/bindings/pattern-overrides.js +++ b/packages/editor/src/bindings/pattern-overrides.js @@ -7,28 +7,32 @@ const CONTENT = 'content'; export default { name: 'core/pattern-overrides', - getValue( { registry, clientId, context, attributeName } ) { + getValues( { registry, clientId, context, bindings } ) { const patternOverridesContent = context[ 'pattern/overrides' ]; const { getBlockAttributes } = registry.select( blockEditorStore ); const currentBlockAttributes = getBlockAttributes( clientId ); - if ( ! patternOverridesContent ) { - return currentBlockAttributes[ attributeName ]; - } - - const overridableValue = - patternOverridesContent?.[ - currentBlockAttributes?.metadata?.name - ]?.[ attributeName ]; + const overridesValues = {}; + for ( const attributeName of Object.keys( bindings ) ) { + const overridableValue = + patternOverridesContent?.[ + currentBlockAttributes?.metadata?.name + ]?.[ attributeName ]; - // If there is no pattern client ID, or it is not overwritten, return the default value. - if ( overridableValue === undefined ) { - return currentBlockAttributes[ attributeName ]; + // If it has not been overriden, return the original value. + // Check undefined because empty string is a valid value. + if ( overridableValue === undefined ) { + overridesValues[ attributeName ] = + currentBlockAttributes[ attributeName ]; + continue; + } else { + overridesValues[ attributeName ] = + overridableValue === '' ? undefined : overridableValue; + } } - - return overridableValue === '' ? undefined : overridableValue; + return overridesValues; }, - setValues( { registry, clientId, attributes } ) { + setValues( { registry, clientId, bindings } ) { const { getBlockAttributes, getBlockParentsByBlockName, getBlocks } = registry.select( blockEditorStore ); const currentBlockAttributes = getBlockAttributes( clientId ); @@ -43,6 +47,15 @@ export default { true ); + // Extract the updated attributes from the source bindings. + const attributes = Object.entries( bindings ).reduce( + ( attrs, [ key, { newValue } ] ) => { + attrs[ key ] = newValue; + return attrs; + }, + {} + ); + // If there is no pattern client ID, sync blocks with the same name and same attributes. if ( ! patternClientId ) { const syncBlocksWithSameName = ( blocks ) => { diff --git a/packages/editor/src/bindings/post-meta.js b/packages/editor/src/bindings/post-meta.js index 0173e5b5f63020..aafc784a21bd4a 100644 --- a/packages/editor/src/bindings/post-meta.js +++ b/packages/editor/src/bindings/post-meta.js @@ -13,25 +13,37 @@ export default { getPlaceholder( { args } ) { return args.key; }, - getValue( { registry, context, args } ) { - return registry + getValues( { registry, context, bindings } ) { + const meta = registry .select( coreDataStore ) .getEditedEntityRecord( 'postType', context?.postType, context?.postId - ).meta?.[ args.key ]; + )?.meta; + const newValues = {}; + for ( const [ attributeName, source ] of Object.entries( bindings ) ) { + newValues[ attributeName ] = meta?.[ source.args.key ]; + } + return newValues; }, - setValue( { registry, context, args, value } ) { + setValues( { registry, context, bindings } ) { + const newMeta = {}; + Object.values( bindings ).forEach( ( { args, newValue } ) => { + newMeta[ args.key ] = newValue; + } ); registry .dispatch( coreDataStore ) .editEntityRecord( 'postType', context?.postType, context?.postId, { - meta: { - [ args.key ]: value, - }, + meta: newMeta, } ); }, canUserEditValue( { select, context, args } ) { + // Lock editing in query loop. + if ( context?.query || context?.queryId ) { + return false; + } + const postType = context?.postType || select( editorStore ).getCurrentPostType(); @@ -52,11 +64,11 @@ export default { } // Check that the user has the capability to edit post meta. - const canUserEdit = select( coreDataStore ).canUserEditEntityRecord( - 'postType', - context?.postType, - context?.postId - ); + const canUserEdit = select( coreDataStore ).canUser( 'update', { + kind: 'postType', + name: context?.postType, + id: context?.postId, + } ); if ( ! canUserEdit ) { return false; }