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

Limit the block slash inserter to 9 items and show most used by default #25113

Merged
merged 4 commits into from
Sep 9, 2020
Merged
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
63 changes: 36 additions & 27 deletions packages/block-editor/src/autocompleters/block.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { noop, map } from 'lodash';
import { noop, map, orderBy } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -18,6 +18,8 @@ import useBlockTypesState from '../components/inserter/hooks/use-block-types-sta
import { includeVariationsInInserterItems } from '../components/inserter/utils';
import BlockIcon from '../components/block-icon';

const SHOWN_BLOCK_TYPES = 9;

const createBlocksFromInnerBlocksTemplate = ( innerBlocksTemplate ) => {
return map(
innerBlocksTemplate,
Expand Down Expand Up @@ -74,12 +76,18 @@ function createBlockCompleter() {
);

const filteredItems = useMemo( () => {
return searchBlockItems(
items,
categories,
collections,
filterValue
).filter( ( item ) => item.name !== selectedBlockName );
const initialFilteredItems = !! filterValue.trim()
? searchBlockItems(
items,
categories,
collections,
filterValue
)
: orderBy( items, [ 'frecency' ], [ 'desc' ] );

return initialFilteredItems
.filter( ( item ) => item.name !== selectedBlockName )
.slice( 0, SHOWN_BLOCK_TYPES );
}, [
filterValue,
selectedBlockName,
Expand All @@ -90,26 +98,27 @@ function createBlockCompleter() {

const options = useMemo(
() =>
includeVariationsInInserterItems( filteredItems ).map(
( blockItem ) => {
const { title, icon, isDisabled } = blockItem;
return {
key: `block-${ blockItem.id }`,
value: blockItem,
label: (
<>
<BlockIcon
key="icon"
icon={ icon }
showColors
/>
{ title }
</>
),
isDisabled,
};
}
),
includeVariationsInInserterItems(
filteredItems,
SHOWN_BLOCK_TYPES
).map( ( blockItem ) => {
const { title, icon, isDisabled } = blockItem;
return {
key: `block-${ blockItem.id }`,
value: blockItem,
label: (
<>
<BlockIcon
key="icon"
icon={ icon }
showColors
/>
{ title }
</>
),
isDisabled,
};
} ),
[ filteredItems ]
);

Expand Down