import { f as D, g as S } from "./editorContainerHelpers-C7kdWnS0.mjs";
const b = /* @__PURE__ */ new Set(), y = '[data-theme="dark"], .dark, .editora-theme-dark';
function z(t, l) {
  if (l === t.innerHTML) return;
  const i = window.execEditorCommand || window.executeEditorCommand;
  if (typeof i == "function")
    try {
      i("recordDomTransaction", t, l, t.innerHTML);
    } catch (r) {
    }
}
function R() {
  if (typeof window == "undefined" || window.__anchorObserverInitialized) return;
  window.__anchorObserverInitialized = !0, new MutationObserver((l) => {
    l.forEach((i) => {
      i.removedNodes.forEach((r) => {
        var n, e;
        if (r.nodeType === Node.ELEMENT_NODE) {
          const a = r;
          if ((n = a.classList) != null && n.contains("rte-anchor")) {
            const c = a.id;
            c && b.delete(c);
          }
          const f = (e = a.querySelectorAll) == null ? void 0 : e.call(a, ".rte-anchor");
          f == null || f.forEach((c) => {
            const u = c.id;
            u && b.delete(u);
          });
        }
      });
    });
  }).observe(document.body, {
    childList: !0,
    subtree: !0
  });
}
function A(t) {
  return !t || t.trim().length === 0 ? { valid: !1, error: "Anchor ID cannot be empty" } : t.length > 256 ? { valid: !1, error: "Anchor ID must be less than 256 characters" } : /^[a-z_]/.test(t) ? /^[a-z0-9\-_]+$/.test(t) ? { valid: !0, error: "" } : { valid: !1, error: "Anchor ID can only contain letters, numbers, hyphens, and underscores" } : { valid: !1, error: "Anchor ID must start with a letter or underscore" };
}
function M() {
  const t = D();
  if (!t) return;
  const l = S(t);
  if (!l) return;
  const i = l.querySelectorAll(".rte-anchor"), r = /* @__PURE__ */ new Set();
  i.forEach((n) => {
    const e = n.id;
    e && r.add(e);
  }), b.clear(), r.forEach((n) => b.add(n));
}
function H(t) {
  if (t) {
    const r = t.startContainer, n = r.nodeType === Node.ELEMENT_NODE ? r : r.parentElement;
    if (n != null && n.closest(y)) return !0;
  }
  const l = window.getSelection();
  if (l && l.rangeCount > 0) {
    const r = l.getRangeAt(0).startContainer, n = r.nodeType === Node.ELEMENT_NODE ? r : r.parentElement;
    if (n != null && n.closest(y)) return !0;
  }
  const i = document.activeElement;
  return i != null && i.closest(y) ? !0 : document.body.matches(y) || document.documentElement.matches(y);
}
function _(t, l, i, r) {
  M();
  const n = H(r), e = n ? {
    overlay: "rgba(0, 0, 0, 0.62)",
    dialogBg: "#1f2937",
    panelBg: "#222d3a",
    border: "#3b4657",
    text: "#e2e8f0",
    muted: "#94a3b8",
    closeHoverBg: "#334155",
    fieldBg: "#111827",
    fieldFocusBg: "#111827",
    fieldBorder: "#4b5563",
    fieldText: "#e2e8f0",
    fieldErrorBg: "#3f2124",
    fieldErrorBorder: "#ef4444",
    cancelBg: "#334155",
    cancelHover: "#475569",
    cancelText: "#e2e8f0",
    saveBg: "#3b82f6",
    saveHover: "#2563eb",
    saveDisabledBg: "#374151",
    saveDisabledText: "#7f8ca1",
    help: "#9fb0c6",
    focusRing: "rgba(88, 166, 255, 0.25)",
    errorRing: "rgba(239, 68, 68, 0.25)"
  } : {
    overlay: "rgba(0, 0, 0, 0.5)",
    dialogBg: "#ffffff",
    panelBg: "#f9f9f9",
    border: "#e0e0e0",
    text: "#333333",
    muted: "#999999",
    closeHoverBg: "#e0e0e0",
    fieldBg: "#ffffff",
    fieldFocusBg: "#f9f9ff",
    fieldBorder: "#d0d0d0",
    fieldText: "#333333",
    fieldErrorBg: "#ffebee",
    fieldErrorBorder: "#d32f2f",
    cancelBg: "#f0f0f0",
    cancelHover: "#e0e0e0",
    cancelText: "#333333",
    saveBg: "#0066cc",
    saveHover: "#0052a3",
    saveDisabledBg: "#d0d0d0",
    saveDisabledText: "#999999",
    help: "#999999",
    focusRing: "rgba(0, 102, 204, 0.1)",
    errorRing: "rgba(211, 47, 47, 0.1)"
  }, a = document.createElement("div");
  a.className = "rte-anchor-dialog-overlay", a.style.cssText = `
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: ${e.overlay};
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
  `;
  const f = document.createElement("div");
  if (f.className = "rte-anchor-dialog", f.style.cssText = `
    background: ${e.dialogBg};
    border: 1px solid ${e.border};
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    width: 90%;
    max-width: 450px;
    overflow: hidden;
    animation: rte-anchor-dialog-appear 0.2s ease;
  `, !document.getElementById("rte-anchor-dialog-styles")) {
    const d = document.createElement("style");
    d.id = "rte-anchor-dialog-styles", d.textContent = `
      @keyframes rte-anchor-dialog-appear {
        from { opacity: 0; transform: scale(0.95); }
        to { opacity: 1; transform: scale(1); }
      }
      .rte-anchor-dialog input:focus {
        outline: none !important;
      }
    `, document.head.appendChild(d);
  }
  let c = "";
  const u = document.createElement("div");
  u.style.cssText = `
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid ${e.border};
    background: ${e.panelBg};
  `;
  const m = document.createElement("h3");
  m.style.cssText = `margin: 0; font-size: 18px; font-weight: 600; color: ${e.text};`, m.textContent = "Add Anchor";
  const p = document.createElement("button");
  p.textContent = "✕", p.style.cssText = `
    background: none;
    border: none;
    font-size: 24px;
    color: ${e.muted};
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s ease;
  `, p.onmouseover = () => {
    p.style.background = e.closeHoverBg, p.style.color = "#f8fafc";
  }, p.onmouseout = () => {
    p.style.background = "none", p.style.color = e.muted;
  }, u.appendChild(m), u.appendChild(p);
  const C = document.createElement("div");
  C.style.cssText = "padding: 20px;";
  const x = document.createElement("div");
  x.style.cssText = "margin-bottom: 0;";
  const v = document.createElement("label");
  v.textContent = "Anchor ID", v.style.cssText = `display: block; font-size: 14px; font-weight: 500; color: ${e.text}; margin-bottom: 8px;`, v.setAttribute("for", "anchor-id-input");
  const o = document.createElement("input");
  o.id = "anchor-id-input", o.type = "text", o.placeholder = "e.g., section-introduction", o.value = "", o.style.cssText = `
    width: 100%;
    padding: 10px 12px;
    font-size: 14px;
    border: 1px solid ${e.fieldBorder};
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    color: ${e.fieldText};
    background: ${e.fieldBg};
    transition: all 0.2s ease;
    box-sizing: border-box;
  `, o.style.setProperty("caret-color", e.fieldText);
  const g = document.createElement("div");
  g.style.cssText = `
    color: #d32f2f;
    font-size: 12px;
    margin-top: 6px;
    display: none;
  `;
  const B = document.createElement("div");
  B.textContent = "URL-safe ID (letters, numbers, hyphens, underscores). Must start with letter or underscore.", B.style.cssText = `color: ${e.help}; font-size: 12px; margin-top: 8px; line-height: 1.4;`, x.appendChild(v), x.appendChild(o), x.appendChild(g), x.appendChild(B), C.appendChild(x);
  const w = document.createElement("div");
  w.style.cssText = `
    display: flex;
    gap: 12px;
    padding: 16px 20px;
    border-top: 1px solid ${e.border};
    background: ${e.panelBg};
    justify-content: flex-end;
  `;
  const h = document.createElement("button");
  h.textContent = "Cancel", h.style.cssText = `
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: ${e.cancelBg};
    color: ${e.cancelText};
  `, h.onmouseover = () => h.style.background = e.cancelHover, h.onmouseout = () => h.style.background = e.cancelBg;
  const s = document.createElement("button");
  s.textContent = "Add Anchor", s.style.cssText = `
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: ${e.saveBg};
    color: white;
  `, s.disabled = !o.value.trim();
  const $ = () => {
    o.value.trim() ? (s.disabled = !1, s.style.background = e.saveBg, s.style.color = "white", s.style.cursor = "pointer") : (s.disabled = !0, s.style.background = e.saveDisabledBg, s.style.color = e.saveDisabledText, s.style.cursor = "not-allowed");
  };
  s.onmouseover = () => {
    s.disabled || (s.style.background = e.saveHover, s.style.boxShadow = n ? "0 2px 8px rgba(59, 130, 246, 0.35)" : "0 2px 8px rgba(0, 102, 204, 0.3)");
  }, s.onmouseout = () => {
    s.disabled || (s.style.background = e.saveBg, s.style.boxShadow = "none");
  }, w.appendChild(h), w.appendChild(s), o.oninput = () => {
    const d = o.value;
    if ($(), d.trim()) {
      const k = A(d);
      k.valid ? b.has(d) ? (c = `Anchor ID already exists: ${d}`, g.textContent = "⚠ " + c, g.style.display = "block", o.style.borderColor = e.fieldErrorBorder, o.style.background = e.fieldErrorBg) : (c = "", g.style.display = "none", o.style.borderColor = e.fieldBorder, o.style.background = e.fieldBg) : (c = k.error, g.textContent = "⚠ " + c, g.style.display = "block", o.style.borderColor = e.fieldErrorBorder, o.style.background = e.fieldErrorBg);
    } else
      g.style.display = "none", o.style.borderColor = e.fieldBorder, o.style.background = e.fieldBg;
  }, o.onfocus = () => {
    o.style.borderColor = c ? e.fieldErrorBorder : e.saveBg, o.style.boxShadow = c ? `0 0 0 3px ${e.errorRing}` : `0 0 0 3px ${e.focusRing}`, o.style.background = c ? e.fieldErrorBg : e.fieldFocusBg;
  }, o.onblur = () => {
    o.style.boxShadow = "none", c || (o.style.background = e.fieldBg);
  };
  const T = () => {
    const d = o.value.trim();
    !d || !A(d).valid || b.has(d) || (i && i(d), a.remove());
  }, E = () => {
    a.remove();
  };
  s.onclick = T, h.onclick = E, p.onclick = E, o.onkeydown = (d) => {
    d.key === "Enter" ? (d.preventDefault(), T()) : d.key === "Escape" && (d.preventDefault(), E());
  }, a.onclick = (d) => {
    d.target === a && E();
  }, f.appendChild(u), f.appendChild(C), f.appendChild(w), a.appendChild(f), document.body.appendChild(a), setTimeout(() => o.focus(), 100);
}
function I(t, l) {
  var c;
  let i;
  if (l)
    i = l;
  else {
    const u = window.getSelection();
    if (!u || u.rangeCount === 0) return;
    i = u.getRangeAt(0);
  }
  let r = null, n = i.startContainer;
  for (; n && n !== document.body; ) {
    if (n.nodeType === Node.ELEMENT_NODE) {
      const u = n;
      if (u.getAttribute("contenteditable") === "true") {
        r = u;
        break;
      }
    }
    n = n.parentNode;
  }
  const e = (c = r == null ? void 0 : r.innerHTML) != null ? c : "", a = document.createElement("span");
  a.id = t, a.className = "rte-anchor", a.setAttribute("data-type", "anchor"), a.setAttribute("data-anchor-id", t), a.setAttribute("title", `Anchor: ${t}`), a.style.cssText = `
    display: inline;
    position: relative;
    cursor: pointer;
  `, i.insertNode(a), b.add(t), i.setStart(a.nextSibling || a.parentNode, 0), i.collapse(!0);
  const f = window.getSelection();
  if (f && (f.removeAllRanges(), f.addRange(i)), r)
    z(r, e), r.dispatchEvent(new Event("input", { bubbles: !0 }));
  else {
    const u = D();
    if (u) {
      const m = S(u);
      m && m.dispatchEvent(new Event("input", { bubbles: !0 }));
    }
  }
  N();
}
function N() {
  if (document.getElementById("rte-anchor-styles")) return;
  const t = document.createElement("style");
  t.id = "rte-anchor-styles", t.textContent = `
    .rte-anchor {
      display: inline;
      position: relative;
      cursor: pointer;
      transition: all 0.2s ease;
    }
    
    .rte-anchor:hover::before {
      content: '⚓';
      position: absolute;
      top: -1.2em;
      left: 0;
      background: #333;
      color: #fff;
      padding: 2px 6px;
      border-radius: 3px;
      font-size: 0.8em;
      white-space: nowrap;
      z-index: 100;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }
    
    .rte-anchor:hover::after {
      content: attr(data-anchor-id);
      position: absolute;
      top: -1.2em;
      left: 1.4em;
      background: #333;
      color: #fff;
      padding: 2px 6px;
      border-radius: 3px;
      font-size: 0.75em;
      font-family: 'Courier New', monospace;
      white-space: nowrap;
      z-index: 100;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }
    
    [contenteditable='true'] .rte-anchor::before {
      content: '';
      position: absolute;
      width: 8px;
      height: 8px;
      background: #0066cc;
      border-radius: 50%;
      top: -3px;
      left: 0;
      opacity: 0.5;
      transition: opacity 0.2s ease;
    }
    
    [contenteditable='true'] .rte-anchor:hover::before {
      opacity: 1;
      width: auto;
      height: auto;
      background: #333;
      border-radius: 3px;
      top: -1.2em;
      padding: 2px 6px;
      font-size: 0.8em;
      content: '⚓';
    }
    
    @media print {
      .rte-anchor::before,
      .rte-anchor::after {
        display: none;
      }
      .rte-anchor {
        cursor: auto;
      }
    }
    
    .rte-anchor:focus {
      outline: 2px solid #0066cc;
      outline-offset: 2px;
    }
  `, document.head.appendChild(t);
}
const O = () => (typeof window != "undefined" && (R(), N()), {
  name: "anchor",
  toolbar: [
    {
      label: "Anchor",
      command: "insertAnchor",
      icon: '<svg width="24px" height="24px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M12 8.4C13.4912 8.4 14.7 7.19117 14.7 5.7C14.7 4.20883 13.4912 3 12 3C10.5088 3 9.3 4.20883 9.3 5.7C9.3 7.19117 10.5088 8.4 12 8.4ZM12 8.4V20.9999M12 20.9999C9.61305 20.9999 7.32387 20.0518 5.63604 18.364C3.94821 16.6761 3 14.3869 3 12H5M12 20.9999C14.3869 20.9999 16.6761 20.0518 18.364 18.364C20.0518 16.6761 21 14.3869 21 12H19" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>',
      shortcut: "Mod-Shift-k"
    }
  ],
  commands: {
    insertAnchor: () => {
      try {
        const t = window.getSelection();
        if (!t || t.rangeCount === 0)
          return alert("Please place your cursor where you want to insert the anchor."), !1;
        const l = t.getRangeAt(0).cloneRange();
        return _("add", "", (i) => {
          I(i, l);
        }, l), !0;
      } catch (t) {
        return console.error("Failed to insert anchor:", t), !1;
      }
    }
  },
  keymap: {
    "Mod-Shift-k": "insertAnchor"
  }
});
export {
  O as AnchorPlugin
};
//# sourceMappingURL=AnchorPlugin.native-BcYGEtd9.mjs.map
