From c972adf2993a2fe8625ccc33fc0f8edbcc6f3d3d Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Tue, 6 Jun 2023 17:06:27 +0100 Subject: [PATCH] findCriticalBlocks: don't dismiss children of matching node * Refactor by embracing `flatMap` semantics * When `isBlockCritical( blockName )`, don't immediately return: there could be other matching block types within that node's inner blocks. --- .../src/utils/show-block-removal-warning.js | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/block-editor/src/utils/show-block-removal-warning.js b/packages/block-editor/src/utils/show-block-removal-warning.js index 78c6b43c35221..2d3e349efbf27 100644 --- a/packages/block-editor/src/utils/show-block-removal-warning.js +++ b/packages/block-editor/src/utils/show-block-removal-warning.js @@ -41,20 +41,15 @@ export function useBlockRemovalWarning() { useDispatch( blockEditorStore ); function findCriticalBlocks( clientIds ) { - return clientIds - .map( ( clientId ) => { - const blockName = getBlockName( clientId ); - if ( isBlockCritical( blockName ) ) { - return blockName; - } - const innerBlocks = getBlockOrder( clientId ); - if ( innerBlocks.length ) { - return findCriticalBlocks( innerBlocks ); - } - return false; - } ) - .flat() - .filter( ( blockName ) => blockName ); + return clientIds.flatMap( ( clientId ) => { + const found = []; + const blockName = getBlockName( clientId ); + if ( isBlockCritical( blockName ) ) { + found.push( blockName ); + } + const innerBlocks = getBlockOrder( clientId ); + return found.concat( findCriticalBlocks( innerBlocks ) ); + } ); } return ( clientIds, selectPrevious = true ) => {