Skip to content

Commit

Permalink
prioritze core blocks in the inserter context
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Feb 12, 2021
1 parent 2c56f53 commit 3eed9a1
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/block-editor/src/components/inserter/block-types-tab.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { map, findIndex, flow, sortBy, groupBy, orderBy } from 'lodash';
import { map, flow, groupBy, orderBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -43,19 +43,23 @@ export function BlockTypesTab( {
}, [ items ] );

const itemsPerCategory = useMemo( () => {
const getCategoryIndex = ( item ) => {
return findIndex(
categories,
( category ) => category.slug === item.category
);
// Prioritize core blocks's display in inserter.
const sortCoreBlocks = ( a, b ) => {
const coreBlockNamePrefix = 'core/';
const firstIsCoreBlock = a.name.startsWith( coreBlockNamePrefix );
const secondIsCoreBlock = b.name.startsWith( coreBlockNamePrefix );
if ( firstIsCoreBlock && secondIsCoreBlock ) {
return 0;
}
return firstIsCoreBlock && ! secondIsCoreBlock ? -1 : 1;
};

return flow(
( itemList ) =>
itemList.filter(
( item ) => item.category && item.category !== 'reusable'
),
( itemList ) => sortBy( itemList, getCategoryIndex ),
( itemList ) => itemList.sort( sortCoreBlocks ),
( itemList ) => groupBy( itemList, 'category' )
)( items );
}, [ items, categories ] );
Expand Down

0 comments on commit 3eed9a1

Please sign in to comment.