UNPKG

@wordpress/block-library

Version:
211 lines (210 loc) • 6.96 kB
import { __unstableStripHTML, focus } from "@wordpress/dom"; import { Button, Popover, __experimentalVStack } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { LinkControl, useBlockEditingMode } from "@wordpress/block-editor"; import { forwardRef, useEffect, useMemo, useRef, useState } from "@wordpress/element"; import { store } from "@wordpress/core-data"; import { useSelect } from "@wordpress/data"; import { plus } from "@wordpress/icons"; import { useInstanceId } from "@wordpress/compose"; import { VisuallyHidden } from "@wordpress/ui"; import { isURL } from "@wordpress/url"; import { LinkUIPageCreator } from "./page-creator.mjs"; import LinkUIBlockInserter from "./block-inserter.mjs"; import { isBlockSuggestion, useBlockSuggestions, useEntityBinding } from "../shared/index.mjs"; import { getSuggestionsQuery, useLinkPreview } from "../../utils/link-picker.mjs"; import { jsx, jsxs } from "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 } = useLinkPreview({ url, entityRecord, type, hasBinding, isEntityAvailable }); const { clientId } = props; const postType = type || "page"; const [addingBlock, setAddingBlock] = useState(false); const [addingPage, setAddingPage] = useState(false); const [shouldFocusPane, setShouldFocusPane] = useState(null); const [initialSearchValue, setInitialSearchValue] = useState(""); const searchInputValueRef = useRef(""); const updateSearchValue = (value) => { searchInputValueRef.current = value; setInitialSearchValue(value); }; const linkControlWrapperRef = useRef(); const addPageButtonRef = useRef(); const addBlockButtonRef = useRef(); const { isBoundEntityAvailable } = useEntityBinding({ clientId, attributes: props.link }); const link = useMemo(() => ({ url, opensInNewTab, title: label && __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 = useInstanceId(LinkUI, "link-ui-link-control__title"); const dialogDescriptionId = useInstanceId(LinkUI, "link-ui-link-control__description"); useEffect(() => { if (shouldFocusPane && linkControlWrapperRef.current) { if (shouldFocusPane?.current) shouldFocusPane.current.focus(); else (focus.tabbable.find(linkControlWrapperRef.current)[0] || linkControlWrapperRef.current).focus(); setShouldFocusPane(false); } }, [shouldFocusPane]); const canAddBlock = useBlockEditingMode() !== "disabled"; const canAddPage = useSelect((select) => { if (!canAddBlock || type !== "page") return false; return !!select(store).canUser("create", { kind: "postType", name: "page" }); }, [canAddBlock, type]); const { transformSuggestions, insertBlockFromSuggestion } = useBlockSuggestions({ clientId, isEnabled: canAddBlock && !link?.url?.length, allowedBlockNames: props.allowedBlockNames, onBlockInsert: props.onBlockInsert, onClose: props.onClose }); return /* @__PURE__ */ jsxs(Popover, { ref, placement: "bottom", onClose: props.onClose, anchor: props.anchor, shift: true, children: [ !addingBlock && !addingPage && /* @__PURE__ */ jsxs("div", { ref: linkControlWrapperRef, role: "dialog", "aria-labelledby": dialogTitleId, "aria-describedby": dialogDescriptionId, children: [/* @__PURE__ */ jsxs(VisuallyHidden, { children: [/* @__PURE__ */ jsx("h2", { id: dialogTitleId, children: __("Add link") }), /* @__PURE__ */ jsx("p", { id: dialogDescriptionId, children: __("Search for and add a link to your Navigation.") })] }), /* @__PURE__ */ jsx(LinkControl, { hasTextControl: true, hasRichPreviews: true, value: link, showInitialSuggestions: true, withCreateSuggestion: false, noDirectEntry: !!type, noURLSuggestion: !!type, suggestionsQuery: getSuggestionsQuery(type, kind), transformSuggestions, onChange: (updatedValue) => { if (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__ */ jsx(LinkUITools, { addPageButtonRef, addBlockButtonRef, setAddingBlock: () => { setAddingBlock(true); }, setAddingPage: () => { setAddingPage(true); }, canAddPage, canAddBlock }); } })] }), addingBlock && /* @__PURE__ */ jsx(LinkUIBlockInserter, { clientId: props.clientId, onBack: () => { setAddingBlock(false); setShouldFocusPane(addBlockButtonRef); updateSearchValue(searchInputValueRef.current); }, onBlockInsert: props?.onBlockInsert }), addingPage && /* @__PURE__ */ jsx(LinkUIPageCreator, { postType, onBack: () => { setAddingPage(false); setShouldFocusPane(addPageButtonRef); updateSearchValue(searchInputValueRef.current); }, onPageCreated: handlePageCreated, initialTitle: searchInputValueRef.current && !isURL(searchInputValueRef.current) ? searchInputValueRef.current : "" }) ] }); } const LinkUI = forwardRef(UnforwardedLinkUI); const LinkUITools = ({ addPageButtonRef, addBlockButtonRef, setAddingBlock, setAddingPage, canAddPage, canAddBlock }) => { const blockInserterAriaRole = "listbox"; if (!canAddPage && !canAddBlock) return null; return /* @__PURE__ */ jsxs(__experimentalVStack, { spacing: 0, className: "link-ui-tools", children: [canAddPage && /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, ref: addPageButtonRef, icon: plus, onClick: (e) => { e.preventDefault(); setAddingPage(true); }, "aria-haspopup": blockInserterAriaRole, children: __("Create page") }), canAddBlock && /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, ref: addBlockButtonRef, icon: plus, onClick: (e) => { e.preventDefault(); setAddingBlock(true); }, "aria-haspopup": blockInserterAriaRole, children: __("Add block") })] }); }; export { LinkUI, LinkUITools as default }; //# sourceMappingURL=index.mjs.map