diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 0ab3ae4259da5..cbe8d527a8e40 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -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. *