@wordpress/block-library
Version:
Block library for the WordPress editor.
133 lines (132 loc) • 6.16 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
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 __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let clsx = require("clsx");
clsx = __toESM(clsx);
let _wordpress_components = require("@wordpress/components");
let _wordpress_icons = require("@wordpress/icons");
let _wordpress_element = require("@wordpress/element");
let _wordpress_data = require("@wordpress/data");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_core_data = require("@wordpress/core-data");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/term-template/edit.jsx
const INNER_BLOCKS_LAYOUT = { type: "default" };
function TermTemplateInnerBlocks({ classList }) {
const innerBlocksProps = (0, _wordpress_block_editor.useInnerBlocksProps)({ className: (0, clsx.default)("wp-block-term", classList) }, {
__unstableDisableLayoutClassNames: true,
layout: INNER_BLOCKS_LAYOUT
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", { ...innerBlocksProps });
}
function TermTemplateBlockPreview({ blocks, blockContextId, classList, isHidden, setActiveBlockContextId }) {
const blockPreviewProps = (0, _wordpress_block_editor.__experimentalUseBlockPreview)({
blocks,
props: { className: (0, clsx.default)("wp-block-term", classList) }
});
const handleOnClick = () => {
setActiveBlockContextId(blockContextId);
};
const style = { display: isHidden ? "none" : void 0 };
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", {
...blockPreviewProps,
tabIndex: 0,
role: "button",
onClick: handleOnClick,
onKeyPress: handleOnClick,
style
});
}
const MemoizedTermTemplateBlockPreview = (0, _wordpress_element.memo)(TermTemplateBlockPreview);
function TermTemplateEdit({ clientId, attributes: { layout }, setAttributes, context: { termQuery: { taxonomy, order, orderBy, hideEmpty, showNested = false, perPage, include } = {} }, __unstableLayoutClassNames }) {
const { type: layoutType, columnCount = 3 } = layout || {};
const [activeBlockContextId, setActiveBlockContextId] = (0, _wordpress_element.useState)();
const queryArgs = {
hide_empty: hideEmpty,
order,
orderby: orderBy,
per_page: perPage || -1
};
if (!showNested && !include?.length) queryArgs.parent = 0;
if (include?.length) {
queryArgs.include = include;
queryArgs.orderby = "include";
queryArgs.order = "asc";
}
const { records: terms } = (0, _wordpress_core_data.useEntityRecords)("taxonomy", taxonomy, queryArgs);
const blocks = (0, _wordpress_data.useSelect)((select) => select(_wordpress_block_editor.store).getBlocks(clientId), [clientId]);
const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: __unstableLayoutClassNames });
const blockContexts = (0, _wordpress_element.useMemo)(() => terms?.map((term) => ({
taxonomy,
termId: term.id,
classList: `term-${term.id}`,
termData: term
})), [terms, taxonomy]);
if (!terms) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", {
className: "wp-block-term term-loading",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "term-loading-placeholder" })
})
});
if (!terms.length) return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("p", {
...blockProps,
children: [" ", (0, _wordpress_i18n.__)("No terms found.")]
});
const setDisplayLayout = (newDisplayLayout) => setAttributes((prevAttributes) => ({ layout: {
...prevAttributes.layout,
...newDisplayLayout
} }));
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarGroup, { controls: [{
icon: _wordpress_icons.list,
title: (0, _wordpress_i18n._x)("List view", "Term template block display setting"),
onClick: () => setDisplayLayout({ type: "default" }),
isActive: layoutType === "default" || layoutType === "constrained"
}, {
icon: _wordpress_icons.grid,
title: (0, _wordpress_i18n._x)("Grid view", "Term template block display setting"),
onClick: () => setDisplayLayout({
type: "grid",
columnCount
}),
isActive: layoutType === "grid"
}] }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", {
...blockProps,
children: blockContexts?.map((blockContext) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_block_editor.BlockContextProvider, {
value: blockContext,
children: [blockContext.termId === (activeBlockContextId || blockContexts[0]?.termId) ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TermTemplateInnerBlocks, { classList: blockContext.classList }) : null, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MemoizedTermTemplateBlockPreview, {
blocks,
blockContextId: blockContext.termId,
classList: blockContext.classList,
setActiveBlockContextId,
isHidden: blockContext.termId === (activeBlockContextId || blockContexts[0]?.termId)
})]
}, blockContext.termId))
})] });
}
exports.default = TermTemplateEdit;
//# sourceMappingURL=edit.cjs.map