let r = null, f = null, g = null, i = "#000000";
const y = [
  "#000000",
  "#ffffff",
  "#808080",
  "#ff0000",
  "#00ff00",
  "#0000ff",
  "#ffff00",
  "#ff00ff",
  "#00ffff",
  "#ffa500",
  "#800080",
  "#ffc0cb"
];
function u(e) {
  try {
    if (g) {
      const s = window.getSelection();
      s && (s.removeAllRanges(), s.addRange(g));
    }
    const o = window.getSelection();
    if (!o || o.rangeCount === 0 || o.isCollapsed)
      return !1;
    const t = o.getRangeAt(0), l = t.startContainer.nodeType === Node.TEXT_NODE ? t.startContainer.parentElement : t.startContainer, c = t.endContainer.nodeType === Node.TEXT_NODE ? t.endContainer.parentElement : t.endContainer;
    let n = null, a = l;
    for (; a && a !== document.body; ) {
      if (a.classList.contains("rte-text-color")) {
        const s = document.createRange();
        if (s.selectNodeContents(a), s.compareBoundaryPoints(Range.START_TO_START, t) <= 0 && s.compareBoundaryPoints(Range.END_TO_END, t) >= 0) {
          n = a;
          break;
        }
      }
      a = a.parentElement;
    }
    if (n)
      return n.style.color = e, !0;
    const p = document.createElement("span");
    p.style.color = e, p.className = "rte-text-color";
    const h = t.extractContents();
    return p.appendChild(h), t.insertNode(p), t.setStartAfter(p), t.collapse(!0), o.removeAllRanges(), o.addRange(t), !0;
  } catch (o) {
    return console.error("Failed to set text color:", o), !1;
  }
}
function v() {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return "#000000";
  let t = e.getRangeAt(0).startContainer;
  for (; t && t !== document.body; ) {
    if (t.nodeType === Node.ELEMENT_NODE) {
      const l = t, c = l.style.color || window.getComputedStyle(l).color;
      if (c && c !== "rgb(0, 0, 0)")
        return w(c);
    }
    t = t.parentNode;
  }
  return "#000000";
}
function w(e) {
  if (e.startsWith("#")) return e;
  const o = e.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  if (!o) return "#000000";
  const t = parseInt(o[1]), l = parseInt(o[2]), c = parseInt(o[3]);
  return "#" + [t, l, c].map((n) => {
    const a = n.toString(16);
    return a.length === 1 ? "0" + a : a;
  }).join("");
}
function C(e) {
  const o = window.getSelection();
  o && o.rangeCount > 0 && (g = o.getRangeAt(0).cloneRange()), i = v(), r = document.createElement("div"), r.className = "rte-inline-color-picker", r.addEventListener("click", (l) => l.stopPropagation()), r.innerHTML = `
    <div class="rte-color-picker-header">
      <span class="rte-color-picker-title">Text Color</span>
      <button class="rte-color-picker-close" aria-label="Close">×</button>
    </div>
    
    <div class="rte-color-picker-body">
      <!-- Current Color Preview -->
      <div class="rte-color-preview-section">
        <div class="rte-color-preview-box" style="background-color: ${i}; ${i === "#ffffff" ? "border: 1px solid #ccc;" : ""}"></div>
        <span class="rte-color-preview-label">${i.toUpperCase()}</span>
      </div>

      <!-- Preset Colors -->
      <div class="rte-color-section">
        <label class="rte-color-section-label">Colors</label>
        <div class="rte-color-palette">
          ${y.map((l) => `
            <button
              class="rte-color-swatch ${i === l ? "selected" : ""}"
              style="background-color: ${l}; ${l === "#ffffff" ? "border: 1px solid #ccc;" : ""}"
              data-color="${l}"
              title="${l.toUpperCase()}"
              aria-label="${l.toUpperCase()}"
            ></button>
          `).join("")}
        </div>
      </div>

      <!-- Custom Color -->
      <div class="rte-color-section">
        <label class="rte-color-section-label">Custom</label>
        <div class="rte-custom-color-inputs">
          <input
            type="color"
            value="${i}"
            class="rte-color-input-native"
            aria-label="Color picker"
          />
          <input
            type="text"
            value="${i}"
            placeholder="#000000"
            pattern="^#[0-9A-Fa-f]{6}$"
            class="rte-color-input-text"
            aria-label="Hex color input"
          />
        </div>
      </div>
    </div>
  `;
  const t = e.getBoundingClientRect();
  r.style.position = "absolute", r.style.top = `${t.bottom + window.scrollY + 4}px`, r.style.left = `${t.left + window.scrollX}px`, r.style.zIndex = "10000", document.body.appendChild(r), f = e, k();
}
function k() {
  if (!r) return;
  const e = r.querySelector(".rte-color-picker-close");
  e == null || e.addEventListener("click", () => d()), r.querySelectorAll(".rte-color-swatch").forEach((c) => {
    c.addEventListener("click", () => {
      const n = c.getAttribute("data-color");
      n && (i = n, u(n), d());
    });
  });
  const t = r.querySelector(".rte-color-input-native");
  t == null || t.addEventListener("change", (c) => {
    const n = c.target.value;
    i = n, u(n), d();
  });
  const l = r.querySelector(".rte-color-input-text");
  l == null || l.addEventListener("change", (c) => {
    const n = c.target.value;
    /^#[0-9A-Fa-f]{6}$/.test(n) && (i = n, u(n), d());
  }), t == null || t.addEventListener("input", (c) => {
    const n = c.target.value;
    i = n, x(n), b(n), T(n);
  }), l == null || l.addEventListener("input", (c) => {
    const n = c.target.value;
    /^#[0-9A-Fa-f]{6}$/.test(n) && (i = n, x(n), b(n), E(n));
  });
}
function x(e) {
  if (!r) return;
  const o = r.querySelector(".rte-color-preview-box"), t = r.querySelector(".rte-color-preview-label");
  o && (o.style.backgroundColor = e, o.style.border = e === "#ffffff" ? "1px solid #ccc" : "none"), t && (t.textContent = e.toUpperCase());
}
function b(e) {
  if (!r) return;
  r.querySelectorAll(".rte-color-swatch").forEach((t) => {
    t.getAttribute("data-color") === e ? t.classList.add("selected") : t.classList.remove("selected");
  });
}
function E(e) {
  if (!r) return;
  const o = r.querySelector(".rte-color-input-native");
  o && (o.value = e);
}
function T(e) {
  if (!r) return;
  const o = r.querySelector(".rte-color-input-text");
  o && (o.value = e);
}
function d() {
  r && (r.remove(), r = null), f = null, g = null;
}
function S() {
  if (r)
    return d(), !0;
  const e = document.querySelector('[data-command="openTextColorPicker"]');
  return e ? (C(e), !0) : !1;
}
function m() {
  if (!window.__textColorPluginInitialized && (window.__textColorPluginInitialized = !0, document.addEventListener("click", (e) => {
    if (r && f) {
      const o = e.target;
      !r.contains(o) && !f.contains(o) && d();
    }
  }), !document.getElementById("text-color-plugin-styles"))) {
    const e = document.createElement("style");
    e.id = "text-color-plugin-styles", e.textContent = `
      .rte-inline-color-picker {
        background: white;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        width: 220px;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      }

      .rte-color-picker-header {
        padding: 12px 16px;
        border-bottom: 1px solid #eee;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .rte-color-picker-title {
        font-size: 14px;
        font-weight: 600;
        color: #333;
      }

      .rte-color-picker-close {
        background: none;
        border: none;
        font-size: 24px;
        cursor: pointer;
        color: #999;
        padding: 0;
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        line-height: 1;
      }

      .rte-color-picker-close:hover {
        color: #333;
      }

      .rte-color-picker-body {
        padding: 8px;
      }

      .rte-color-preview-section {
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 8px;
        padding: 6px;
        background-color: #f8f9fa;
        border-radius: 6px;
        border: 1px solid #e0e0e0;
      }

      .rte-color-preview-box {
        width: 24px;
        height: 24px;
        border-radius: 4px;
        flex-shrink: 0;
      }

      .rte-color-preview-label {
        font-size: 13px;
        font-weight: 500;
        color: #666;
        font-family: monospace;
      }

      .rte-color-section {
        margin-bottom: 16px;
      }

      .rte-color-section:last-child {
        margin-bottom: 0;
      }

      .rte-color-section-label {
        display: block;
        font-size: 12px;
        font-weight: 600;
        color: #666;
        margin-bottom: 8px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
      }

      .rte-color-palette {
        display: grid;
        grid-template-columns: repeat(7, 1fr);
        gap: 6px;
        max-width: 180px;
      }

      .rte-color-swatch {
        width: 100%;
        aspect-ratio: 1;
        border: 1px solid #e0e0e0;
        border-radius: 3px;
        cursor: pointer;
        transition: all 0.15s ease;
        padding: 0;
        min-height: 20px;
      }

      .rte-color-swatch:hover {
        transform: scale(1.05);
        border-color: #ccc;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
      }

      .rte-color-swatch.selected {
        border-color: #1976d2;
        box-shadow: 0 0 0 1px rgba(25, 118, 210, 0.3);
      }

      .rte-custom-color-inputs {
        display: flex;
        gap: 8px;
      }

      .rte-color-input-native {
        width: 50px;
        height: 26px;
        border: 1px solid #ddd;
        border-radius: 4px;
        cursor: pointer;
        padding: 2px;
      }

      .rte-color-input-text {
        flex: 1;
        height: 26px;
        width: 50px;
        border: 1px solid #ddd;
        border-radius: 4px;
        padding: 0 12px;
        font-size: 13px;
        font-family: monospace;
      }

      .rte-color-input-text:focus {
        outline: none;
        border-color: #1976d2;
      }

      .rte-color-picker-footer {
        padding: 12px 16px;
        border-top: 1px solid #eee;
        display: flex;
        gap: 8px;
        justify-content: flex-end;
      }

      .rte-btn-primary,
      .rte-btn-secondary {
        padding: 6px 16px;
        border-radius: 4px;
        font-size: 13px;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.2s;
        border: none;
      }

      .rte-btn-primary {
        background-color: #1976d2;
        color: white;
      }

      .rte-btn-primary:hover {
        background-color: #1565c0;
      }

      .rte-btn-secondary {
        background-color: #f5f5f5;
        color: #333;
        border: 1px solid #ddd;
      }

      .rte-btn-secondary:hover {
        background-color: #eeeeee;
      }
    `, document.head.appendChild(e);
  }
}
document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", m) : setTimeout(m, 100);
const R = () => ({
  name: "textColor",
  marks: {
    textColor: {
      attrs: {
        color: { default: "#000000" }
      },
      parseDOM: [
        {
          tag: "span[style*=color]",
          getAttrs: (e) => {
            const t = (e.getAttribute("style") || "").match(/color:\s*([^;]+)/);
            return t ? { color: t[1] } : null;
          }
        },
        {
          tag: "font[color]",
          getAttrs: (e) => {
            const o = e.getAttribute("color");
            return o ? { color: o } : null;
          }
        }
      ],
      toDOM: (e) => {
        var o;
        return [
          "span",
          {
            style: `color: ${((o = e.attrs) == null ? void 0 : o.color) || "#000000"}`,
            class: "rte-text-color"
          },
          0
        ];
      }
    }
  },
  toolbar: [
    {
      label: "Text Color",
      command: "openTextColorPicker",
      icon: '<svg width="24" height="24" focusable="false"><g fill-rule="evenodd"><path class="tox-icon-text-color__color" d="M3 18h18v3H3z" fill="currentColor"></path><path d="M8.7 16h-.8a.5.5 0 0 1-.5-.6l2.7-9c.1-.3.3-.4.5-.4h2.8c.2 0 .4.1.5.4l2.7 9a.5.5 0 0 1-.5.6h-.8a.5.5 0 0 1-.4-.4l-.7-2.2c0-.3-.3-.4-.5-.4h-3.4c-.2 0-.4.1-.5.4l-.7 2.2c0 .3-.2.4-.4.4Zm2.6-7.6-.6 2a.5.5 0 0 0 .5.6h1.6a.5.5 0 0 0 .5-.6l-.6-2c0-.3-.3-.4-.5-.4h-.4c-.2 0-.4.1-.5.4Z"></path></g></svg>'
    }
  ],
  commands: {
    openTextColorPicker: () => S(),
    setTextColor: (e) => e ? u(e) : !1
  },
  keymap: {}
});
export {
  R as TextColorPlugin
};
//# sourceMappingURL=TextColorPlugin.native-D6SmTglm.mjs.map
