@wordpress/block-library
Version:
Block library for the WordPress editor.
143 lines (142 loc) • 4.52 kB
JavaScript
// packages/block-library/src/navigation/edit/overlay-template-part-selector.js
import { useMemo } from "@wordpress/element";
import { useEntityRecords } from "@wordpress/core-data";
import { SelectControl, Spinner, Button } from "@wordpress/components";
import { __, sprintf } from "@wordpress/i18n";
import { decodeEntities } from "@wordpress/html-entities";
import { createTemplatePartId } from "../../template-part/edit/utils/create-template-part-id";
import { jsx, jsxs } from "react/jsx-runtime";
function parseTemplatePartId(templatePartId) {
if (!templatePartId || typeof templatePartId !== "string") {
return null;
}
const parts = templatePartId.split("//");
if (parts.length !== 2) {
return null;
}
return {
theme: parts[0],
slug: parts[1]
};
}
function OverlayTemplatePartSelector({
overlay,
setAttributes,
onNavigateToEntityRecord
}) {
const {
records: templateParts,
isResolving,
hasResolved
} = useEntityRecords("postType", "wp_template_part", {
per_page: -1
});
const overlayTemplateParts = useMemo(() => {
if (!templateParts) {
return [];
}
return templateParts.filter(
(templatePart) => templatePart.area === "overlay"
);
}, [templateParts]);
const options = useMemo(() => {
const baseOptions = [
{
label: __("None (default)"),
value: ""
}
];
if (!hasResolved || isResolving) {
return baseOptions;
}
const templatePartOptions = overlayTemplateParts.map(
(templatePart) => {
const templatePartId = createTemplatePartId(
templatePart.theme,
templatePart.slug
);
const label = templatePart.title?.rendered ? decodeEntities(templatePart.title.rendered) : templatePart.slug;
return {
label,
value: templatePartId
};
}
);
return [...baseOptions, ...templatePartOptions];
}, [overlayTemplateParts, hasResolved, isResolving]);
const parsedTemplatePart = useMemo(() => {
return parseTemplatePartId(overlay);
}, [overlay]);
const selectedTemplatePart = useMemo(() => {
if (!overlay || !overlayTemplateParts) {
return null;
}
return overlayTemplateParts.find((templatePart) => {
const templatePartId = createTemplatePartId(
templatePart.theme,
templatePart.slug
);
return templatePartId === overlay;
});
}, [overlay, overlayTemplateParts]);
const handleSelectChange = (value) => {
setAttributes({
overlay: value || void 0
});
};
const handleEditClick = () => {
if (!overlay || !onNavigateToEntityRecord) {
return;
}
onNavigateToEntityRecord({
postId: overlay,
postType: "wp_template_part"
});
};
const isEditButtonDisabled = !overlay || !parsedTemplatePart || !onNavigateToEntityRecord || isResolving;
if (isResolving && !hasResolved) {
return /* @__PURE__ */ jsxs("div", { className: "wp-block-navigation__overlay-selector", children: [
/* @__PURE__ */ jsx(Spinner, {}),
/* @__PURE__ */ jsx("p", { children: __("Loading overlays\u2026") })
] });
}
return /* @__PURE__ */ jsxs("div", { className: "wp-block-navigation__overlay-selector", children: [
/* @__PURE__ */ jsx(
SelectControl,
{
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
label: __("Overlay template"),
value: overlay || "",
options,
onChange: handleSelectChange,
disabled: isResolving,
accessibleWhenDisabled: true,
help: overlayTemplateParts.length === 0 && hasResolved ? __("No overlays found.") : __("Select an overlay to use for the navigation.")
}
),
overlay && /* @__PURE__ */ jsx(
Button,
{
__next40pxDefaultSize: true,
variant: "secondary",
onClick: handleEditClick,
disabled: isEditButtonDisabled,
accessibleWhenDisabled: true,
"aria-label": selectedTemplatePart ? sprintf(
/* translators: %s: Overlay title. */
__("Edit overlay: %s"),
selectedTemplatePart.title?.rendered ? decodeEntities(
selectedTemplatePart.title.rendered
) : selectedTemplatePart.slug
) : __("Edit overlay"),
className: "wp-block-navigation__overlay-edit-button",
children: __("Edit")
}
)
] });
}
export {
OverlayTemplatePartSelector as default
};
//# sourceMappingURL=overlay-template-part-selector.js.map