const u = '.rte-page-break[data-type="page-break"]', f = ".rte-content, .editora-content";
let p = null, m = !1;
const h = (e, n) => {
  if (n === e.innerHTML) return;
  const t = window.execEditorCommand || window.executeEditorCommand;
  if (typeof t == "function")
    try {
      t("recordDomTransaction", e, n, e.innerHTML);
    } catch {
    }
}, x = /* @__PURE__ */ new Set([
  "DIV",
  "P",
  "BLOCKQUOTE",
  "PRE",
  "H1",
  "H2",
  "H3",
  "H4",
  "H5",
  "H6",
  "LI",
  "TD",
  "TH"
]), C = () => {
  p || typeof document > "u" || (p = document.createElement("style"), p.textContent = `
    .rte-page-break {
      display: block;
      position: relative;
      height: 12px;
      margin: 8px 0;
      background: linear-gradient(90deg, #ccc 0%, transparent 100%);
      border-top: 2px dashed #999;
      border-bottom: none;
      cursor: pointer;
      user-select: none;
      transition: all 0.2s ease;
      outline: none;
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
    }

    .rte-page-break::before {
      content: '⎙ PAGE BREAK';
      position: absolute;
      top: -12px;
      left: 0;
      font-size: 10px;
      font-weight: bold;
      color: #666;
      background: white;
      padding: 2px 6px;
      letter-spacing: 0.5px;
      opacity: 0.7;
      pointer-events: none;
    }

    .rte-page-break:hover {
      background: linear-gradient(90deg, #999 0%, transparent 100%);
      border-top-color: #666;
      box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
    }

    .rte-page-break:hover::before {
      opacity: 1;
      color: #333;
    }

    .rte-page-break:focus,
    .rte-page-break:focus-visible,
    .rte-page-break-selected {
      outline: 2px solid #0066cc;
      outline-offset: -2px;
      border-top-color: #0066cc;
      background: linear-gradient(90deg, #0066cc 0%, transparent 100%);
    }

    .rte-page-break * {
      user-select: none;
    }

    @media print {
      .rte-page-break {
        display: block;
        height: 0;
        margin: 0;
        background: none;
        border: none;
        page-break-after: always;
      }

      .rte-page-break::before {
        display: none;
      }
    }
  `, document.head.appendChild(p));
}, N = () => {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return null;
  const n = e.getRangeAt(0);
  return (n.startContainer.nodeType === Node.ELEMENT_NODE ? n.startContainer : n.startContainer.parentElement)?.closest(f) || null;
}, v = () => {
  const e = N();
  if (e) return e;
  const t = document.activeElement?.closest(f);
  return t || document.querySelector(f);
}, E = (e, n) => {
  let t = e;
  for (; t && t !== n; ) {
    if (t.nodeType === Node.ELEMENT_NODE) {
      const r = t;
      if (x.has(r.tagName))
        return r;
    }
    t = t.parentNode;
  }
  return null;
}, T = () => {
  const e = document.createElement("div");
  return e.className = "rte-page-break", e.setAttribute("data-page-break", "true"), e.setAttribute("data-type", "page-break"), e.setAttribute("contenteditable", "false"), e.setAttribute("tabindex", "0"), e.setAttribute("role", "separator"), e.setAttribute("aria-label", "Page break"), e;
}, S = (e) => {
  let n = e.nextElementSibling;
  for (; n && n.matches(u); ) {
    const r = n;
    n = n.nextElementSibling, r.remove();
  }
  let t = e.previousElementSibling;
  for (; t && t.matches(u); ) {
    const r = t;
    t = t.previousElementSibling, r.remove();
  }
}, w = (e) => {
  const n = e.nextElementSibling;
  if (n && !n.matches(u))
    return n;
  const t = document.createElement("p");
  return t.innerHTML = "<br>", e.parentNode?.insertBefore(t, e.nextSibling), t;
}, d = (e) => {
  const n = window.getSelection();
  if (!n) return;
  const t = document.createRange();
  e.nodeType, Node.TEXT_NODE, t.setStart(e, 0), t.collapse(!0), n.removeAllRanges(), n.addRange(t);
}, b = (e) => {
  const n = window.getSelection();
  if (!n) return;
  const t = document.createRange();
  if (e.nodeType === Node.TEXT_NODE) {
    const r = e;
    t.setStart(r, r.data.length);
  } else
    t.selectNodeContents(e), t.collapse(!1);
  n.removeAllRanges(), n.addRange(t);
}, g = (e, n) => {
  let t = e;
  for (; t; ) {
    if (!(t instanceof HTMLElement && t.matches(u)))
      return t;
    t = n === "previous" ? t.previousSibling : t.nextSibling;
  }
  return null;
}, A = (e) => {
  const n = window.getSelection();
  if (!n || !e.parentNode) return;
  const t = e.parentNode, r = Array.from(t.childNodes).indexOf(e);
  if (r < 0) return;
  const o = document.createRange();
  o.setStart(t, r), o.setEnd(t, r + 1), n.removeAllRanges(), n.addRange(o), e.focus({ preventScroll: !0 });
}, y = (e) => {
  if (e.collapsed || e.startContainer !== e.endContainer || e.endOffset !== e.startOffset + 1 || !(e.startContainer instanceof Element || e.startContainer instanceof DocumentFragment)) return null;
  const n = e.startContainer.childNodes[e.startOffset];
  return n instanceof HTMLElement && n.matches(u) ? n : null;
}, B = (e, n, t) => {
  if (!e.collapsed) return null;
  const { startContainer: r, startOffset: o } = e, a = (i) => i instanceof HTMLElement && i.matches(u) ? i : null, c = (i) => {
    if (r.nodeType === Node.ELEMENT_NODE) {
      const l = r;
      if (i === "previous") {
        if (o > 0)
          return l.childNodes[o - 1] || null;
      } else if (o < l.childNodes.length)
        return l.childNodes[o] || null;
    }
    if (r.nodeType === Node.TEXT_NODE && (i === "previous" && o < r.data.length || i === "next" && o > 0))
      return null;
    let s = r;
    for (; s && s !== n; ) {
      const l = i === "previous" ? s.previousSibling : s.nextSibling;
      if (l) return l;
      s = s.parentNode;
    }
    return null;
  };
  if (r.nodeType === Node.ELEMENT_NODE) {
    const i = r;
    return t === "Backspace" && o > 0 ? a(i.childNodes[o - 1] || null) : t === "Delete" ? a(i.childNodes[o] || null) : null;
  }
  if (r.nodeType === Node.TEXT_NODE) {
    const i = r;
    if (t === "Backspace" && o === 0) {
      const s = a(i.previousSibling);
      return s || a(c("previous"));
    }
    if (t === "Delete" && o === i.data.length) {
      const s = a(i.nextSibling);
      return s || a(c("next"));
    }
  }
  return a(c(t === "Backspace" ? "previous" : "next"));
}, k = (e, n) => {
  const t = e.closest(f), r = t?.innerHTML ?? "", o = e.previousSibling, a = e.nextSibling;
  e.remove();
  const c = g(o, "previous"), i = g(a, "next");
  if (n === "Backspace") {
    if (c)
      b(c);
    else if (i)
      d(i);
    else if (t) {
      const s = document.createElement("p");
      s.innerHTML = "<br>", t.appendChild(s), d(s);
    }
  } else if (i)
    d(i);
  else if (c)
    b(c);
  else if (t) {
    const s = document.createElement("p");
    s.innerHTML = "<br>", t.appendChild(s), d(s);
  }
  return t && (h(t, r), t.dispatchEvent(new Event("input", { bubbles: !0 }))), !0;
}, D = () => {
  const e = v();
  if (!e) return !1;
  const n = e.innerHTML, t = window.getSelection();
  if (!t) return !1;
  let r;
  t.rangeCount > 0 && e.contains(t.getRangeAt(0).commonAncestorContainer) ? r = t.getRangeAt(0) : (r = document.createRange(), r.selectNodeContents(e), r.collapse(!1), t.removeAllRanges(), t.addRange(r));
  const o = E(r.endContainer, e) || E(r.startContainer, e), a = T();
  o && o.parentNode ? o.parentNode.insertBefore(a, o.nextSibling) : e.appendChild(a), S(a);
  const c = w(a);
  return d(c), h(e, n), e.dispatchEvent(new Event("input", { bubbles: !0 })), !0;
}, R = () => {
  m || typeof document > "u" || (m = !0, document.addEventListener("click", (e) => {
    const t = e.target?.closest(u);
    t && (e.preventDefault(), e.stopPropagation(), A(t));
  }), document.addEventListener("keydown", (e) => {
    const n = e.key;
    if (!["Backspace", "Delete", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(n))
      return;
    const t = window.getSelection();
    if (!t || t.rangeCount === 0) return;
    const r = t.getRangeAt(0), o = v();
    if (!o || !o.contains(r.commonAncestorContainer)) return;
    const a = y(r);
    if (a) {
      if (n === "Backspace" || n === "Delete") {
        e.preventDefault(), e.stopPropagation(), k(a, n);
        return;
      }
      if (n === "ArrowRight" || n === "ArrowDown") {
        e.preventDefault();
        const c = g(a.nextSibling, "next") || w(a);
        d(c);
        return;
      }
      if (n === "ArrowLeft" || n === "ArrowUp") {
        e.preventDefault();
        const c = g(a.previousSibling, "previous");
        c ? b(c) : d(o);
        return;
      }
    }
    if (n === "Backspace" || n === "Delete") {
      const c = B(
        r,
        o,
        n
      );
      if (!c) return;
      e.preventDefault(), e.stopPropagation(), k(c, n);
    }
  }));
}, P = () => (C(), R(), {
  name: "pageBreak",
  toolbar: [
    {
      label: "Page Break",
      command: "insertPageBreak",
      icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"><path d="M5 5H19" stroke="currentColor" stroke-width="1.9" stroke-linecap="round"/><path d="M5 9H19" stroke="currentColor" stroke-width="1.9" stroke-linecap="round"/><path d="M5 15H19" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-dasharray="3.2 3.2"/><path d="M5 19H19" stroke="currentColor" stroke-width="1.9" stroke-linecap="round"/></svg>',
      shortcut: "Mod-Enter"
    }
  ],
  commands: {
    insertPageBreak: D
  },
  keymap: {
    "Mod-Enter": "insertPageBreak"
  }
});
export {
  P as PageBreakPlugin
};
