let r = null, a = !1, i = null;
const v = (e) => {
  if (!e) return null;
  let t = e;
  for (; t; ) {
    if (t.hasAttribute("contenteditable") && t.getAttribute("contenteditable") === "true" || t.hasAttribute("data-editora-content"))
      return t;
    t = t.parentElement;
  }
  return null;
}, w = (e) => {
  if (!r) {
    console.warn("No selection range stored");
    return;
  }
  const t = r.startContainer, n = t.nodeType === Node.TEXT_NODE ? t.parentElement : t, s = v(n);
  if (s) {
    if (a && i) {
      i.href = e.url, i.textContent = e.text, i.target = e.target, e.target === "_blank" ? i.setAttribute("rel", "noopener noreferrer") : i.removeAttribute("rel"), e.title ? i.title = e.title : i.removeAttribute("title");
      const o = document.createRange();
      o.selectNodeContents(i);
      const l = window.getSelection();
      l && (l.removeAllRanges(), l.addRange(o));
    } else {
      const o = document.createElement("a");
      o.href = e.url, o.textContent = e.text, o.target = e.target, e.target === "_blank" && o.setAttribute("rel", "noopener noreferrer"), e.title && (o.title = e.title), r.deleteContents(), r.insertNode(o), r.setStartAfter(o), r.setEndAfter(o);
      const l = window.getSelection();
      l && (l.removeAllRanges(), l.addRange(r));
    }
    s.focus(), r = null, a = !1, i = null;
  }
}, g = (e) => {
  const t = document.createElement("div");
  t.className = "link-dialog-overlay", t.style.cssText = `
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
  `;
  const n = document.createElement("div");
  n.className = "link-dialog", n.style.cssText = `
    background: white;
    border-radius: 8px;
    width: 500px;
    max-width: 90%;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  `, n.innerHTML = `
    <div class="link-dialog-header" style="padding: 16px 20px; border-bottom: 1px solid #ddd; display: flex; justify-content: space-between; align-items: center;">
      <h3 style="margin: 0; font-size: 18px;">${e.isEditing ? "Edit Link" : "Insert Link"}</h3>
      <button class="link-dialog-close" style="background: none; border: none; font-size: 24px; cursor: pointer; padding: 0; width: 30px; height: 30px;">×</button>
    </div>
    <form id="link-form">
      <div class="link-dialog-body" style="padding: 20px;">
        <div class="form-group" style="margin-bottom: 16px;">
          <label for="link-text" style="display: block; margin-bottom: 6px; font-weight: 500;">Link Text:</label>
          <input
            id="link-text"
            type="text"
            value="${e.text || ""}"
            placeholder="Enter link text"
            style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; font-size: 14px;"
          />
        </div>
        <div class="form-group" style="margin-bottom: 16px;">
          <label for="link-url" style="display: block; margin-bottom: 6px; font-weight: 500;">URL:</label>
          <input
            id="link-url"
            type="url"
            value="${e.url || ""}"
            placeholder="https://example.com"
            required
            style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; font-size: 14px;"
          />
        </div>
        <div class="form-group" style="margin-bottom: 16px;">
          <label for="link-title" style="display: block; margin-bottom: 6px; font-weight: 500;">Title (optional):</label>
          <input
            id="link-title"
            type="text"
            value="${e.title || ""}"
            placeholder="Link tooltip text"
            style="width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; font-size: 14px;"
          />
        </div>
        <div class="form-group" style="margin-bottom: 0;">
          <label style="display: flex; align-items: center; cursor: pointer;">
            <input
              id="link-target"
              type="checkbox"
              ${e.target === "_blank" ? "checked" : ""}
              style="margin-right: 8px;"
            />
            Open in new window/tab
          </label>
        </div>
      </div>
      <div class="link-dialog-footer" style="padding: 12px 20px; border-top: 1px solid #ddd; display: flex; justify-content: flex-end; gap: 10px;">
        <button type="button" class="btn-cancel" style="padding: 8px 16px; border: 1px solid #ccc; background: white; border-radius: 4px; cursor: pointer;">Cancel</button>
        <button type="submit" class="btn-submit" style="padding: 8px 16px; border: none; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
          ${e.isEditing ? "Update Link" : "Insert Link"}
        </button>
      </div>
    </form>
  `, t.appendChild(n), document.body.appendChild(t);
  const s = n.querySelector("#link-form"), o = n.querySelector("#link-text"), l = n.querySelector("#link-url"), x = n.querySelector("#link-title"), k = n.querySelector("#link-target"), y = n.querySelector(".link-dialog-close"), h = n.querySelector(".btn-cancel"), d = () => {
    document.body.removeChild(t);
  };
  y.addEventListener("click", d), h.addEventListener("click", d), t.addEventListener("click", (c) => {
    c.target === t && d();
  }), s.addEventListener("submit", (c) => {
    c.preventDefault();
    const u = l.value.trim();
    u && (w({
      text: o.value.trim() || u,
      url: u,
      target: k.checked ? "_blank" : "_self",
      title: x.value.trim() || void 0
    }), d());
  }), setTimeout(() => o.focus(), 100);
}, b = () => {
  const e = window.getSelection();
  if (!e || e.rangeCount === 0) return !1;
  const t = e.getRangeAt(0).cloneRange();
  r = t;
  const n = e.toString() || "", s = t.startContainer, o = s.nodeType === Node.TEXT_NODE ? s.parentElement : s, l = o == null ? void 0 : o.closest("a");
  return l ? (a = !0, i = l, g({
    text: l.textContent || "",
    url: l.href,
    target: l.target || "_self",
    title: l.title || "",
    isEditing: !0
  })) : (a = !1, i = null, g({
    text: n,
    url: "",
    target: "_self",
    isEditing: !1
  })), !0;
}, m = () => (document.execCommand("unlink", !1), !0), p = (e, t) => {
  var n;
  typeof window != "undefined" && ((n = window.registerEditorCommand) == null || n.call(window, e, t));
}, f = () => {
  p("openLinkDialog", b), p("removeLink", m), p("createLink", (e) => {
    e && document.execCommand("createLink", !1, e);
  });
};
typeof window != "undefined" && (document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", f) : f());
const L = () => ({
  name: "link",
  marks: {
    link: {
      attrs: {
        href: {},
        title: { default: null },
        target: { default: null }
      },
      parseDOM: [
        {
          tag: "a[href]",
          getAttrs: (e) => ({
            href: e.getAttribute("href"),
            title: e.getAttribute("title"),
            target: e.getAttribute("target")
          })
        }
      ],
      toDOM: (e) => [
        "a",
        {
          href: e.attrs.href,
          title: e.attrs.title,
          target: e.attrs.target,
          rel: e.attrs.target === "_blank" ? "noopener noreferrer" : null
        },
        0
      ]
    }
  },
  toolbar: [
    {
      label: "Link",
      command: "openLinkDialog",
      type: "button",
      icon: '<svg width="24" height="24" focusable="false"><path d="M6.2 12.3a1 1 0 0 1 1.4 1.4l-2 2a2 2 0 1 0 2.6 2.8l4.8-4.8a1 1 0 0 0 0-1.4 1 1 0 1 1 1.4-1.3 2.9 2.9 0 0 1 0 4L9.6 20a3.9 3.9 0 0 1-5.5-5.5l2-2Zm11.6-.6a1 1 0 0 1-1.4-1.4l2-2a2 2 0 1 0-2.6-2.8L11 10.3a1 1 0 0 0 0 1.4A1 1 0 1 1 9.6 13a2.9 2.9 0 0 1 0-4L14.4 4a3.9 3.9 0 0 1 5.5 5.5l-2 2Z" fill-rule="nonzero"></path></svg>',
      shortcut: "Mod-k"
    }
  ],
  commands: {
    openLinkDialog: b,
    removeLink: m
  },
  keymap: {
    "Mod-k": "openLinkDialog"
  }
});
export {
  L as LinkPlugin,
  b as openLinkDialog,
  m as removeLink
};
//# sourceMappingURL=LinkPlugin.native-BdAOV-iu.mjs.map
