From a44020d8fddaf7d4d9665d74f70c4d421d87cfd7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 27 Feb 2024 11:27:34 +0100 Subject: [PATCH 1/4] Block Hooks: Show toggle for hooked blocks added by filter --- packages/block-editor/src/hooks/block-hooks.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/hooks/block-hooks.js b/packages/block-editor/src/hooks/block-hooks.js index eb84352ab62f09..70c680ea61ff1a 100644 --- a/packages/block-editor/src/hooks/block-hooks.js +++ b/packages/block-editor/src/hooks/block-hooks.js @@ -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( @@ -219,6 +229,7 @@ function BlockHooksControlPure( { name, clientId } ) { export default { edit: BlockHooksControlPure, + attributeKeys: [ 'metadata' ], hasSupport() { return true; }, From 4eaf47960c679c7a22c0320a4dc542c524206cd0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 27 Feb 2024 11:28:39 +0100 Subject: [PATCH 2/4] Set toggle status correctly --- packages/block-editor/src/hooks/block-hooks.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/block-editor/src/hooks/block-hooks.js b/packages/block-editor/src/hooks/block-hooks.js index 70c680ea61ff1a..77b040374f6554 100644 --- a/packages/block-editor/src/hooks/block-hooks.js +++ b/packages/block-editor/src/hooks/block-hooks.js @@ -89,6 +89,16 @@ function BlockHooksControlPure( { // inserted and then moved around a bit by the user. candidates = getBlocks( clientId ); break; + + default: + // 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( From 349298cabd9ebfcf7a6b2aefad98f4f0f0211e36 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 27 Feb 2024 11:34:42 +0100 Subject: [PATCH 3/4] Make an educated guess when inserting --- packages/block-editor/src/hooks/block-hooks.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/block-editor/src/hooks/block-hooks.js b/packages/block-editor/src/hooks/block-hooks.js index 77b040374f6554..c69aeef5e9e038 100644 --- a/packages/block-editor/src/hooks/block-hooks.js +++ b/packages/block-editor/src/hooks/block-hooks.js @@ -171,6 +171,18 @@ function BlockHooksControlPure( { false ); break; + + default: + // 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; } }; From addb757c9fdf444a61c2de5993541db8af85f88c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 28 Feb 2024 14:24:32 +0100 Subject: [PATCH 4/4] Change default: to case undefined: --- packages/block-editor/src/hooks/block-hooks.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/hooks/block-hooks.js b/packages/block-editor/src/hooks/block-hooks.js index c69aeef5e9e038..93bf87f42124b1 100644 --- a/packages/block-editor/src/hooks/block-hooks.js +++ b/packages/block-editor/src/hooks/block-hooks.js @@ -90,7 +90,7 @@ function BlockHooksControlPure( { candidates = getBlocks( clientId ); break; - default: + 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. @@ -172,7 +172,7 @@ function BlockHooksControlPure( { ); break; - default: + 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.