import { Element, Editor, Transforms, Range } from "slate";
import { HorizontalRuleRenderer } from "./HorizontalRuleRenderer.js";
const horizontalRuleElementType = "horizontalRule";
const isHorizontalRuleElement = (element) => Element.isElement(element) && element.type === horizontalRuleElementType;
const isHorizontalRuleElementActive = (editor) => {
  const [hr] = Editor.nodes(editor, { match: isHorizontalRuleElement });
  return !!hr;
};
const horizontalRuleElementPlugin = {
  type: horizontalRuleElementType,
  render: HorizontalRuleRenderer,
  isVoid: true,
  toggleElement: ({ editor }) => {
    if (isHorizontalRuleElementActive(editor)) {
      removeHorizontalRule(editor);
    } else {
      insertHorizontalRule(editor);
    }
  }
};
const removeHorizontalRule = (editor) => {
  Transforms.removeNodes(editor, { match: isHorizontalRuleElement });
};
const insertHorizontalRule = (editor) => {
  const selection = editor.selection;
  const isCollapsed = selection ? Range.isCollapsed(selection) : false;
  if (!isCollapsed || isHorizontalRuleElementActive(editor)) {
    return;
  }
  const horizontalRule = {
    type: horizontalRuleElementType,
    children: [{ text: "" }]
  };
  Transforms.insertNodes(editor, horizontalRule);
};
export {
  horizontalRuleElementPlugin,
  horizontalRuleElementType,
  isHorizontalRuleElement,
  isHorizontalRuleElementActive
};
//# sourceMappingURL=HorizontalRuleElement.js.map
