Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ function UnforwardedLinkUI( props, ref ) {
useBlockSuggestions( {
clientId,
isEnabled: canAddBlock && ! link?.url?.length,
allowedBlockNames: props.allowedBlockNames,
onBlockInsert: props.onBlockInsert,
onClose: props.onClose,
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,25 @@ export function addBlockSuggestions( suggestions, blockItems, searchTerm ) {
* The allowed blocks are read when a search runs, not subscribed to, so an
* open link UI does no work as the editor changes.
*
* @param {Object} options Hook options.
* @param {string} options.clientId Client ID of the link or submenu.
* @param {boolean} options.isEnabled Whether to list blocks at all.
* @param {Function=} options.onBlockInsert Called with the inserted block by a
* caller that tracks the new link,
* such as the list view.
* @param {Function=} options.onClose Closes the link UI.
* @param {Object} options Hook options.
* @param {string} options.clientId Client ID of the link or submenu.
* @param {boolean} options.isEnabled Whether to list blocks at all.
* @param {string[]=} options.allowedBlockNames Names of the blocks to list.
* Every allowed block is listed
* when left out, and none when
* empty.
* @param {Function=} options.onBlockInsert Called with the inserted block by a
* caller that tracks the new link,
* such as the list view.
* @param {Function=} options.onClose Closes the link UI.
* @return {{ transformSuggestions: Function|undefined, insertBlockFromSuggestion: Function }}
* The `transformSuggestions` to give LinkControl, and the handler for a chosen
* block suggestion.
*/
export function useBlockSuggestions( {
clientId,
isEnabled,
allowedBlockNames,
onBlockInsert,
onClose,
} ) {
Expand All @@ -133,15 +138,22 @@ export function useBlockSuggestions( {
const { insertBlock, replaceBlock } = useDispatch( blockEditorStore );

const transformSuggestions = useCallback(
( suggestions, { isInitialSuggestions, searchTerm } ) =>
isInitialSuggestions
? suggestions
: addBlockSuggestions(
suggestions,
getInserterItems( getBlockRootClientId( clientId ) ),
searchTerm
),
[ clientId, getBlockRootClientId, getInserterItems ]
( suggestions, { isInitialSuggestions, searchTerm } ) => {
// An empty list asks for no blocks.
if ( isInitialSuggestions || allowedBlockNames?.length === 0 ) {
return suggestions;
}

const items = getInserterItems( getBlockRootClientId( clientId ) );
const blockItems = allowedBlockNames
? items.filter( ( { name } ) =>
allowedBlockNames.includes( name )
)
: items;

return addBlockSuggestions( suggestions, blockItems, searchTerm );
},
[ clientId, getBlockRootClientId, getInserterItems, allowedBlockNames ]
);

const insertBlockFromSuggestion = ( itemId ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const BLOCKS_WITH_LINK_UI_SUPPORT = [
export function NavigationLinkUI( {
insertedBlockClientId,
setInsertedBlockClientId,
allowedBlockNames,
} ) {
const { updateBlockAttributes, removeBlock } =
useDispatch( blockEditorStore );
Expand Down Expand Up @@ -96,6 +97,7 @@ export function NavigationLinkUI( {
<LinkUI
clientId={ insertedBlockClientId }
link={ insertedBlockAttributes }
allowedBlockNames={ allowedBlockNames }
onBlockInsert={ handleSetInsertedBlock }
onClose={ () => {
// Use cleanup function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ import LeafMoreMenu from './leaf-more-menu';
const { PrivateListView } = unlock( blockEditorPrivateApis );
const { NavigationLinkUI } = unlock( blockLibraryPrivateApis );

// The sidebar's link search lists only these blocks, which work as menu items
// without editing them from the sidebar, which is not available yet. The other
// allowed blocks stay reachable from the "Add block" pane. The extensible site
// editor keeps every block on purpose. This wrapper, and the
// `allowedBlockNames` prop it sets, can go when this sidebar does.
const ALLOWED_BLOCK_NAMES = [
'core/home-link',
'core/search',
'core/loginout',
'core/site-title',
];
Comment thread
jeryj marked this conversation as resolved.
Resolved

function SidebarNavigationLinkUI( props ) {
return (
<NavigationLinkUI
{ ...props }
allowedBlockNames={ ALLOWED_BLOCK_NAMES }
/>
);
}

// Needs to be kept in sync with the query used at packages/block-library/src/page-list/edit.jsx.
const MAX_PAGE_COUNT = 100;
const PAGES_QUERY = [
Expand Down Expand Up @@ -87,7 +108,7 @@ export default function NavigationMenuContent( { rootClientId } ) {
onSelect={ openListViewContentPanel }
blockSettingsMenu={ LeafMoreMenu }
showAppender
additionalBlockContent={ NavigationLinkUI }
additionalBlockContent={ SidebarNavigationLinkUI }
showContentPopover
isExpanded
/>
Expand Down
Loading