From 1b1d1ef94bb55550a41cf1426adbfa64620930ee Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 6 Oct 2025 12:44:11 +0200 Subject: [PATCH 1/5] Block Bindings: Deduplicate compatible data computation code --- .../block-editor/src/hooks/block-bindings.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/block-editor/src/hooks/block-bindings.js b/packages/block-editor/src/hooks/block-bindings.js index 87ab4e36321028..345fa62d43bfd2 100644 --- a/packages/block-editor/src/hooks/block-bindings.js +++ b/packages/block-editor/src/hooks/block-bindings.js @@ -332,26 +332,28 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { select, context, } ); - const hasCompatibleData = _bindableAttributes.some( + + const allowedAttributeTypes = _bindableAttributes.map( ( attribute ) => { - const _attributeType = + const attributeType = getBlockType( blockName ).attributes?.[ attribute ]?.type; - const attributeType = - _attributeType === 'rich-text' - ? 'string' - : _attributeType; - - return editorUIResult.data?.some( - ( item ) => item?.type === attributeType - ); + return attributeType === 'rich-text' + ? 'string' + : attributeType; } ); - if ( hasCompatibleData ) { + const compatibleData = editorUIResult?.data?.filter( + ( item ) => + allowedAttributeTypes.includes( item?.type ) + ); + + if ( compatibleData?.length > 0 ) { _sources[ sourceName ] = { ...editorUIResult, + data: compatibleData, label, getValues, }; From a203d2d66fdf64fc87b4de018bf1e9604a6ecfcc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 6 Oct 2025 12:46:44 +0200 Subject: [PATCH 2/5] Hoist allowedAttributeTypes --- .../block-editor/src/hooks/block-bindings.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/block-editor/src/hooks/block-bindings.js b/packages/block-editor/src/hooks/block-bindings.js index 345fa62d43bfd2..f9642fcd7853be 100644 --- a/packages/block-editor/src/hooks/block-bindings.js +++ b/packages/block-editor/src/hooks/block-bindings.js @@ -319,6 +319,18 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { sourceName, { editorUI, getFieldsList, usesContext, label, getValues }, ] ) => { + const allowedAttributeTypes = _bindableAttributes.map( + ( attribute ) => { + const attributeType = + getBlockType( blockName ).attributes?.[ + attribute + ]?.type; + return attributeType === 'rich-text' + ? 'string' + : attributeType; + } + ); + if ( editorUI ) { // Populate context. const context = {}; @@ -333,18 +345,6 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { context, } ); - const allowedAttributeTypes = _bindableAttributes.map( - ( attribute ) => { - const attributeType = - getBlockType( blockName ).attributes?.[ - attribute - ]?.type; - return attributeType === 'rich-text' - ? 'string' - : attributeType; - } - ); - const compatibleData = editorUIResult?.data?.filter( ( item ) => allowedAttributeTypes.includes( item?.type ) From 7ee8574da30a6f1cc5faacaca3e7ad21b528bef3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 6 Oct 2025 12:48:36 +0200 Subject: [PATCH 3/5] Reuse code for getFieldsList --- .../block-editor/src/hooks/block-bindings.js | 21 ++++--------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/packages/block-editor/src/hooks/block-bindings.js b/packages/block-editor/src/hooks/block-bindings.js index f9642fcd7853be..c0e48ba5791e06 100644 --- a/packages/block-editor/src/hooks/block-bindings.js +++ b/packages/block-editor/src/hooks/block-bindings.js @@ -382,27 +382,14 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { } ) ); - const hasCompatibleData = _bindableAttributes.some( - ( attribute ) => { - const _attributeType = - getBlockType( blockName ).attributes?.[ - attribute - ]?.type; - const attributeType = - _attributeType === 'rich-text' - ? 'string' - : _attributeType; - - return data.some( - ( item ) => item?.type === attributeType - ); - } + const compatibleData = data?.filter( ( item ) => + allowedAttributeTypes.includes( item?.type ) ); - if ( hasCompatibleData ) { + if ( compatibleData?.length > 0 ) { _sources[ sourceName ] = { mode: 'dropdown', // Default mode for backward compatibility - data, + data: compatibleData, label, getValues, }; From ba920c4d38bfbe9f0d5e695d27449bd3c0fbe64a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 6 Oct 2025 12:50:37 +0200 Subject: [PATCH 4/5] Deduplicate context computation --- .../block-editor/src/hooks/block-bindings.js | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/packages/block-editor/src/hooks/block-bindings.js b/packages/block-editor/src/hooks/block-bindings.js index c0e48ba5791e06..58a639a1afc785 100644 --- a/packages/block-editor/src/hooks/block-bindings.js +++ b/packages/block-editor/src/hooks/block-bindings.js @@ -331,15 +331,15 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { } ); - if ( editorUI ) { - // Populate context. - const context = {}; - if ( usesContext?.length ) { - for ( const key of usesContext ) { - context[ key ] = blockContext[ key ]; - } + // Populate context. + const context = {}; + if ( usesContext?.length ) { + for ( const key of usesContext ) { + context[ key ] = blockContext[ key ]; } + } + if ( editorUI ) { const editorUIResult = editorUI( { select, context, @@ -360,12 +360,6 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { } } else if ( getFieldsList ) { // Backward compatibility: Convert getFieldsList to editorUI format - const context = {}; - if ( usesContext?.length ) { - for ( const key of usesContext ) { - context[ key ] = blockContext[ key ]; - } - } const fieldsListResult = getFieldsList( { select, From eb516a8a99541e20aa6222a3c146270f6c62a95f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 6 Oct 2025 12:58:42 +0200 Subject: [PATCH 5/5] Remove now-redundant source data filtering --- .../block-editor/src/hooks/block-bindings.js | 30 +++++-------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/packages/block-editor/src/hooks/block-bindings.js b/packages/block-editor/src/hooks/block-bindings.js index 58a639a1afc785..ed3f0b68186980 100644 --- a/packages/block-editor/src/hooks/block-bindings.js +++ b/packages/block-editor/src/hooks/block-bindings.js @@ -29,7 +29,6 @@ import { useBlockBindingsUtils } from '../utils/block-bindings'; import { unlock } from '../lock-unlock'; import InspectorControls from '../components/inspector-controls'; import BlockContext from '../components/block-context'; -import { useBlockEditContext } from '../components/block-edit'; import { store as blockEditorStore } from '../store'; const { Menu } = unlock( componentsPrivateApis ); @@ -55,34 +54,19 @@ function BlockBindingsPanelMenuContent( { sources, onOpenModal, } ) { - const { clientId } = useBlockEditContext(); const { updateBlockBindings } = useBlockBindingsUtils(); const isMobile = useViewportMatch( 'medium', '<' ); const blockContext = useContext( BlockContext ); - const { attributeType, select } = useSelect( - ( _select ) => { - const { name: blockName } = - _select( blockEditorStore ).getBlock( clientId ); - const _attributeType = - getBlockType( blockName ).attributes?.[ attribute ]?.type; - return { - attributeType: - _attributeType === 'rich-text' ? 'string' : _attributeType, - select: _select, - }; - }, - [ clientId, attribute ] - ); + const { select } = useSelect( ( _select ) => { + return { + select: _select, + }; + }, [] ); return ( { Object.entries( sources ).map( ( [ sourceKey, source ] ) => { - // Only show sources that have compatible data for this specific attribute. - const sourceDataItems = source.data?.filter( - ( item ) => item?.type === attributeType - ); - const noItemsAvailable = - ! sourceDataItems || sourceDataItems.length === 0; + ! source.data || source.data?.length === 0; if ( source.mode === 'dropdown' ) { return ( @@ -105,7 +89,7 @@ function BlockBindingsPanelMenuContent( { { ! noItemsAvailable && ( - { sourceDataItems.map( ( item ) => { + { source.data.map( ( item ) => { const itemBindings = { source: sourceKey, args: item?.args || {