Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Order ids in getClientIdsOfDescendants and getClientIdsWithDescendant…
…s selectors.
  • Loading branch information
ZebulanStanphill committed Apr 1, 2022
commit 7f0dedc2f57358e655cd2d5fb550f01f584cd49e
10 changes: 6 additions & 4 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,9 @@ _Properties_

### getClientIdsOfDescendants

Returns an array containing the clientIds of all descendants
of the blocks given.
Returns an array containing the clientIds of all descendants of the
blocks given. Ids are returned in the same order that they appear in
the editor.

_Parameters_

Expand All @@ -450,8 +451,9 @@ _Returns_

### getClientIdsWithDescendants

Returns an array containing the clientIds of the top-level blocks
and their descendants of any depth (for nested blocks).
Returns an array containing the clientIds of the top-level blocks and
their descendants of any depth (for nested blocks). Ids are returned
in the same order that they appear in the editor.

_Parameters_

Expand Down
36 changes: 17 additions & 19 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import {
castArray,
flatMap,
first,
isArray,
isBoolean,
Expand Down Expand Up @@ -217,39 +216,38 @@ export const __unstableGetClientIdsTree = createSelector(
);

/**
* Returns an array containing the clientIds of all descendants
* of the blocks given.
* Returns an array containing the clientIds of all descendants of the
* blocks given. Ids are returned in the same order that they appear in
* the editor.
*
* @param {Object} state Global application state.
* @param {Array} clientIds Array of blocks to inspect.
*
* @return {Array} ids of descendants.
*/
export const getClientIdsOfDescendants = ( state, clientIds ) =>
flatMap( clientIds, ( clientId ) => {
const descendants = getBlockOrder( state, clientId );
return [
...descendants,
...getClientIdsOfDescendants( state, descendants ),
];
} );
clientIds.flatMap( ( clientId ) =>
getBlockOrder( state, clientId ).flatMap( ( descendantId ) => [
descendantId,
...getClientIdsOfDescendants( state, [ descendantId ] ),
] )
);

/**
* Returns an array containing the clientIds of the top-level blocks
* and their descendants of any depth (for nested blocks).
* Returns an array containing the clientIds of the top-level blocks and
* their descendants of any depth (for nested blocks). Ids are returned
* in the same order that they appear in the editor.
*
* @param {Object} state Global application state.
*
* @return {Array} ids of top-level and descendant blocks.
*/
export const getClientIdsWithDescendants = createSelector(
( state ) => {
const topLevelIds = getBlockOrder( state );
return [
...topLevelIds,
...getClientIdsOfDescendants( state, topLevelIds ),
];
},
( state ) =>
getBlockOrder( state ).flatMap( ( topLevelId ) => [
topLevelId,
...getClientIdsOfDescendants( state, [ topLevelId ] ),
] ),
( state ) => [ state.blocks.order ]
);

Expand Down
10 changes: 5 additions & 5 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ describe( 'selectors', () => {
} );

describe( 'getClientIdsOfDescendants', () => {
it( 'should return the ids of any descendants, given an array of clientIds', () => {
it( 'should return the ids of any descendants in sequential order, given an array of clientIds', () => {
const state = {
blocks: {
byClientId: {
Expand Down Expand Up @@ -541,8 +541,8 @@ describe( 'selectors', () => {
getClientIdsOfDescendants( state, [ 'uuid-10' ] )
).toEqual( [
'uuid-12',
'uuid-14',
'uuid-16',
'uuid-14',
'uuid-18',
'uuid-24',
'uuid-26',
Expand All @@ -553,7 +553,7 @@ describe( 'selectors', () => {
} );

describe( 'getClientIdsWithDescendants', () => {
it( 'should return the ids for top-level blocks and their descendants of any depth (for nested blocks).', () => {
it( 'should return the ids for top-level blocks and their descendants of any depth (for nested blocks) in sequential order.', () => {
const state = {
blocks: {
byClientId: {
Expand Down Expand Up @@ -645,15 +645,15 @@ describe( 'selectors', () => {
'uuid-6',
'uuid-8',
'uuid-10',
'uuid-22',
'uuid-12',
'uuid-14',
'uuid-16',
'uuid-14',
'uuid-18',
'uuid-24',
'uuid-26',
'uuid-28',
'uuid-30',
'uuid-22',
] );
} );
} );
Expand Down