const g = [
  { label: "1.0", value: "1.0" },
  { label: "1.15", value: "1.15" },
  { label: "1.5", value: "1.5" },
  { label: "2.0", value: "2.0" },
  { label: "2.5", value: "2.5" },
  { label: "3.0", value: "3.0" }
], m = /* @__PURE__ */ new Set([
  "P",
  "DIV",
  "H1",
  "H2",
  "H3",
  "H4",
  "H5",
  "H6",
  "LI",
  "BLOCKQUOTE",
  "PRE"
]), d = (e) => m.has(e.tagName) && e.getAttribute("contenteditable") !== "true", E = () => {
  const e = window.getSelection();
  if (e && e.rangeCount > 0) {
    let n = e.getRangeAt(0).startContainer;
    for (; n && n !== document.body; ) {
      if (n.nodeType === Node.ELEMENT_NODE) {
        const o = n;
        if (o.getAttribute("contenteditable") === "true")
          return o;
      }
      n = n.parentNode;
    }
  }
  const t = document.activeElement;
  if (t) {
    if (t.getAttribute("contenteditable") === "true")
      return t;
    const n = t.closest('[contenteditable="true"]');
    if (n) return n;
  }
  return document.querySelector('[contenteditable="true"]');
}, u = (e) => {
  let t = e;
  for (; t; ) {
    if (t.nodeType === Node.ELEMENT_NODE) {
      const n = t;
      if (d(n))
        return n;
      if (n.getAttribute("contenteditable") === "true") break;
    }
    t = t.parentNode;
  }
  return null;
}, H = (e, t) => {
  const n = [], o = /* @__PURE__ */ new Set(), r = (i) => {
    !i || o.has(i) || t.contains(i) && d(i) && (o.add(i), n.push(i));
  };
  if (e.collapsed)
    return r(u(e.startContainer)), n;
  const s = document.createTreeWalker(
    t,
    NodeFilter.SHOW_ELEMENT,
    {
      acceptNode: (i) => {
        const c = i;
        if (!d(c)) return NodeFilter.FILTER_SKIP;
        if (typeof e.intersectsNode == "function")
          return e.intersectsNode(c) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
        const l = document.createRange();
        return l.selectNodeContents(c), e.compareBoundaryPoints(Range.END_TO_START, l) > 0 && e.compareBoundaryPoints(Range.START_TO_END, l) < 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
      }
    }
  );
  let a = s.nextNode();
  for (; a; )
    r(a), a = s.nextNode();
  return n.length === 0 && r(u(e.commonAncestorContainer)), n;
}, L = (e) => {
  e.dispatchEvent(new Event("input", { bubbles: !0 }));
}, b = (e, t) => {
  if (t === e.innerHTML) return;
  const n = window.execEditorCommand || window.executeEditorCommand;
  if (typeof n == "function")
    try {
      n("recordDomTransaction", e, t, e.innerHTML);
    } catch {
    }
}, h = (e) => {
  if (!e) return !1;
  try {
    const t = E();
    if (!t) return !1;
    const n = window.getSelection();
    if (!n || n.rangeCount === 0) return !1;
    const o = n.getRangeAt(0);
    if (!t.contains(o.commonAncestorContainer)) return !1;
    const r = H(o, t);
    if (r.length === 0) return !1;
    const s = t.innerHTML;
    return r.forEach((a) => {
      a.style.lineHeight = e;
    }), b(t, s), L(t), !0;
  } catch (t) {
    return console.error("Failed to set line height:", t), !1;
  }
}, p = (e, t) => {
  typeof window < "u" && window.registerEditorCommand?.(e, t);
}, f = () => {
  p("setLineHeight", h);
};
typeof window < "u" && (document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", f) : f());
const N = () => ({
  name: "lineHeight",
  marks: {
    lineHeight: {
      attrs: {
        height: { default: null }
      },
      parseDOM: [
        {
          tag: 'span[style*="line-height"]',
          getAttrs: (e) => {
            const n = e.style.lineHeight;
            return n ? { height: n } : !1;
          }
        }
      ],
      toDOM: (e) => ["span", { style: `line-height: ${e.attrs?.height}` }, 0]
    }
  },
  toolbar: [
    {
      label: "Line Height",
      command: "setLineHeight",
      type: "inline-menu",
      options: g,
      icon: '<svg width="24" height="24" focusable="false"><path d="M21 5a1 1 0 0 1 .1 2H13a1 1 0 0 1-.1-2H21zm0 4a1 1 0 0 1 .1 2H13a1 1 0 0 1-.1-2H21zm0 4a1 1 0 0 1 .1 2H13a1 1 0 0 1-.1-2H21zm0 4a1 1 0 0 1 .1 2H13a1 1 0 0 1-.1-2H21zM7 3.6l3.7 3.7a1 1 0 0 1-1.3 1.5h-.1L8 7.3v9.2l1.3-1.3a1 1 0 0 1 1.3 0h.1c.4.4.4 1 0 1.3v.1L7 20.4l-3.7-3.7a1 1 0 0 1 1.3-1.5h.1L6 16.7V7.4L4.7 8.7a1 1 0 0 1-1.3 0h-.1a1 1 0 0 1 0-1.3v-.1L7 3.6z"></path></svg>'
    }
  ],
  commands: {
    setLineHeight: h
  }
});
export {
  N as LineHeightPlugin
};
