@wordpress/block-editor
Version:
150 lines (148 loc) • 5.72 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-editor/src/autocompleters/block.js
var block_exports = {};
__export(block_exports, {
default: () => block_default
});
module.exports = __toCommonJS(block_exports);
var import_data = require("@wordpress/data");
var import_blocks = require("@wordpress/blocks");
var import_element = require("@wordpress/element");
var import_search_items = require("../components/inserter/search-items");
var import_use_block_types_state = __toESM(require("../components/inserter/hooks/use-block-types-state"));
var import_block_icon = __toESM(require("../components/block-icon"));
var import_store = require("../store");
var import_sorting = require("../utils/sorting");
var import_order_inserter_block_items = require("../utils/order-inserter-block-items");
var import_jsx_runtime = require("react/jsx-runtime");
var noop = () => {
};
var SHOWN_BLOCK_TYPES = 9;
function createBlockCompleter() {
return {
name: "blocks",
className: "block-editor-autocompleters__block",
triggerPrefix: "/",
useItems(filterValue) {
const { rootClientId, selectedBlockId, prioritizedBlocks } = (0, import_data.useSelect)((select) => {
const {
getSelectedBlockClientId,
getBlock,
getBlockListSettings,
getBlockRootClientId
} = select(import_store.store);
const { getActiveBlockVariation } = select(import_blocks.store);
const selectedBlockClientId = getSelectedBlockClientId();
const { name: blockName, attributes } = getBlock(
selectedBlockClientId
);
const activeBlockVariation = getActiveBlockVariation(
blockName,
attributes
);
const _rootClientId = getBlockRootClientId(
selectedBlockClientId
);
return {
selectedBlockId: activeBlockVariation ? `${blockName}/${activeBlockVariation.name}` : blockName,
rootClientId: _rootClientId,
prioritizedBlocks: getBlockListSettings(_rootClientId)?.prioritizedInserterBlocks
};
}, []);
const [items, categories, collections] = (0, import_use_block_types_state.default)(
rootClientId,
noop,
true
);
const filteredItems = (0, import_element.useMemo)(() => {
const initialFilteredItems = !!filterValue.trim() ? (0, import_search_items.searchBlockItems)(
items,
categories,
collections,
filterValue
) : (0, import_order_inserter_block_items.orderInserterBlockItems)(
(0, import_sorting.orderBy)(items, "frecency", "desc"),
prioritizedBlocks
);
return initialFilteredItems.filter((item) => item.id !== selectedBlockId).slice(0, SHOWN_BLOCK_TYPES);
}, [
filterValue,
selectedBlockId,
items,
categories,
collections,
prioritizedBlocks
]);
const options = (0, import_element.useMemo)(
() => filteredItems.map((blockItem) => {
const { title, icon, isDisabled } = blockItem;
return {
key: `block-${blockItem.id}`,
value: blockItem,
label: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_block_icon.default,
{
icon,
showColors: true
},
"icon"
),
title
] }),
isDisabled
};
}),
[filteredItems]
);
return [options];
},
allowContext(before, after) {
return !(/\S/.test(before) || /\S/.test(after));
},
getOptionCompletion(inserterItem) {
const { name, initialAttributes, innerBlocks, syncStatus, blocks } = inserterItem;
return {
action: "replace",
value: syncStatus === "unsynced" ? (blocks ?? []).map(
(block) => (0, import_blocks.cloneBlock)(block)
) : (0, import_blocks.createBlock)(
name,
initialAttributes,
(0, import_blocks.createBlocksFromInnerBlocksTemplate)(
innerBlocks
)
)
};
}
};
}
var block_default = createBlockCompleter();
//# sourceMappingURL=block.js.map