UNPKG

@uva-glass/component-library

Version:

React components UvA

249 lines (248 loc) 8.89 kB
import { jsx as T, jsxs as J, Fragment as Q } from "react/jsx-runtime"; import { useMemo as B, useState as x, useRef as E, useLayoutEffect as Z, useEffect as H, useCallback as b } from "react"; import { o as F } from "./LexicalComposerContext.prod-DOyoZx0P.js"; import { d as ee, T as te, A as k, I as $, U as ne, z as oe, m as re, W as le, a as se } from "./LexicalTable.prod-BBI7yaOy.js"; import { w as ie, R as K } from "./LexicalUtils.prod-D8DAKg7O.js"; import { j as L, g as ce } from "./Lexical.prod-BAE-wnll.js"; import { createPortal as ue } from "react-dom"; import { tableHasContent as ae, getEditorParrentCellWidth as de, getEditorContentWidth as fe } from "./components/RteEditor/Plugins/TablePlugin/helpers.js"; import { TableWrapperNode as he, $createTableWrapperNode as ge } from "./components/RteEditor/nodes/TableWrapperNode/TableWrapperNode.js"; import './assets/TableCellResizerPlugin.css';const we = typeof window < "u" && window.document !== void 0 && window.document.createElement !== void 0 ? Z : H; function me(l) { return { initialValueFn: () => l.isEditable(), subscribe: (d) => l.registerEditableListener(d) }; } function pe() { return (function(l) { const [d] = F(), m = B(() => l(d), [d, l]), [p, g] = x(() => m.initialValueFn()), w = E(p); return we(() => { const { initialValueFn: R, subscribe: c } = m, C = R(); return w.current !== C && (w.current = C, g(C)), c((z) => { w.current = z, g(z); }); }, [m, l]), p; })(me); } const S = { "table-cell-resizer__resizer": "_table-cell-resizer__resizer_2hhfo_17" }, Ce = 33, y = 32, A = 2; function be({ editor: l }) { const d = E(null), m = E(null), p = E(null), g = E(null), [w, R] = x(null), [c, C] = x(null), [z, M] = x(!1), [a, I] = x(null), v = b(() => { C(null), d.current = null, I(null), g.current = null, p.current = null; }, []), U = (e) => (e.buttons & 1) === 1; H(() => l.registerNodeTransform(ee, (e) => { if (!e.getColWidths()) { const t = e.getChildren(), n = e.getColumnCount(), i = new Array(n).fill(void 0); t[0].getChildren().forEach((o, u) => { const f = l.getElementByKey(o.getKey()); if (f && f.style.width) i[u] = parseInt(f.style.width, 10), f.style.width = ""; else if (ae(e)) { const h = e ? ie(e, te) : null, W = (h ? de(h, n, l) : fe(l)) / n; i[u] = Math.max(W, y); } else i[u] = y; }), e.setColWidths(i); } if (e && !(e.getParent() instanceof he)) { const t = ge(); e.replace(t), t.append(e); } return e; }), [l]), H(() => { const e = (s) => { const r = s.target; if (ce(r)) { if (a) { R({ x: s.clientX, y: s.clientY }); return; } if (M(U(s)), !(m.current && m.current.contains(r)) && d.current !== r) { d.current = r; const o = le(r); o && c !== o ? l.getEditorState().read( () => { const u = L(o.elem); if (!u) throw new Error("TableCellResizer: Table cell node not found."); const f = $(u), h = se(f, l.getElementByKey(f.getKey())); if (!h) throw new Error("TableCellResizer: Table element not found."); d.current = r, p.current = h.getBoundingClientRect(), C(o); }, { editor: l } ) : o == null && v(); } } }, t = () => { M(!0); }, n = () => { M(!1); }, i = l.registerRootListener((s, r) => { r?.removeEventListener("mousemove", e), r?.removeEventListener("mousedown", t), r?.removeEventListener("mouseup", n), s?.addEventListener("mousemove", e), s?.addEventListener("mousedown", t), s?.addEventListener("mouseup", n); }); return () => { i(); }; }, [c, a, l, v]); const D = (e) => e === "bottom", O = b( (e) => { if (!c) throw new Error("TableCellResizer: Expected active cell."); l.update( () => { const t = L(c.elem); if (!k(t)) throw new Error("TableCellResizer: Table cell node not found."); const n = $(t), i = ne(t) + t.getRowSpan() - 1, s = n.getChildren(); if (i >= s.length || i < 0) throw new Error("Expected table cell to be inside of table row."); const r = s[i]; if (!oe(r)) throw new Error("Expected table row"); let o = r.getHeight(); if (o === void 0) { const f = r.getChildren(); o = Math.min(...f.map((h) => j(h, l) ?? 1 / 0)); } const u = Math.max(o + e, Ce); r.setHeight(u); }, { tag: "skip-scroll-into-view" } ); }, [c, l] ), j = (e, t) => t.getElementByKey(e.getKey())?.clientHeight, V = (e, t) => { for (let n = 0; n < t.length; n++) for (let i = 0; i < t[n].length; i++) if (t[n][i].cell === e) return i; return -1; }, N = b( (e) => { if (!c) throw new Error("TableCellResizer: Expected active cell."); l.update( () => { const t = L(c.elem); if (!k(t)) throw new Error("TableCellResizer: Table cell node not found."); const n = $(t), [i] = re(n, null, null), s = V(t, i); if (s === void 0) throw new Error("TableCellResizer: Table column not found."); const r = n.getColWidths(); if (!r) return; const o = r[s]; if (o === void 0) return; const u = [...r], f = Math.max(o + e, y); if (u[s] = f, n.setColWidths(u), s < n.getColumnCount() - 1) { const h = n.getColWidths(); if (!h) return; const _ = [...h], W = s + 1, G = h[W], q = Math.max(G - e, y); _[W] = q, n.setColWidths(_); } }, { tag: "skip-scroll-into-view" } ); }, [c, l] ), P = b( (e) => { const t = (n) => { if (n.preventDefault(), n.stopPropagation(), !c) throw new Error("TableCellResizer: Expected active cell."); if (g.current) { const { x: i, y: s } = g.current; if (c === null) return; const r = K(n.target); if (D(e)) { const o = (n.clientY - s) / r; O(o); } else { const o = (n.clientX - i) / r; N(o); } v(), document.removeEventListener("mouseup", t); } }; return t; }, [c, v, N, O] ), X = b( (e) => (t) => { if (t.preventDefault(), t.stopPropagation(), !c) throw new Error("TableCellResizer: Expected active cell."); g.current = { x: t.clientX, y: t.clientY }, R(g.current), I(e), document.addEventListener("mouseup", P(e)); }, [c, P] ), Y = b(() => { if (c) { const { height: e, width: t, top: n, left: i } = c.elem.getBoundingClientRect(), s = K(c.elem), r = 10, o = { bottom: { backgroundColor: "none", cursor: "ns-resize", height: `${r}px`, left: `${window.pageXOffset + i}px`, top: `${window.pageYOffset + n + e - r / A}px`, width: `${t}px`, zIndex: 5e3 }, right: { backgroundColor: "none", cursor: "ew-resize", height: `${e}px`, left: `${window.pageXOffset + i + t - r / A}px`, top: `${window.pageYOffset + n}px`, width: `${r}px`, zIndex: 5e3 } }, u = p.current; return a && w && u && (D(a) ? (o[a].left = `${window.pageXOffset + u.left}px`, o[a].top = `${window.pageYOffset + w.y / s}px`, o[a].height = "3px", o[a].width = `${u.width}px`) : (o[a].top = `${window.pageYOffset + u.top}px`, o[a].left = `${window.pageXOffset + w.x / s}px`, o[a].width = "3px", o[a].height = `${u.height}px`), o[a].backgroundColor = "#adf"), o; } return { bottom: null, left: null, right: null, top: null }; }, [c, a, w])(); return /* @__PURE__ */ T("div", { ref: m, children: c != null && !z && /* @__PURE__ */ J(Q, { children: [ /* @__PURE__ */ T( "div", { className: S["table-cell-resizer__resizer"], style: Y.right || void 0, onMouseDown: X("right") } ), /* @__PURE__ */ T( "div", { className: S["table-cell-resizer__resizer"], style: Y.bottom || void 0, onMouseDown: X("bottom") } ) ] }) }); } const $e = () => { const [l] = F(), d = pe(); return B( () => d ? ue(/* @__PURE__ */ T(be, { editor: l }), document.body) : null, [l, d] ); }; export { $e as T, pe as a }; //# sourceMappingURL=TableCellResizerPlugin-HPo4ZkHo.js.map