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: show all blocks (alternative) #62169

Merged
merged 10 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
201 changes: 118 additions & 83 deletions packages/block-editor/src/components/inserter/block-types-tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __, _x } from '@wordpress/i18n';
import { useMemo, useEffect, forwardRef } from '@wordpress/element';
import { pipe, useAsyncList } from '@wordpress/compose';
import { useAsyncList } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -27,15 +27,14 @@ const MAX_SUGGESTED_ITEMS = 6;
*/
const EMPTY_ARRAY = [];

export function BlockTypesTab(
{ rootClientId, onInsert, onHover, showMostUsedBlocks },
ref
) {
const [ items, categories, collections, onSelectItem ] = useBlockTypesState(
rootClientId,
onInsert
);

export function BlockTypesTabPanel( {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is added because we now split the blocks tab in two panels, one for blocks inserted in the current selection insertion point one that can be inserted in the nearest available insertion point relative to the current selection.

items,
collections,
categories,
onSelectItem,
onHover,
showMostUsedBlocks,
} ) {
const suggestedItems = useMemo( () => {
return orderBy( items, 'frecency', 'desc' ).slice(
0,
Expand All @@ -47,24 +46,6 @@ export function BlockTypesTab(
return items.filter( ( item ) => ! item.category );
}, [ items ] );

const itemsPerCategory = useMemo( () => {
return pipe(
( itemList ) =>
itemList.filter(
( item ) => item.category && item.category !== 'reusable'
),
( itemList ) =>
itemList.reduce( ( acc, item ) => {
const { category } = item;
if ( ! acc[ category ] ) {
acc[ category ] = [];
}
acc[ category ].push( item );
return acc;
}, {} )
)( items );
}, [ items ] );

const itemsPerCollection = useMemo( () => {
// Create a new Object to avoid mutating collection.
const result = { ...collections };
Expand Down Expand Up @@ -101,82 +82,136 @@ export function BlockTypesTab(
didRenderAllCategories ? collectionEntries : EMPTY_ARRAY
);

if ( ! items.length ) {
return <InserterNoResults />;
}

return (
<InserterListbox>
<div ref={ ref }>
{ showMostUsedBlocks && !! suggestedItems.length && (
<InserterPanel title={ _x( 'Most used', 'blocks' ) }>
<>
{ showMostUsedBlocks && !! suggestedItems.length && (
<InserterPanel title={ _x( 'Most used', 'blocks' ) }>
<BlockTypesList
items={ suggestedItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ _x( 'Most used', 'blocks' ) }
/>
</InserterPanel>
) }

{ currentlyRenderedCategories.map( ( category ) => {
const categoryItems = items.filter(
( item ) => item.category === category.slug
);
if ( ! categoryItems || ! categoryItems.length ) {
return null;
}
return (
<InserterPanel
key={ category.slug }
title={ category.title }
icon={ category.icon }
>
<BlockTypesList
items={ suggestedItems }
items={ categoryItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ _x( 'Most used', 'blocks' ) }
label={ category.title }
/>
</InserterPanel>
) }

{ currentlyRenderedCategories.map( ( category ) => {
const categoryItems = itemsPerCategory[ category.slug ];
if ( ! categoryItems || ! categoryItems.length ) {
);
} ) }

{ didRenderAllCategories && uncategorizedItems.length > 0 && (
<InserterPanel
className="block-editor-inserter__uncategorized-blocks-panel"
title={ __( 'Uncategorized' ) }
>
<BlockTypesList
items={ uncategorizedItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ __( 'Uncategorized' ) }
/>
</InserterPanel>
) }

{ currentlyRenderedCollections.map(
( [ namespace, collection ] ) => {
const collectionItems = itemsPerCollection[ namespace ];
if ( ! collectionItems || ! collectionItems.length ) {
return null;
}

return (
<InserterPanel
key={ category.slug }
title={ category.title }
icon={ category.icon }
key={ namespace }
title={ collection.title }
icon={ collection.icon }
>
<BlockTypesList
items={ categoryItems }
items={ collectionItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ category.title }
label={ collection.title }
/>
</InserterPanel>
);
} ) }
}
) }
</>
);
}

{ didRenderAllCategories && uncategorizedItems.length > 0 && (
<InserterPanel
className="block-editor-inserter__uncategorized-blocks-panel"
title={ __( 'Uncategorized' ) }
>
<BlockTypesList
items={ uncategorizedItems }
onSelect={ onSelectItem }
export function BlockTypesTab(
{ rootClientId, onInsert, onHover, showMostUsedBlocks },
ref
) {
const [ items, categories, collections, onSelectItem ] = useBlockTypesState(
rootClientId,
onInsert
);

if ( ! items.length ) {
return <InserterNoResults />;
}

const itemsForCurrentRoot = [];
const itemsRemaining = [];

for ( const item of items ) {
// Skip reusable blocks, they moved to the patterns tab.
if ( item.category === 'reusable' ) {
continue;
}

if ( rootClientId && item.rootClientId === rootClientId ) {
itemsForCurrentRoot.push( item );
} else {
itemsRemaining.push( item );
}
}

return (
<InserterListbox>
<div ref={ ref }>
{ !! itemsForCurrentRoot.length && (
<>
<BlockTypesTabPanel
items={ itemsForCurrentRoot }
categories={ categories }
collections={ collections }
onSelectItem={ onSelectItem }
onHover={ onHover }
label={ __( 'Uncategorized' ) }
showMostUsedBlocks={ showMostUsedBlocks }
/>
</InserterPanel>
) }

{ currentlyRenderedCollections.map(
( [ namespace, collection ] ) => {
const collectionItems = itemsPerCollection[ namespace ];
if ( ! collectionItems || ! collectionItems.length ) {
return null;
}

return (
<InserterPanel
key={ namespace }
title={ collection.title }
icon={ collection.icon }
>
<BlockTypesList
items={ collectionItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ collection.title }
/>
</InserterPanel>
);
}
<hr />
</>
) }
<BlockTypesTabPanel
items={ itemsRemaining }
categories={ categories }
collections={ collections }
onSelectItem={ onSelectItem }
onHover={ onHover }
showMostUsedBlocks={ showMostUsedBlocks }
/>
</div>
</InserterListbox>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useCallback } from '@wordpress/element';
* Internal dependencies
*/
import { store as blockEditorStore } from '../../../store';
import { withRootClientIdOptionKey } from '../../../store/utils';

/**
* Retrieves the block types inserter state.
Expand All @@ -25,7 +26,9 @@ import { store as blockEditorStore } from '../../../store';
const useBlockTypesState = ( rootClientId, onInsert ) => {
const [ items ] = useSelect(
( select ) => [
select( blockEditorStore ).getInserterItems( rootClientId ),
select( blockEditorStore ).getInserterItems( rootClientId, {
[ withRootClientIdOptionKey ]: true,
} ),
],
[ rootClientId ]
);
Expand All @@ -37,7 +40,14 @@ const useBlockTypesState = ( rootClientId, onInsert ) => {

const onSelectItem = useCallback(
(
{ name, initialAttributes, innerBlocks, syncStatus, content },
{
name,
initialAttributes,
innerBlocks,
syncStatus,
content,
rootClientId: _rootClientId,
},
shouldFocusBlock
) => {
const insertedBlock =
Expand All @@ -51,7 +61,12 @@ const useBlockTypesState = ( rootClientId, onInsert ) => {
createBlocksFromInnerBlocksTemplate( innerBlocks )
);

onInsert( insertedBlock, undefined, shouldFocusBlock );
onInsert(
insertedBlock,
undefined,
shouldFocusBlock,
_rootClientId
);
},
[ onInsert ]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch, useRegistry, useSelect } from '@wordpress/data';
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import { _n, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
Expand All @@ -13,6 +13,34 @@ import { useCallback } from '@wordpress/element';
import { store as blockEditorStore } from '../../../store';
import { unlock } from '../../../lock-unlock';

function getIndex( {
destinationRootClientId,
destinationIndex,
rootClientId,
registry,
} ) {
if ( rootClientId === destinationRootClientId ) {
return destinationIndex;
}
const parents = [
'',
...registry
.select( blockEditorStore )
.getBlockParents( destinationRootClientId ),
destinationRootClientId,
];
const parentIndex = parents.indexOf( rootClientId );
if ( parentIndex !== -1 ) {
return (
registry
.select( blockEditorStore )
.getBlockIndex( parents[ parentIndex + 1 ] ) + 1
);
}
return registry.select( blockEditorStore ).getBlockOrder( rootClientId )
.length;
}

/**
* @typedef WPInserterConfig
*
Expand Down Expand Up @@ -42,6 +70,7 @@ function useInsertionPoint( {
shouldFocusBlock = true,
selectBlockOnInsert = true,
} ) {
const registry = useRegistry();
const { getSelectedBlock } = useSelect( blockEditorStore );
const { destinationRootClientId, destinationIndex } = useSelect(
( select ) => {
Expand Down Expand Up @@ -91,7 +120,7 @@ function useInsertionPoint( {
} = unlock( useDispatch( blockEditorStore ) );

const onInsertBlocks = useCallback(
( blocks, meta, shouldForceFocusBlock = false ) => {
( blocks, meta, shouldForceFocusBlock = false, _rootClientId ) => {
// When we are trying to move focus or select a new block on insert, we also
// need to clear the last focus to avoid the focus being set to the wrong block
// when tabbing back into the canvas if the block was added from outside the
Expand Down Expand Up @@ -121,8 +150,13 @@ function useInsertionPoint( {
} else {
insertBlocks(
blocks,
destinationIndex,
destinationRootClientId,
getIndex( {
destinationRootClientId,
destinationIndex,
rootClientId: _rootClientId,
registry,
} ),
_rootClientId,
selectBlockOnInsert,
shouldFocusBlock || shouldForceFocusBlock ? 0 : null,
meta
Expand Down Expand Up @@ -154,9 +188,17 @@ function useInsertionPoint( {
);

const onToggleInsertionPoint = useCallback(
( show ) => {
if ( show ) {
showInsertionPoint( destinationRootClientId, destinationIndex );
( item ) => {
if ( item?.hasOwnProperty( 'rootClientId' ) ) {
showInsertionPoint(
item.rootClientId,
getIndex( {
destinationRootClientId,
destinationIndex,
Copy link
Member Author

Choose a reason for hiding this comment

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

At some point we should remove all this destination stuff from and calculate it here inline, it would simplify things.

rootClientId: item.rootClientId,
registry,
} )
);
} else {
hideInsertionPoint();
}
Expand Down
Loading
Loading