UNPKG

@wordpress/block-library

Version:
248 lines (247 loc) • 12.3 kB
"use strict"; 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_compose = require("@wordpress/compose"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_html_entities = require("@wordpress/html-entities"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_icons = require("@wordpress/icons"); let _wordpress_core_data = require("@wordpress/core-data"); let _wordpress_data = require("@wordpress/data"); let _wordpress_ui = require("@wordpress/ui"); let _wordpress_notices = require("@wordpress/notices"); let ___utils_hooks_js = require("../utils/hooks.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/categories/edit.jsx function CategoriesEdit({ attributes: { displayAsDropdown, showHierarchy, showPostCounts, showOnlyTopLevel, showEmpty, label, showLabel, taxonomy: taxonomySlug }, setAttributes, className, clientId }) { const selectId = (0, _wordpress_compose.useInstanceId)(CategoriesEdit, "blocks-category-select"); const { records: allTaxonomies, isResolvingTaxonomies } = (0, _wordpress_core_data.useEntityRecords)("root", "taxonomy", { per_page: -1 }); const taxonomies = allTaxonomies?.filter((t) => t.visibility.public); const taxonomyItems = (taxonomies ?? []).map((t) => ({ label: t.name, value: t.slug })); const taxonomy = taxonomies?.find((t) => t.slug === taxonomySlug); const isHierarchicalTaxonomy = !isResolvingTaxonomies && taxonomy?.hierarchical; const query = { per_page: -1, hide_empty: !showEmpty, context: "view" }; if (isHierarchicalTaxonomy && showOnlyTopLevel) query.parent = 0; const { records: categories, isResolving } = (0, _wordpress_core_data.useEntityRecords)("taxonomy", taxonomySlug, query); const { createWarningNotice } = (0, _wordpress_data.useDispatch)(_wordpress_notices.store); const showRedirectionPreventedNotice = (event) => { event.preventDefault(); createWarningNotice((0, _wordpress_i18n.__)("Links are disabled in the editor."), { id: `block-library/core/categories/redirection-prevented/${clientId}`, type: "snackbar" }); }; const getCategoriesList = (parentId) => { if (!categories?.length) return []; if (parentId === null) return categories; return categories.filter(({ parent }) => parent === parentId); }; const toggleAttribute = (attributeName) => (newValue) => setAttributes({ [attributeName]: newValue }); const renderCategoryName = (name) => !name ? (0, _wordpress_i18n.__)("(Untitled)") : (0, _wordpress_html_entities.decodeEntities)(name).trim(); const renderCategoryList = () => { return getCategoriesList(isHierarchicalTaxonomy && showHierarchy ? 0 : null).map((category) => renderCategoryListItem(category)); }; const renderCategoryListItem = (category) => { const childCategories = getCategoriesList(category.id); const { id, link, count, name } = category; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", { className: `cat-item cat-item-${id}`, children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("a", { href: link, onClick: showRedirectionPreventedNotice, children: renderCategoryName(name) }), showPostCounts && ` (${count})`, isHierarchicalTaxonomy && showHierarchy && !!childCategories.length && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ul", { className: "children", children: childCategories.map((childCategory) => renderCategoryListItem(childCategory)) }) ] }, id); }; const renderCategoryDropdown = () => { const categoriesList = getCategoriesList(isHierarchicalTaxonomy && showHierarchy ? 0 : null); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [showLabel ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.RichText, { identifier: "label", className: "wp-block-categories__label", "aria-label": (0, _wordpress_i18n.__)("Label text"), placeholder: taxonomy?.name, withoutInteractiveFormatting: true, value: label, onChange: (html) => setAttributes({ label: html }) }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.VisuallyHidden, { render: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("label", { htmlFor: selectId }), children: label ? label : taxonomy?.name }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("select", { id: selectId, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("option", { children: (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Select %s"), taxonomy?.labels?.singular_name) }), categoriesList.map((category) => renderCategoryDropdownItem(category, 0))] })] }); }; const renderCategoryDropdownItem = (category, level) => { const { id, count, name } = category; const childCategories = getCategoriesList(id); return [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("option", { className: `level-${level}`, children: [ Array.from({ length: level * 3 }).map(() => "\xA0"), renderCategoryName(name), showPostCounts && ` (${count})` ] }, id), isHierarchicalTaxonomy && showHierarchy && !!childCategories.length && childCategories.map((childCategory) => renderCategoryDropdownItem(childCategory, level + 1))]; }; const TagName = !!categories?.length && !displayAsDropdown && !isResolving ? "ul" : "div"; const classes = (0, clsx.default)(className, `wp-block-categories-taxonomy-${taxonomySlug}`, { "wp-block-categories-list": !!categories?.length && !displayAsDropdown && !isResolving, "wp-block-categories-dropdown": !!categories?.length && displayAsDropdown && !isResolving }); const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: classes }); const dropdownMenuProps = (0, ___utils_hooks_js.useToolsPanelDropdownMenuProps)(); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(TagName, { ...blockProps, children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalToolsPanel, { label: (0, _wordpress_i18n.__)("Settings"), resetAll: () => { setAttributes({ taxonomy: "category", displayAsDropdown: false, showHierarchy: false, showPostCounts: false, showOnlyTopLevel: false, showEmpty: false, showLabel: true }); }, dropdownMenuProps, children: [ taxonomyItems.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { hasValue: () => { return taxonomySlug !== "category"; }, label: (0, _wordpress_i18n.__)("Taxonomy"), onDeselect: () => { setAttributes({ taxonomy: "category" }); }, isShownByDefault: true, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.SelectControl, { label: (0, _wordpress_i18n.__)("Taxonomy"), items: taxonomyItems, value: taxonomyItems.find((item) => item.value === taxonomySlug) ?? taxonomyItems[0], onValueChange: (item) => item && setAttributes({ taxonomy: item.value }) }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { hasValue: () => !!displayAsDropdown, label: (0, _wordpress_i18n.__)("Display as dropdown"), onDeselect: () => setAttributes({ displayAsDropdown: false }), isShownByDefault: true, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Display as dropdown"), checked: displayAsDropdown, onChange: toggleAttribute("displayAsDropdown") }) }), displayAsDropdown && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { hasValue: () => !showLabel, label: (0, _wordpress_i18n.__)("Show label"), onDeselect: () => setAttributes({ showLabel: true }), isShownByDefault: true, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { className: "wp-block-categories__indentation", label: (0, _wordpress_i18n.__)("Show label"), checked: showLabel, onChange: toggleAttribute("showLabel") }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { hasValue: () => !!showPostCounts, label: (0, _wordpress_i18n.__)("Show post counts"), onDeselect: () => setAttributes({ showPostCounts: false }), isShownByDefault: true, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Show post counts"), checked: showPostCounts, onChange: toggleAttribute("showPostCounts") }) }), isHierarchicalTaxonomy && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { hasValue: () => !!showOnlyTopLevel, label: (0, _wordpress_i18n.__)("Show only top level terms"), onDeselect: () => setAttributes({ showOnlyTopLevel: false }), isShownByDefault: true, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Show only top level terms"), checked: showOnlyTopLevel, onChange: toggleAttribute("showOnlyTopLevel") }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { hasValue: () => !!showEmpty, label: (0, _wordpress_i18n.__)("Show empty terms"), onDeselect: () => setAttributes({ showEmpty: false }), isShownByDefault: true, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Show empty terms"), checked: showEmpty, onChange: toggleAttribute("showEmpty") }) }), isHierarchicalTaxonomy && !showOnlyTopLevel && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { hasValue: () => !!showHierarchy, label: (0, _wordpress_i18n.__)("Show hierarchy"), onDeselect: () => setAttributes({ showHierarchy: false }), isShownByDefault: true, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Show hierarchy"), checked: showHierarchy, onChange: toggleAttribute("showHierarchy") }) }) ] }) }), isResolving && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Placeholder, { icon: _wordpress_icons.pin, label: (0, _wordpress_i18n.__)("Terms"), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Spinner, {}) }), !isResolving && categories?.length === 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: taxonomy.labels.no_terms }), !isResolving && categories?.length > 0 && (displayAsDropdown ? renderCategoryDropdown() : renderCategoryList()) ] }); } exports.default = CategoriesEdit; //# sourceMappingURL=edit.cjs.map