@wordpress/block-editor
Version:
128 lines (126 loc) • 4.51 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/category.js
var category_exports = {};
__export(category_exports, {
default: () => category_default
});
module.exports = __toCommonJS(category_exports);
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_components = require("@wordpress/components");
var import_checklist = __toESM(require("./checklist"));
var import_jsx_runtime = require("react/jsx-runtime");
function BlockManagerCategory({
title,
blockTypes,
selectedBlockTypes,
onChange
}) {
const instanceId = (0, import_compose.useInstanceId)(BlockManagerCategory);
const toggleVisible = (0, import_element.useCallback)(
(blockType, nextIsChecked) => {
if (nextIsChecked) {
onChange([...selectedBlockTypes, blockType]);
} else {
onChange(
selectedBlockTypes.filter(
({ name }) => name !== blockType.name
)
);
}
},
[selectedBlockTypes, onChange]
);
const toggleAllVisible = (0, import_element.useCallback)(
(nextIsChecked) => {
if (nextIsChecked) {
onChange([
...selectedBlockTypes,
...blockTypes.filter(
(blockType) => !selectedBlockTypes.find(
({ name }) => name === blockType.name
)
)
]);
} else {
onChange(
selectedBlockTypes.filter(
(selectedBlockType) => !blockTypes.find(
({ name }) => name === selectedBlockType.name
)
)
);
}
},
[blockTypes, selectedBlockTypes, onChange]
);
if (!blockTypes.length) {
return null;
}
const checkedBlockNames = blockTypes.map(({ name }) => name).filter(
(type) => (selectedBlockTypes ?? []).some(
(selectedBlockType) => selectedBlockType.name === type
)
);
const titleId = "block-editor-block-manager__category-title-" + instanceId;
const isAllChecked = checkedBlockNames.length === blockTypes.length;
const isIndeterminate = !isAllChecked && checkedBlockNames.length > 0;
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
"div",
{
role: "group",
"aria-labelledby": titleId,
className: "block-editor-block-manager__category",
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.CheckboxControl,
{
__nextHasNoMarginBottom: true,
checked: isAllChecked,
onChange: toggleAllVisible,
className: "block-editor-block-manager__category-title",
indeterminate: isIndeterminate,
label: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: titleId, children: title })
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_checklist.default,
{
blockTypes,
value: checkedBlockNames,
onItemChange: toggleVisible
}
)
]
}
);
}
var category_default = BlockManagerCategory;
//# sourceMappingURL=category.js.map