Skip to content

Commit

Permalink
Revert "[Inserter]: Prioritize blocks per category in main inserter"
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Nov 25, 2021
1 parent 18024d9 commit dc615f4
Showing 1 changed file with 1 addition and 54 deletions.
55 changes: 1 addition & 54 deletions packages/block-editor/src/components/inserter/block-types-tab.js
Expand Up @@ -30,17 +30,6 @@ const MAX_SUGGESTED_ITEMS = 6;
*/
const EMPTY_ARRAY = [];

// Blocks that should be prioritized in the Inserter tab.
const PRIORITIZED_BLOCKS = {
theme: [
'core/navigation',
'core/template-part/header',
'core/template-part/footer',
'core/query/posts-list',
],
// design: [],
};

export function BlockTypesTab( {
rootClientId,
onInsert,
Expand Down Expand Up @@ -73,47 +62,6 @@ export function BlockTypesTab( {
)( items );
}, [ items ] );

const sortPrioritizedBlocks = ( _category ) => {
const categoryPrioritizedBlocks = PRIORITIZED_BLOCKS[ _category ];
/**
* We always add one(1) to the returned index to simplify our checks for no
* matches and zero index handling.
*
* For example when `findIndex` doesn't find a match will return `-1`,
* so by adding `1` will become zero that will help us use `boolean` checks.
*
* @param {string} blockId The block id is the block name. In block variations the variation name is appended.
*/
const getBlockIndex = ( blockId ) =>
categoryPrioritizedBlocks.findIndex(
( _blockId ) => _blockId === blockId
) + 1;
return ( a, b ) => {
const firstBlockIndex = getBlockIndex( a.id );
const secondBlockIndex = getBlockIndex( b.id );
if ( !! firstBlockIndex && !! secondBlockIndex ) {
/**
* We also need to check our prefered order, because items order
* depends on block declaration order as well.
*/
return firstBlockIndex > secondBlockIndex ? 1 : -1;
}
return !! firstBlockIndex && ! secondBlockIndex ? -1 : 1;
};
};
// Prioritize blocks's display order in Inserter per block category.
const prioritizedItemsPerCategory = useMemo( () => {
return Object.entries( itemsPerCategory ).reduce(
( accumulator, [ category, _items ] ) => {
accumulator[ category ] = ! PRIORITIZED_BLOCKS[ category ]
? _items
: [ ..._items ].sort( sortPrioritizedBlocks( category ) );
return accumulator;
},
{}
);
}, [ itemsPerCategory ] );

const itemsPerCollection = useMemo( () => {
// Create a new Object to avoid mutating collection.
const result = { ...collections };
Expand Down Expand Up @@ -165,8 +113,7 @@ export function BlockTypesTab( {
) }

{ map( currentlyRenderedCategories, ( category ) => {
const categoryItems =
prioritizedItemsPerCategory[ category.slug ];
const categoryItems = itemsPerCategory[ category.slug ];
if ( ! categoryItems || ! categoryItems.length ) {
return null;
}
Expand Down

0 comments on commit dc615f4

Please sign in to comment.