Skip to content

Commit

Permalink
Block Hooks: Do not remove toggle if hooked block is present elsewhere (
Browse files Browse the repository at this point in the history
#57928)

Don't remove hooked block toggle from "Plugins" panel if the hooked block is not in its expected location but elsewhere in the block tree.

Fixes https://core.trac.wordpress.org/ticket/59955.
  • Loading branch information
ockham committed Jan 23, 2024
1 parent 41d6a41 commit e8dbce0
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions packages/block-editor/src/hooks/block-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ function BlockHooksControlPure( { name, clientId } ) {
const _hookedBlockClientIds = hookedBlocksForCurrentBlock.reduce(
( clientIds, block ) => {
// If the block doesn't exist anywhere in the block tree,
// we know that we have to display the toggle for it, and set
// it to disabled.
// we know that we have to set the toggle to disabled.
if ( getGlobalBlockCount( block.name ) === 0 ) {
return clientIds;
}
Expand Down Expand Up @@ -96,13 +95,8 @@ function BlockHooksControlPure( { name, clientId } ) {
}

// If no hooked block was found in any of its designated locations,
// but it exists elsewhere in the block tree, we consider it manually inserted.
// In this case, we take note and will remove the corresponding toggle from the
// block inspector panel.
return {
...clientIds,
[ block.name ]: false,
};
// we set the toggle to disabled.
return clientIds;
},
{}
);
Expand All @@ -118,13 +112,7 @@ function BlockHooksControlPure( { name, clientId } ) {

const { insertBlock, removeBlock } = useDispatch( blockEditorStore );

// Remove toggle if block isn't present in the designated location but elsewhere in the block tree.
const hookedBlocksForCurrentBlockIfNotPresentElsewhere =
hookedBlocksForCurrentBlock?.filter(
( block ) => hookedBlockClientIds?.[ block.name ] !== false
);

if ( ! hookedBlocksForCurrentBlockIfNotPresentElsewhere.length ) {
if ( ! hookedBlocksForCurrentBlock.length ) {
return null;
}

Expand Down

0 comments on commit e8dbce0

Please sign in to comment.