const f = "data-editora-selection-marker";
function w(e) {
  return e ? e.nodeType === Node.ELEMENT_NODE ? e : e.parentElement : null;
}
function m(e) {
  return e ? e.getAttribute("contenteditable") === "true" ? e : e.querySelector('[contenteditable="true"]') : null;
}
function y(e) {
  var r;
  if ((e == null ? void 0 : e.contentElement) instanceof HTMLElement) return e.contentElement;
  const t = m((r = e == null ? void 0 : e.editorElement) != null ? r : null);
  if (t) return t;
  const n = typeof window != "undefined" ? window.__editoraCommandEditorRoot : null, o = n instanceof HTMLElement ? m(n) : null;
  if (o) return o;
  const l = window.getSelection();
  if (l && l.rangeCount > 0) {
    const a = w(l.getRangeAt(0).startContainer), i = a == null ? void 0 : a.closest('.rte-content, .editora-content, [contenteditable="true"]');
    if (i instanceof HTMLElement) return i;
  }
  const c = document.activeElement;
  if (c instanceof HTMLElement) {
    const a = c.closest('.rte-content, .editora-content, [contenteditable="true"]');
    if (a instanceof HTMLElement) return a;
  }
  return document.querySelector('.rte-content, .editora-content, [contenteditable="true"]');
}
function R(e) {
  const t = window.getSelection();
  if (!t || t.rangeCount === 0) return !1;
  const n = t.getRangeAt(0);
  return e.contains(n.startContainer) && e.contains(n.endContainer);
}
function E(e) {
  const t = document.createElement("span");
  return t.setAttribute(f, e), t.style.display = "inline-block", t.style.width = "0", t.style.height = "0", t.style.overflow = "hidden", t.style.lineHeight = "0", t.appendChild(document.createTextNode("\uFEFF")), t;
}
function b(e) {
  const t = window.getSelection();
  if (!t || t.rangeCount === 0) return null;
  const n = t.getRangeAt(0);
  if (!e.contains(n.startContainer) || !e.contains(n.endContainer))
    return null;
  const o = n.collapsed, l = E("start"), c = o ? null : E("end"), r = n.cloneRange();
  if (c) {
    const i = r.cloneRange();
    i.collapse(!1), i.insertNode(c);
  }
  const a = r.cloneRange();
  return a.collapse(!0), a.insertNode(l), () => {
    const i = window.getSelection(), s = document.createRange(), d = e.querySelector(
      `[${f}="start"]`
    ), u = e.querySelector(
      `[${f}="end"]`
    );
    if (!i || !d) {
      e.querySelectorAll(`[${f}]`).forEach((g) => g.remove());
      return;
    }
    o || !u ? (s.setStartBefore(d), s.collapse(!0)) : (s.setStartAfter(d), s.setEndBefore(u)), i.removeAllRanges(), i.addRange(s), d.remove(), u == null || u.remove();
  };
}
function p(e, t, n) {
  if (t !== n) {
    if (typeof window.execEditorCommand == "function") {
      window.execEditorCommand("recordDomTransaction", e, t, n);
      return;
    }
    if (typeof window.executeEditorCommand == "function")
      try {
        window.executeEditorCommand("recordDomTransaction", { editor: e, beforeHTML: t, afterHTML: n });
      } catch (o) {
      }
  }
}
function S(e) {
  const t = y(e.context);
  if (!t) return !1;
  const n = t.innerHTML;
  R(t) && t.focus({ preventScroll: !0 });
  let o = !1;
  try {
    o = document.execCommand(e.command, !1);
  } catch (r) {
    o = !1;
  }
  const l = t.innerHTML;
  if (e.normalize && l !== n) {
    const r = b(t);
    e.normalize(t), r == null || r();
  }
  const c = t.innerHTML;
  return n !== c && (p(t, n, c), t.dispatchEvent(new Event("input", { bubbles: !0 }))), o !== !1 || n !== c;
}
export {
  S as a
};
//# sourceMappingURL=inlineFormatting-DJ0-NZtx.mjs.map
