@wordpress/block-editor
Version:
143 lines (141 loc) • 5.75 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/components/block-manager/index.js
var block_manager_exports = {};
__export(block_manager_exports, {
default: () => BlockManager
});
module.exports = __toCommonJS(block_manager_exports);
var import_blocks = require("@wordpress/blocks");
var import_data = require("@wordpress/data");
var import_components = require("@wordpress/components");
var import_i18n = require("@wordpress/i18n");
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_a11y = require("@wordpress/a11y");
var import_category = __toESM(require("./category"));
var import_jsx_runtime = require("react/jsx-runtime");
function BlockManager({
blockTypes,
selectedBlockTypes,
onChange,
showSelectAll = true
}) {
const debouncedSpeak = (0, import_compose.useDebounce)(import_a11y.speak, 500);
const [search, setSearch] = (0, import_element.useState)("");
const { categories, isMatchingSearchTerm } = (0, import_data.useSelect)((select) => {
return {
categories: select(import_blocks.store).getCategories(),
isMatchingSearchTerm: select(import_blocks.store).isMatchingSearchTerm
};
}, []);
const filteredBlockTypes = blockTypes.filter((blockType) => {
return !search || isMatchingSearchTerm(blockType, search);
});
const isIndeterminate = selectedBlockTypes.length > 0 && selectedBlockTypes.length !== blockTypes.length;
const isAllChecked = blockTypes.length > 0 && selectedBlockTypes.length === blockTypes.length;
(0, import_element.useEffect)(() => {
if (!search) {
return;
}
const count = filteredBlockTypes.length;
const resultsFoundMessage = (0, import_i18n.sprintf)(
/* translators: %d: number of results. */
(0, import_i18n._n)("%d result found.", "%d results found.", count),
count
);
debouncedSpeak(resultsFoundMessage);
}, [filteredBlockTypes?.length, search, debouncedSpeak]);
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalVStack, { className: "block-editor-block-manager__content", spacing: 4, children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.SearchControl,
{
__nextHasNoMarginBottom: true,
label: (0, import_i18n.__)("Search for a block"),
placeholder: (0, import_i18n.__)("Search for a block"),
value: search,
onChange: (nextSearch) => setSearch(nextSearch),
className: "block-editor-block-manager__search"
}
),
showSelectAll && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.CheckboxControl,
{
className: "block-editor-block-manager__select-all",
label: (0, import_i18n.__)("Select all"),
checked: isAllChecked,
onChange: () => {
if (isAllChecked) {
onChange([]);
} else {
onChange(blockTypes);
}
},
indeterminate: isIndeterminate,
__nextHasNoMarginBottom: true
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
"div",
{
tabIndex: "0",
role: "region",
"aria-label": (0, import_i18n.__)("Available block types"),
className: "block-editor-block-manager__results",
children: [
filteredBlockTypes.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "block-editor-block-manager__no-results", children: (0, import_i18n.__)("No blocks found.") }),
categories.map((category) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_category.default,
{
title: category.title,
blockTypes: filteredBlockTypes.filter(
(blockType) => blockType.category === category.slug
),
selectedBlockTypes,
onChange
},
category.slug
)),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_category.default,
{
title: (0, import_i18n.__)("Uncategorized"),
blockTypes: filteredBlockTypes.filter(
({ category }) => !category
),
selectedBlockTypes,
onChange
}
)
]
}
)
] });
}
//# sourceMappingURL=index.js.map