UNPKG

@uva-glass/component-library

Version:

React components UvA

241 lines (240 loc) 7.82 kB
import { jsx as k, jsxs as X } from "react/jsx-runtime"; import { o as Y } from "../../../../LexicalComposerContext.prod-DLlRM5wP.js"; import { c as q } from "../../../../clsx-OuTLNxxd.js"; import { p as U, k as G, N as J } from "../../../../LexicalLink.prod-DbxnRszt.js"; import { useRef as a, useState as Q, useEffect as m } from "react"; import { W as Z, a as f, j as $, ap as tt, c as N, N as et, g as ot, X as rt } from "../../../../Lexical.prod-CWyHQUEM.js"; import { A as nt } from "../../../../TablePlugin-Ba6PaBhf.js"; import { e as it } from "../../../../helpers-BUwfVZhW.js"; import { LinkContextMenu as st } from "./LinkContextMenu/LinkContextMenu.js"; import { useFlyout as ct } from "../../hooks/useFlyout.js"; import { Icon as lt } from "../../../Icon/Icon.js"; import "../../../Buttons/Button.js"; import "../../../Buttons/LinkButton.js"; import "@react-aria/button"; import "../../../Checkbox/Checkbox.js"; import "@react-aria/dialog"; import "@react-aria/focus"; import "@react-aria/overlays"; import "../../../OverlayCloseButton/OverlayCloseButton.js"; import "../../../GridRow/GridRow.js"; import "../../../IconButton/IconButton.js"; import "../../../Input/Input.js"; import "../../../InputField/InputField.js"; import "react-router"; import { setScrollableContainers as z, removeScrollableContainers as at } from "../../helpers.js"; import { s as h } from "../../../../RteEditor.module-oFMKEW6f.js"; import { useTranslation as ut } from "../../hooks/useTranslation.js"; import "../../Providers/LanguageProvider.js"; import "../../../../index-B3c5pcHk.js"; import "../../../SelectListbox/SelectListbox.js"; import "../../../SelectListbox/SelectProvider.js"; import "../../../Sortable/components/SortableItem.js"; import "../../../../SortableProvider-Ck_XnBVq.js"; const mt = 200, I = (p) => { let n = p; for (; n; ) { if (U(n)) return n; n = n.getParent(); } return null; }, O = (p) => { let n = p; for (; n; ) { if (U(n)) return { active: !0, url: n.getURL(), linktext: n.getTextContent() }; n = n.getParent(); } return !1; }, Wt = ({ parentScrollPosition: p }) => { const n = ut(), [c] = Y(), { flyout: j, showFlyout: _, hideFlyout: C, isVisible: u } = ct(), d = a(void 0), w = a(!1), y = a(""), g = a(""), [L, A] = Q({ active: !1, url: "", linktext: "" }), P = a(u), x = a(null), S = a(null), T = a(void 0), b = () => { c.update(() => { const e = f(); x.current = e; }); }, R = () => { c.update(() => { x.current && ot(x.current); }); }, H = (e, t) => { _({ position: t, children: /* @__PURE__ */ k( st, { url: e, onSave: (o) => K(o, ""), onDelete: () => M(), onClose: D, ref: T } ) }); }, D = () => { var e; (e = T.current) == null || e.clearForm(), C(), R(); }, F = () => { var t; const e = (t = S.current) == null ? void 0 : t.getBoundingClientRect(); !w.current && e && (b(), _({ position: { top: e.bottom, left: e.left }, ...V }), setTimeout(() => { var o; return (o = d.current) == null ? void 0 : o.setDefaults("", g.current); }, 0)); }, v = () => { C(), R(); }, K = (e, t) => { c.update(() => { const o = f(); if (N(o)) { const i = o.anchor.getNode(), s = I(i); if (t && t !== "") { if (s) { const l = rt(t); s.replace(l), l.select(); } if (t !== y.current && y.current === "") { const { anchor: l, focus: E } = o; o.insertText(t), l.offset -= t.length, E.offset = l.offset + t.length; } } J(it(e), { target: "_blank" }); } v(); }); }, M = () => { c.dispatchCommand(G, null), D(); }, V = { children: /* @__PURE__ */ k( nt, { isLink: L, onCloseHandler: v, onSaveHandler: (e, t) => K(e, t), ref: d } ) }; return m(() => { var e, t; u ? (w.current = !0, (t = d.current) == null || t.focusFirstField()) : ((e = d.current) == null || e.clearForm(), y.current = g.current = "", setTimeout(() => w.current = !1, mt)); }, [d, u]), m(() => { const e = (r) => { if (N(r) && r.isCollapsed()) { const i = O(r.anchor.getNode()); if (i) { const s = c.getElementByKey(r.anchor.getNode().getKey()), l = s ? { top: s.getBoundingClientRect().bottom, left: s.getBoundingClientRect().left } : { top: 0, left: 0 }; return H(i.url, l), !0; } } return !1; }, t = c.registerCommand( Z, () => { const r = f(); return g.current = (r == null ? void 0 : r.getTextContent()) ?? "", e(r), !1; }, 0 ), o = c.registerCommand( $, (r) => { const i = f(); return b(), r.preventDefault(), r.stopPropagation(), r.stopImmediatePropagation(), e(i), !1; }, 0 ); return () => { o(), t(); }; }, [c]), m(() => { const e = c.registerCommand( tt, (o) => { if (o.ctrlKey && o.shiftKey && o.key === "U") { const r = f(); if (b(), !N(r)) return !1; const i = O(r.anchor.getNode()), s = (E = "", W = "") => { F(), setTimeout(() => { var B; return (B = d.current) == null ? void 0 : B.setDefaults(E, W); }, 0); }; if (i) return o.preventDefault(), o.stopPropagation(), o.stopImmediatePropagation(), y.current = i.linktext, s(i.url, i.linktext), !0; const l = r.getTextContent(); return l !== "" ? (g.current = l, s("", g.current), !0) : (F(), !0); } return !1; }, et ), t = c.registerUpdateListener(({ editorState: o }) => { A( o.read(() => { const r = f(); if (b(), N(r)) { const i = r.anchor.getNode(), s = I(i); if (s) return { active: !0, url: s.getURL(), linktext: s.getTextContent() }; } return { active: !1, url: "", linktext: "" }; }) ); }); return () => { e(), t(); }; }, [c]), m(() => { var e; P.current = u, u || (e = T.current) == null || e.clearForm(); }, [u]), m(() => { u && v(); }, [p]), m(() => { L.active || C(); }, [L]), m(() => { const e = (o) => (z(t), o.key === "Escape" && P.current && (o.preventDefault(), o.stopPropagation(), v()), !0); window.addEventListener("keydown", e, { capture: !0 }); const t = () => { C(); }; return z(t), window.addEventListener("scroll", t), window.addEventListener("resize", t), () => { window.removeEventListener("keydown", e), window.removeEventListener("scroll", t), window.removeEventListener("resize", t), at(t); }; }, []), /* @__PURE__ */ k("div", { className: h["editor__toolbar-button-group"], children: /* @__PURE__ */ X("div", { className: h["editor__toolbar-button-flyoutwrapper"], children: [ /* @__PURE__ */ k( "button", { onClick: F, title: n.links.linkTooltip, className: q( h["editor__toolbar-button"], h["editor__toolbar-button--link"], L.active && h["editor__toolbar-button--active"] ), "aria-label": n.aria.labels.link, "aria-keyshortcuts": n.aria.keyshortcuts.link, type: "button", ref: S, children: /* @__PURE__ */ k(lt, { name: "Link", size: 20 }) } ), j ] }) }); }; export { Wt as LinkPlugin, O as getLinkNodeTag }; //# sourceMappingURL=LinkPlugin.js.map