@wordpress/block-library
Version:
Block library for the WordPress editor.
201 lines (200 loc) • 7.36 kB
JavaScript
import { Button, CheckboxControl, TextControl, TextareaControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { __, sprintf } from "@wordpress/i18n";
import { privateApis, store } from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { useToolsPanelDropdownMenuProps } from "../../utils/hooks.mjs";
import { applyPlainTextEdit, getPlainText } from "../../utils/plain-text-edit.mjs";
import { useHandleLinkChange } from "./use-handle-link-change.mjs";
import { useEntityBinding } from "./use-entity-binding.mjs";
import { getSuggestionsQuery, useLinkPreview } from "../../utils/link-picker.mjs";
import { useIsInvalidLink } from "./use-is-invalid-link.mjs";
import ViewLinkButton from "../../utils/view-link-button.mjs";
import { unlock } from "../../lock-unlock.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/navigation-link/shared/controls.jsx
const { LinkPicker } = unlock(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 __("post");
case "page": return __("page");
default: return type || __("post");
}
if (kind === "taxonomy") switch (type) {
case "category": return __("category");
case "tag": return __("tag");
default: return type || __("term");
}
return type || __("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 = useToolsPanelDropdownMenuProps();
const { hasUrlBinding, isBoundEntityAvailable, entityRecord } = useEntityBinding({
clientId,
attributes
});
const [isInvalid, isDraft] = 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 = useHandleLinkChange({
clientId,
attributes,
setAttributes
});
const onNavigateToEntityRecord = useSelect((select) => select(store).getSettings().onNavigateToEntityRecord, []);
const isContentOnly = useSelect((select) => select(store).getBlockEditingMode(clientId), [clientId]) === "contentOnly";
const preview = useLinkPreview({
url,
entityRecord,
type: attributes.type,
hasBinding: hasUrlBinding,
isEntityAvailable: isBoundEntityAvailable
});
return /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
label: "",
url: "",
description: "",
rel: "",
opensInNewTab: false
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!label,
label: __("Text"),
onDeselect: () => setAttributes({ label: "" }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(TextControl, {
label: __("Text"),
value: getPlainText(label),
onChange: (labelValue) => {
setAttributes({ label: applyPlainTextEdit(label, labelValue) });
},
autoComplete: "off"
})
}),
isLinkEditable && /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!url,
label: __("Link to"),
onDeselect: () => setAttributes({
url: void 0,
id: void 0,
kind: void 0,
type: void 0
}),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(LinkPicker, {
preview,
onSelect: handleLinkChange,
suggestionsQuery: getSuggestionsQuery(attributes.type, attributes.kind),
label: __("Link to"),
help: helpText ? helpText : void 0
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!opensInNewTab,
label: __("Open in new tab"),
onDeselect: () => setAttributes({ opensInNewTab: false }),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(CheckboxControl, {
label: __("Open in new tab"),
checked: opensInNewTab,
onChange: (value) => setAttributes({ opensInNewTab: value })
})
}),
!!url && hasUrlBinding && isBoundEntityAvailable && entityRecord?.id && attributes.kind === "post-type" && onNavigateToEntityRecord && /* @__PURE__ */ jsx(Button, {
variant: "secondary",
onClick: () => {
onNavigateToEntityRecord({
postId: entityRecord.id,
postType: attributes.type
});
},
__next40pxDefaultSize: true,
className: "navigation-link-to__action-button",
children: __("Edit")
}),
/* @__PURE__ */ jsx(ViewLinkButton, { url })
] }),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!description,
label: __("Description"),
onDeselect: () => setAttributes({ description: "" }),
isShownByDefault: !isContentOnly,
children: /* @__PURE__ */ jsx(TextareaControl, {
label: __("Description"),
value: description || "",
onChange: (descriptionValue) => {
setAttributes({ description: descriptionValue });
},
help: __("The description will be displayed in the menu if the current theme supports it.")
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => !!rel,
label: __("Rel attribute"),
onDeselect: () => setAttributes({ rel: "" }),
isShownByDefault: !isContentOnly,
children: /* @__PURE__ */ jsx(TextControl, {
label: __("Rel attribute"),
value: rel || "",
onChange: (relValue) => {
setAttributes({ rel: relValue });
},
autoComplete: "off",
help: __("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 __("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 sprintf(__("This link is to a draft %1$s and will not appear on your site until the %1$s is published."), entityType);
}
export { Controls, getInvalidLinkHelpText };
//# sourceMappingURL=controls.mjs.map