@wordpress/block-library
Version:
Block library for the WordPress editor.
205 lines (204 loc) • 8.59 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 _wordpress_components = require("@wordpress/components");
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 _wordpress_server_side_render = require("@wordpress/server-side-render");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_ui = require("@wordpress/ui");
let ___utils_hooks_js = require("../utils/hooks.cjs");
let ___utils_html_renderer_jsx = require("../utils/html-renderer.cjs");
___utils_html_renderer_jsx = __toESM(___utils_html_renderer_jsx);
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/tag-cloud/edit.jsx
/**
* Minimum number of tags a user can show using this block.
*
* @type {number}
*/
const MIN_TAGS = 1;
/**
* Maximum number of tags a user can show using this block.
*
* @type {number}
*/
const MAX_TAGS = 100;
const MIN_FONT_SIZE = .1;
const MAX_FONT_SIZE = 100;
function TagCloudEdit({ attributes, setAttributes, name }) {
const { taxonomy, showTagCounts, numberOfTags, smallestFontSize, largestFontSize } = attributes;
const [availableUnits] = (0, _wordpress_block_editor.useSettings)("spacing.units");
const dropdownMenuProps = (0, ___utils_hooks_js.useToolsPanelDropdownMenuProps)();
const units = (0, _wordpress_components.__experimentalUseCustomUnits)({ availableUnits: availableUnits ? [...availableUnits, "pt"] : [
"%",
"px",
"em",
"rem",
"pt"
] });
const taxonomies = (0, _wordpress_data.useSelect)((select) => select(_wordpress_core_data.store).getTaxonomies({ per_page: -1 }), []);
const taxonomyItems = [{
label: (0, _wordpress_i18n.__)("- Select -"),
value: "",
disabled: true
}, ...(taxonomies ?? []).filter((tax) => !!tax.show_cloud).map((item) => {
return {
value: item.slug,
label: item.name
};
})];
const onFontSizeChange = (fontSizeLabel, newValue) => {
const [quantity, newUnit] = (0, _wordpress_components.__experimentalParseQuantityAndUnitFromRawValue)(newValue);
if (!Number.isFinite(quantity)) return;
const updateObj = { [fontSizeLabel]: newValue };
Object.entries({
smallestFontSize,
largestFontSize
}).forEach(([attribute, currentValue]) => {
const [currentQuantity, currentUnit] = (0, _wordpress_components.__experimentalParseQuantityAndUnitFromRawValue)(currentValue);
if (attribute !== fontSizeLabel && currentUnit !== newUnit) updateObj[attribute] = `${currentQuantity}${newUnit}`;
});
setAttributes(updateObj);
};
const inspectorControls = /* @__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: "post_tag",
showTagCounts: false,
numberOfTags: 45,
smallestFontSize: "8pt",
largestFontSize: "22pt"
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => taxonomy !== "post_tag",
label: (0, _wordpress_i18n.__)("Taxonomy"),
onDeselect: () => setAttributes({ taxonomy: "post_tag" }),
isShownByDefault: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.SelectControl, {
label: (0, _wordpress_i18n.__)("Taxonomy"),
items: taxonomyItems,
itemToStringValue: (item) => `taxonomy:${item.value}`,
placeholder: "",
value: taxonomyItems.find((item) => item.value === taxonomy) ?? taxonomyItems.find((item) => !item.disabled) ?? null,
onValueChange: (item) => item && setAttributes({ taxonomy: item.value })
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => smallestFontSize !== "8pt" || largestFontSize !== "22pt",
label: (0, _wordpress_i18n.__)("Font size"),
onDeselect: () => setAttributes({
smallestFontSize: "8pt",
largestFontSize: "22pt"
}),
isShownByDefault: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.Flex, {
gap: 4,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.FlexItem, {
isBlock: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalUnitControl, {
label: (0, _wordpress_i18n.__)("Smallest size"),
value: smallestFontSize,
onChange: (value) => {
onFontSizeChange("smallestFontSize", value);
},
units,
min: MIN_FONT_SIZE,
max: MAX_FONT_SIZE
})
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.FlexItem, {
isBlock: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalUnitControl, {
label: (0, _wordpress_i18n.__)("Largest size"),
value: largestFontSize,
onChange: (value) => {
onFontSizeChange("largestFontSize", value);
},
units,
min: MIN_FONT_SIZE,
max: MAX_FONT_SIZE
})
})]
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => numberOfTags !== 45,
label: (0, _wordpress_i18n.__)("Number of tags"),
onDeselect: () => setAttributes({ numberOfTags: 45 }),
isShownByDefault: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.RangeControl, {
label: (0, _wordpress_i18n.__)("Number of tags"),
value: numberOfTags,
onChange: (value) => setAttributes({ numberOfTags: value }),
min: MIN_TAGS,
max: MAX_TAGS,
required: true
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => showTagCounts !== false,
label: (0, _wordpress_i18n.__)("Show tag counts"),
onDeselect: () => setAttributes({ showTagCounts: false }),
isShownByDefault: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, {
label: (0, _wordpress_i18n.__)("Show tag counts"),
checked: showTagCounts,
onChange: () => setAttributes({ showTagCounts: !showTagCounts })
})
})
]
}) });
const { content, status, error } = (0, _wordpress_server_side_render.useServerSideRender)({
attributes,
skipBlockSupportAttributes: true,
block: name
});
const disabledRef = (0, _wordpress_compose.useDisabled)();
const blockProps = (0, _wordpress_block_editor.useBlockProps)({ ref: disabledRef });
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
inspectorControls,
status === "loading" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Spinner, {})
}),
status === "error" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { children: (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Error: %s"), error) })
}),
status === "success" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___utils_html_renderer_jsx.default, {
wrapperProps: blockProps,
html: content
})
] });
}
exports.default = TagCloudEdit;
//# sourceMappingURL=edit.cjs.map