const y = () => ({
  name: "fontSize",
  marks: {
    fontSize: {
      attrs: {
        size: { default: null }
      },
      parseDOM: [
        {
          tag: 'span[style*="font-size"]',
          getAttrs: (t) => {
            const e = t.style.fontSize;
            return e ? { size: e } : !1;
          }
        },
        {
          tag: "font[size]",
          getAttrs: (t) => {
            const e = t.getAttribute("size");
            return e ? { size: e } : !1;
          }
        }
      ],
      toDOM: (t) => ["span", { style: `font-size: ${t.attrs.size}` }, 0]
    }
  },
  toolbar: [
    {
      label: "Font Size",
      command: "fontSize",
      type: "group",
      items: [
        {
          label: "Decrease Font Size",
          command: "decreaseFontSize",
          icon: "−",
          type: "button"
        },
        {
          label: "Font Size",
          command: "setFontSize",
          type: "input",
          placeholder: "14"
        },
        {
          label: "Increase Font Size",
          command: "increaseFontSize",
          icon: "+",
          type: "button"
        }
      ]
    }
  ],
  commands: {
    decreaseFontSize: () => {
      try {
        return u(-2), m(), !0;
      } catch (t) {
        return console.error("Failed to decrease font size:", t), !1;
      }
    },
    increaseFontSize: () => {
      try {
        return u(2), m(), !0;
      } catch (t) {
        return console.error("Failed to increase font size:", t), !1;
      }
    },
    setFontSize: (t) => {
      var o;
      if (!t) return !1;
      try {
        const n = t.trim().match(/^(\d+(?:\.\d+)?)(px|em|rem)?$/i);
        if (!n) return !1;
        const r = parseFloat(n[1]), i = ((o = n[2]) == null ? void 0 : o.toLowerCase()) || "px";
        return i === "px" && (r < 8 || r > 72) || (i === "em" || i === "rem") && (r < 0.5 || r > 5) ? !1 : (d(r, i), !0);
      } catch (e) {
        return console.error("Failed to set font size:", e), !1;
      }
    }
  },
  keymap: {}
});
function u(t) {
  const o = window.getSelection();
  if (!o || o.rangeCount === 0) return;
  const { value: e, unit: n } = f();
  let r = t;
  (n === "em" || n === "rem") && (r = t * 0.125);
  let i;
  n === "px" ? i = t < 0 ? Math.max(8, e + t) : Math.min(72, e + t) : i = t < 0 ? Math.max(0.5, e + r) : Math.min(5, e + r), d(i, n);
}
function f() {
  var r;
  const t = window.getSelection();
  if (!t || t.rangeCount === 0)
    return { value: 14, unit: "px" };
  const e = t.getRangeAt(0).startContainer, n = e.nodeType === Node.TEXT_NODE ? e.parentElement : e;
  if (n) {
    const i = (r = n.style) == null ? void 0 : r.fontSize;
    if (i) {
      const l = i.match(/^(\d+(?:\.\d+)?)(px|em|rem)$/i);
      if (l)
        return {
          value: parseFloat(l[1]),
          unit: l[2].toLowerCase()
        };
    }
    const s = window.getComputedStyle(n).fontSize.match(/^(\d+(?:\.\d+)?)(px|em|rem)$/i);
    if (s)
      return {
        value: parseFloat(s[1]),
        unit: s[2].toLowerCase()
      };
  }
  return { value: 14, unit: "px" };
}
function d(t, o = "px") {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return;
  const n = e.getRangeAt(0);
  if (n.collapsed)
    return;
  const r = `${t}${o}`, i = n.commonAncestorContainer, c = S(i);
  if (c && z(n, c)) {
    c.style.fontSize = r;
    const a = document.createRange();
    a.selectNodeContents(c), e.removeAllRanges(), e.addRange(a);
  } else {
    const a = document.createElement("span");
    a.style.fontSize = r;
    try {
      n.surroundContents(a);
    } catch (l) {
      const p = n.extractContents();
      a.appendChild(p), n.insertNode(a);
    }
    const s = document.createRange();
    s.selectNodeContents(a), e.removeAllRanges(), e.addRange(s);
  }
}
function S(t) {
  let o = t;
  for (; o && o !== document.body; ) {
    if (o.nodeType === Node.ELEMENT_NODE) {
      const e = o;
      if (e.tagName === "SPAN" && e.style.fontSize)
        return e;
    }
    o = o.parentNode;
  }
  return null;
}
function z(t, o) {
  const e = t.startContainer, n = t.endContainer;
  return o.contains(e) && o.contains(n);
}
function m() {
  setTimeout(() => {
    const { value: t, unit: o } = f();
    let e = null;
    if (e = document.querySelector(
      'input[placeholder="14"]'
    ), e || (e = document.querySelector(
      '.rte-toolbar-input[type="text"]'
    )), !e) {
      const n = Array.from(document.querySelectorAll("button")).find(
        (r) => {
          var i;
          return ((i = r.textContent) == null ? void 0 : i.trim()) === "−";
        }
      );
      n && n.parentElement && (e = n.parentElement.querySelector(
        'input[type="text"]'
      ));
    }
    if (e) {
      const n = t % 1 === 0 ? t.toString() : t.toFixed(2).replace(/\.?0+$/, "");
      e.value = `${n}${o}`;
    }
  }, 10);
}
export {
  y as FontSizePlugin
};
//# sourceMappingURL=FontSizePlugin.native-DkLMLPue.mjs.map
