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

List View: Add a new prop to allow blocks in the inserter to be prioritized #49959

Closed
wants to merge 5 commits into from
Closed
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: 31 additions & 2 deletions packages/block-editor/src/components/list-view/appender.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import { useInstanceId } from '@wordpress/compose';
import { speak } from '@wordpress/a11y';
import { useSelect } from '@wordpress/data';
import { forwardRef, useState, useEffect } from '@wordpress/element';
import {
forwardRef,
useState,
useEffect,
useCallback,
} from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';

/**
Expand All @@ -13,9 +18,12 @@ import { __, sprintf } from '@wordpress/i18n';
import { store as blockEditorStore } from '../../store';
import useBlockDisplayTitle from '../block-title/use-block-display-title';
import Inserter from '../inserter';
getdave marked this conversation as resolved.
Show resolved Hide resolved
import { useListViewContext } from './context';

export const Appender = forwardRef(
( { nestingLevel, blockCount, clientId, ...props }, ref ) => {
const { prioritizedInserterBlocks } = useListViewContext();

const [ insertedBlock, setInsertedBlock ] = useState( null );

const instanceId = useInstanceId( Appender );
Expand Down Expand Up @@ -58,11 +66,27 @@ export const Appender = forwardRef(
);
}, [ insertedBlockTitle ] );

const orderInitialBlockItems = useCallback(
( items ) => {
items.sort( ( { id: aName }, { id: bName } ) => {
// Sort block items according to `prioritizedInserterBlocks`.
let aIndex = prioritizedInserterBlocks.indexOf( aName );
let bIndex = prioritizedInserterBlocks.indexOf( bName );
// All other block items should come after that.
if ( aIndex < 0 ) aIndex = prioritizedInserterBlocks.length;
if ( bIndex < 0 ) bIndex = prioritizedInserterBlocks.length;
return aIndex - bIndex;
} );
return items;
},
[ prioritizedInserterBlocks ]
);

if ( hideInserter ) {
return null;
}

const descriptionId = `list-view-appender__${ instanceId }`;

const description = sprintf(
/* translators: 1: The name of the block. 2: The numerical position of the block. 3: The level of nesting for the block. */
__( 'Append to %1$s block at position %2$d, Level %3$d' ),
Expand All @@ -88,6 +112,11 @@ export const Appender = forwardRef(
setInsertedBlock( maybeInsertedBlock );
}
} }
orderInitialBlockItems={
prioritizedInserterBlocks
? orderInitialBlockItems
: null
}
/>
<div
className="list-view-appender__description"
Expand Down
27 changes: 16 additions & 11 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ export const BLOCK_LIST_ITEM_HEIGHT = 36;
/**
* Show a hierarchical list of blocks.
*
* @param {Object} props Components props.
* @param {string} props.id An HTML element id for the root element of ListView.
* @param {Array} props.blocks _deprecated_ Custom subset of block client IDs to be used instead of the default hierarchy.
* @param {?boolean} props.showBlockMovers Flag to enable block movers. Defaults to `false`.
* @param {?boolean} props.isExpanded Flag to determine whether nested levels are expanded by default. Defaults to `false`.
* @param {?boolean} props.showAppender Flag to show or hide the block appender. Defaults to `false`.
* @param {?ComponentType} props.blockSettingsMenu Optional more menu substitution. Defaults to the standard `BlockSettingsDropdown` component.
* @param {string} props.rootClientId The client id of the root block from which we determine the blocks to show in the list.
* @param {string} props.description Optional accessible description for the tree grid component.
* @param {Function} props.renderAdditionalBlockUI Function that renders additional block content UI.
* @param {Ref} ref Forwarded ref
* @param {Object} props Components props.
* @param {string} props.id An HTML element id for the root element of ListView.
* @param {Array} props.blocks _deprecated_ Custom subset of block client IDs to be used instead of the default hierarchy.
* @param {?boolean} props.showBlockMovers Flag to enable block movers. Defaults to `false`.
* @param {?boolean} props.isExpanded Flag to determine whether nested levels are expanded by default. Defaults to `false`.
* @param {?boolean} props.showAppender Flag to show or hide the block appender. Defaults to `false`.
* @param {?ComponentType} props.blockSettingsMenu Optional more menu substitution. Defaults to the standard `BlockSettingsDropdown` component.
* @param {string} props.rootClientId The client id of the root block from which we determine the blocks to show in the list.
* @param {string} props.description Optional accessible description for the tree grid component.
* @param {Function} props.renderAdditionalBlockUI Function that renders additional block content UI.
* @param {Array} props.prioritizedInserterBlocks An array of block types to show first in the appender.
* @param {Ref} ref Forwarded ref
*/
function ListViewComponent(
{
Expand All @@ -79,6 +80,7 @@ function ListViewComponent(
rootClientId,
description,
renderAdditionalBlockUI,
prioritizedInserterBlocks,
},
ref
) {
Expand Down Expand Up @@ -206,6 +208,7 @@ function ListViewComponent(
BlockSettingsMenu,
listViewInstanceId: instanceId,
renderAdditionalBlockUI,
prioritizedInserterBlocks,
} ),
[
draggedClientIds,
Expand All @@ -215,6 +218,7 @@ function ListViewComponent(
BlockSettingsMenu,
instanceId,
renderAdditionalBlockUI,
prioritizedInserterBlocks,
]
);

Expand Down Expand Up @@ -269,6 +273,7 @@ export default forwardRef( ( props, ref ) => {
blockSettingsMenu={ BlockSettingsDropdown }
rootClientId={ null }
renderAdditionalBlockUICallback={ null }
prioritizedInserterBlocks={ null }
/>
);
} );