@wordpress/block-library
Version:
Block library for the WordPress editor.
126 lines (125 loc) • 5.21 kB
JavaScript
import { BlockControls, HeadingLevelDropdown, InspectorControls, store, useBlockEditingMode, useBlockProps, useInnerBlocksProps } from "@wordpress/block-editor";
import { __ } from "@wordpress/i18n";
import { ToggleControl, ToolbarButton, ToolbarGroup, __experimentalToggleGroupControl, __experimentalToggleGroupControlOption, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { useDispatch, useRegistry, useSelect } from "@wordpress/data";
import { createBlock } from "@wordpress/blocks";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import { Fragment, jsx, jsxs } from "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 = useRegistry();
const { getBlockOrder } = useSelect(store);
const blockProps = useBlockProps({ role: "group" });
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const { updateBlockAttributes, insertBlock } = useDispatch(store);
const isContentOnlyMode = useBlockEditingMode() === "contentOnly";
const innerBlocksProps = useInnerBlocksProps(blockProps, {
defaultBlock: ACCORDION_BLOCK,
directInsert: true
});
const addAccordionItemBlock = () => {
const newAccordionItem = createBlock(ACCORDION_BLOCK_NAME, {}, [createBlock(ACCORDION_HEADING_BLOCK_NAME, { level: headingLevel }), createBlock("core/accordion-panel", {})]);
insertBlock(newAccordionItem, void 0, clientId);
};
/**
* Update all child Accordion Header 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);
accordionHeaderClientIds.push(...headerClientIds);
});
registry.batch(() => {
setAttributes({ headingLevel: newHeadingLevel });
updateBlockAttributes(accordionHeaderClientIds, { level: newHeadingLevel });
});
};
return /* @__PURE__ */ jsxs(Fragment, { children: [
isSingleSelected && !isContentOnlyMode && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(BlockControls, { children: /* @__PURE__ */ jsx(ToolbarGroup, { children: /* @__PURE__ */ jsx(HeadingLevelDropdown, {
value: headingLevel,
options: levelOptions,
onChange: updateHeadingLevel
}) }) }), /* @__PURE__ */ jsx(BlockControls, {
group: "other",
children: /* @__PURE__ */ jsx(ToolbarButton, {
onClick: addAccordionItemBlock,
children: __("Add item")
})
})] }),
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
autoclose: false,
showIcon: true,
iconPosition: "right"
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Auto-close"),
isShownByDefault: true,
hasValue: () => !!autoclose,
onDeselect: () => setAttributes({ autoclose: false }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Auto-close"),
onChange: (value) => {
setAttributes({ autoclose: value });
},
checked: autoclose,
help: __("Automatically close accordions when a new one is opened.")
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Show icon"),
isShownByDefault: true,
hasValue: () => !showIcon,
onDeselect: () => setAttributes({ showIcon: true }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Show icon"),
onChange: (value) => {
setAttributes({
showIcon: value,
iconPosition: value ? iconPosition : "right"
});
},
checked: showIcon,
help: __("Display a plus icon next to the accordion header.")
})
}),
showIcon && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Icon Position"),
isShownByDefault: true,
hasValue: () => iconPosition !== "right",
onDeselect: () => setAttributes({ iconPosition: "right" }),
children: /* @__PURE__ */ jsxs(__experimentalToggleGroupControl, {
isBlock: true,
label: __("Icon Position"),
value: iconPosition,
onChange: (value) => {
setAttributes({ iconPosition: value });
},
children: [/* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
label: __("Left"),
value: "left"
}), /* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
label: __("Right"),
value: "right"
})]
})
})
]
}) }, "setting"),
/* @__PURE__ */ jsx("div", { ...innerBlocksProps })
] });
}
export { Edit as default };
//# sourceMappingURL=edit.mjs.map