From f25282d8ab40d949a72d63c596743b11d1d2b589 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Tue, 24 Sep 2024 10:12:18 +0200 Subject: [PATCH 1/2] Pass updated context as `context` prop --- .../src/hooks/use-bindings-attributes.js | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index ac045004cc654..2e94e86889bf7 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -103,7 +103,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( const sources = useSelect( ( select ) => unlock( select( blocksStore ) ).getAllBlockBindingsSources() ); - const { name, clientId } = props; + const { name, clientId, context } = props; const hasParentPattern = !! props.context[ 'pattern/overrides' ]; const hasPatternOverridesDefaultBinding = props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ] @@ -121,6 +121,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( // used purposely here to ensure `boundAttributes` is updated whenever // there are attribute updates. // `source.getValues` may also call a selector via `registry.select`. + const updatedContext = { ...context }; const boundAttributes = useSelect( () => { if ( ! blockBindings ) { return; @@ -139,6 +140,11 @@ export const withBlockBindingSupport = createHigherOrderComponent( continue; } + // Populate context. + for ( const key of source.usesContext || [] ) { + updatedContext[ key ] = blockContext[ key ]; + } + blockBindingsBySource.set( source, { ...blockBindingsBySource.get( source ), [ attributeName ]: { @@ -149,15 +155,6 @@ export const withBlockBindingSupport = createHigherOrderComponent( if ( blockBindingsBySource.size ) { for ( const [ source, bindings ] of blockBindingsBySource ) { - // Populate context. - const context = {}; - - if ( source.usesContext?.length ) { - for ( const key of source.usesContext ) { - context[ key ] = blockContext[ key ]; - } - } - // Get values in batch if the source supports it. let values = {}; if ( ! source.getValues ) { @@ -168,7 +165,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( } else { values = source.getValues( { registry, - context, + context: updatedContext, clientId, bindings, } ); @@ -190,7 +187,14 @@ export const withBlockBindingSupport = createHigherOrderComponent( } return attributes; - }, [ blockBindings, name, clientId, blockContext, registry, sources ] ); + }, [ + blockBindings, + name, + clientId, + updatedContext, + registry, + sources, + ] ); const { setAttributes } = props; @@ -236,18 +240,9 @@ export const withBlockBindingSupport = createHigherOrderComponent( source, bindings, ] of blockBindingsBySource ) { - // Populate context. - const context = {}; - - if ( source.usesContext?.length ) { - for ( const key of source.usesContext ) { - context[ key ] = blockContext[ key ]; - } - } - source.setValues( { registry, - context, + context: updatedContext, clientId, bindings, } ); @@ -277,7 +272,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( blockBindings, name, clientId, - blockContext, + updatedContext, setAttributes, sources, hasPatternOverridesDefaultBinding, @@ -291,6 +286,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( { ...props } attributes={ { ...props.attributes, ...boundAttributes } } setAttributes={ _setAttributes } + context={ updatedContext } /> ); From af03ccc31423eb1a2f01eb191a700903cdf3a7ca Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Tue, 24 Sep 2024 10:24:30 +0200 Subject: [PATCH 2/2] Use `updatedContext` in pattern overrides --- .../block-editor/src/hooks/use-bindings-attributes.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index 2e94e86889bf7..9f9234ad47d10 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -103,11 +103,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( const sources = useSelect( ( select ) => unlock( select( blocksStore ) ).getAllBlockBindingsSources() ); - const { name, clientId, context } = props; - const hasParentPattern = !! props.context[ 'pattern/overrides' ]; - const hasPatternOverridesDefaultBinding = - props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ] - ?.source === 'core/pattern-overrides'; + const { name, clientId, context, setAttributes } = props; const blockBindings = useMemo( () => replacePatternOverrideDefaultBindings( @@ -196,7 +192,10 @@ export const withBlockBindingSupport = createHigherOrderComponent( sources, ] ); - const { setAttributes } = props; + const hasParentPattern = !! updatedContext[ 'pattern/overrides' ]; + const hasPatternOverridesDefaultBinding = + props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ] + ?.source === 'core/pattern-overrides'; const _setAttributes = useCallback( ( nextAttributes ) => {