UNPKG

@wordpress/block-library

Version:
311 lines (310 loc) • 13.6 kB
"use strict"; 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_blocks = require("@wordpress/blocks"); 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_ui = require("@wordpress/ui"); let _wordpress_icons = require("@wordpress/icons"); let _wordpress_compose = require("@wordpress/compose"); let ___navigation_edit_utils_js = require("../navigation/edit/utils.cjs"); let __shared_index_js = require("./shared/index.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/navigation-link/edit.jsx const DEFAULT_BLOCK = { name: "core/navigation-link" }; const NESTING_BLOCK_NAMES = ["core/navigation-link", "core/navigation-submenu"]; /** * Focuses the appender of the block list that contains the given element. * * The parent's appender changes from the button appender to the dropdown * appender once it has items, so the appender that opened the Link UI may no * longer be there to receive focus when the Link UI closes. * * @param {?Element} container The block list container element. */ function focusAppender(container) { container?.querySelector(":scope > .block-list-appender button")?.focus(); } function getMissingText(type) { let missingText = ""; switch (type) { case "post": missingText = (0, _wordpress_i18n.__)("Select post"); break; case "page": missingText = (0, _wordpress_i18n.__)("Select page"); break; case "category": missingText = (0, _wordpress_i18n.__)("Select category"); break; case "tag": missingText = (0, _wordpress_i18n.__)("Select tag"); break; default: missingText = (0, _wordpress_i18n.__)("Add link"); } return missingText; } function NavigationLinkEdit({ attributes, isSelected, setAttributes, insertBlocksAfter, mergeBlocks, onReplace, context, clientId }) { const { id, label, type, url, description, kind, metadata } = attributes; const { maxNestingLevel } = context; const { replaceBlock, __unstableMarkNextChangeAsNotPersistent, selectBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const [isLinkOpen, setIsLinkOpen] = (0, _wordpress_element.useState)(isSelected && !url); const [popoverAnchor, setPopoverAnchor] = (0, _wordpress_element.useState)(null); const listItemRef = (0, _wordpress_element.useRef)(null); const isDraggingWithin = (0, __shared_index_js.useIsDraggingWithin)(listItemRef); const itemLabelPlaceholder = (0, _wordpress_i18n.__)("Add label…"); const ref = (0, _wordpress_element.useRef)(); const linkUIref = (0, _wordpress_element.useRef)(); const isNewLink = (0, _wordpress_element.useRef)(label === void 0); const shouldFocusAppenderOnClose = (0, _wordpress_element.useRef)(false); const { isAtMaxNesting, isTopLevelLink, isParentOfSelectedBlock, hasChildren, parentBlockClientId } = (0, _wordpress_data.useSelect)((select) => { const { getBlockCount, getBlockName, getBlockRootClientId, hasSelectedInnerBlock, getBlockParentsByBlockName } = select(_wordpress_block_editor.store); const rootClientId = getBlockRootClientId(clientId); const parentBlockName = getBlockName(rootClientId); const isTopLevel = parentBlockName === "core/navigation"; const rootNavigationClientId = isTopLevel ? rootClientId : getBlockParentsByBlockName(clientId, "core/navigation")[0]; const parentBlockId = parentBlockName === "core/navigation-submenu" ? rootClientId : rootNavigationClientId; return { isAtMaxNesting: getBlockParentsByBlockName(clientId, NESTING_BLOCK_NAMES).length >= maxNestingLevel, isTopLevelLink: isTopLevel, isParentOfSelectedBlock: hasSelectedInnerBlock(clientId, true), hasChildren: !!getBlockCount(clientId), parentBlockClientId: parentBlockId }; }, [clientId, maxNestingLevel]); const validateLinkStatus = (0, __shared_index_js.useEnableLinkStatusValidation)(clientId); const { getBlocks } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store); const { hasUrlBinding, isBoundEntityAvailable, entityRecord } = (0, __shared_index_js.useEntityBinding)({ clientId, attributes }); const handleLinkChange = (0, __shared_index_js.useHandleLinkChange)({ clientId, attributes, setAttributes, allowTextUpdate: true }); const [isInvalid, isDraft] = (0, __shared_index_js.useIsInvalidLink)(kind, type, id, validateLinkStatus); /** * Transform to submenu block. */ const transformToSubmenu = (0, _wordpress_element.useCallback)(() => { let innerBlocks = getBlocks(clientId); if (innerBlocks.length === 0) { innerBlocks = [(0, _wordpress_blocks.createBlock)("core/navigation-link")]; selectBlock(innerBlocks[0].clientId); } const newSubmenu = (0, _wordpress_blocks.createBlock)("core/navigation-submenu", attributes, innerBlocks); replaceBlock(clientId, newSubmenu); }, [ getBlocks, clientId, selectBlock, replaceBlock, attributes ]); (0, _wordpress_element.useEffect)(() => { if (isNewLink.current && isSelected) selectBlock(parentBlockClientId); }, []); (0, _wordpress_element.useEffect)(() => { if (hasChildren) { __unstableMarkNextChangeAsNotPersistent(); transformToSubmenu(); } }, [ hasChildren, __unstableMarkNextChangeAsNotPersistent, transformToSubmenu ]); (0, _wordpress_element.useEffect)(() => { if (!isNewLink.current || !url || !isLinkOpen) return; isNewLink.current = false; if ((0, _wordpress_url.isURL)((0, _wordpress_url.prependHTTP)(label)) && /^.+\.[a-z]+/.test(label)) (0, __shared_index_js.selectLabelText)(ref); else { selectBlock(clientId, null); const parentBlocks = getBlocks(parentBlockClientId); if (parentBlocks.length === 1 && parentBlocks[0].clientId === clientId) shouldFocusAppenderOnClose.current = true; } }, [ url, isLinkOpen, isNewLink, label ]); /** * Removes the current link if set. */ function removeLink() { setAttributes({ url: void 0, label: void 0, id: void 0, kind: void 0, type: void 0, opensInNewTab: false }); setIsLinkOpen(false); } const { textColor, customTextColor, backgroundColor, customBackgroundColor } = (0, ___navigation_edit_utils_js.getColors)(context, !isTopLevelLink); function onKeyDown(event) { if (_wordpress_keycodes.isKeyboardEvent.primary(event, "k")) { event.preventDefault(); event.stopPropagation(); setIsLinkOpen(true); } } const instanceId = (0, _wordpress_compose.useInstanceId)(NavigationLinkEdit); const hasMissingEntity = hasUrlBinding && !isBoundEntityAvailable; const missingEntityDescriptionId = hasMissingEntity ? (0, _wordpress_i18n.sprintf)("navigation-link-edit-%d-desc", instanceId) : void 0; 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 }), "aria-describedby": missingEntityDescriptionId, "aria-invalid": hasMissingEntity, style: { color: !textColor && customTextColor, backgroundColor: !backgroundColor && customBackgroundColor }, onKeyDown }); const innerBlocksProps = (0, _wordpress_block_editor.useInnerBlocksProps)({ className: "remove-outline" }, { defaultBlock: DEFAULT_BLOCK, directInsert: true, renderAppender: false }); const needsValidLink = !url && !(hasUrlBinding && isBoundEntityAvailable) || isInvalid || isDraft || hasUrlBinding && !isBoundEntityAvailable; if (needsValidLink) blockProps.onClick = () => { setIsLinkOpen(true); }; const classes = (0, clsx.default)("wp-block-navigation-item__content", { "wp-block-navigation-link__placeholder": needsValidLink }); const missingText = getMissingText(type); const invalidLinkHelpText = (0, __shared_index_js.getInvalidLinkHelpText)(); 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: [/* @__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); } }), !isAtMaxNesting && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, { name: "submenu", icon: _wordpress_icons.addSubmenu, title: (0, _wordpress_i18n.__)("Add submenu"), onClick: transformToSubmenu })] }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { group: "content", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__shared_index_js.Controls, { attributes, setAttributes, clientId }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { ...blockProps, children: [ hasMissingEntity && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.VisuallyHidden, { id: missingEntityDescriptionId, children: invalidLinkHelpText }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("a", { className: classes, children: [!url && !metadata?.bindings?.url ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "wp-block-navigation-link__placeholder-text", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: missingText }) }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { 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, __unstableOnSplitAtEnd: insertBlocksAfter ? () => insertBlocksAfter((0, _wordpress_blocks.createBlock)("core/navigation-link")) : void 0, "aria-label": (0, _wordpress_i18n.__)("Navigation link text"), placeholder: itemLabelPlaceholder, withoutInteractiveFormatting: 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)(__shared_index_js.InvalidDraftDisplay, { label, isInvalid, isDraft, className: "wp-block-navigation-link__label" })] }), isLinkOpen && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__shared_index_js.LinkUI, { ref: linkUIref, clientId, link: attributes, entity: { entityRecord, hasBinding: hasUrlBinding, isEntityAvailable: isBoundEntityAvailable }, onClose: () => { setIsLinkOpen(false); if (!url && !hasUrlBinding && onReplace) { const container = listItemRef.current?.parentElement; const isOnlyChild = getBlocks(parentBlockClientId).length === 1; onReplace([]); if (isOnlyChild) container?.ownerDocument.defaultView.requestAnimationFrame(() => focusAppender(container)); return; } if (shouldFocusAppenderOnClose.current) { shouldFocusAppenderOnClose.current = false; focusAppender(listItemRef.current?.parentElement); } }, anchor: popoverAnchor, onRemove: removeLink, onChange: handleLinkChange })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...innerBlocksProps }) ] }) ] }); } exports.default = NavigationLinkEdit; //# sourceMappingURL=edit.cjs.map