Skip to content

Commit

Permalink
findCriticalBlocks: don't dismiss children of matching node
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
mcsf committed Jun 6, 2023
1 parent f0247fe commit c972adf
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions packages/block-editor/src/utils/show-block-removal-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) => {
Expand Down

0 comments on commit c972adf

Please sign in to comment.