From 183f671258824a6b32b881c0ab8f739ae081812f Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 28 Jan 2025 16:13:25 +0400 Subject: [PATCH] Block Editor: Fix 'isBlockVisibleInTheInserter' selector helper performance (#68898) * Block Editor: Fix 'isBlockVisibleInTheInserter' selector helper performance * Use the loop, Luke! Unlinked contributors: ktmn. Co-authored-by: Mamaduka Co-authored-by: youknowriad Co-authored-by: mmtr --- packages/block-editor/src/store/selectors.js | 22 +++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 22d725bbcd65de..e2e2ef6b655564 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -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;