UNPKG

@wordpress/block-library

Version:
241 lines (240 loc) • 9.06 kB
import clsx from "clsx"; import { createBlock } from "@wordpress/blocks"; import { BlockControls, InspectorControls, Warning, getColorClassName, store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor"; import { Button, ComboboxControl, Notice, Spinner, ToolbarButton, __experimentalConfirmDialog, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { __, sprintf } from "@wordpress/i18n"; import { useEffect, useMemo, useState } from "@wordpress/element"; import { useEntityRecords } from "@wordpress/core-data"; import { useDispatch, useSelect } from "@wordpress/data"; import { useConvertToNavigationLinks } from "./use-convert-to-navigation-links.mjs"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/page-list/edit.jsx const MAX_PAGE_COUNT = 100; const NOOP = () => {}; function DetachPageListDialog({ isBusy, onConfirm, onCancel }) { return /* @__PURE__ */ jsx(__experimentalConfirmDialog, { isOpen: true, title: __("Detach Page List"), __experimentalHideHeader: false, confirmButtonText: __("Detach"), onConfirm, onCancel, isBusy, size: "medium", children: __("This Navigation Menu displays your website's pages. Detaching will enable you to add, delete, or reorder pages. However, new pages will no longer be added automatically.") }); } function BlockContent({ blockProps, innerBlocksProps, hasResolvedPages, blockList, pages, parentPageID }) { if (!hasResolvedPages) return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx("div", { className: "wp-block-page-list__loading-indicator-container", children: /* @__PURE__ */ jsx(Spinner, { className: "wp-block-page-list__loading-indicator" }) }) }); if (pages === null) return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(Notice, { status: "warning", isDismissible: false, children: __("Page List: Cannot retrieve Pages.") }) }); if (pages.length === 0) return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(Notice, { status: "info", isDismissible: false, children: __("Page List: Cannot retrieve Pages.") }) }); if (blockList.length === 0) { const parentPageDetails = pages.find((page) => page.id === parentPageID); if (parentPageDetails?.title?.rendered) return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(Warning, { children: sprintf(__("Page List: \"%s\" page has no children."), parentPageDetails.title.rendered) }) }); return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(Notice, { status: "warning", isDismissible: false, children: __("Page List: Cannot retrieve Pages.") }) }); } if (pages.length > 0) return /* @__PURE__ */ jsx("ul", { ...innerBlocksProps }); } function PageListEdit({ context, clientId, attributes, setAttributes }) { const { parentPageID } = attributes; const [isConfirmingDetach, setIsConfirmingDetach] = useState(false); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const { records: pages, hasResolved: hasResolvedPages } = useEntityRecords("postType", "page", { per_page: MAX_PAGE_COUNT, _fields: [ "id", "link", "menu_order", "parent", "title", "type" ], orderby: "menu_order", order: "asc" }); const allowConvertToLinks = "showSubmenuIcon" in context && pages?.length > 0 && pages?.length <= MAX_PAGE_COUNT; const pagesByParentId = useMemo(() => { if (pages === null) return /* @__PURE__ */ new Map(); return pages.sort((a, b) => { if (a.menu_order === b.menu_order) return a.title.rendered.localeCompare(b.title.rendered); return a.menu_order - b.menu_order; }).reduce((accumulator, page) => { const { parent } = page; if (accumulator.has(parent)) accumulator.get(parent).push(page); else accumulator.set(parent, [page]); return accumulator; }, /* @__PURE__ */ new Map()); }, [pages]); const blockProps = useBlockProps({ className: clsx("wp-block-page-list", { "has-text-color": !!context.textColor, [getColorClassName("color", context.textColor)]: !!context.textColor, "has-background": !!context.backgroundColor, [getColorClassName("background-color", context.backgroundColor)]: !!context.backgroundColor, "open-on-click": context.submenuVisibility === "click", "open-always": context.submenuVisibility === "always" }), style: { ...context.style?.color } }); const pagesTree = useMemo(function makePagesTree(parentId = 0, level = 0) { const childPages = pagesByParentId.get(parentId); if (!childPages?.length) return []; return childPages.reduce((tree, page) => { const hasChildren = pagesByParentId.has(page.id); const item = { value: page.id, label: "— ".repeat(level) + page.title.rendered, rawName: page.title.rendered }; tree.push(item); if (hasChildren) tree.push(...makePagesTree(page.id, level + 1)); return tree; }, []); }, [pagesByParentId]); const blockList = useMemo(function getBlockList(parentId = parentPageID) { const childPages = pagesByParentId.get(parentId); if (!childPages?.length) return []; return childPages.reduce((template, page) => { const hasChildren = pagesByParentId.has(page.id); const pageProps = { id: page.id, label: page.title?.rendered?.trim() !== "" ? page.title?.rendered : __("(no title)"), title: page.title?.rendered?.trim() !== "" ? page.title?.rendered : __("(no title)"), link: page.url, hasChildren }; let item = null; const children = getBlockList(page.id); item = createBlock("core/page-list-item", pageProps, children); template.push(item); return template; }, []); }, [pagesByParentId, parentPageID]); const { hasSelectedChild, parentClientId, hasDraggedChild, isChildOfNavigation } = useSelect((select) => { const { getBlockParentsByBlockName, hasSelectedInnerBlock, hasDraggedInnerBlock } = select(store); const navigationBlockParents = getBlockParentsByBlockName(clientId, "core/navigation", true); return { isChildOfNavigation: navigationBlockParents.length > 0, hasSelectedChild: hasSelectedInnerBlock(clientId, true), hasDraggedChild: hasDraggedInnerBlock(clientId, true), parentClientId: navigationBlockParents[0] }; }, [clientId]); const convertToNavigationLinks = useConvertToNavigationLinks({ clientId, pages, parentClientId, parentPageID }); const innerBlocksProps = useInnerBlocksProps(blockProps, { renderAppender: false, __unstableDisableDropZone: true, templateLock: isChildOfNavigation ? false : "all", onInput: NOOP, onChange: NOOP, value: blockList }); const { selectBlock } = useDispatch(store); useEffect(() => { if (hasSelectedChild || hasDraggedChild) { setIsConfirmingDetach(true); selectBlock(parentClientId); } }, [ hasSelectedChild, hasDraggedChild, parentClientId, selectBlock ]); return /* @__PURE__ */ jsxs(Fragment, { children: [ (pagesTree.length > 0 || allowConvertToLinks) && /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ parentPageID: 0 }); }, dropdownMenuProps, children: [pagesTree.length > 0 && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Parent Page"), hasValue: () => parentPageID !== 0, onDeselect: () => setAttributes({ parentPageID: 0 }), isShownByDefault: true, children: /* @__PURE__ */ jsx(ComboboxControl, { className: "editor-page-attributes__parent", label: __("Parent"), value: parentPageID, options: pagesTree, onChange: (value) => setAttributes({ parentPageID: value ?? 0 }), help: __("Choose a page to show only its subpages.") }) }), allowConvertToLinks && /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, variant: "secondary", accessibleWhenDisabled: true, disabled: !hasResolvedPages, onClick: () => setIsConfirmingDetach(true), style: { gridColumn: "1 / -1", justifyContent: "center" }, children: __("Detach Page List") })] }) }), allowConvertToLinks && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx(ToolbarButton, { title: __("Detach"), disabled: !hasResolvedPages, onClick: () => setIsConfirmingDetach(true), children: __("Detach") }) }), isConfirmingDetach && /* @__PURE__ */ jsx(DetachPageListDialog, { isBusy: !hasResolvedPages, onConfirm: () => { convertToNavigationLinks(); setIsConfirmingDetach(false); }, onCancel: () => setIsConfirmingDetach(false) })] }), /* @__PURE__ */ jsx(BlockContent, { blockProps, innerBlocksProps, hasResolvedPages, blockList, pages, parentPageID }) ] }); } export { PageListEdit as default }; //# sourceMappingURL=edit.mjs.map