UNPKG

@wordpress/block-library

Version:
120 lines (119 loc) • 4.57 kB
import { BlockControls, store, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor"; import { ToolbarButton } from "@wordpress/components"; import { useDispatch, useRegistry, useSelect } from "@wordpress/data"; import { __, isRTL } from "@wordpress/i18n"; import { formatListBullets, formatListBulletsRTL, formatListNumbered, formatListNumberedRTL, formatOutdent, formatOutdentRTL } from "@wordpress/icons"; import { createBlock } from "@wordpress/blocks"; import { useCallback, useEffect } from "@wordpress/element"; import deprecated from "@wordpress/deprecated"; import OrderedListSettings from "./ordered-list-settings.mjs"; import { migrateToListV2 } from "./utils.mjs"; import TagName from "./tag-name.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/list/edit.jsx const DEFAULT_BLOCK = { name: "core/list-item" }; /** * At the moment, deprecations don't handle create blocks from attributes * (like when using CPT templates). For this reason, this hook is necessary * to avoid breaking templates using the old list block format. * * @param {Object} attributes Block attributes. * @param {string} clientId Block client ID. */ function useMigrateOnLoad(attributes, clientId) { const registry = useRegistry(); const { updateBlockAttributes, replaceInnerBlocks, selectBlock } = useDispatch(store); useEffect(() => { if (!attributes.values) return; const [newAttributes, newInnerBlocks] = migrateToListV2(attributes); deprecated("Value attribute on the list block", { since: "6.0", version: "6.5", alternative: "inner blocks" }); const shouldReselectBlock = registry.select(store).hasSelectedInnerBlock(clientId, true); registry.batch(() => { updateBlockAttributes(clientId, newAttributes); replaceInnerBlocks(clientId, newInnerBlocks); if (shouldReselectBlock) selectBlock(clientId); }); }, [attributes.values]); } function useOutdentList(clientId) { const { replaceBlocks, selectionChange } = useDispatch(store); const { getBlockRootClientId, getBlockAttributes, getBlock } = useSelect(store); return useCallback(() => { const parentBlockId = getBlockRootClientId(clientId); const parentBlockAttributes = getBlockAttributes(parentBlockId); const newParentBlock = createBlock("core/list-item", parentBlockAttributes); const { innerBlocks } = getBlock(clientId); replaceBlocks([parentBlockId], [newParentBlock, ...innerBlocks]); selectionChange(innerBlocks[innerBlocks.length - 1].clientId); }, [clientId]); } function IndentUI({ clientId }) { const outdentList = useOutdentList(clientId); const canOutdent = useSelect((select) => { const { getBlockRootClientId, getBlockName } = select(store); return getBlockName(getBlockRootClientId(clientId)) === "core/list-item"; }, [clientId]); return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(ToolbarButton, { icon: isRTL() ? formatOutdentRTL : formatOutdent, title: __("Outdent"), description: __("Outdent list item"), disabled: !canOutdent, onClick: outdentList }) }); } function Edit({ attributes, setAttributes, clientId }) { const { ordered, type, reversed, start } = attributes; const blockProps = useBlockProps({ style: { listStyleType: ordered && type !== "decimal" ? type : void 0 } }); const innerBlocksProps = useInnerBlocksProps(blockProps, { defaultBlock: DEFAULT_BLOCK, directInsert: true, templateLock: false, __experimentalCaptureToolbars: true }); useMigrateOnLoad(attributes, clientId); const controls = /* @__PURE__ */ jsxs(BlockControls, { group: "block", children: [ /* @__PURE__ */ jsx(ToolbarButton, { icon: isRTL() ? formatListBulletsRTL : formatListBullets, title: __("Unordered"), description: __("Convert to unordered list"), isActive: ordered === false, onClick: () => { setAttributes({ ordered: false }); } }), /* @__PURE__ */ jsx(ToolbarButton, { icon: isRTL() ? formatListNumberedRTL : formatListNumbered, title: __("Ordered"), description: __("Convert to ordered list"), isActive: ordered === true, onClick: () => { setAttributes({ ordered: true }); } }), /* @__PURE__ */ jsx(IndentUI, { clientId }) ] }); return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(TagName, { ordered, reversed, start, ...innerBlocksProps }), controls, ordered && /* @__PURE__ */ jsx(OrderedListSettings, { setAttributes, reversed, start, type }) ] }); } export { Edit as default }; //# sourceMappingURL=edit.mjs.map