From 3eed9a107781d9e46d35bd72fc3fea6666a42633 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 11 Feb 2021 14:59:27 +0200 Subject: [PATCH] prioritze core blocks in the inserter context --- .../src/components/inserter/block-types-tab.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/components/inserter/block-types-tab.js b/packages/block-editor/src/components/inserter/block-types-tab.js index 94b76b02ac694..2044204d1f167 100644 --- a/packages/block-editor/src/components/inserter/block-types-tab.js +++ b/packages/block-editor/src/components/inserter/block-types-tab.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { map, findIndex, flow, sortBy, groupBy, orderBy } from 'lodash'; +import { map, flow, groupBy, orderBy } from 'lodash'; /** * WordPress dependencies @@ -43,11 +43,15 @@ 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( @@ -55,7 +59,7 @@ export function BlockTypesTab( { itemList.filter( ( item ) => item.category && item.category !== 'reusable' ), - ( itemList ) => sortBy( itemList, getCategoryIndex ), + ( itemList ) => itemList.sort( sortCoreBlocks ), ( itemList ) => groupBy( itemList, 'category' ) )( items ); }, [ items, categories ] );