Skip to content

Commit

Permalink
reorder blocks and make variations appear right after their source block
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Nov 25, 2021
1 parent dc615f4 commit 32a7053
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
40 changes: 14 additions & 26 deletions packages/block-editor/src/store/selectors.js
Expand Up @@ -1643,22 +1643,19 @@ export const getInserterItems = createSelector(
? getReusableBlocks( state ).map( buildReusableBlockInserterItem )
: [];

// Exclude any block type item that is to be replaced by a default
// variation.
const visibleBlockTypeInserterItems = blockTypeInserterItems.filter(
( { variations = [] } ) =>
! variations.some( ( { isDefault } ) => isDefault )
);

const blockVariations = [];
// Show all available blocks with variations
for ( const item of blockTypeInserterItems ) {
const items = blockTypeInserterItems.reduce( ( accumulator, item ) => {
const { variations = [] } = item;
// Exclude any block type item that is to be replaced by a default variation
if ( ! variations.some( ( { isDefault } ) => isDefault ) ) {
accumulator.push( item );
}
if ( variations.length ) {
const variationMapper = getItemFromVariation( state, item );
blockVariations.push( ...variations.map( variationMapper ) );
accumulator.push( ...variations.map( variationMapper ) );
}
}
return accumulator;
}, [] );

// Ensure core blocks are prioritized in the returned results,
// because third party blocks can be registered earlier than
// the core blocks (usually by using the `init` action),
Expand All @@ -1671,20 +1668,11 @@ export const getInserterItems = createSelector(
type.push( block );
return blocks;
};
const items = visibleBlockTypeInserterItems.reduce( groupByType, {
core: [],
noncore: [],
} );
const variations = blockVariations.reduce( groupByType, {
core: [],
noncore: [],
} );
const sortedBlockTypes = [
...items.core,
...variations.core,
...items.noncore,
...variations.noncore,
];
const {
core: coreItems,
noncore: nonCoreItems,
} = items.reduce( groupByType, { core: [], noncore: [] } );
const sortedBlockTypes = [ ...coreItems, ...nonCoreItems ];
return [ ...sortedBlockTypes, ...reusableBlockInserterItems ];
},
( state, rootClientId ) => [
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/index.js
Expand Up @@ -149,6 +149,8 @@ export const __experimentalGetCoreBlocks = () => [
html,
latestComments,
latestPosts,
templatePart,
query,
logInOut,
mediaText,
missing,
Expand All @@ -171,7 +173,6 @@ export const __experimentalGetCoreBlocks = () => [
postTitle,
preformatted,
pullquote,
query,
queryPagination,
queryPaginationNext,
queryPaginationNumbers,
Expand All @@ -191,7 +192,6 @@ export const __experimentalGetCoreBlocks = () => [
table,
// tableOfContents,
tagCloud,
templatePart,
termDescription,
textColumns,
verse,
Expand Down

0 comments on commit 32a7053

Please sign in to comment.