From 8f1e950e2fe16f04c307f1feaffa5ca05fd0ea97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Su=C3=A1rez?= Date: Mon, 1 Apr 2024 11:38:34 +0100 Subject: [PATCH] refact canBindBlock based on source handler settings --- .../src/hooks/use-bindings-attributes.js | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index 565a196fd69d9..bcf43478989ff 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -3,7 +3,7 @@ */ import { getBlockType, store as blocksStore } from '@wordpress/blocks'; import { createHigherOrderComponent } from '@wordpress/compose'; -import { useSelect } from '@wordpress/data'; +import { useSelect, select } from '@wordpress/data'; import { useLayoutEffect, useCallback, useState } from '@wordpress/element'; import { addFilter } from '@wordpress/hooks'; import { RichTextData } from '@wordpress/rich-text'; @@ -34,15 +34,26 @@ const DEFAULT_BLOCK_BINDINGS_ALLOWED_BLOCKS = { * Based on the given block name, * check if it is possible to bind the block. * - * @param {string} blockName - The block name. - * @param {Object} allowBlocks - The allowed blocks settings. + * @param {string} blockName - The block name. * @return {boolean} Whether it is possible to bind the block to sources. */ -export function canBindBlock( - blockName, - allowBlocks = DEFAULT_BLOCK_BINDINGS_ALLOWED_BLOCKS -) { - return blockName in allowBlocks; +export function canBindBlock( blockName ) { + // Pick blocks list from all source handlers settings. + const blockBindingsSources = unlock( + select( blocksStore ) + ).getAllBlockBindingsSources(); + + let blockNames = Object.keys( DEFAULT_BLOCK_BINDINGS_ALLOWED_BLOCKS ); + Object.keys( blockBindingsSources ).forEach( ( sourceName ) => { + const blocks = blockBindingsSources[ sourceName ].settings?.blocks; + if ( blocks ) { + blockNames.push( ...Object.keys( blocks ) ); + } + } ); + + blockNames = [ ...new Set( blockNames ) ]; + + return blockNames.includes( blockName ); } /** @@ -59,10 +70,7 @@ export function canBindAttribute( attributeName, allowBlocks = DEFAULT_BLOCK_BINDINGS_ALLOWED_BLOCKS ) { - return ( - canBindBlock( blockName, allowBlocks ) && - allowBlocks[ blockName ]?.includes( attributeName ) - ); + return allowBlocks[ blockName ]?.includes( attributeName ); } /**