@wordpress/block-library
Version:
Block library for the WordPress editor.
266 lines (265 loc) • 12.6 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 clsx = require("clsx");
clsx = __toESM(clsx);
let _wordpress_data = require("@wordpress/data");
let _wordpress_components = require("@wordpress/components");
let _wordpress_keycodes = require("@wordpress/keycodes");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_url = require("@wordpress/url");
let _wordpress_element = require("@wordpress/element");
let _wordpress_icons = require("@wordpress/icons");
let _wordpress_a11y = require("@wordpress/a11y");
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_compose = require("@wordpress/compose");
let __icons_jsx = require("./icons.cjs");
let ___navigation_link_shared_index_js = require("../navigation-link/shared/index.cjs");
let ___navigation_edit_utils_js = require("../navigation/edit/utils.cjs");
let ___navigation_constants_js = require("../navigation/constants.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/navigation-submenu/edit.jsx
const ALLOWED_BLOCKS = [
"core/navigation-link",
"core/navigation-submenu",
"core/page-list",
"core/loginout"
];
/**
* @typedef {'post-type'|'custom'|'taxonomy'|'post-type-archive'} WPNavigationLinkKind
*/
/**
* Navigation Link Block Attributes
*
* @typedef {Object} WPNavigationLinkBlockAttributes
*
* @property {string} [label] Link text.
* @property {WPNavigationLinkKind} [kind] Kind is used to differentiate between term and post ids to check post draft status.
* @property {string} [type] The type such as post, page, tag, category and other custom types.
* @property {string} [rel] The relationship of the linked URL.
* @property {number} [id] A post or term id.
* @property {boolean} [opensInNewTab] Sets link target to _blank when true.
* @property {string} [url] Link href.
*/
function NavigationSubmenuEdit({ attributes, isSelected, setAttributes, mergeBlocks, onReplace, context, clientId }) {
const { label, url, description, kind, type, id } = attributes;
const { showSubmenuIcon, maxNestingLevel, submenuVisibility } = context;
const openSubmenusOnClick = (0, _wordpress_block_editor.useBlockEditingMode)() !== "default" ? true : submenuVisibility === "click";
const { hasUrlBinding, isBoundEntityAvailable, entityRecord } = (0, ___navigation_link_shared_index_js.useEntityBinding)({
clientId,
attributes
});
const handleLinkChange = (0, ___navigation_link_shared_index_js.useHandleLinkChange)({
clientId,
attributes,
setAttributes,
allowTextUpdate: true
});
const { __unstableMarkNextChangeAsNotPersistent, replaceBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const [isLinkOpen, setIsLinkOpen] = (0, _wordpress_element.useState)(false);
const [popoverAnchor, setPopoverAnchor] = (0, _wordpress_element.useState)(null);
const listItemRef = (0, _wordpress_element.useRef)(null);
const isDraggingWithin = (0, ___navigation_link_shared_index_js.useIsDraggingWithin)(listItemRef);
const itemLabelPlaceholder = (0, _wordpress_i18n.__)("Add text…");
const ref = (0, _wordpress_element.useRef)();
const { parentCount, isParentOfSelectedBlock, hasChildren, selectedBlockHasChildren, onlyDescendantIsEmptyLink } = (0, _wordpress_data.useSelect)((select) => {
const { hasSelectedInnerBlock, getSelectedBlockClientId, getBlockParentsByBlockName, getBlock, getBlockCount, getBlockOrder } = select(_wordpress_block_editor.store);
let _onlyDescendantIsEmptyLink;
const selectedBlockChildren = getBlockOrder(getSelectedBlockClientId());
if (selectedBlockChildren?.length === 1) {
const singleBlock = getBlock(selectedBlockChildren[0]);
_onlyDescendantIsEmptyLink = singleBlock?.name === "core/navigation-link" && !singleBlock?.attributes?.label;
}
return {
parentCount: getBlockParentsByBlockName(clientId, "core/navigation-submenu").length,
isParentOfSelectedBlock: hasSelectedInnerBlock(clientId, true),
hasChildren: !!getBlockCount(clientId),
selectedBlockHasChildren: !!selectedBlockChildren?.length,
onlyDescendantIsEmptyLink: _onlyDescendantIsEmptyLink
};
}, [clientId]);
const validateLinkStatus = (0, ___navigation_link_shared_index_js.useEnableLinkStatusValidation)(clientId);
const prevHasChildren = (0, _wordpress_compose.usePrevious)(hasChildren);
const [isInvalid, isDraft] = (0, ___navigation_link_shared_index_js.useIsInvalidLink)(kind, type, id, validateLinkStatus);
(0, _wordpress_element.useEffect)(() => {
if (!openSubmenusOnClick && !url) setIsLinkOpen(true);
}, []);
/**
* The hook shouldn't be necessary but due to a focus loss happening
* when selecting a suggestion in the link popover, we force close on block unselection.
*/
if (!isSelected && isLinkOpen) setIsLinkOpen(false);
(0, _wordpress_element.useEffect)(() => {
if (isLinkOpen && url) {
if ((0, _wordpress_url.isURL)((0, _wordpress_url.prependHTTP)(label)) && /^.+\.[a-z]+/.test(label)) (0, ___navigation_link_shared_index_js.selectLabelText)(ref);
}
}, [url]);
const { textColor, customTextColor, backgroundColor, customBackgroundColor } = (0, ___navigation_edit_utils_js.getColors)(context, parentCount > 0);
function onKeyDown(event) {
if (_wordpress_keycodes.isKeyboardEvent.primary(event, "k")) {
event.preventDefault();
event.stopPropagation();
setIsLinkOpen(true);
}
}
const blockProps = (0, _wordpress_block_editor.useBlockProps)({
ref: (0, _wordpress_compose.useMergeRefs)([setPopoverAnchor, listItemRef]),
className: (0, clsx.default)("wp-block-navigation-item", {
"is-editing": isSelected || isParentOfSelectedBlock,
"is-dragging-within": isDraggingWithin,
"has-link": !!url,
"has-child": hasChildren,
"has-text-color": !!textColor || !!customTextColor,
[(0, _wordpress_block_editor.getColorClassName)("color", textColor)]: !!textColor,
"has-background": !!backgroundColor || customBackgroundColor,
[(0, _wordpress_block_editor.getColorClassName)("background-color", backgroundColor)]: !!backgroundColor,
"open-on-click": openSubmenusOnClick,
"open-always": submenuVisibility === "always"
}),
style: {
color: !textColor && customTextColor,
backgroundColor: !backgroundColor && customBackgroundColor
},
onKeyDown
});
const innerBlocksColors = (0, ___navigation_edit_utils_js.getColors)(context, true);
const allowedBlocks = parentCount >= maxNestingLevel ? ALLOWED_BLOCKS.filter((blockName) => blockName !== "core/navigation-submenu") : ALLOWED_BLOCKS;
const shadowProps = (0, _wordpress_block_editor.__experimentalGetShadowClassesAndStyles)(attributes);
const navigationChildBlockProps = (0, ___navigation_edit_utils_js.getNavigationChildBlockProps)(innerBlocksColors);
const submenuBlockProps = {
...navigationChildBlockProps,
style: {
...navigationChildBlockProps.style,
...shadowProps.style
}
};
const innerBlocksProps = (0, _wordpress_block_editor.useInnerBlocksProps)(submenuBlockProps, {
allowedBlocks,
defaultBlock: ___navigation_constants_js.DEFAULT_BLOCK,
directInsert: true,
__experimentalCaptureToolbars: true,
renderAppender: hasChildren ? _wordpress_block_editor.InnerBlocks.DefaultBlockAppender : isSelected && _wordpress_block_editor.InnerBlocks.ButtonBlockAppender
});
const ParentElement = openSubmenusOnClick ? "button" : "a";
function transformToLink() {
const newLinkBlock = (0, _wordpress_blocks.createBlock)("core/navigation-link", attributes);
replaceBlock(clientId, newLinkBlock);
}
(0, _wordpress_element.useEffect)(() => {
if (!hasChildren && prevHasChildren) {
__unstableMarkNextChangeAsNotPersistent();
transformToLink();
}
}, [hasChildren, prevHasChildren]);
const canConvertToLink = !selectedBlockHasChildren || onlyDescendantIsEmptyLink;
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.ToolbarGroup, { children: [!openSubmenusOnClick && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, {
name: "link",
icon: _wordpress_icons.link,
title: (0, _wordpress_i18n.__)("Link"),
shortcut: _wordpress_keycodes.displayShortcut.primary("k"),
onClick: () => {
setIsLinkOpen(true);
}
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, {
name: "revert",
icon: _wordpress_icons.removeSubmenu,
title: (0, _wordpress_i18n.__)("Convert to Link"),
onClick: transformToLink,
className: "wp-block-navigation__submenu__revert",
disabled: !canConvertToLink
})] }) }),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, {
group: "content",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___navigation_link_shared_index_js.Controls, {
attributes,
setAttributes,
clientId,
isLinkEditable: !openSubmenusOnClick
})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
...blockProps,
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(ParentElement, {
className: "wp-block-navigation-item__content",
children: [
!isInvalid && !isDraft && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.RichText, {
ref,
identifier: "label",
className: "wp-block-navigation-item__label",
value: label,
onChange: (labelValue) => setAttributes({ label: labelValue }),
onMerge: mergeBlocks,
onReplace,
"aria-label": (0, _wordpress_i18n.__)("Navigation link text"),
placeholder: itemLabelPlaceholder,
withoutInteractiveFormatting: true,
onClick: () => {
if (!openSubmenusOnClick && !url) setIsLinkOpen(true);
}
}), description && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
className: "wp-block-navigation-item__description",
children: description
})] }),
(isInvalid || isDraft) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___navigation_link_shared_index_js.InvalidDraftDisplay, {
label,
isInvalid,
isDraft,
className: "wp-block-navigation-item__label"
}),
!openSubmenusOnClick && isLinkOpen && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___navigation_link_shared_index_js.LinkUI, {
clientId,
link: attributes,
entity: {
entityRecord,
hasBinding: hasUrlBinding,
isEntityAvailable: isBoundEntityAvailable
},
onClose: () => {
setIsLinkOpen(false);
},
anchor: popoverAnchor,
onRemove: () => {
setAttributes({ url: "" });
(0, _wordpress_a11y.speak)((0, _wordpress_i18n.__)("Link removed."), "assertive");
},
onChange: handleLinkChange
})
]
}),
(showSubmenuIcon || openSubmenusOnClick) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
className: "wp-block-navigation__submenu-icon",
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__icons_jsx.ItemSubmenuIcon, {})
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...innerBlocksProps })
]
})
] });
}
exports.default = NavigationSubmenuEdit;
//# sourceMappingURL=edit.cjs.map