UNPKG

@wordpress/block-library

Version:
312 lines (310 loc) 12.3 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_element = require("@wordpress/element"); var import_compose = require("@wordpress/compose"); var import_url = require("@wordpress/url"); var import_dom = require("@wordpress/dom"); var import_icons = require("@wordpress/icons"); var import_data = require("@wordpress/data"); var import_block_editor = require("@wordpress/block-editor"); var import_hooks = require("../../utils/hooks"); var import_update_attributes = require("./update-attributes"); var import_use_entity_binding = require("./use-entity-binding"); var import_jsx_runtime = require("react/jsx-runtime"); 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 lastURLRef = (0, import_element.useRef)(url); const dropdownMenuProps = (0, import_hooks.useToolsPanelDropdownMenuProps)(); const urlInputRef = (0, import_element.useRef)(); const shouldFocusURLInputRef = (0, import_element.useRef)(false); const inputId = (0, import_compose.useInstanceId)(Controls, "link-input"); const helpTextId = `${inputId}__help`; const [inputValue, setInputValue] = (0, import_element.useState)(url); (0, import_element.useEffect)(() => { setInputValue(url); lastURLRef.current = url; }, [url]); const { hasUrlBinding, isBoundEntityAvailable, clearBinding } = (0, import_use_entity_binding.useEntityBinding)({ clientId, attributes }); const { updateBlockAttributes } = (0, import_data.useDispatch)(import_block_editor.store); const unsyncBoundLink = () => { clearBinding(); updateBlockAttributes(clientId, { url: lastURLRef.current, // set the lastURLRef as the new editable value so we avoid bugs from empty link states id: void 0 }); }; (0, import_element.useEffect)(() => { if (!hasUrlBinding && shouldFocusURLInputRef.current) { urlInputRef.current?.select(); } shouldFocusURLInputRef.current = false; }, [hasUrlBinding]); 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, { __nextHasNoMarginBottom: true, __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"), onDeselect: () => setAttributes({ url: "" }), isShownByDefault: true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.__experimentalInputControl, { ref: urlInputRef, __nextHasNoMarginBottom: true, __next40pxDefaultSize: true, id: inputId, label: (0, import_i18n.__)("Link"), value: (() => { if (hasUrlBinding && !isBoundEntityAvailable) { return ""; } return inputValue ? (0, import_url.safeDecodeURI)(inputValue) : ""; })(), autoComplete: "off", type: "url", disabled: hasUrlBinding, "aria-invalid": hasUrlBinding && !isBoundEntityAvailable ? "true" : void 0, "aria-describedby": helpTextId, className: hasUrlBinding && !isBoundEntityAvailable ? "navigation-link-control__input-with-error-suffix" : void 0, onChange: (newValue) => { if (isBoundEntityAvailable) { return; } setInputValue(newValue); }, onFocus: () => { if (isBoundEntityAvailable) { return; } lastURLRef.current = url; }, onBlur: () => { if (isBoundEntityAvailable) { return; } const finalValue = !inputValue ? lastURLRef.current : inputValue; setInputValue(finalValue); (0, import_update_attributes.updateAttributes)({ url: finalValue }, setAttributes, { ...attributes, url: lastURLRef.current }); }, help: hasUrlBinding && !isBoundEntityAvailable ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)( MissingEntityHelp, { id: helpTextId, type: attributes.type, kind: attributes.kind } ) : isBoundEntityAvailable && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( BindingHelpText, { type: attributes.type, kind: attributes.kind } ), suffix: hasUrlBinding && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Button, { icon: import_icons.linkOff, onClick: () => { unsyncBoundLink(); shouldFocusURLInputRef.current = true; }, "aria-describedby": helpTextId, showTooltip: true, label: (0, import_i18n.__)("Unsync and edit"), __next40pxDefaultSize: true, className: hasUrlBinding && !isBoundEntityAvailable ? "navigation-link-control__error-suffix-button" : 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, { __nextHasNoMarginBottom: true, 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, { __nextHasNoMarginBottom: true, 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, { __nextHasNoMarginBottom: true, __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 ); } function MissingEntityHelp({ id, type, kind }) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id, className: "navigation-link-control__error-text", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MissingEntityHelpText, { type, kind }) }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BindingHelpText, Controls, MissingEntityHelpText }); //# sourceMappingURL=controls.js.map