Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inserter: Prevent non-deterministic order of inserter items #34078

Merged
merged 4 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1618,25 +1618,32 @@ export const getInserterItems = createSelector(
blockVariations.push( ...variations.map( variationMapper ) );
}
}
// Prioritize core blocks's display in inserter.
const prioritizeCoreBlocks = ( 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;
};
// 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),
// thus affecting the display order.
// We don't sort reusable blocks as they are handled differently.
const groupByType = ( blocks, block ) => {
const { core, noncore } = blocks;
const type = block.name.startsWith( 'core/' ) ? core : noncore;

type.push( block );
return blocks;
};
const items = visibleBlockTypeInserterItems.reduce( groupByType, {
core: [],
noncore: [],
} );
const variations = blockVariations.reduce( groupByType, {
core: [],
noncore: [],
} );
const sortedBlockTypes = [
...visibleBlockTypeInserterItems,
...blockVariations,
].sort( prioritizeCoreBlocks );
...items.core,
...variations.core,
...items.noncore,
...variations.noncore,
];
return [ ...sortedBlockTypes, ...reusableBlockInserterItems ];
},
( state, rootClientId ) => [
Expand Down
25 changes: 24 additions & 1 deletion packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3725,6 +3725,13 @@ describe( 'getInserterItems with core blocks prioritization', () => {
title: 'Another Plugin Block B',
icon: 'test',
} );
registerBlockType( 'plugin/block-c-with-variations', {
save() {},
category: 'text',
title: 'Plugin Block C with variations',
icon: 'test',
variations: [ { name: 'variation-a' }, { name: 'variation-b' } ],
} );
registerBlockType( 'core/block', {
save() {},
category: 'text',
Expand All @@ -3737,13 +3744,23 @@ describe( 'getInserterItems with core blocks prioritization', () => {
icon: 'test',
keywords: [ 'testing' ],
} );
registerBlockType( 'core/test-block-with-variations', {
save() {},
category: 'text',
title: 'Core Block C with variations',
icon: 'test',
keywords: [ 'testing' ],
variations: [ { name: 'variation-a' }, { name: 'variation-b' } ],
} );
} );
afterEach( () => {
[
'plugin/block-a',
'another-plugin/block-b',
'plugin/block-c-with-variations',
'core/block',
'core/test-block-a',
'core/test-block-with-variations',
].forEach( unregisterBlockType );
} );
it( 'should prioritize core blocks by sorting them at the top of the returned list', () => {
Expand All @@ -3763,10 +3780,16 @@ describe( 'getInserterItems with core blocks prioritization', () => {
const expectedResult = [
'core/block',
'core/test-block-a',
'core/test-block-with-variations',
'core/test-block-with-variations/variation-a',
'core/test-block-with-variations/variation-b',
'plugin/block-a',
'another-plugin/block-b',
'plugin/block-c-with-variations',
'plugin/block-c-with-variations/variation-a',
'plugin/block-c-with-variations/variation-b',
];
expect( items.map( ( { name } ) => name ) ).toEqual( expectedResult );
expect( items.map( ( { id } ) => id ) ).toEqual( expectedResult );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch with id, here 👍🏻

} );
} );

Expand Down