@wordpress/block-library
Version:
Block library for the WordPress editor.
69 lines (68 loc) • 2.07 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { BlockControls, useBlockProps } from "@wordpress/block-editor";
import { ToolbarDropdownMenu } from "@wordpress/components";
import { bareNumber, numberInAngleBrackets, numberInCurlyBrackets, numberInParenthesis, numberInSquareBrackets } from "./icons.mjs";
import { useTermCount } from "./use-term-count.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/term-count/edit.jsx
const BRACKET_TYPES = {
none: {
label: __("No brackets"),
icon: bareNumber
},
round: {
label: __("Round brackets"),
icon: numberInParenthesis,
before: "(",
after: ")"
},
square: {
label: __("Square brackets"),
icon: numberInSquareBrackets,
before: "[",
after: "]"
},
curly: {
label: __("Curly brackets"),
icon: numberInCurlyBrackets,
before: "{",
after: "}"
},
angle: {
label: __("Angle brackets"),
icon: numberInAngleBrackets,
before: "<",
after: ">"
}
};
function TermCountEdit({ attributes, setAttributes, context: { termId, taxonomy } }) {
const { bracketType } = attributes;
const termCount = useTermCount(termId, taxonomy)?.termCount || 0;
const blockProps = useBlockProps();
const bracketTypeControls = Object.entries(BRACKET_TYPES).map(([type, { label, icon }]) => ({
role: "menuitemradio",
title: label,
isActive: bracketType === type,
icon,
onClick: () => {
setAttributes({ bracketType: type });
}
}));
const formatTermCount = (count, type) => {
const { before = "", after = "" } = BRACKET_TYPES[type] || {};
return `${before}${count}${after}`;
};
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(BlockControls, {
group: "block",
children: /* @__PURE__ */ jsx(ToolbarDropdownMenu, {
icon: BRACKET_TYPES[bracketType]?.icon ?? bareNumber,
label: __("Change bracket type"),
controls: bracketTypeControls
})
}), /* @__PURE__ */ jsx("div", {
...blockProps,
children: formatTermCount(termCount, bracketType)
})] });
}
export { TermCountEdit as default };
//# sourceMappingURL=edit.mjs.map