@wordpress/block-library
Version:
Block library for the WordPress editor.
136 lines (135 loc) • 6.12 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_a11y = require("@wordpress/a11y");
let _wordpress_element = require("@wordpress/element");
let _wordpress_data = require("@wordpress/data");
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_block_editor = require("@wordpress/block-editor");
// packages/block-library/src/navigation-link/shared/use-block-suggestions.js
const { searchItems, normalizeString } = (0, require("../../lock-unlock.cjs").unlock)(_wordpress_block_editor.privateApis);
/**
* The `type` of a suggestion that inserts a block rather than setting a link.
*/
const BLOCK_SUGGESTION_TYPE = "block";
/**
* Whether a suggestion inserts a block rather than setting a link.
*
* The `type` alone is not enough: a site can register a post type called
* "block", whose results arrive with that type. Only a block suggestion
* carries the inserter item it was made from.
*
* @param {Object} suggestion A link suggestion.
* @return {boolean} Whether it is a block suggestion.
*/
function isBlockSuggestion(suggestion) {
return suggestion?.type === "block" && typeof suggestion.blockItemId === "string";
}
/**
* The link variations are left out: searching already finds the pages, posts
* and terms they would link to.
*/
const EXCLUDED_BLOCK_NAMES = ["core/navigation-link"];
const SEARCH_FIELDS = [{ get: (item) => item.title }, { get: (item) => item.keywords }];
/**
* Adds the blocks that match a search to the link suggestions.
*
* A block whose title starts with the search is what was asked for by name,
* so it goes before the links. Any other matching block goes after them.
*
* @param {Array} suggestions Link suggestions.
* @param {Array} blockItems Inserter items the Navigation allows.
* @param {string} searchTerm What the user typed.
* @return {Array} The suggestions, with any matching blocks added.
*/
function addBlockSuggestions(suggestions, blockItems, searchTerm) {
if (!searchTerm?.trim()) return suggestions;
const matches = searchItems(blockItems, searchTerm, {
fields: SEARCH_FIELDS,
filter: (item) => !item.isDisabled && !EXCLUDED_BLOCK_NAMES.includes(item.name)
});
if (!matches.length) return suggestions;
const search = normalizeString(searchTerm.trim());
const before = [];
const after = [];
for (const item of matches) {
const suggestion = {
id: item.id,
blockItemId: item.id,
type: BLOCK_SUGGESTION_TYPE,
title: item.title,
icon: (0, _wordpress_element.createElement)(_wordpress_block_editor.BlockIcon, { icon: item.icon }),
typeLabel: (0, _wordpress_i18n.__)("Block")
};
if (normalizeString(item.title).startsWith(search)) before.push(suggestion);
else after.push(suggestion);
}
return [
...before,
...suggestions,
...after
];
}
/**
* Lists the blocks a Navigation allows in a link's search results, and
* inserts the one that is chosen.
*
* 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 {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.
*/
function useBlockSuggestions({ clientId, isEnabled, allowedBlockNames, onBlockInsert, onClose }) {
const { getBlockIndex, getBlockOrder, getBlockRootClientId, getInserterItems } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store);
const { insertBlock, replaceBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const transformSuggestions = (0, _wordpress_element.useCallback)((suggestions, { isInitialSuggestions, searchTerm }) => {
if (isInitialSuggestions || allowedBlockNames?.length === 0) return suggestions;
const items = getInserterItems(getBlockRootClientId(clientId));
return addBlockSuggestions(suggestions, allowedBlockNames ? items.filter(({ name }) => allowedBlockNames.includes(name)) : items, searchTerm);
}, [
clientId,
getBlockRootClientId,
getInserterItems,
allowedBlockNames
]);
const insertBlockFromSuggestion = (itemId) => {
const rootClientId = getBlockRootClientId(clientId);
const item = getInserterItems(rootClientId).find(({ id }) => id === itemId);
if (!item) return;
const block = (0, _wordpress_blocks.createBlock)(item.name, item.initialAttributes, (0, _wordpress_blocks.createBlocksFromInnerBlocksTemplate)(item.innerBlocks));
const index = getBlockIndex(clientId);
if (onBlockInsert) {
insertBlock(block, index, rootClientId, false);
onBlockInsert(block);
} else if (getBlockOrder(clientId).length) {
insertBlock(block, index, rootClientId);
onClose?.();
} else replaceBlock(clientId, block);
(0, _wordpress_a11y.speak)((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("%s block added."), item.title));
};
return {
transformSuggestions: isEnabled ? transformSuggestions : void 0,
insertBlockFromSuggestion
};
}
exports.BLOCK_SUGGESTION_TYPE = BLOCK_SUGGESTION_TYPE;
exports.addBlockSuggestions = addBlockSuggestions;
exports.isBlockSuggestion = isBlockSuggestion;
exports.useBlockSuggestions = useBlockSuggestions;
//# sourceMappingURL=use-block-suggestions.cjs.map