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 Hooks: Display toggle for hooked blocks added via filter #59396

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = [] } = {},
ockham marked this conversation as resolved.
Show resolved Hide resolved
} ) {
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
Loading