@wordpress/block-library
Version:
Block library for the WordPress editor.
163 lines (162 loc) • 6.35 kB
JavaScript
import clsx from "clsx";
import { ToggleControl, __experimentalToggleGroupControl, __experimentalToggleGroupControlOption, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { SelectControl } from "@wordpress/ui";
import { InspectorControls, RichText, __experimentalGetDimensionsClassesAndStyles, __experimentalGetShadowClassesAndStyles, __experimentalGetSpacingClassesAndStyles, __experimentalUseBorderProps, useBackgroundProps, useBlockProps } from "@wordpress/block-editor";
import { __, _x } from "@wordpress/i18n";
import { useSelect } from "@wordpress/data";
import { store } from "@wordpress/core-data";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/post-navigation-link/edit.jsx
function PostNavigationLinkEdit({ context: { postType }, attributes, setAttributes }) {
const { type, label, showTitle, linkLabel, arrow, taxonomy } = attributes;
const isNext = type === "next";
let placeholder = isNext ? __("Next") : __("Previous");
const displayArrow = {
none: "",
arrow: isNext ? "→" : "←",
chevron: isNext ? "»" : "«"
}[arrow];
if (showTitle) placeholder = isNext ? __("Next: ") : __("Previous: ");
const ariaLabel = isNext ? __("Next post") : __("Previous post");
const backgroundProps = useBackgroundProps(attributes);
const borderProps = __experimentalUseBorderProps(attributes);
const dimensionsProps = __experimentalGetDimensionsClassesAndStyles(attributes);
const shadowProps = __experimentalGetShadowClassesAndStyles(attributes);
const spacingProps = __experimentalGetSpacingClassesAndStyles(attributes);
const blockProps = useBlockProps({
className: clsx(backgroundProps.className, borderProps.className),
style: {
...backgroundProps.style,
...borderProps.style,
...dimensionsProps.style,
...shadowProps.style,
...spacingProps.style
}
});
const taxonomies = useSelect((select) => {
const { getTaxonomies } = select(store);
return getTaxonomies({
type: postType,
per_page: -1
});
}, [postType]);
const taxonomyOptions = [{
label: __("Unfiltered"),
value: ""
}, ...(taxonomies ?? []).filter(({ visibility }) => !!visibility?.publicly_queryable).map((item) => {
return {
value: item.slug,
label: item.name
};
})];
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
showTitle: false,
linkLabel: false,
arrow: "none"
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Display the title as a link"),
isShownByDefault: true,
hasValue: () => showTitle,
onDeselect: () => setAttributes({ showTitle: false }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Display the title as a link"),
help: __("If you have entered a custom label, it will be prepended before the title."),
checked: !!showTitle,
onChange: () => setAttributes({ showTitle: !showTitle })
})
}),
showTitle && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Include the label as part of the link"),
isShownByDefault: true,
hasValue: () => !!linkLabel,
onDeselect: () => setAttributes({ linkLabel: false }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Include the label as part of the link"),
checked: !!linkLabel,
onChange: () => setAttributes({ linkLabel: !linkLabel })
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Arrow"),
isShownByDefault: true,
hasValue: () => arrow !== "none",
onDeselect: () => setAttributes({ arrow: "none" }),
children: /* @__PURE__ */ jsxs(__experimentalToggleGroupControl, {
label: __("Arrow"),
value: arrow,
onChange: (value) => {
setAttributes({ arrow: value });
},
help: __("A decorative arrow for the next and previous link."),
isBlock: true,
children: [
/* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
value: "none",
label: _x("None", "Arrow option for Next/Previous link")
}),
/* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
value: "arrow",
label: _x("Arrow", "Arrow option for Next/Previous link")
}),
/* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
value: "chevron",
label: _x("Chevron", "Arrow option for Next/Previous link")
})
]
})
})
]
}) }),
/* @__PURE__ */ jsx(InspectorControls, {
group: "advanced",
children: /* @__PURE__ */ jsx(SelectControl, {
label: __("Filter by taxonomy"),
items: taxonomyOptions,
itemToStringValue: (item) => `taxonomy:${item.value}`,
value: taxonomyOptions.find((item) => item.value === taxonomy) ?? taxonomyOptions[0],
onValueChange: (item) => item && setAttributes({ taxonomy: item.value }),
description: __("Only link to posts that have the same taxonomy terms as the current post. For example the same tags or categories.")
})
}),
/* @__PURE__ */ jsxs("div", {
...blockProps,
children: [
!isNext && displayArrow && /* @__PURE__ */ jsx("span", {
className: `wp-block-post-navigation-link__arrow-previous is-arrow-${arrow}`,
children: displayArrow
}),
/* @__PURE__ */ jsx(RichText, {
tagName: "a",
identifier: "label",
"aria-label": ariaLabel,
placeholder,
value: label,
withoutInteractiveFormatting: true,
onChange: (newLabel) => setAttributes({ label: newLabel })
}),
showTitle && /* @__PURE__ */ jsx("a", {
href: "#post-navigation-pseudo-link",
onClick: (event) => event.preventDefault(),
children: __("An example title")
}),
isNext && displayArrow && /* @__PURE__ */ jsx("span", {
className: `wp-block-post-navigation-link__arrow-next is-arrow-${arrow}`,
"aria-hidden": true,
children: displayArrow
})
]
})
] });
}
export { PostNavigationLinkEdit as default };
//# sourceMappingURL=edit.mjs.map