UNPKG

@wordpress/block-library

Version:
178 lines (177 loc) • 7.25 kB
import { BlockControls, BlockIcon, InspectorControls, store, useBlockProps } from "@wordpress/block-editor"; import { createBlock } from "@wordpress/blocks"; import { Placeholder, ToggleControl, ToolbarButton, ToolbarGroup, __experimentalConfirmDialog, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { SelectControl } from "@wordpress/ui"; import { useDispatch, useSelect } from "@wordpress/data"; import { useState } from "@wordpress/element"; import { __, isRTL } from "@wordpress/i18n"; import { useInstanceId } from "@wordpress/compose"; import { store as store$1 } from "@wordpress/notices"; import { formatListBullets, formatListBulletsRTL, formatListNumbered, formatListNumberedRTL, tableOfContents } from "@wordpress/icons"; import TableOfContentsList from "./list.mjs"; import { createListItemBlocks, linearToNestedHeadingList } from "./utils.mjs"; import { useHeadings } from "./hooks.mjs"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/table-of-contents/edit.jsx const HEADING_LEVEL_ITEMS = [ { value: "all", label: __("All levels") }, { value: "1", label: __("Heading 1") }, { value: "2", label: __("Heading 2") }, { value: "3", label: __("Heading 3") }, { value: "4", label: __("Heading 4") }, { value: "5", label: __("Heading 5") }, { value: "6", label: __("Heading 6") } ]; /** @typedef {import('./utils').HeadingData} HeadingData */ function TableOfContentsToolbar({ canDetach, clientId, headingTree, ordered, setAttributes }) { const canInsertList = useSelect((select) => { const { getBlockRootClientId, canInsertBlockType } = select(store); return canInsertBlockType("core/list", getBlockRootClientId(clientId)); }, [clientId]); const { replaceBlocks } = useDispatch(store); const [isConfirmingDetach, setIsConfirmingDetach] = useState(false); return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(BlockControls, { children: [/* @__PURE__ */ jsxs(ToolbarGroup, { children: [/* @__PURE__ */ jsx(ToolbarButton, { icon: isRTL() ? formatListBulletsRTL : formatListBullets, title: __("Unordered"), description: __("Convert to unordered list"), onClick: () => setAttributes({ ordered: false }), isActive: ordered === false }), /* @__PURE__ */ jsx(ToolbarButton, { icon: isRTL() ? formatListNumberedRTL : formatListNumbered, title: __("Ordered"), description: __("Convert to ordered list"), onClick: () => setAttributes({ ordered: true }), isActive: ordered === true })] }), canDetach && canInsertList && /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx(ToolbarButton, { onClick: () => setIsConfirmingDetach(true), children: __("Detach") }) })] }), isConfirmingDetach && /* @__PURE__ */ jsx(__experimentalConfirmDialog, { isOpen: true, title: __("Detach Table of Contents"), __experimentalHideHeader: false, confirmButtonText: __("Detach"), onConfirm: () => { setIsConfirmingDetach(false); replaceBlocks(clientId, createBlock("core/list", { ordered }, createListItemBlocks(headingTree, ordered))); }, onCancel: () => setIsConfirmingDetach(false), size: "medium", children: __("The Table of Contents block lists the headings in the post. Detaching will enable you to edit, reorder, or remove entries. However, new headings will no longer be added automatically.") })] }); } function TableOfContentsEdit({ attributes: { onlyIncludeCurrentPage, maxLevel, ordered = true }, clientId, context: { postId, postType }, setAttributes }) { const postHeadings = useHeadings({ clientId, postId, postType, onlyIncludeCurrentPage, maxLevel }); const headings = postId ? postHeadings : [{ content: __("Headings from the post being viewed appear here"), level: 2 }, { content: __("Subheading"), level: 3 }]; const blockProps = useBlockProps(); const instanceId = useInstanceId(TableOfContentsEdit, "table-of-contents"); const { createWarningNotice } = useDispatch(store$1); const showRedirectionPreventedNotice = (event) => { event.preventDefault(); createWarningNotice(__("Links are disabled in the editor."), { id: `block-library/core/table-of-contents/redirection-prevented/${instanceId}`, type: "snackbar" }); }; const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const headingTree = linearToNestedHeadingList(headings); const toolbarControls = /* @__PURE__ */ jsx(TableOfContentsToolbar, { canDetach: !!postId, clientId, headingTree, ordered, setAttributes }); const inspectorControls = /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ onlyIncludeCurrentPage: false, maxLevel: void 0, ordered: true }); }, dropdownMenuProps, children: [/* @__PURE__ */ jsx(__experimentalToolsPanelItem, { hasValue: () => !!onlyIncludeCurrentPage, label: __("Only include current page"), onDeselect: () => setAttributes({ onlyIncludeCurrentPage: false }), isShownByDefault: true, children: /* @__PURE__ */ jsx(ToggleControl, { label: __("Only include current page"), checked: onlyIncludeCurrentPage, onChange: (value) => setAttributes({ onlyIncludeCurrentPage: value }), help: onlyIncludeCurrentPage ? __("Only including headings from the current page (if the post is paginated).") : __("Include headings from all pages (if the post is paginated).") }) }), /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { hasValue: () => !!maxLevel, label: __("Limit heading levels"), onDeselect: () => setAttributes({ maxLevel: void 0 }), isShownByDefault: true, children: /* @__PURE__ */ jsx(SelectControl, { label: __("Include headings down to level"), value: HEADING_LEVEL_ITEMS.find((item) => item.value === (maxLevel ? String(maxLevel) : "all")) ?? HEADING_LEVEL_ITEMS[0], items: HEADING_LEVEL_ITEMS, onValueChange: (item) => setAttributes({ maxLevel: item?.value && item.value !== "all" ? parseInt(item.value, 10) : void 0 }), description: !maxLevel ? __("Including all heading levels in the table of contents.") : __("Only include headings up to and including this level.") }) })] }) }); if (headings.length === 0) return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(Placeholder, { icon: /* @__PURE__ */ jsx(BlockIcon, { icon: tableOfContents }), label: __("Table of Contents"), instructions: __("Start adding Heading blocks to create a table of contents automatically.") }) }), inspectorControls] }); const ListTag = ordered ? "ol" : "ul"; return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx("nav", { ...blockProps, children: /* @__PURE__ */ jsx(ListTag, { children: /* @__PURE__ */ jsx(TableOfContentsList, { nestedHeadingList: headingTree, disableLinkActivation: true, onClick: showRedirectionPreventedNotice, ordered }) }) }), toolbarControls, inspectorControls ] }); } export { TableOfContentsEdit as default }; //# sourceMappingURL=edit.mjs.map