Skip to content

Commit

Permalink
Include the block variations on the inserter selector
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Sep 9, 2020
1 parent 51e5a2c commit 595547d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 173 deletions.
6 changes: 1 addition & 5 deletions packages/block-editor/src/autocompleters/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { useMemo } from '@wordpress/element';
*/
import { searchBlockItems } from '../components/inserter/search-items';
import useBlockTypesState from '../components/inserter/hooks/use-block-types-state';
import { includeVariationsInInserterItems } from '../components/inserter/utils';
import BlockIcon from '../components/block-icon';

const SHOWN_BLOCK_TYPES = 9;
Expand Down Expand Up @@ -98,10 +97,7 @@ function createBlockCompleter() {

const options = useMemo(
() =>
includeVariationsInInserterItems(
filteredItems,
SHOWN_BLOCK_TYPES
).map( ( blockItem ) => {
filteredItems.map( ( blockItem ) => {
const { title, icon, isDisabled } = blockItem;
return {
key: `block-${ blockItem.id }`,
Expand Down
10 changes: 2 additions & 8 deletions packages/block-editor/src/components/block-types-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@ import { useEffect } from '@wordpress/element';
* Internal dependencies
*/
import InserterListItem from '../inserter-list-item';
import { includeVariationsInInserterItems } from '../inserter/utils';

function BlockTypesList( {
items = [],
onSelect,
onHover = () => {},
children,
label,
limit,
} ) {
const composite = useCompositeState();
const normalizedItems = includeVariationsInInserterItems( items, limit );
const orderId = normalizedItems.reduce(
( acc, item ) => acc + '--' + item.id,
''
);
const orderId = items.reduce( ( acc, item ) => acc + '--' + item.id, '' );

// This ensures the composite state refreshes when the list order changes.
useEffect( () => {
Expand All @@ -47,7 +41,7 @@ function BlockTypesList( {
className="block-editor-block-types-list"
aria-label={ label }
>
{ normalizedItems.map( ( item ) => {
{ items.map( ( item ) => {
return (
<InserterListItem
key={ item.id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export function BlockTypesTab( {
onSelect={ onSelectItem }
onHover={ onHover }
label={ _x( 'Most used', 'blocks' ) }
limit={ MAX_SUGGESTED_ITEMS }
/>
</InserterPanel>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ function QuickInserterList( {
onSelect={ onSelectBlockType }
onHover={ onHover }
label={ __( 'Blocks' ) }
limit={ SHOWN_BLOCK_TYPES }
/>
</InserterPanel>
) }
Expand Down
99 changes: 0 additions & 99 deletions packages/block-editor/src/components/inserter/test/utils.js

This file was deleted.

58 changes: 0 additions & 58 deletions packages/block-editor/src/components/inserter/utils.js

This file was deleted.

48 changes: 47 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,29 @@ const canIncludeBlockTypeInInserter = ( state, blockType, rootClientId ) => {
return canInsertBlockTypeUnmemoized( state, blockType.name, rootClientId );
};

/**
* Return a function to be used to tranform a block variation to an inserter item
*
* @param {Object} item Denormalized inserter item
* @return {Function} Function to transform a block variation to inserter item
*/
const getItemFromVariation = ( item ) => ( variation ) => ( {
...item,
id: `${ item.id }-${ variation.name }`,
icon: variation.icon || item.icon,
title: variation.title || item.title,
description: variation.description || item.description,
// If `example` is explicitly undefined for the variation, the preview will not be shown.
example: variation.hasOwnProperty( 'example' )
? variation.example
: item.example,
initialAttributes: {
...item.initialAttributes,
...variation.attributes,
},
innerBlocks: variation.innerBlocks,
} );

/**
* Determines the items that appear in the inserter. Includes both static
* items (e.g. a regular block type) and dynamic items (e.g. a reusable block).
Expand Down Expand Up @@ -1502,7 +1525,30 @@ export const getInserterItems = createSelector(
? getReusableBlocks( state ).map( buildReusableBlockInserterItem )
: [];

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

// Fill `variationsToAdd` until there are as many items in total as
// `limit`.
const blockVariations = [];
// Show all available blocks with variations
for ( const item of blockTypeInserterItems ) {
const { variations = [] } = item;
if ( variations.length ) {
const variationMapper = getItemFromVariation( item );
blockVariations.push( ...variations.map( variationMapper ) );
}
}

return [
...visibleBlockTypeInserterItems,
...blockVariations,
...reusableBlockInserterItems,
];
},
( state, rootClientId ) => [
state.blockListSettings[ rootClientId ],
Expand Down

0 comments on commit 595547d

Please sign in to comment.