Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Bindings: set allow blocks list from source handler #60335

Open
wants to merge 9 commits into
base: trunk
Choose a base branch
from
Prev Previous commit
Next Next commit
refact canBindBlock based on source handler settings
retrofox committed Apr 13, 2024
commit 44bf2b6a4ecf24e4af31c4df8393448be62eac22
32 changes: 20 additions & 12 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
@@ -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 );
}

/**