const c = ["P", "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "LI", "BLOCKQUOTE", "PRE", "TD", "TH"];
function d(i) {
  let t = i;
  if (t.nodeType === Node.ELEMENT_NODE) {
    const e = t;
    if (c.includes(e.tagName))
      return e;
  }
  for (; t; ) {
    if (t.nodeType === Node.ELEMENT_NODE) {
      const e = t;
      if (c.includes(e.tagName))
        return e;
      if (e.hasAttribute("contenteditable"))
        return null;
    }
    t = t.parentNode;
  }
  return null;
}
function f(i) {
  const t = [], e = d(i.startContainer), l = d(i.endContainer);
  if (!e && !l) return t;
  if (i.collapsed)
    return e && t.push(e), t;
  if (e === l)
    e && t.push(e);
  else {
    let s = e;
    const o = /* @__PURE__ */ new Set();
    for (e && (t.push(e), o.add(e)); s && s !== l && !o.has(l); ) {
      let n = s.nextElementSibling;
      for (; n; ) {
        if (c.includes(n.tagName)) {
          s = n, o.has(s) || (t.push(s), o.add(s));
          break;
        }
        n = n.nextElementSibling;
      }
      if (!n) break;
    }
    l && !t.includes(l) && t.push(l);
  }
  return t;
}
const g = (i) => {
  var o;
  if (!i || !["left", "center", "right", "justify"].includes(i)) return !1;
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return !1;
  const l = e.getRangeAt(0).cloneRange(), s = f(l);
  if (s.length > 0) {
    s.forEach((a) => {
      a && (a.style.textAlign = i);
    }), e.removeAllRanges(), e.addRange(l);
    const n = (o = l.commonAncestorContainer.parentElement) == null ? void 0 : o.closest('[contenteditable="true"]');
    n && n.dispatchEvent(new Event("input", { bubbles: !0 }));
  } else
    try {
      const n = document.createElement("div");
      n.style.textAlign = i;
      const a = l.extractContents();
      n.appendChild(a), l.insertNode(n);
      const r = document.createRange();
      r.selectNodeContents(n), e.removeAllRanges(), e.addRange(r);
      const u = n.closest('[contenteditable="true"]');
      u && u.dispatchEvent(new Event("input", { bubbles: !0 }));
    } catch (n) {
      return console.error("Failed to wrap content for alignment:", n), !1;
    }
  return !0;
}, m = () => ({
  name: "textAlignment",
  toolbar: [
    {
      label: "Text Alignment",
      command: "setTextAlignment",
      type: "inline-menu",
      options: [
        { label: "Left", value: "left" },
        { label: "Center", value: "center" },
        { label: "Right", value: "right" },
        { label: "Justify", value: "justify" }
      ],
      icon: '<svg width="24" height="24" focusable="false"><path d="M5 5h14c.6 0 1 .4 1 1s-.4 1-1 1H5a1 1 0 1 1 0-2Zm0 4h8c.6 0 1 .4 1 1s-.4 1-1 1H5a1 1 0 1 1 0-2Zm0 8h8c.6 0 1 .4 1 1s-.4 1-1 1H5a1 1 0 0 1 0-2Zm0-4h14c.6 0 1 .4 1 1s-.4 1-1 1H5a1 1 0 0 1 0-2Z" fill-rule="evenodd"></path></svg>'
    }
  ],
  commands: {
    setTextAlignment: g
  },
  keymap: {}
});
export {
  m as TextAlignmentPlugin,
  g as setTextAlignmentCommand
};
//# sourceMappingURL=TextAlignmentPlugin.native-CQIs1m7R.mjs.map
