Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: Fix 'isBlockVisibleInTheInserter' selector helper performance #68898

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,33 @@ export const getBlockParents = createSelector(
( state ) => [ state.blocks.parents ]
);

/**
* Given a block client ID and a block name, returns the list of all its parents
* from top to bottom, filtered by the given name(s).
*
* The function is not exported and not memoized because
* it's not efficient to call memoized selectors inside other selectors.
*
* @param {Object} state Editor state.
* @param {string} clientId Block from which to find root client ID.
* @param {string|string[]} blockName Block name(s) to filter.
* @param {boolean} ascending Order results from bottom to top (true) or top to bottom (false).
*
* @return {Array} ClientIDs of the parent blocks.
*/
function getBlockParentsByBlockNameUnmemoized(
state,
clientId,
blockName,
ascending = false
) {
const parents = getBlockParents( state, clientId, ascending );
const hasName = Array.isArray( blockName )
? ( name ) => blockName.includes( name )
: ( name ) => blockName === name;
return parents.filter( ( id ) => hasName( getBlockName( state, id ) ) );
}

/**
* Given a block client ID and a block name, returns the list of all its parents
* from top to bottom, filtered by the given name(s). For example, if passed
Expand All @@ -637,13 +664,7 @@ export const getBlockParents = createSelector(
* @return {Array} ClientIDs of the parent blocks.
*/
export const getBlockParentsByBlockName = createSelector(
( state, clientId, blockName, ascending = false ) => {
const parents = getBlockParents( state, clientId, ascending );
const hasName = Array.isArray( blockName )
? ( name ) => blockName.includes( name )
: ( name ) => blockName === name;
return parents.filter( ( id ) => hasName( getBlockName( state, id ) ) );
},
getBlockParentsByBlockNameUnmemoized,
( state ) => [ state.blocks.parents ]
);
/**
Expand Down Expand Up @@ -1629,11 +1650,16 @@ const isBlockVisibleInTheInserter = (
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 (
if (
parents.includes( 'core/post-content' ) ||
parents.includes( rootBlockName ) ||
getBlockParentsByBlockName( state, rootClientId, parents ).length >
0
parents.includes( rootBlockName )
) {
return true;
}

return (
getBlockParentsByBlockNameUnmemoized( state, rootClientId, parents )
.length > 0
);
}

Expand Down
Loading