UNPKG

@wordpress/block-library

Version:
131 lines (130 loc) • 6.58 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_components = require("@wordpress/components"); let _wordpress_data = require("@wordpress/data"); let _wordpress_blocks = require("@wordpress/blocks"); let ___utils_hooks_js = require("../utils/hooks.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/accordion/edit.jsx const ACCORDION_BLOCK_NAME = "core/accordion-item"; const ACCORDION_HEADING_BLOCK_NAME = "core/accordion-heading"; const ACCORDION_BLOCK = { name: ACCORDION_BLOCK_NAME }; function Edit({ attributes: { autoclose, iconPosition, showIcon, headingLevel, levelOptions }, clientId, setAttributes, isSelected: isSingleSelected }) { const registry = (0, _wordpress_data.useRegistry)(); const { getBlockOrder, getBlockName } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store); const blockProps = (0, _wordpress_block_editor.useBlockProps)({ role: "group" }); const dropdownMenuProps = (0, ___utils_hooks_js.useToolsPanelDropdownMenuProps)(); const { updateBlockAttributes, insertBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const isContentOnlyMode = (0, _wordpress_block_editor.useBlockEditingMode)() === "contentOnly"; const innerBlocksProps = (0, _wordpress_block_editor.useInnerBlocksProps)(blockProps, { defaultBlock: ACCORDION_BLOCK, directInsert: true }); const addAccordionItemBlock = () => { const newAccordionItem = (0, _wordpress_blocks.createBlock)(ACCORDION_BLOCK_NAME, {}, [(0, _wordpress_blocks.createBlock)(ACCORDION_HEADING_BLOCK_NAME, { level: headingLevel }), (0, _wordpress_blocks.createBlock)("core/accordion-panel", {})]); insertBlock(newAccordionItem, void 0, clientId); }; /** * Update all child Accordion Heading blocks with a new heading level * based on the accordion group setting. * @param {number} newHeadingLevel The new heading level to set */ const updateHeadingLevel = (newHeadingLevel) => { const innerBlockClientIds = getBlockOrder(clientId); const accordionHeaderClientIds = []; innerBlockClientIds.forEach((contentClientId) => { const headerClientIds = getBlockOrder(contentClientId).filter((childClientId) => getBlockName(childClientId) === ACCORDION_HEADING_BLOCK_NAME); accordionHeaderClientIds.push(...headerClientIds); }); registry.batch(() => { setAttributes({ headingLevel: newHeadingLevel }); updateBlockAttributes(accordionHeaderClientIds, { level: newHeadingLevel }); }); }; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ isSingleSelected && !isContentOnlyMode && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarGroup, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.HeadingLevelDropdown, { value: headingLevel, options: levelOptions, onChange: updateHeadingLevel }) }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { group: "other", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, { onClick: addAccordionItemBlock, children: (0, _wordpress_i18n.__)("Add item") }) })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalToolsPanel, { label: (0, _wordpress_i18n.__)("Settings"), resetAll: () => { setAttributes({ autoclose: false, showIcon: true, iconPosition: "right" }); }, dropdownMenuProps, children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Auto-close"), isShownByDefault: true, hasValue: () => !!autoclose, onDeselect: () => setAttributes({ autoclose: false }), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Auto-close"), onChange: (value) => { setAttributes({ autoclose: value }); }, checked: autoclose, help: (0, _wordpress_i18n.__)("Automatically close accordions when a new one is opened.") }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Show icon"), isShownByDefault: true, hasValue: () => !showIcon, onDeselect: () => setAttributes({ showIcon: true }), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, { label: (0, _wordpress_i18n.__)("Show icon"), onChange: (value) => { setAttributes({ showIcon: value, iconPosition: value ? iconPosition : "right" }); }, checked: showIcon, help: (0, _wordpress_i18n.__)("Display a plus icon next to the accordion header.") }) }), showIcon && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Icon Position"), isShownByDefault: true, hasValue: () => iconPosition !== "right", onDeselect: () => setAttributes({ iconPosition: "right" }), children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalToggleGroupControl, { isBlock: true, label: (0, _wordpress_i18n.__)("Icon Position"), value: iconPosition, onChange: (value) => { setAttributes({ iconPosition: value }); }, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToggleGroupControlOption, { label: (0, _wordpress_i18n._x)("Start", "Icon position option for Accordion block"), value: "left" }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToggleGroupControlOption, { label: (0, _wordpress_i18n._x)("End", "Icon position option for Accordion block"), value: "right" })] }) }) ] }) }, "setting"), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...innerBlocksProps }) ] }); } exports.default = Edit; //# sourceMappingURL=edit.cjs.map