Skip to content

Commit

Permalink
Block editor: optimise getGlobalBlockCount/getClientIdsWithDescendants (
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Jan 30, 2024
1 parent a3a55bb commit 3fc9c5e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ Returns an array containing the clientIds of all descendants of the blocks given
_Parameters_

- _state_ `Object`: Global application state.
- _clientIds_ `string|string[]`: Client ID(s) for which descendant blocks are to be returned.
- _rootIds_ `string|string[]`: Client ID(s) for which descendant blocks are to be returned.

_Returns_

Expand Down
62 changes: 34 additions & 28 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,24 +252,37 @@ export const __unstableGetClientIdsTree = createSelector(
* given. Returned ids are ordered first by the order of the ids given, then
* by the order that they appear in the editor.
*
* @param {Object} state Global application state.
* @param {string|string[]} clientIds Client ID(s) for which descendant blocks are to be returned.
* @param {Object} state Global application state.
* @param {string|string[]} rootIds Client ID(s) for which descendant blocks are to be returned.
*
* @return {Array} Client IDs of descendants.
*/
export const getClientIdsOfDescendants = createSelector(
( state, clientIds ) => {
const givenIds = Array.isArray( clientIds ) ? clientIds : [ clientIds ];
const collectedIds = [];
for ( const givenId of givenIds ) {
for ( const descendantId of getBlockOrder( state, givenId ) ) {
collectedIds.push(
descendantId,
...getClientIdsOfDescendants( state, descendantId )
);
( state, rootIds ) => {
rootIds = Array.isArray( rootIds ) ? [ ...rootIds ] : [ rootIds ];
const ids = [];

// Add the descendants of the root blocks first.
for ( const rootId of rootIds ) {
const order = state.blocks.order.get( rootId );
if ( order ) {
ids.push( ...order );
}
}

let index = 0;

// Add the descendants of the descendants, recursively.
while ( index < ids.length ) {
const id = ids[ index ];
const order = state.blocks.order.get( id );
if ( order ) {
ids.splice( index + 1, 0, ...order );
}
index++;
}
return collectedIds;

return ids;
},
( state ) => [ state.blocks.order ]
);
Expand All @@ -283,19 +296,8 @@ export const getClientIdsOfDescendants = createSelector(
*
* @return {Array} ids of top-level and descendant blocks.
*/
export const getClientIdsWithDescendants = createSelector(
( state ) => {
const collectedIds = [];
for ( const topLevelId of getBlockOrder( state ) ) {
collectedIds.push(
topLevelId,
...getClientIdsOfDescendants( state, topLevelId )
);
}
return collectedIds;
},
( state ) => [ state.blocks.order ]
);
export const getClientIdsWithDescendants = ( state ) =>
getClientIdsOfDescendants( state, '' );

/**
* Returns the total number of blocks, or the total number of blocks with a specific name in a post.
Expand All @@ -312,10 +314,14 @@ export const getGlobalBlockCount = createSelector(
if ( ! blockName ) {
return clientIds.length;
}
return clientIds.reduce( ( accumulator, clientId ) => {
let count = 0;
for ( const clientId of clientIds ) {
const block = state.blocks.byClientId.get( clientId );
return block.name === blockName ? accumulator + 1 : accumulator;
}, 0 );
if ( block.name === blockName ) {
count++;
}
}
return count;
},
( state ) => [ state.blocks.order, state.blocks.byClientId ]
);
Expand Down

1 comment on commit 3fc9c5e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 3fc9c5e.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7707165905
📝 Reported issues:

Please sign in to comment.