UNPKG

@wordpress/block-library

Version:
164 lines (162 loc) 6.08 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-library/src/navigation/edit/overlay-template-part-selector.js var overlay_template_part_selector_exports = {}; __export(overlay_template_part_selector_exports, { default: () => OverlayTemplatePartSelector }); module.exports = __toCommonJS(overlay_template_part_selector_exports); var import_element = require("@wordpress/element"); var import_core_data = require("@wordpress/core-data"); var import_components = require("@wordpress/components"); var import_i18n = require("@wordpress/i18n"); var import_html_entities = require("@wordpress/html-entities"); var import_create_template_part_id = require("../../template-part/edit/utils/create-template-part-id"); var import_jsx_runtime = require("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 } = (0, import_core_data.useEntityRecords)("postType", "wp_template_part", { per_page: -1 }); const overlayTemplateParts = (0, import_element.useMemo)(() => { if (!templateParts) { return []; } return templateParts.filter( (templatePart) => templatePart.area === "overlay" ); }, [templateParts]); const options = (0, import_element.useMemo)(() => { const baseOptions = [ { label: (0, import_i18n.__)("None (default)"), value: "" } ]; if (!hasResolved || isResolving) { return baseOptions; } const templatePartOptions = overlayTemplateParts.map( (templatePart) => { const templatePartId = (0, import_create_template_part_id.createTemplatePartId)( templatePart.theme, templatePart.slug ); const label = templatePart.title?.rendered ? (0, import_html_entities.decodeEntities)(templatePart.title.rendered) : templatePart.slug; return { label, value: templatePartId }; } ); return [...baseOptions, ...templatePartOptions]; }, [overlayTemplateParts, hasResolved, isResolving]); const parsedTemplatePart = (0, import_element.useMemo)(() => { return parseTemplatePartId(overlay); }, [overlay]); const selectedTemplatePart = (0, import_element.useMemo)(() => { if (!overlay || !overlayTemplateParts) { return null; } return overlayTemplateParts.find((templatePart) => { const templatePartId = (0, import_create_template_part_id.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__ */ (0, import_jsx_runtime.jsxs)("div", { className: "wp-block-navigation__overlay-selector", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, {}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { children: (0, import_i18n.__)("Loading overlays\u2026") }) ] }); } return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "wp-block-navigation__overlay-selector", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.SelectControl, { __next40pxDefaultSize: true, __nextHasNoMarginBottom: true, label: (0, import_i18n.__)("Overlay template"), value: overlay || "", options, onChange: handleSelectChange, disabled: isResolving, accessibleWhenDisabled: true, help: overlayTemplateParts.length === 0 && hasResolved ? (0, import_i18n.__)("No overlays found.") : (0, import_i18n.__)("Select an overlay to use for the navigation.") } ), overlay && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Button, { __next40pxDefaultSize: true, variant: "secondary", onClick: handleEditClick, disabled: isEditButtonDisabled, accessibleWhenDisabled: true, "aria-label": selectedTemplatePart ? (0, import_i18n.sprintf)( /* translators: %s: Overlay title. */ (0, import_i18n.__)("Edit overlay: %s"), selectedTemplatePart.title?.rendered ? (0, import_html_entities.decodeEntities)( selectedTemplatePart.title.rendered ) : selectedTemplatePart.slug ) : (0, import_i18n.__)("Edit overlay"), className: "wp-block-navigation__overlay-edit-button", children: (0, import_i18n.__)("Edit") } ) ] }); } //# sourceMappingURL=overlay-template-part-selector.js.map