UNPKG

@wordpress/block-library

Version:
149 lines (148 loc) • 6.75 kB
"use strict"; 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_block_editor = require("@wordpress/block-editor"); let _wordpress_components = require("@wordpress/components"); let _wordpress_data = require("@wordpress/data"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_icons = require("@wordpress/icons"); let _wordpress_blocks = require("@wordpress/blocks"); let _wordpress_element = require("@wordpress/element"); let _wordpress_deprecated = require("@wordpress/deprecated"); _wordpress_deprecated = __toESM(_wordpress_deprecated); let __ordered_list_settings_jsx = require("./ordered-list-settings.cjs"); __ordered_list_settings_jsx = __toESM(__ordered_list_settings_jsx); let __utils_js = require("./utils.cjs"); let __tag_name_jsx = require("./tag-name.cjs"); __tag_name_jsx = __toESM(__tag_name_jsx); let react_jsx_runtime = require("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 = (0, _wordpress_data.useRegistry)(); const { updateBlockAttributes, replaceInnerBlocks, selectBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); (0, _wordpress_element.useEffect)(() => { if (!attributes.values) return; const [newAttributes, newInnerBlocks] = (0, __utils_js.migrateToListV2)(attributes); (0, _wordpress_deprecated.default)("Value attribute on the list block", { since: "6.0", version: "6.5", alternative: "inner blocks" }); const shouldReselectBlock = registry.select(_wordpress_block_editor.store).hasSelectedInnerBlock(clientId, true); registry.batch(() => { updateBlockAttributes(clientId, newAttributes); replaceInnerBlocks(clientId, newInnerBlocks); if (shouldReselectBlock) selectBlock(clientId); }); }, [attributes.values]); } function useOutdentList(clientId) { const { replaceBlocks, selectionChange } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const { getBlockRootClientId, getBlockAttributes, getBlock } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store); return (0, _wordpress_element.useCallback)(() => { const parentBlockId = getBlockRootClientId(clientId); const parentBlockAttributes = getBlockAttributes(parentBlockId); const newParentBlock = (0, _wordpress_blocks.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 = (0, _wordpress_data.useSelect)((select) => { const { getBlockRootClientId, getBlockName } = select(_wordpress_block_editor.store); return getBlockName(getBlockRootClientId(clientId)) === "core/list-item"; }, [clientId]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, { icon: (0, _wordpress_i18n.isRTL)() ? _wordpress_icons.formatOutdentRTL : _wordpress_icons.formatOutdent, title: (0, _wordpress_i18n.__)("Outdent"), description: (0, _wordpress_i18n.__)("Outdent list item"), disabled: !canOutdent, onClick: outdentList }) }); } function Edit({ attributes, setAttributes, clientId }) { const { ordered, type, reversed, start } = attributes; const blockProps = (0, _wordpress_block_editor.useBlockProps)({ style: { listStyleType: ordered && type !== "decimal" ? type : void 0 } }); const innerBlocksProps = (0, _wordpress_block_editor.useInnerBlocksProps)(blockProps, { defaultBlock: DEFAULT_BLOCK, directInsert: true, templateLock: false, __experimentalCaptureToolbars: true }); useMigrateOnLoad(attributes, clientId); const controls = /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_block_editor.BlockControls, { group: "block", children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, { icon: (0, _wordpress_i18n.isRTL)() ? _wordpress_icons.formatListBulletsRTL : _wordpress_icons.formatListBullets, title: (0, _wordpress_i18n.__)("Unordered"), description: (0, _wordpress_i18n.__)("Convert to unordered list"), isActive: ordered === false, onClick: () => { setAttributes({ ordered: false }); } }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, { icon: (0, _wordpress_i18n.isRTL)() ? _wordpress_icons.formatListNumberedRTL : _wordpress_icons.formatListNumbered, title: (0, _wordpress_i18n.__)("Ordered"), description: (0, _wordpress_i18n.__)("Convert to ordered list"), isActive: ordered === true, onClick: () => { setAttributes({ ordered: true }); } }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(IndentUI, { clientId }) ] }); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__tag_name_jsx.default, { ordered, reversed, start, ...innerBlocksProps }), controls, ordered && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__ordered_list_settings_jsx.default, { setAttributes, reversed, start, type }) ] }); } exports.default = Edit; //# sourceMappingURL=edit.cjs.map