UNPKG

@wordpress/block-library

Version:
104 lines (103 loc) 3.04 kB
import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import clsx from "clsx"; import { __ } from "@wordpress/i18n"; import { useEffect, Platform } from "@wordpress/element"; import { useDispatch, useSelect } from "@wordpress/data"; import { AlignmentControl, BlockControls, RichText, useBlockProps, store as blockEditorStore, HeadingLevelDropdown, useBlockEditingMode } from "@wordpress/block-editor"; import { generateAnchor, setAnchor } from "./autogenerate-anchors"; function HeadingEdit({ attributes, setAttributes, mergeBlocks, onReplace, style, clientId }) { const { textAlign, content, level, levelOptions, placeholder, anchor } = attributes; const tagName = "h" + level; const blockProps = useBlockProps({ className: clsx({ [`has-text-align-${textAlign}`]: textAlign }), style }); const blockEditingMode = useBlockEditingMode(); const { canGenerateAnchors } = useSelect((select) => { const { getGlobalBlockCount, getSettings } = select(blockEditorStore); const settings = getSettings(); return { canGenerateAnchors: !!settings.generateAnchors || getGlobalBlockCount("core/table-of-contents") > 0 }; }, []); const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(blockEditorStore); useEffect(() => { if (!canGenerateAnchors) { return; } if (!anchor && content) { __unstableMarkNextChangeAsNotPersistent(); setAttributes({ anchor: generateAnchor(clientId, content) }); } setAnchor(clientId, anchor); return () => setAnchor(clientId, null); }, [anchor, content, clientId, canGenerateAnchors]); const onContentChange = (value) => { const newAttrs = { content: value }; if (canGenerateAnchors && (!anchor || !value || generateAnchor(clientId, content) === anchor)) { newAttrs.anchor = generateAnchor(clientId, value); } setAttributes(newAttrs); }; return /* @__PURE__ */ jsxs(Fragment, { children: [ blockEditingMode === "default" && /* @__PURE__ */ jsxs(BlockControls, { group: "block", children: [ /* @__PURE__ */ jsx( HeadingLevelDropdown, { value: level, options: levelOptions, onChange: (newLevel) => setAttributes({ level: newLevel }) } ), /* @__PURE__ */ jsx( AlignmentControl, { value: textAlign, onChange: (nextAlign) => { setAttributes({ textAlign: nextAlign }); } } ) ] }), /* @__PURE__ */ jsx( RichText, { identifier: "content", tagName, value: content, onChange: onContentChange, onMerge: mergeBlocks, onReplace, onRemove: () => onReplace([]), placeholder: placeholder || __("Heading"), textAlign, ...Platform.isNative && { deleteEnter: true }, ...blockProps } ) ] }); } var edit_default = HeadingEdit; export { edit_default as default }; //# sourceMappingURL=edit.js.map