import { Transforms, Editor } from "slate";
import { ContemberEditor } from "../../../ContemberEditor/index.js";
import { HeadingRenderer } from "./HeadingRenderer.js";
const headingElementType = "heading";
const isHeadingElement = (element, suchThat) => ContemberEditor.isElementType(element, headingElementType, suchThat);
const ejectHeadingElement = (editor, elementPath) => {
  ContemberEditor.ejectElement(editor, elementPath);
  Transforms.setNodes(editor, { type: editor.defaultElementType }, { at: elementPath });
};
const headingElementPlugin = {
  type: headingElementType,
  render: HeadingRenderer,
  canContainAnyBlocks: false,
  acceptsAttributes: ({ editor, suchThat }) => {
    return "align" in suchThat;
  },
  toggleElement: ({ editor, suchThat }) => {
    Editor.withoutNormalizing(editor, () => {
      const headings = Array.from(Editor.nodes(editor, { match: (it) => isHeadingElement(it, suchThat) }));
      if (headings.length) {
        for (const heading of headings) {
          ejectHeadingElement(editor, heading[1]);
        }
      } else {
        Transforms.setNodes(editor, {
          ...suchThat,
          children: [],
          type: headingElementType
        });
      }
    });
  }
};
export { ejectHeadingElement, headingElementPlugin, headingElementType, isHeadingElement };
//# sourceMappingURL=HeadingElement.js.map
