@wordpress/block-library
Version:
Block library for the WordPress editor.
229 lines (228 loc) • 10 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_components = require("@wordpress/components");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_data = require("@wordpress/data");
let ______utils_hooks_js = require("../../utils/hooks.cjs");
let ______utils_plain_text_edit_ts = require("../../utils/plain-text-edit.cjs");
let __use_handle_link_change_js = require("./use-handle-link-change.cjs");
let __use_entity_binding_js = require("./use-entity-binding.cjs");
let ______utils_link_picker_ts = require("../../utils/link-picker.cjs");
let __use_is_invalid_link_js = require("./use-is-invalid-link.cjs");
let ______utils_view_link_button_tsx = require("../../utils/view-link-button.cjs");
______utils_view_link_button_tsx = __toESM(______utils_view_link_button_tsx);
let ______lock_unlock_ts = require("../../lock-unlock.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/navigation-link/shared/controls.jsx
const { LinkPicker } = (0, ______lock_unlock_ts.unlock)(_wordpress_block_editor.privateApis);
/**
* Get a human-readable entity type name.
*
* @param {string} type - The entity type
* @param {string} kind - The entity kind
* @return {string} Human-readable entity type name
*/
function getEntityTypeName(type, kind) {
if (kind === "post-type") switch (type) {
case "post": return (0, _wordpress_i18n.__)("post");
case "page": return (0, _wordpress_i18n.__)("page");
default: return type || (0, _wordpress_i18n.__)("post");
}
if (kind === "taxonomy") switch (type) {
case "category": return (0, _wordpress_i18n.__)("category");
case "tag": return (0, _wordpress_i18n.__)("tag");
default: return type || (0, _wordpress_i18n.__)("term");
}
return type || (0, _wordpress_i18n.__)("item");
}
/**
* Shared Controls component for Navigation Link and Navigation Submenu blocks.
*
* This component provides the inspector controls (ToolsPanel) that are identical
* between both navigation blocks.
*
* @param {Object} props - Component props
* @param {Object} props.attributes - Block attributes
* @param {Function} props.setAttributes - Function to update block attributes
* @param {string} props.clientId - Block client ID
* @param {boolean} props.isLinkEditable - Whether link editing should be allowed
*/
function Controls({ attributes, setAttributes, clientId, isLinkEditable = true }) {
const { label, url, description, rel, opensInNewTab } = attributes;
const dropdownMenuProps = (0, ______utils_hooks_js.useToolsPanelDropdownMenuProps)();
const { hasUrlBinding, isBoundEntityAvailable, entityRecord } = (0, __use_entity_binding_js.useEntityBinding)({
clientId,
attributes
});
const [isInvalid, isDraft] = (0, __use_is_invalid_link_js.useIsInvalidLink)(attributes.kind, attributes.type, entityRecord?.id, hasUrlBinding);
let helpText = "";
if (isInvalid || hasUrlBinding && !isBoundEntityAvailable) helpText = getInvalidLinkHelpText();
else if (isDraft) helpText = getDraftHelpText({
type: attributes.type,
kind: attributes.kind
});
const handleLinkChange = (0, __use_handle_link_change_js.useHandleLinkChange)({
clientId,
attributes,
setAttributes
});
const onNavigateToEntityRecord = (0, _wordpress_data.useSelect)((select) => select(_wordpress_block_editor.store).getSettings().onNavigateToEntityRecord, []);
const isContentOnly = (0, _wordpress_data.useSelect)((select) => select(_wordpress_block_editor.store).getBlockEditingMode(clientId), [clientId]) === "contentOnly";
const preview = (0, ______utils_link_picker_ts.useLinkPreview)({
url,
entityRecord,
type: attributes.type,
hasBinding: hasUrlBinding,
isEntityAvailable: isBoundEntityAvailable
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalToolsPanel, {
label: (0, _wordpress_i18n.__)("Settings"),
resetAll: () => {
setAttributes({
label: "",
url: "",
description: "",
rel: "",
opensInNewTab: false
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => !!label,
label: (0, _wordpress_i18n.__)("Text"),
onDeselect: () => setAttributes({ label: "" }),
isShownByDefault: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, {
label: (0, _wordpress_i18n.__)("Text"),
value: (0, ______utils_plain_text_edit_ts.getPlainText)(label),
onChange: (labelValue) => {
setAttributes({ label: (0, ______utils_plain_text_edit_ts.applyPlainTextEdit)(label, labelValue) });
},
autoComplete: "off"
})
}),
isLinkEditable && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => !!url,
label: (0, _wordpress_i18n.__)("Link to"),
onDeselect: () => setAttributes({
url: void 0,
id: void 0,
kind: void 0,
type: void 0
}),
isShownByDefault: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LinkPicker, {
preview,
onSelect: handleLinkChange,
suggestionsQuery: (0, ______utils_link_picker_ts.getSuggestionsQuery)(attributes.type, attributes.kind),
label: (0, _wordpress_i18n.__)("Link to"),
help: helpText ? helpText : void 0
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => !!opensInNewTab,
label: (0, _wordpress_i18n.__)("Open in new tab"),
onDeselect: () => setAttributes({ opensInNewTab: false }),
isShownByDefault: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.CheckboxControl, {
label: (0, _wordpress_i18n.__)("Open in new tab"),
checked: opensInNewTab,
onChange: (value) => setAttributes({ opensInNewTab: value })
})
}),
!!url && hasUrlBinding && isBoundEntityAvailable && entityRecord?.id && attributes.kind === "post-type" && onNavigateToEntityRecord && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, {
variant: "secondary",
onClick: () => {
onNavigateToEntityRecord({
postId: entityRecord.id,
postType: attributes.type
});
},
__next40pxDefaultSize: true,
className: "navigation-link-to__action-button",
children: (0, _wordpress_i18n.__)("Edit")
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(______utils_view_link_button_tsx.default, { url })
] }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => !!description,
label: (0, _wordpress_i18n.__)("Description"),
onDeselect: () => setAttributes({ description: "" }),
isShownByDefault: !isContentOnly,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextareaControl, {
label: (0, _wordpress_i18n.__)("Description"),
value: description || "",
onChange: (descriptionValue) => {
setAttributes({ description: descriptionValue });
},
help: (0, _wordpress_i18n.__)("The description will be displayed in the menu if the current theme supports it.")
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => !!rel,
label: (0, _wordpress_i18n.__)("Rel attribute"),
onDeselect: () => setAttributes({ rel: "" }),
isShownByDefault: !isContentOnly,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, {
label: (0, _wordpress_i18n.__)("Rel attribute"),
value: rel || "",
onChange: (relValue) => {
setAttributes({ rel: relValue });
},
autoComplete: "off",
help: (0, _wordpress_i18n.__)("The relationship of the linked URL as space-separated link types.")
})
})
]
});
}
/**
* Returns help text for invalid links.
*
* @return {string} Error help text string (empty string if valid).
*/
function getInvalidLinkHelpText() {
return (0, _wordpress_i18n.__)("This link is invalid and will not appear on your site. Please update the link.");
}
/**
* Returns the help text for links to draft entities
*
* @param {Object} props - Function props
* @param {string} props.type - The entity type
* @param {string} props.kind - The entity kind
* @return {string} Draft help text
*/
function getDraftHelpText({ type, kind }) {
const entityType = getEntityTypeName(type, kind);
return (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("This link is to a draft %1$s and will not appear on your site until the %1$s is published."), entityType);
}
exports.Controls = Controls;
exports.getInvalidLinkHelpText = getInvalidLinkHelpText;
//# sourceMappingURL=controls.cjs.map