@wordpress/block-library
Version:
Block library for the WordPress editor.
142 lines (140 loc) • 4.96 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-library/src/navigation-link/shared/use-block-suggestions.js
var use_block_suggestions_exports = {};
__export(use_block_suggestions_exports, {
BLOCK_SUGGESTION_TYPE: () => BLOCK_SUGGESTION_TYPE,
addBlockSuggestions: () => addBlockSuggestions,
isBlockSuggestion: () => isBlockSuggestion,
useBlockSuggestions: () => useBlockSuggestions
});
module.exports = __toCommonJS(use_block_suggestions_exports);
var import_i18n = require("@wordpress/i18n");
var import_a11y = require("@wordpress/a11y");
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_blocks = require("@wordpress/blocks");
var import_block_editor = require("@wordpress/block-editor");
var import_lock_unlock = require("../../lock-unlock.cjs");
var { searchItems, normalizeString } = (0, import_lock_unlock.unlock)(import_block_editor.privateApis);
var BLOCK_SUGGESTION_TYPE = "block";
function isBlockSuggestion(suggestion) {
return suggestion?.type === BLOCK_SUGGESTION_TYPE && typeof suggestion.blockItemId === "string";
}
var EXCLUDED_BLOCK_NAMES = ["core/navigation-link"];
var SEARCH_FIELDS = [
{ get: (item) => item.title },
{ get: (item) => item.keywords }
];
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, import_element.createElement)(import_block_editor.BlockIcon, { icon: item.icon }),
typeLabel: (0, import_i18n.__)("Block")
};
if (normalizeString(item.title).startsWith(search)) {
before.push(suggestion);
} else {
after.push(suggestion);
}
}
return [...before, ...suggestions, ...after];
}
function useBlockSuggestions({
clientId,
isEnabled,
onBlockInsert,
onClose
}) {
const {
getBlockIndex,
getBlockOrder,
getBlockRootClientId,
getInserterItems
} = (0, import_data.useSelect)(import_block_editor.store);
const { insertBlock, replaceBlock } = (0, import_data.useDispatch)(import_block_editor.store);
const transformSuggestions = (0, import_element.useCallback)(
(suggestions, { isInitialSuggestions, searchTerm }) => isInitialSuggestions ? suggestions : addBlockSuggestions(
suggestions,
getInserterItems(getBlockRootClientId(clientId)),
searchTerm
),
[clientId, getBlockRootClientId, getInserterItems]
);
const insertBlockFromSuggestion = (itemId) => {
const rootClientId = getBlockRootClientId(clientId);
const item = getInserterItems(rootClientId).find(
({ id }) => id === itemId
);
if (!item) {
return;
}
const block = (0, import_blocks.createBlock)(
item.name,
item.initialAttributes,
(0, import_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, import_a11y.speak)(
(0, import_i18n.sprintf)(
/* translators: %s: The title of the block that was added. */
(0, import_i18n.__)("%s block added."),
item.title
)
);
};
return {
transformSuggestions: isEnabled ? transformSuggestions : void 0,
insertBlockFromSuggestion
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BLOCK_SUGGESTION_TYPE,
addBlockSuggestions,
isBlockSuggestion,
useBlockSuggestions
});
//# sourceMappingURL=use-block-suggestions.cjs.map