From b3e3df5c99f1703fe77821f62c5a910c4e7af68e Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:06:27 +0100 Subject: [PATCH 1/2] Use role content to enable block bindings attributes --- .../block-editor/src/utils/block-bindings.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/packages/block-editor/src/utils/block-bindings.js b/packages/block-editor/src/utils/block-bindings.js index dcf80d985473b2..4088406d72daed 100644 --- a/packages/block-editor/src/utils/block-bindings.js +++ b/packages/block-editor/src/utils/block-bindings.js @@ -2,6 +2,7 @@ * WordPress dependencies */ import { useDispatch, useRegistry } from '@wordpress/data'; +import { getBlockType } from '@wordpress/blocks'; /** * Internal dependencies @@ -13,6 +14,50 @@ function isObjectEmpty( object ) { return ! object || Object.keys( object ).length === 0; } +export const BLOCK_BINDINGS_ALLOWED_BLOCKS = { + 'core/paragraph': [ 'content' ], + 'core/heading': [ 'content' ], + 'core/image': [ 'id', 'url', 'title', 'alt' ], + 'core/button': [ 'url', 'text', 'linkTarget', 'rel' ], +}; + +/** + * 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; +} + +/** + * 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 getBindableAttributes( blockName ) { + const blockType = getBlockType( blockName ); + const allowedAttributes = BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ] || []; + // Still comparing with the allowed blocks constant until we can automate the bindings server process. + return Object.keys( blockType.attributes ).filter( + ( key ) => + blockType.attributes[ key ].role === 'content' && + allowedAttributes.includes( key ) + ); +} + /** * Contains utils to update the block `bindings` metadata. * From cef5d2d11081961fba2b1be6f744db9874972e25 Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:50:34 +0100 Subject: [PATCH 2/2] Update with last revert --- .../src/hooks/use-bindings-attributes.js | 11 ++++- .../block-editor/src/utils/block-bindings.js | 45 ------------------- 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index fdc617fda20c05..cee908bf30619f 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'; @@ -93,7 +93,14 @@ export function canBindAttribute( blockName, attributeName ) { } export function getBindableAttributes( blockName ) { - return BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ]; + const blockType = getBlockType( blockName ); + const allowedAttributes = BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ] || []; + // Still comparing with the allowed blocks constant until we can automate the bindings server process. + return Object.keys( blockType.attributes ).filter( + ( key ) => + blockType.attributes[ key ].role === 'content' && + allowedAttributes.includes( key ) + ); } export const withBlockBindingSupport = createHigherOrderComponent( diff --git a/packages/block-editor/src/utils/block-bindings.js b/packages/block-editor/src/utils/block-bindings.js index 4088406d72daed..dcf80d985473b2 100644 --- a/packages/block-editor/src/utils/block-bindings.js +++ b/packages/block-editor/src/utils/block-bindings.js @@ -2,7 +2,6 @@ * WordPress dependencies */ import { useDispatch, useRegistry } from '@wordpress/data'; -import { getBlockType } from '@wordpress/blocks'; /** * Internal dependencies @@ -14,50 +13,6 @@ function isObjectEmpty( object ) { return ! object || Object.keys( object ).length === 0; } -export const BLOCK_BINDINGS_ALLOWED_BLOCKS = { - 'core/paragraph': [ 'content' ], - 'core/heading': [ 'content' ], - 'core/image': [ 'id', 'url', 'title', 'alt' ], - 'core/button': [ 'url', 'text', 'linkTarget', 'rel' ], -}; - -/** - * 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; -} - -/** - * 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 getBindableAttributes( blockName ) { - const blockType = getBlockType( blockName ); - const allowedAttributes = BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ] || []; - // Still comparing with the allowed blocks constant until we can automate the bindings server process. - return Object.keys( blockType.attributes ).filter( - ( key ) => - blockType.attributes[ key ].role === 'content' && - allowedAttributes.includes( key ) - ); -} - /** * Contains utils to update the block `bindings` metadata. *