Skip to content

Commit

Permalink
Block Editor: Fix 'isBlockVisibleInTheInserter' selector helper perfo…
Browse files Browse the repository at this point in the history
…rmance (WordPress#68898)

* Block Editor: Fix 'isBlockVisibleInTheInserter' selector helper performance
* Use the loop, Luke!

Unlinked contributors: ktmn.

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: mmtr <[email protected]>
  • Loading branch information
4 people authored Jan 28, 2025
1 parent 0a26fe6 commit 183f671
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1626,15 +1626,23 @@ const isBlockVisibleInTheInserter = (
Array.isArray( blockType.parent ) ? blockType.parent : []
).concat( Array.isArray( blockType.ancestor ) ? blockType.ancestor : [] );
if ( parents.length > 0 ) {
const rootBlockName = getBlockName( state, rootClientId );
// This is an exception to the rule that says that all blocks are visible in the inserter.
// Blocks that require a given parent or ancestor are only visible if we're within that parent.
return (
parents.includes( 'core/post-content' ) ||
parents.includes( rootBlockName ) ||
getBlockParentsByBlockName( state, rootClientId, parents ).length >
0
);
if ( parents.includes( 'core/post-content' ) ) {
return true;
}

let current = rootClientId;
let hasParent = false;
do {
if ( parents.includes( getBlockName( state, current ) ) ) {
hasParent = true;
break;
}
current = state.blocks.parents.get( current );
} while ( current );

return hasParent;
}

return true;
Expand Down

0 comments on commit 183f671

Please sign in to comment.