@wordpress/block-library
Version:
Block library for the WordPress editor.
239 lines (238 loc) • 9.18 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_dom = require("@wordpress/dom");
let _wordpress_components = require("@wordpress/components");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_element = require("@wordpress/element");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_data = require("@wordpress/data");
let _wordpress_icons = require("@wordpress/icons");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_ui = require("@wordpress/ui");
let _wordpress_url = require("@wordpress/url");
let __page_creator_jsx = require("./page-creator.cjs");
let __block_inserter_jsx = require("./block-inserter.cjs");
__block_inserter_jsx = __toESM(__block_inserter_jsx);
let ___shared_index_js = require("../shared/index.cjs");
let ______utils_link_picker_ts = require("../../utils/link-picker.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/navigation-link/link-ui/index.jsx
function UnforwardedLinkUI(props, ref) {
const { label, url, opensInNewTab, type, kind, id } = props.link;
const { entityRecord, hasBinding, isEntityAvailable } = props.entity || {};
const { image, badges } = (0, ______utils_link_picker_ts.useLinkPreview)({
url,
entityRecord,
type,
hasBinding,
isEntityAvailable
});
const { clientId } = props;
const postType = type || "page";
const [addingBlock, setAddingBlock] = (0, _wordpress_element.useState)(false);
const [addingPage, setAddingPage] = (0, _wordpress_element.useState)(false);
const [shouldFocusPane, setShouldFocusPane] = (0, _wordpress_element.useState)(null);
const [initialSearchValue, setInitialSearchValue] = (0, _wordpress_element.useState)("");
const searchInputValueRef = (0, _wordpress_element.useRef)("");
const updateSearchValue = (value) => {
searchInputValueRef.current = value;
setInitialSearchValue(value);
};
const linkControlWrapperRef = (0, _wordpress_element.useRef)();
const addPageButtonRef = (0, _wordpress_element.useRef)();
const addBlockButtonRef = (0, _wordpress_element.useRef)();
const { isBoundEntityAvailable } = (0, ___shared_index_js.useEntityBinding)({
clientId,
attributes: props.link
});
const link = (0, _wordpress_element.useMemo)(() => ({
url,
opensInNewTab,
title: label && (0, _wordpress_dom.__unstableStripHTML)(label),
entityTitle: entityRecord?.title?.rendered || entityRecord?.name,
kind,
type,
id,
image,
badges
}), [
label,
opensInNewTab,
url,
kind,
type,
id,
image,
badges,
entityRecord
]);
const handlePageCreated = (pageLink) => {
props.onChange(pageLink);
setAddingPage(false);
setShouldFocusPane(true);
updateSearchValue("");
};
const dialogTitleId = (0, _wordpress_compose.useInstanceId)(LinkUI, "link-ui-link-control__title");
const dialogDescriptionId = (0, _wordpress_compose.useInstanceId)(LinkUI, "link-ui-link-control__description");
(0, _wordpress_element.useEffect)(() => {
if (shouldFocusPane && linkControlWrapperRef.current) {
if (shouldFocusPane?.current) shouldFocusPane.current.focus();
else (_wordpress_dom.focus.tabbable.find(linkControlWrapperRef.current)[0] || linkControlWrapperRef.current).focus();
setShouldFocusPane(false);
}
}, [shouldFocusPane]);
const canAddBlock = (0, _wordpress_block_editor.useBlockEditingMode)() !== "disabled";
const canAddPage = (0, _wordpress_data.useSelect)((select) => {
if (!canAddBlock || type !== "page") return false;
return !!select(_wordpress_core_data.store).canUser("create", {
kind: "postType",
name: "page"
});
}, [canAddBlock, type]);
const { transformSuggestions, insertBlockFromSuggestion } = (0, ___shared_index_js.useBlockSuggestions)({
clientId,
isEnabled: canAddBlock && !link?.url?.length,
allowedBlockNames: props.allowedBlockNames,
onBlockInsert: props.onBlockInsert,
onClose: props.onClose
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.Popover, {
ref,
placement: "bottom",
onClose: props.onClose,
anchor: props.anchor,
shift: true,
children: [
!addingBlock && !addingPage && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
ref: linkControlWrapperRef,
role: "dialog",
"aria-labelledby": dialogTitleId,
"aria-describedby": dialogDescriptionId,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_ui.VisuallyHidden, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("h2", {
id: dialogTitleId,
children: (0, _wordpress_i18n.__)("Add link")
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
id: dialogDescriptionId,
children: (0, _wordpress_i18n.__)("Search for and add a link to your Navigation.")
})] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.LinkControl, {
hasTextControl: true,
hasRichPreviews: true,
value: link,
showInitialSuggestions: true,
withCreateSuggestion: false,
noDirectEntry: !!type,
noURLSuggestion: !!type,
suggestionsQuery: (0, ______utils_link_picker_ts.getSuggestionsQuery)(type, kind),
transformSuggestions,
onChange: (updatedValue) => {
if ((0, ___shared_index_js.isBlockSuggestion)(updatedValue)) {
insertBlockFromSuggestion(updatedValue.blockItemId);
return;
}
props.onChange(updatedValue);
},
onInputChange: (value) => {
searchInputValueRef.current = value;
},
inputValue: initialSearchValue,
onRemove: props.onRemove,
onCancel: props.onCancel,
handleEntities: isBoundEntityAvailable,
forceIsEditingLink: link?.url ? false : void 0,
renderControlBottom: () => {
if (link?.url?.length) return null;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LinkUITools, {
addPageButtonRef,
addBlockButtonRef,
setAddingBlock: () => {
setAddingBlock(true);
},
setAddingPage: () => {
setAddingPage(true);
},
canAddPage,
canAddBlock
});
}
})]
}),
addingBlock && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__block_inserter_jsx.default, {
clientId: props.clientId,
onBack: () => {
setAddingBlock(false);
setShouldFocusPane(addBlockButtonRef);
updateSearchValue(searchInputValueRef.current);
},
onBlockInsert: props?.onBlockInsert
}),
addingPage && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__page_creator_jsx.LinkUIPageCreator, {
postType,
onBack: () => {
setAddingPage(false);
setShouldFocusPane(addPageButtonRef);
updateSearchValue(searchInputValueRef.current);
},
onPageCreated: handlePageCreated,
initialTitle: searchInputValueRef.current && !(0, _wordpress_url.isURL)(searchInputValueRef.current) ? searchInputValueRef.current : ""
})
]
});
}
const LinkUI = (0, _wordpress_element.forwardRef)(UnforwardedLinkUI);
const LinkUITools = ({ addPageButtonRef, addBlockButtonRef, setAddingBlock, setAddingPage, canAddPage, canAddBlock }) => {
const blockInserterAriaRole = "listbox";
if (!canAddPage && !canAddBlock) return null;
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalVStack, {
spacing: 0,
className: "link-ui-tools",
children: [canAddPage && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, {
__next40pxDefaultSize: true,
ref: addPageButtonRef,
icon: _wordpress_icons.plus,
onClick: (e) => {
e.preventDefault();
setAddingPage(true);
},
"aria-haspopup": blockInserterAriaRole,
children: (0, _wordpress_i18n.__)("Create page")
}), canAddBlock && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, {
__next40pxDefaultSize: true,
ref: addBlockButtonRef,
icon: _wordpress_icons.plus,
onClick: (e) => {
e.preventDefault();
setAddingBlock(true);
},
"aria-haspopup": blockInserterAriaRole,
children: (0, _wordpress_i18n.__)("Add block")
})]
});
};
exports.LinkUI = LinkUI;
exports.default = LinkUITools;
//# sourceMappingURL=index.cjs.map