Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 28, 2024
1 parent 9c030aa commit 27eb256
Show file tree
Hide file tree
Showing 2 changed files with 31 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
58 changes: 30 additions & 28 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,24 +251,33 @@ 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 );
}
}

// Add the descendants of the descendants, recursively.
for ( const id of ids ) {
const order = state.blocks.order.get( id );
if ( order ) {
ids.push( ...order );
}
}
return collectedIds;

return ids;
},
( state ) => [ state.blocks.order ]
);
Expand All @@ -282,19 +291,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 @@ -311,10 +309,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

0 comments on commit 27eb256

Please sign in to comment.