From 320dcb9633b2c2fcb5dd6ae99f517ce04e265e9e Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 26 Jun 2024 15:31:16 +0800 Subject: [PATCH] Use block context to detect presence of parent pattern --- .../src/hooks/use-bindings-attributes.js | 34 +++++++------------ packages/block-library/src/block/block.json | 6 +++- .../editor/src/bindings/pattern-overrides.js | 19 +++++------ 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index a0757bbb6d386..a374108615ecb 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -11,7 +11,6 @@ import { addFilter } from '@wordpress/hooks'; * Internal dependencies */ import { unlock } from '../lock-unlock'; -import { store as blockEditorStore } from '../store'; /** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */ /** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */ @@ -95,24 +94,12 @@ export const withBlockBindingSupport = createHigherOrderComponent( ( BlockEdit ) => ( props ) => { const registry = useRegistry(); const { name, clientId, context } = props; - const { sources, hasParentPattern } = useSelect( - ( select ) => { - const { getAllBlockBindingsSources } = unlock( - select( blocksStore ) - ); - const { getBlockParentsByBlockName } = unlock( - select( blockEditorStore ) - ); - - return { - sources: getAllBlockBindingsSources(), - hasParentPattern: - getBlockParentsByBlockName( clientId, 'core/block' ) - .length > 0, - }; - }, - [ clientId ] + const sources = useSelect( + ( select ) => + unlock( select( blocksStore ) ).getAllBlockBindingsSources(), + [] ); + const hasParentPattern = !! props.context.patternOverridesContent; const hasPatternOverridesDefaultBinding = props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ] ?.source === 'core/pattern-overrides'; @@ -254,12 +241,13 @@ export const withBlockBindingSupport = createHigherOrderComponent( [ registry, bindings, - name, - clientId, - context, + hasPatternOverridesDefaultBinding, + hasParentPattern, setAttributes, + name, sources, - hasPatternOverridesDefaultBinding, + context, + clientId, ] ); @@ -292,6 +280,8 @@ function shimAttributeSource( settings, name ) { return { ...settings, edit: withBlockBindingSupport( settings.edit ), + // TODO - move this to be located with pattern overrides code. + usesContext: [ 'patternOverridesContent', ...settings?.usesContext ], }; } diff --git a/packages/block-library/src/block/block.json b/packages/block-library/src/block/block.json index 34dcb9a396ac6..af42aa5ca354a 100644 --- a/packages/block-library/src/block/block.json +++ b/packages/block-library/src/block/block.json @@ -12,9 +12,13 @@ "type": "number" }, "content": { - "type": "object" + "type": "object", + "default": {} } }, + "providesContext": { + "patternOverridesContent": "content" + }, "supports": { "customClassName": false, "html": false, diff --git a/packages/editor/src/bindings/pattern-overrides.js b/packages/editor/src/bindings/pattern-overrides.js index 107ed72e722ba..90182c0db075c 100644 --- a/packages/editor/src/bindings/pattern-overrides.js +++ b/packages/editor/src/bindings/pattern-overrides.js @@ -9,23 +9,22 @@ const CONTENT = 'content'; export default { name: 'core/pattern-overrides', label: _x( 'Pattern Overrides', 'block bindings source' ), - getValue( { registry, clientId, attributeName } ) { - const { getBlockAttributes, getBlockParentsByBlockName } = - registry.select( blockEditorStore ); + getValue( { registry, clientId, context, attributeName } ) { + const { patternOverridesContent } = context; + const { getBlockAttributes } = registry.select( blockEditorStore ); const currentBlockAttributes = getBlockAttributes( clientId ); - const [ patternClientId ] = getBlockParentsByBlockName( - clientId, - 'core/block', - true - ); + + if ( ! patternOverridesContent ) { + return currentBlockAttributes[ attributeName ]; + } const overridableValue = - getBlockAttributes( patternClientId )?.[ CONTENT ]?.[ + patternOverridesContent?.[ currentBlockAttributes?.metadata?.name ]?.[ attributeName ]; // If there is no pattern client ID, or it is not overwritten, return the default value. - if ( ! patternClientId || overridableValue === undefined ) { + if ( overridableValue === undefined ) { return currentBlockAttributes[ attributeName ]; }