Skip to content

Commit

Permalink
wip add flat list selector
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Aug 12, 2021
1 parent 1b9dd08 commit 5268ff8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2230,6 +2230,38 @@ export const __experimentalGetActiveBlockIdByBlockNames = createSelector(
]
);

/**
* Flat representation of blocks. This is useful when rendering the list view.
*/
export const __experimentalGetFlatList = createSelector(
( state, withoutIds = [], parentId = '', level = 0 ) => {
let skipModifier = 0;
return getBlockOrder( state, parentId ).reduce(
( list, clientId, index ) => {
if ( withoutIds.indexOf( clientId ) !== -1 ) {
skipModifier++;
return list;
}
return list.concat( [
{ clientId, level, parentId, index: index - skipModifier },
...__experimentalGetFlatList(
state,
withoutIds,
clientId,
level + 1
),
] );
},
[]
);
},
( state, parentId, withoutIds ) => [
state.blocks.order,
parentId,
withoutIds,
]
);

/**
* Tells if the block with the passed clientId was just inserted.
*
Expand Down

0 comments on commit 5268ff8

Please sign in to comment.