Skip to content

Commit

Permalink
Block Hooks: Display toggle for hooked blocks added via filter (#59396)
Browse files Browse the repository at this point in the history
Show the Block Hooks toggle for hooked blocks that were added via the `hooked_block_types` filter (rather than at block registration time, i.e. via the `blockHooks` field in `block.json`), by evaluating the anchor block's `ignoredHookedBlocks` metadata attribute. (This attribute is only present if the containing template/part/pattern has user modifications.)

Co-authored-by: ockham <bernhard-reiter@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: michalczaplinski <czapla@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
  • Loading branch information
5 people authored Feb 28, 2024
1 parent 9840fbe commit a65b156
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions packages/block-editor/src/hooks/block-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@ import { store as blockEditorStore } from '../store';

const EMPTY_OBJECT = {};

function BlockHooksControlPure( { name, clientId } ) {
function BlockHooksControlPure( {
name,
clientId,
metadata: { ignoredHookedBlocks = [] } = {},
} ) {
const blockTypes = useSelect(
( select ) => select( blocksStore ).getBlockTypes(),
[]
);

// A hooked block added via a filter will not be exposed through a block
// type's `blockHooks` property; however, if the containing layout has been
// modified, it will be present in the anchor block's `ignoredHookedBlocks`
// metadata.
const hookedBlocksForCurrentBlock = useMemo(
() =>
blockTypes?.filter(
( { blockHooks } ) => blockHooks && name in blockHooks
( { name: blockName, blockHooks } ) =>
( blockHooks && name in blockHooks ) ||
ignoredHookedBlocks.includes( blockName )
),
[ blockTypes, name ]
[ blockTypes, name, ignoredHookedBlocks ]
);

const { blockIndex, rootClientId, innerBlocksLength } = useSelect(
Expand Down Expand Up @@ -79,6 +89,16 @@ function BlockHooksControlPure( { name, clientId } ) {
// inserted and then moved around a bit by the user.
candidates = getBlocks( clientId );
break;

case undefined:
// If we haven't found a blockHooks field with a relative position for the hooked
// block, it means that it was added by a filter. In this case, we look for the block
// both among the current block's siblings and its children.
candidates = [
...getBlocks( rootClientId ),
...getBlocks( clientId ),
];
break;
}

const hookedBlock = candidates?.find(
Expand Down Expand Up @@ -151,6 +171,18 @@ function BlockHooksControlPure( { name, clientId } ) {
false
);
break;

case undefined:
// If we do not know the relative position, it is because the block was
// added via a filter. In this case, we default to inserting it after the
// current block.
insertBlock(
block,
blockIndex + 1,
rootClientId, // Insert as a child of the current block's parent
false
);
break;
}
};

Expand Down Expand Up @@ -219,6 +251,7 @@ function BlockHooksControlPure( { name, clientId } ) {

export default {
edit: BlockHooksControlPure,
attributeKeys: [ 'metadata' ],
hasSupport() {
return true;
},
Expand Down

0 comments on commit a65b156

Please sign in to comment.