From e91174b57a09608aeca08b73307c5eb3bbec23bb Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Sat, 13 Jul 2024 09:47:43 +0200 Subject: [PATCH] Read `usesContext` during bindings processing --- .../src/hooks/use-bindings-attributes.js | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index a63f59b69ca21c..c6a19987fee1c7 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -4,13 +4,14 @@ import { store as blocksStore } from '@wordpress/blocks'; import { createHigherOrderComponent } from '@wordpress/compose'; import { useRegistry, useSelect } from '@wordpress/data'; -import { useCallback, useMemo } from '@wordpress/element'; +import { useCallback, useMemo, useContext } from '@wordpress/element'; import { addFilter } from '@wordpress/hooks'; /** * Internal dependencies */ import { unlock } from '../lock-unlock'; +import BlockContext from '../components/block-context'; /** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */ /** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */ @@ -93,10 +94,11 @@ export function canBindAttribute( blockName, attributeName ) { export const withBlockBindingSupport = createHigherOrderComponent( ( BlockEdit ) => ( props ) => { const registry = useRegistry(); + const blockContext = useContext( BlockContext ); const sources = useSelect( ( select ) => unlock( select( blocksStore ) ).getAllBlockBindingsSources() ); - const { name, clientId, context } = props; + const { name, clientId } = props; const hasParentPattern = !! props.context[ 'pattern/overrides' ]; const hasPatternOverridesDefaultBinding = props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ] @@ -132,6 +134,14 @@ export const withBlockBindingSupport = createHigherOrderComponent( continue; } + const context = {}; + + if ( source.usesContext?.length ) { + for ( const key of source.usesContext ) { + context[ key ] = blockContext[ key ]; + } + } + const args = { registry, context, @@ -153,7 +163,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( } return attributes; - }, [ bindings, name, clientId, context, registry, sources ] ); + }, [ bindings, name, clientId, blockContext, registry, sources ] ); const { setAttributes } = props; @@ -196,6 +206,14 @@ export const withBlockBindingSupport = createHigherOrderComponent( source, attributes, ] of updatesBySource ) { + const context = {}; + + if ( source.usesContext?.length ) { + for ( const key of source.usesContext ) { + context[ key ] = blockContext[ key ]; + } + } + if ( source.setValues ) { source.setValues( { registry, @@ -245,7 +263,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( bindings, name, clientId, - context, + blockContext, setAttributes, sources, hasPatternOverridesDefaultBinding,