diff --git a/packages/block-editor/src/components/block-edit/edit.js b/packages/block-editor/src/components/block-edit/edit.js index 97d69503a6c4de..6b1ddd86f4c76e 100644 --- a/packages/block-editor/src/components/block-edit/edit.js +++ b/packages/block-editor/src/components/block-edit/edit.js @@ -18,6 +18,8 @@ import { useContext, useMemo } from '@wordpress/element'; * Internal dependencies */ import BlockContext from '../block-context'; +import { withBlockBindingsSupport } from './with-block-bindings-support'; +import { canBindBlock } from '../../utils/block-bindings'; /** * Default value used for blocks which do not define their own context needs, @@ -47,7 +49,7 @@ const Edit = ( props ) => { const EditWithFilters = withFilters( 'editor.BlockEdit' )( Edit ); -// const EditWithFiltersAndBindings = withBlockBindingsSupport( EditWithFilters ); +const EditWithFiltersAndBindings = withBlockBindingsSupport( EditWithFilters ); const EditWithGeneratedProps = ( props ) => { const { attributes = {}, name } = props; @@ -69,8 +71,12 @@ const EditWithGeneratedProps = ( props ) => { return null; } + const EditComponent = canBindBlock( name ) + ? EditWithFiltersAndBindings + : EditWithFilters; + if ( blockType.apiVersion > 1 ) { - return ; + return ; } // Generate a class name for the block's editable form. @@ -84,7 +90,7 @@ const EditWithGeneratedProps = ( props ) => { ); return ( - unlock( select( blocksStore ) ).getAllBlockBindingsSources() ); - const { name, clientId, context, setAttributes } = props; - const blockBindings = useMemo( - () => - replacePatternOverrideDefaultBindings( + const { name, clientId, setAttributes } = props; + const { blockBindings, sourcesContext } = useMemo( () => { + const _sourcesContext = {}; + if ( props.attributes?.metadata?.bindings ) { + const registeredSources = getBlockBindingsSources(); + Object.values( + props.attributes?.metadata?.bindings || {} + ).forEach( ( binding ) => { + registeredSources[ binding?.source ]?.usesContext?.forEach( + ( key ) => { + _sourcesContext[ key ] = blockContext[ key ]; + } + ); + } ); + } + return { + blockBindings: replacePatternOverrideDefaultBindings( name, props.attributes?.metadata?.bindings ), - [ props.attributes?.metadata?.bindings, name ] - ); + sourcesContext: _sourcesContext, + }; + }, [ props.attributes?.metadata?.bindings, name, blockContext ] ); // While this hook doesn't directly call any selectors, `useSelect` is // 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 = {}; const boundAttributes = useSelect( ( select ) => { if ( ! blockBindings ) { @@ -104,11 +120,6 @@ export const withBlockBindingsSupport = createHigherOrderComponent( continue; } - // Populate context. - for ( const key of source.usesContext || [] ) { - updatedContext[ key ] = blockContext[ key ]; - } - blockBindingsBySource.set( source, { ...blockBindingsBySource.get( source ), [ attributeName ]: { @@ -132,7 +143,7 @@ export const withBlockBindingsSupport = createHigherOrderComponent( } else { values = source.getValues( { select, - context: updatedContext, + context: sourcesContext, clientId, bindings, } ); @@ -155,10 +166,10 @@ export const withBlockBindingsSupport = createHigherOrderComponent( return attributes; }, - [ blockBindings, name, clientId, updatedContext, sources ] + [ blockBindings, name, clientId, sourcesContext, sources ] ); - const hasParentPattern = !! updatedContext[ 'pattern/overrides' ]; + const hasParentPattern = !! sourcesContext[ 'pattern/overrides' ]; const hasPatternOverridesDefaultBinding = props.attributes?.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ] ?.source === 'core/pattern-overrides'; @@ -208,7 +219,7 @@ export const withBlockBindingsSupport = createHigherOrderComponent( source.setValues( { select: registry.select, dispatch: registry.dispatch, - context: updatedContext, + context: sourcesContext, clientId, bindings, } ); @@ -238,7 +249,7 @@ export const withBlockBindingsSupport = createHigherOrderComponent( blockBindings, name, clientId, - updatedContext, + sourcesContext, setAttributes, sources, hasPatternOverridesDefaultBinding, @@ -251,7 +262,6 @@ export const withBlockBindingsSupport = createHigherOrderComponent( { ...props } attributes={ { ...props.attributes, ...boundAttributes } } setAttributes={ _setAttributes } - context={ { ...context, ...updatedContext } } /> ); },