@wordpress/block-library
Version:
Block library for the WordPress editor.
178 lines (177 loc) • 6.26 kB
JavaScript
import { Flex, FlexItem, RangeControl, Spinner, ToggleControl, __experimentalParseQuantityAndUnitFromRawValue, __experimentalToolsPanel, __experimentalToolsPanelItem, __experimentalUnitControl, __experimentalUseCustomUnits } from "@wordpress/components";
import { useSelect } from "@wordpress/data";
import { __, sprintf } from "@wordpress/i18n";
import { InspectorControls, useBlockProps, useSettings } from "@wordpress/block-editor";
import { store } from "@wordpress/core-data";
import { useServerSideRender } from "@wordpress/server-side-render";
import { useDisabled } from "@wordpress/compose";
import { SelectControl } from "@wordpress/ui";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import HtmlRenderer from "../utils/html-renderer.mjs";
import { Fragment, jsx, jsxs } from "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] = useSettings("spacing.units");
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const units = __experimentalUseCustomUnits({ availableUnits: availableUnits ? [...availableUnits, "pt"] : [
"%",
"px",
"em",
"rem",
"pt"
] });
const taxonomies = useSelect((select) => select(store).getTaxonomies({ per_page: -1 }), []);
const taxonomyItems = [{
label: __("- 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] = __experimentalParseQuantityAndUnitFromRawValue(newValue);
if (!Number.isFinite(quantity)) return;
const updateObj = { [fontSizeLabel]: newValue };
Object.entries({
smallestFontSize,
largestFontSize
}).forEach(([attribute, currentValue]) => {
const [currentQuantity, currentUnit] = __experimentalParseQuantityAndUnitFromRawValue(currentValue);
if (attribute !== fontSizeLabel && currentUnit !== newUnit) updateObj[attribute] = `${currentQuantity}${newUnit}`;
});
setAttributes(updateObj);
};
const inspectorControls = /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
taxonomy: "post_tag",
showTagCounts: false,
numberOfTags: 45,
smallestFontSize: "8pt",
largestFontSize: "22pt"
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => taxonomy !== "post_tag",
label: __("Taxonomy"),
onDeselect: () => setAttributes({ taxonomy: "post_tag" }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(SelectControl, {
label: __("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__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => smallestFontSize !== "8pt" || largestFontSize !== "22pt",
label: __("Font size"),
onDeselect: () => setAttributes({
smallestFontSize: "8pt",
largestFontSize: "22pt"
}),
isShownByDefault: true,
children: /* @__PURE__ */ jsxs(Flex, {
gap: 4,
children: [/* @__PURE__ */ jsx(FlexItem, {
isBlock: true,
children: /* @__PURE__ */ jsx(__experimentalUnitControl, {
label: __("Smallest size"),
value: smallestFontSize,
onChange: (value) => {
onFontSizeChange("smallestFontSize", value);
},
units,
min: MIN_FONT_SIZE,
max: MAX_FONT_SIZE
})
}), /* @__PURE__ */ jsx(FlexItem, {
isBlock: true,
children: /* @__PURE__ */ jsx(__experimentalUnitControl, {
label: __("Largest size"),
value: largestFontSize,
onChange: (value) => {
onFontSizeChange("largestFontSize", value);
},
units,
min: MIN_FONT_SIZE,
max: MAX_FONT_SIZE
})
})]
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => numberOfTags !== 45,
label: __("Number of tags"),
onDeselect: () => setAttributes({ numberOfTags: 45 }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(RangeControl, {
label: __("Number of tags"),
value: numberOfTags,
onChange: (value) => setAttributes({ numberOfTags: value }),
min: MIN_TAGS,
max: MAX_TAGS,
required: true
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => showTagCounts !== false,
label: __("Show tag counts"),
onDeselect: () => setAttributes({ showTagCounts: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Show tag counts"),
checked: showTagCounts,
onChange: () => setAttributes({ showTagCounts: !showTagCounts })
})
})
]
}) });
const { content, status, error } = useServerSideRender({
attributes,
skipBlockSupportAttributes: true,
block: name
});
const disabledRef = useDisabled();
const blockProps = useBlockProps({ ref: disabledRef });
return /* @__PURE__ */ jsxs(Fragment, { children: [
inspectorControls,
status === "loading" && /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(Spinner, {})
}),
status === "error" && /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx("p", { children: sprintf(__("Error: %s"), error) })
}),
status === "success" && /* @__PURE__ */ jsx(HtmlRenderer, {
wrapperProps: blockProps,
html: content
})
] });
}
export { TagCloudEdit as default };
//# sourceMappingURL=edit.mjs.map