@wordpress/block-library
Version:
Block library for the WordPress editor.
55 lines (54 loc) • 2.07 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { useEffect } from "@wordpress/element";
import { useDispatch, useSelect } from "@wordpress/data";
import { RichText, store, useBlockProps } from "@wordpress/block-editor";
import { generateAnchor, setAnchor } from "./autogenerate-anchors.mjs";
import useDeprecatedTextAlign from "../utils/deprecated-text-align-attributes.mjs";
import { jsx } from "react/jsx-runtime";
// packages/block-library/src/heading/edit.jsx
function HeadingEdit(props) {
const { attributes, setAttributes, mergeBlocks, onReplace, style, clientId } = props;
useDeprecatedTextAlign(props);
const { content, level, placeholder, anchor } = attributes;
const tagName = "h" + level;
const blockProps = useBlockProps({ style });
const { canGenerateAnchors } = useSelect((select) => {
const { getGlobalBlockCount, getSettings } = select(store);
return { canGenerateAnchors: !!getSettings().generateAnchors || getGlobalBlockCount("core/table-of-contents") > 0 };
}, []);
const { __unstableMarkNextChangeAsNotPersistent } = useDispatch(store);
useEffect(() => {
if (!canGenerateAnchors) return;
if (!anchor && content) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes({ anchor: generateAnchor(clientId, content) });
}
setAnchor(clientId, anchor);
return () => setAnchor(clientId, null);
}, [
anchor,
content,
clientId,
canGenerateAnchors,
setAttributes,
__unstableMarkNextChangeAsNotPersistent
]);
const onContentChange = (value) => {
const newAttrs = { content: value };
if (canGenerateAnchors && (!anchor || !value || generateAnchor(clientId, content) === anchor)) newAttrs.anchor = generateAnchor(clientId, value);
setAttributes(newAttrs);
};
return /* @__PURE__ */ jsx(RichText, {
identifier: "content",
tagName,
value: content,
onChange: onContentChange,
onMerge: mergeBlocks,
onReplace,
onRemove: onReplace ? () => onReplace([]) : void 0,
placeholder: placeholder || __("Heading"),
...blockProps
});
}
export { HeadingEdit as default };
//# sourceMappingURL=edit.mjs.map