UNPKG

@wordpress/block-library

Version:
258 lines (256 loc) 9.51 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-link/shared/controls.js var controls_exports = {}; __export(controls_exports, { BindingHelpText: () => BindingHelpText, Controls: () => Controls, MissingEntityHelpText: () => MissingEntityHelpText }); module.exports = __toCommonJS(controls_exports); var import_components = require("@wordpress/components"); var import_i18n = require("@wordpress/i18n"); var import_dom = require("@wordpress/dom"); var import_block_editor = require("@wordpress/block-editor"); var import_data = require("@wordpress/data"); var import_core_data = require("@wordpress/core-data"); var import_hooks = require("../../utils/hooks.cjs"); var import_use_handle_link_change = require("./use-handle-link-change.cjs"); var import_use_entity_binding = require("./use-entity-binding.cjs"); var import_link_ui = require("../link-ui/index.cjs"); var import_use_link_preview = require("./use-link-preview.cjs"); var import_lock_unlock = require("../../lock-unlock.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); var { LinkPicker } = (0, import_lock_unlock.unlock)(import_block_editor.privateApis); function getEntityTypeName(type, kind) { if (kind === "post-type") { switch (type) { case "post": return (0, import_i18n.__)("post"); case "page": return (0, import_i18n.__)("page"); default: return type || (0, import_i18n.__)("post"); } } if (kind === "taxonomy") { switch (type) { case "category": return (0, import_i18n.__)("category"); case "tag": return (0, import_i18n.__)("tag"); default: return type || (0, import_i18n.__)("term"); } } return type || (0, import_i18n.__)("item"); } function Controls({ attributes, setAttributes, clientId }) { const { label, url, description, rel, opensInNewTab } = attributes; const dropdownMenuProps = (0, import_hooks.useToolsPanelDropdownMenuProps)(); const { hasUrlBinding, isBoundEntityAvailable, entityRecord } = (0, import_use_entity_binding.useEntityBinding)({ clientId, attributes }); const needsHelpText = hasUrlBinding; const helpText = isBoundEntityAvailable ? BindingHelpText({ type: attributes.type, kind: attributes.kind }) : MissingEntityHelpText({ type: attributes.type, kind: attributes.kind }); const handleLinkChange = (0, import_use_handle_link_change.useHandleLinkChange)({ clientId, attributes, setAttributes }); const linkTitle = entityRecord?.title?.rendered || entityRecord?.title || entityRecord?.name; const linkImage = (0, import_data.useSelect)( (select) => { if (!entityRecord?.featured_media) { return null; } const { getEntityRecord } = select(import_core_data.store); const media = getEntityRecord( "postType", "attachment", entityRecord.featured_media ); return media?.media_details?.sizes?.thumbnail?.source_url || media?.media_details?.sizes?.medium?.source_url || media?.source_url || null; }, [entityRecord?.featured_media] ); const preview = (0, import_use_link_preview.useLinkPreview)({ url, title: linkTitle, image: linkImage, type: attributes.type, entityStatus: entityRecord?.status, hasBinding: hasUrlBinding, isEntityAvailable: isBoundEntityAvailable }); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( import_components.__experimentalToolsPanel, { label: (0, import_i18n.__)("Settings"), resetAll: () => { setAttributes({ label: "", url: "", description: "", rel: "", opensInNewTab: false }); }, dropdownMenuProps, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanelItem, { hasValue: () => !!label, label: (0, import_i18n.__)("Text"), onDeselect: () => setAttributes({ label: "" }), isShownByDefault: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.TextControl, { __next40pxDefaultSize: true, label: (0, import_i18n.__)("Text"), value: label ? (0, import_dom.__unstableStripHTML)(label) : "", onChange: (labelValue) => { setAttributes({ label: labelValue }); }, autoComplete: "off" } ) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanelItem, { hasValue: () => !!url, label: (0, import_i18n.__)("Link to"), onDeselect: () => setAttributes({ url: "" }), isShownByDefault: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( LinkPicker, { preview, onSelect: handleLinkChange, suggestionsQuery: (0, import_link_ui.getSuggestionsQuery)( attributes.type, attributes.kind ), label: (0, import_i18n.__)("Link to"), help: needsHelpText ? helpText : void 0 } ) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanelItem, { hasValue: () => !!opensInNewTab, label: (0, import_i18n.__)("Open in new tab"), onDeselect: () => setAttributes({ opensInNewTab: false }), isShownByDefault: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.CheckboxControl, { label: (0, import_i18n.__)("Open in new tab"), checked: opensInNewTab, onChange: (value) => setAttributes({ opensInNewTab: value }) } ) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanelItem, { hasValue: () => !!description, label: (0, import_i18n.__)("Description"), onDeselect: () => setAttributes({ description: "" }), isShownByDefault: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.TextareaControl, { label: (0, import_i18n.__)("Description"), value: description || "", onChange: (descriptionValue) => { setAttributes({ description: descriptionValue }); }, help: (0, import_i18n.__)( "The description will be displayed in the menu if the current theme supports it." ) } ) } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalToolsPanelItem, { hasValue: () => !!rel, label: (0, import_i18n.__)("Rel attribute"), onDeselect: () => setAttributes({ rel: "" }), isShownByDefault: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.TextControl, { __next40pxDefaultSize: true, label: (0, import_i18n.__)("Rel attribute"), value: rel || "", onChange: (relValue) => { setAttributes({ rel: relValue }); }, autoComplete: "off", help: (0, import_i18n.__)( "The relationship of the linked URL as space-separated link types." ) } ) } ) ] } ); } function BindingHelpText({ type, kind }) { const entityType = getEntityTypeName(type, kind); return (0, import_i18n.sprintf)( /* translators: %s is the entity type (e.g., "page", "post", "category") */ (0, import_i18n.__)("Synced with the selected %s."), entityType ); } function MissingEntityHelpText({ type, kind }) { const entityType = getEntityTypeName(type, kind); return (0, import_i18n.sprintf)( /* translators: %s is the entity type (e.g., "page", "post", "category") */ (0, import_i18n.__)("Synced %s is missing. Please update or remove this link."), entityType ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BindingHelpText, Controls, MissingEntityHelpText }); //# sourceMappingURL=controls.cjs.map