From 9cc357fa56e659f74a508bf4a5f5263e0659e59c Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Sun, 20 Oct 2024 19:52:19 -0400 Subject: [PATCH] Enable bindings for all attributes with 'role' property --- .../block-list/use-block-props/index.js | 2 +- .../src/components/rich-text/index.js | 5 +-- .../block-editor/src/hooks/block-bindings.js | 2 +- .../src/hooks/use-bindings-attributes.js | 38 +++++++++---------- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/packages/block-editor/src/components/block-list/use-block-props/index.js b/packages/block-editor/src/components/block-list/use-block-props/index.js index 45fc1d9eb5ea12..53993393dbb54c 100644 --- a/packages/block-editor/src/components/block-list/use-block-props/index.js +++ b/packages/block-editor/src/components/block-list/use-block-props/index.js @@ -127,7 +127,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) { const blockEditContext = useBlockEditContext(); const hasBlockBindings = !! blockEditContext[ blockBindingsKey ]; const bindingsStyle = - hasBlockBindings && canBindBlock( name ) + hasBlockBindings && canBindBlock() ? { '--wp-admin-theme-color': 'var(--wp-block-synced-color)', '--wp-admin-theme-color--rgb': diff --git a/packages/block-editor/src/components/rich-text/index.js b/packages/block-editor/src/components/rich-text/index.js index 8a5d99a7eb9f0a..7ac2657f274303 100644 --- a/packages/block-editor/src/components/rich-text/index.js +++ b/packages/block-editor/src/components/rich-text/index.js @@ -166,10 +166,7 @@ export function RichTextWrapper( const { disableBoundBlock, bindingsPlaceholder, bindingsLabel } = useSelect( ( select ) => { - if ( - ! blockBindings?.[ identifier ] || - ! canBindBlock( blockName ) - ) { + if ( ! blockBindings?.[ identifier ] || ! canBindBlock() ) { return {}; } diff --git a/packages/block-editor/src/hooks/block-bindings.js b/packages/block-editor/src/hooks/block-bindings.js index 694c9c1c0bb3cc..ac585e8fc20be9 100644 --- a/packages/block-editor/src/hooks/block-bindings.js +++ b/packages/block-editor/src/hooks/block-bindings.js @@ -261,7 +261,7 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { const filteredBindings = { ...bindings }; Object.keys( filteredBindings ).forEach( ( key ) => { if ( - ! canBindAttribute( blockName, key ) || + ! canBindAttribute() || filteredBindings[ key ].source === 'core/pattern-overrides' ) { delete filteredBindings[ key ]; diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index fdc617fda20c05..f6ccbba6d83153 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { store as blocksStore } from '@wordpress/blocks'; +import { store as blocksStore, getBlockType } from '@wordpress/blocks'; import { createHigherOrderComponent } from '@wordpress/compose'; import { useRegistry, useSelect } from '@wordpress/data'; import { useCallback, useMemo, useContext } from '@wordpress/element'; @@ -70,30 +70,34 @@ function replacePatternOverrideDefaultBindings( blockName, bindings ) { * Based on the given block name, * check if it is possible to bind the block. * - * @param {string} blockName - The block name. * @return {boolean} Whether it is possible to bind the block to sources. */ -export function canBindBlock( blockName ) { - return blockName in BLOCK_BINDINGS_ALLOWED_BLOCKS; +export function canBindBlock() { + return true; } /** * Based on the given block name and attribute name, * check if it is possible to bind the block attribute. * - * @param {string} blockName - The block name. - * @param {string} attributeName - The attribute name. * @return {boolean} Whether it is possible to bind the block attribute. */ -export function canBindAttribute( blockName, attributeName ) { - return ( - canBindBlock( blockName ) && - BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ].includes( attributeName ) - ); +export function canBindAttribute() { + return true; } export function getBindableAttributes( blockName ) { - return BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ]; + const bindableAttributes = []; + const blockType = getBlockType( blockName ); + if ( blockType.attributes ) { + for ( const attribute in blockType.attributes ) { + if ( blockType.attributes[ attribute ].role === 'content' ) { + bindableAttributes.push( attribute ); + } + } + } + + return bindableAttributes; } export const withBlockBindingSupport = createHigherOrderComponent( @@ -133,10 +137,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( ) ) { const { source: sourceName, args: sourceArgs } = binding; const source = sources[ sourceName ]; - if ( - ! source || - ! canBindAttribute( name, attributeName ) - ) { + if ( ! source || ! canBindAttribute() ) { continue; } @@ -301,11 +302,10 @@ export const withBlockBindingSupport = createHigherOrderComponent( * to upgrade bound attributes. * * @param {WPBlockSettings} settings - Registered block settings. - * @param {string} name - Block name. * @return {WPBlockSettings} Filtered block settings. */ -function shimAttributeSource( settings, name ) { - if ( ! canBindBlock( name ) ) { +function shimAttributeSource( settings ) { + if ( ! canBindBlock() ) { return settings; }