@mache-hangar/lunar-editor-react 0.0.0-stage → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +128 -2
  3. package/THIRD_PARTY_NOTICES.md +58 -0
  4. package/dist/code-editor-Cq33u9jq.cjs +2 -0
  5. package/dist/code-editor-Cq33u9jq.cjs.map +1 -0
  6. package/dist/code-editor-um45ekRh.js +78 -0
  7. package/dist/code-editor-um45ekRh.js.map +1 -0
  8. package/dist/code-editor.cjs +2 -0
  9. package/dist/code-editor.cjs.map +1 -0
  10. package/dist/code-editor.js +8 -0
  11. package/dist/code-editor.js.map +1 -0
  12. package/dist/extensions/fonts/KaTeX_AMS-Regular.woff2 +0 -0
  13. package/dist/extensions/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
  14. package/dist/extensions/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
  15. package/dist/extensions/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
  16. package/dist/extensions/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
  17. package/dist/extensions/fonts/KaTeX_Main-Bold.woff2 +0 -0
  18. package/dist/extensions/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
  19. package/dist/extensions/fonts/KaTeX_Main-Italic.woff2 +0 -0
  20. package/dist/extensions/fonts/KaTeX_Main-Regular.woff2 +0 -0
  21. package/dist/extensions/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
  22. package/dist/extensions/fonts/KaTeX_Math-Italic.woff2 +0 -0
  23. package/dist/extensions/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
  24. package/dist/extensions/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
  25. package/dist/extensions/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
  26. package/dist/extensions/fonts/KaTeX_Script-Regular.woff2 +0 -0
  27. package/dist/extensions/fonts/KaTeX_Size1-Regular.woff2 +0 -0
  28. package/dist/extensions/fonts/KaTeX_Size2-Regular.woff2 +0 -0
  29. package/dist/extensions/fonts/KaTeX_Size3-Regular.woff2 +0 -0
  30. package/dist/extensions/fonts/KaTeX_Size4-Regular.woff2 +0 -0
  31. package/dist/extensions/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
  32. package/dist/extensions/math/index.cjs +2 -0
  33. package/dist/extensions/math/index.cjs.map +1 -0
  34. package/dist/extensions/math/index.js +285 -0
  35. package/dist/extensions/math/index.js.map +1 -0
  36. package/dist/extensions/math.css +1 -0
  37. package/dist/extensions/table/index.cjs +2 -0
  38. package/dist/extensions/table/index.cjs.map +1 -0
  39. package/dist/extensions/table/index.js +838 -0
  40. package/dist/extensions/table/index.js.map +1 -0
  41. package/dist/extensions/table-ui/index.cjs +2 -0
  42. package/dist/extensions/table-ui/index.cjs.map +1 -0
  43. package/dist/extensions/table-ui/index.js +531 -0
  44. package/dist/extensions/table-ui/index.js.map +1 -0
  45. package/dist/extensions/table-ui.css +1 -0
  46. package/dist/extensions/table.css +1 -0
  47. package/dist/index.cjs +2 -0
  48. package/dist/index.cjs.map +1 -0
  49. package/dist/index.js +10 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/rich-editor.cjs +2 -0
  52. package/dist/rich-editor.cjs.map +1 -0
  53. package/dist/rich-editor.js +67 -0
  54. package/dist/rich-editor.js.map +1 -0
  55. package/dist/styles.css +1 -0
  56. package/dist/types/code-editor-types.d.ts +5 -0
  57. package/dist/types/code-editor.d.ts +6 -0
  58. package/dist/types/controller-hooks.d.ts +6 -0
  59. package/dist/types/extensions/math/index.d.ts +3 -0
  60. package/dist/types/extensions/math/math-input.d.ts +4 -0
  61. package/dist/types/extensions/math/math-node-view.d.ts +27 -0
  62. package/dist/types/extensions/math/nodes.d.ts +9 -0
  63. package/dist/types/extensions/table/cell-drag.d.ts +14 -0
  64. package/dist/types/extensions/table/column-layout.d.ts +9 -0
  65. package/dist/types/extensions/table/commands.d.ts +67 -0
  66. package/dist/types/extensions/table/grid.d.ts +49 -0
  67. package/dist/types/extensions/table/index.d.ts +6 -0
  68. package/dist/types/extensions/table/interaction.d.ts +6 -0
  69. package/dist/types/extensions/table/resize-transaction.d.ts +4 -0
  70. package/dist/types/extensions/table/selection-state.d.ts +3 -0
  71. package/dist/types/extensions/table/table-node-view.d.ts +30 -0
  72. package/dist/types/extensions/table-ui/TableProperties.d.ts +9 -0
  73. package/dist/types/extensions/table-ui/TableTools.d.ts +14 -0
  74. package/dist/types/extensions/table-ui/index.d.ts +2 -0
  75. package/dist/types/extensions/table-ui/useTableOverlay.d.ts +24 -0
  76. package/dist/types/index.d.ts +5 -0
  77. package/dist/types/rich-editor.d.ts +25 -0
  78. package/package.json +95 -3
@@ -0,0 +1,838 @@
1
+ import { Fragment as E, closeHistory as k, PluginKey as Y, TextSelection as C, NodeSelection as Q, Plugin as Z, DecorationSet as tt, Decoration as et } from "@mache-hangar/lunar-editor-core/pm";
2
+ import { compileLunarSourceTransaction as ot } from "@mache-hangar/lunar-editor-core/source/compile-transaction";
3
+ import { getLunarSourceState as z } from "@mache-hangar/lunar-editor-core/source/source-map-plugin";
4
+ import { getSourceMutationSteps as st } from "@mache-hangar/lunar-editor-core/source/source-mutation-step";
5
+ const N = 48;
6
+ function K(s) {
7
+ const t = Array.from(s.rows), e = [], o = [], n = s.ownerDocument.defaultView.getComputedStyle(s).direction === "rtl", r = n ? -1 : 1;
8
+ let l = 0;
9
+ const h = (m, g) => (o[m] ??= []).push(g), i = /* @__PURE__ */ new Map();
10
+ t.forEach((m, g) => i.set(m.parentElement, g + 1)), t.forEach((m, g) => {
11
+ let a = 0;
12
+ for (const c of Array.from(m.cells)) {
13
+ for (; e[g]?.[a]; ) a++;
14
+ const p = c.colSpan, b = i.get(m.parentElement), w = c.rowSpan || b - g;
15
+ for (let v = g; v < Math.min(g + w, b); v++)
16
+ for (let x = a; x < a + p; x++)
17
+ (e[v] ??= [])[x] = !0;
18
+ const D = c.getBoundingClientRect();
19
+ h(a, n ? D.right : D.left), h(a + p, n ? D.left : D.right), a += p, l = Math.max(l, a);
20
+ }
21
+ });
22
+ let d = 0;
23
+ for (const m of Array.from(s.children).filter(
24
+ (g) => g.tagName === "COLGROUP"
25
+ )) {
26
+ const g = Array.from(m.children);
27
+ for (const a of g.length ? g : [m]) {
28
+ const c = a.getBoundingClientRect();
29
+ c.width > 0 && (h(d, n ? c.right : c.left), h(d + a.span, n ? c.left : c.right)), d += a.span;
30
+ }
31
+ }
32
+ if (l = Math.max(l, d), l < 2 || s.getBoundingClientRect().width <= 0) return;
33
+ const u = s.getBoundingClientRect();
34
+ o[0] ??= [n ? u.right : u.left], o[l] ??= [n ? u.left : u.right];
35
+ const f = Array.from({ length: l + 1 }, (m, g) => {
36
+ const a = o[g];
37
+ return a ? a.reduce((c, p) => c + p, 0) / a.length : NaN;
38
+ });
39
+ let y = 0;
40
+ for (; y < l; ) {
41
+ let m = y + 1;
42
+ for (; m < l && !Number.isFinite(f[m]); ) m++;
43
+ for (let g = y + 1; g < m; g++)
44
+ f[g] = f[y] + (f[m] - f[y]) * (g - y) / (m - y);
45
+ y = m;
46
+ }
47
+ const A = f.slice(1).map((m, g) => Math.abs(m - f[g]));
48
+ return A.every((m) => Number.isFinite(m) && m > 0) ? { widths: A, boundaries: f, direction: r } : void 0;
49
+ }
50
+ function H(s, t, e) {
51
+ const o = [...s], n = Math.min(N, s[t]), r = Math.min(N, s[t + 1]), l = Math.max(
52
+ n - s[t],
53
+ Math.min(e, s[t + 1] - r)
54
+ );
55
+ return o[t] += l, o[t + 1] -= l, o;
56
+ }
57
+ function I(s, t, e, o = !1) {
58
+ const n = e.createElement("span").style;
59
+ return n.cssText = s || "", n.setProperty("width", `${Math.round(t * 100) / 100}px`), o && (n.setProperty("min-width", "0"), n.setProperty("max-width", "none"), n.setProperty("table-layout", "fixed")), n.cssText;
60
+ }
61
+ function X(s, t, e) {
62
+ const o = s.state.doc.nodeAt(t);
63
+ if (!s.editable || o?.type.name !== "table") return;
64
+ const n = s.dom.ownerDocument, r = [];
65
+ let l = t + 1;
66
+ o.forEach((a, c) => {
67
+ a.type.name === "caption" && (l = t + 1 + c + a.nodeSize), a.type.name === "colgroup" && r.push({ node: a, position: t + 1 + c });
68
+ });
69
+ const { col: h, colgroup: i } = s.state.schema.nodes, d = [];
70
+ let u = 0;
71
+ const f = (a, c = !1) => {
72
+ const {
73
+ span: p,
74
+ width: b,
75
+ ...w
76
+ } = a?.attrs.htmlAttrs || {};
77
+ return c || delete w.id, h.create({
78
+ sourceKey: c ? a?.attrs.sourceKey : null,
79
+ htmlAttrs: {
80
+ ...w,
81
+ style: I(w.style, e[u++], n)
82
+ }
83
+ });
84
+ };
85
+ for (const { node: a } of r) {
86
+ const c = [];
87
+ if (a.forEach((v) => {
88
+ const x = Math.min(
89
+ 1e3,
90
+ Math.max(1, parseInt(v.attrs.htmlAttrs?.span, 10) || 1)
91
+ );
92
+ for (let S = 0; S < x && u < e.length; S++)
93
+ c.push(f(v, S === 0));
94
+ }), !a.childCount) {
95
+ const v = Math.min(
96
+ 1e3,
97
+ Math.max(1, parseInt(a.attrs.htmlAttrs?.span, 10) || 1)
98
+ );
99
+ for (let x = 0; x < v && u < e.length; x++)
100
+ c.push(f());
101
+ }
102
+ const {
103
+ span: p,
104
+ width: b,
105
+ ...w
106
+ } = a.attrs.htmlAttrs || {}, D = n.createElement("span").style;
107
+ D.cssText = w.style || "", D.removeProperty("width"), D.cssText ? w.style = D.cssText : delete w.style, d.push(
108
+ i.create({ ...a.attrs, htmlAttrs: w }, c)
109
+ );
110
+ }
111
+ if (u < e.length) {
112
+ const a = [];
113
+ for (; u < e.length; ) a.push(f());
114
+ if (d.length) {
115
+ const c = d[d.length - 1];
116
+ d[d.length - 1] = c.copy(
117
+ c.content.append(E.fromArray(a))
118
+ );
119
+ } else d.push(i.create(null, a));
120
+ }
121
+ const y = r[0]?.position ?? l, A = r[r.length - 1], m = A ? A.position + A.node.nodeSize : y, g = s.state.tr.replaceWith(
122
+ y,
123
+ m,
124
+ E.fromArray(d)
125
+ );
126
+ g.setNodeMarkup(t, void 0, {
127
+ ...o.attrs,
128
+ htmlAttrs: {
129
+ ...o.attrs.htmlAttrs,
130
+ style: I(
131
+ o.attrs.htmlAttrs?.style,
132
+ e.reduce((a, c) => a + c, 0),
133
+ n,
134
+ !0
135
+ )
136
+ }
137
+ }), s.dispatch(k(g)), s.dispatch(k(s.state.tr));
138
+ }
139
+ function nt(s, t) {
140
+ Array.from(s.attributes).forEach((e) => {
141
+ e.name in t || s.removeAttribute(e.name);
142
+ }), Object.entries(t).forEach(([e, o]) => {
143
+ s.setAttribute(e, o);
144
+ });
145
+ }
146
+ class rt {
147
+ constructor(t, e, o) {
148
+ this.view = e, this.getPos = o;
149
+ const n = e.dom.ownerDocument;
150
+ this.node = t, this.dom = n.createElement("div"), this.dom.className = "lunar-table-scroll", this.contentDOM = n.createElement("table"), this.dom.appendChild(this.contentDOM), this.controls = n.createElement("div"), this.controls.className = "lunar-table-resize-controls", this.controls.contentEditable = "false", this.dom.append(this.controls), this.dom.addEventListener("pointerover", this.showControls), this.dom.addEventListener("pointerleave", this.hideControls), this.dom.addEventListener("focusin", this.showControls), this.dom.addEventListener("focusout", this.hideControls), this.dom.addEventListener("scroll", this.scheduleLayout), this.controls.addEventListener("pointerdown", this.startDrag), this.controls.addEventListener("keydown", this.onHandleKeyDown), this.update(t);
151
+ }
152
+ view;
153
+ getPos;
154
+ dom;
155
+ contentDOM;
156
+ controls;
157
+ node;
158
+ observer;
159
+ frame = 0;
160
+ previewNodes = /* @__PURE__ */ new WeakSet();
161
+ drag;
162
+ update(t) {
163
+ return t.type.name !== "table" ? !1 : (this.drag && (t !== this.node || !this.view.editable) && this.finishDrag(!1), this.node = t, this.drag || nt(this.contentDOM, t.attrs.htmlAttrs || {}), this.dom.classList.toggle(
164
+ "lunar-table-scroll--fixed",
165
+ this.contentDOM.style.tableLayout === "fixed"
166
+ ), this.controls.childElementCount && this.scheduleLayout(), !0);
167
+ }
168
+ showControls = (t) => {
169
+ if (!this.view.editable || this.drag || this.controls.childElementCount || t.target.closest?.(".lunar-table-scroll") !== this.dom) return;
170
+ this.refreshControls();
171
+ const o = this.dom.ownerDocument.defaultView?.ResizeObserver;
172
+ o && !this.observer && (this.observer = new o(this.scheduleLayout), this.observer.observe(this.contentDOM));
173
+ };
174
+ hideControls = (t) => {
175
+ if (this.drag) return;
176
+ const e = t.relatedTarget;
177
+ e && this.dom.contains(e) || t.type === "pointerleave" && this.controls.contains(this.dom.ownerDocument.activeElement) || (this.controls.replaceChildren(), this.observer?.disconnect(), this.observer = void 0);
178
+ };
179
+ scheduleLayout = () => {
180
+ this.frame || (this.frame = this.dom.ownerDocument.defaultView.requestAnimationFrame(
181
+ () => {
182
+ this.frame = 0, this.controls.childElementCount && this.refreshControls();
183
+ }
184
+ ));
185
+ };
186
+ refreshControls() {
187
+ if (!this.view.editable) {
188
+ this.controls.replaceChildren();
189
+ return;
190
+ }
191
+ const t = this.contentDOM.getBoundingClientRect(), e = this.drag;
192
+ let o;
193
+ if (e) {
194
+ const h = [t.left + e.edgeOffset];
195
+ e.widths.forEach(
196
+ (i) => h.push(
197
+ h[h.length - 1] + i * e.layout.direction
198
+ )
199
+ ), o = {
200
+ widths: e.widths,
201
+ boundaries: h,
202
+ direction: e.layout.direction
203
+ };
204
+ } else o = K(this.contentDOM);
205
+ if (!o) {
206
+ this.controls.replaceChildren();
207
+ return;
208
+ }
209
+ const n = o.widths.length - 1;
210
+ if (this.controls.childElementCount !== n) {
211
+ this.controls.replaceChildren();
212
+ for (let h = 0; h < n; h++) {
213
+ const i = this.dom.ownerDocument.createElement("div");
214
+ i.className = "lunar-table-column-handle", i.dataset.column = String(h), i.setAttribute("role", "separator"), i.setAttribute("aria-orientation", "vertical"), i.setAttribute("aria-label", `Resize column ${h + 1}`), i.title = "Drag to resize column · Arrow keys to adjust · Esc to cancel", i.tabIndex = 0, this.controls.append(i);
215
+ }
216
+ }
217
+ const r = this.controls.getBoundingClientRect(), l = Math.max(
218
+ t.top,
219
+ this.contentDOM.rows[0]?.getBoundingClientRect().top ?? t.top
220
+ );
221
+ Array.from(this.controls.children).forEach((h, i) => {
222
+ const d = h;
223
+ d.style.left = `${o.boundaries[i + 1] - r.left}px`, d.style.top = `${l - r.top}px`, d.style.height = `${t.bottom - l}px`, d.setAttribute(
224
+ "aria-valuenow",
225
+ String(Math.round(o.widths[i]))
226
+ ), d.setAttribute(
227
+ "aria-valuemin",
228
+ String(Math.min(N, Math.round(o.widths[i])))
229
+ ), d.setAttribute(
230
+ "aria-valuemax",
231
+ String(
232
+ Math.round(
233
+ o.widths[i] + o.widths[i + 1] - Math.min(N, o.widths[i + 1])
234
+ )
235
+ )
236
+ );
237
+ });
238
+ }
239
+ handleColumn(t) {
240
+ const e = t.target.closest(
241
+ ".lunar-table-column-handle"
242
+ );
243
+ return e && this.controls.contains(e) ? Number(e.dataset.column) : void 0;
244
+ }
245
+ startDrag = (t) => {
246
+ const e = this.handleColumn(t), o = K(this.contentDOM);
247
+ if (!this.view.editable || t.button !== 0 || e === void 0 || !o || e >= o.widths.length - 1 || this.drag)
248
+ return;
249
+ t.preventDefault(), t.stopPropagation();
250
+ const n = this.dom.ownerDocument, r = n.createElement("colgroup");
251
+ this.previewNodes.add(r);
252
+ const l = Array.from(this.contentDOM.children).filter((i) => i.tagName === "COLGROUP").map((i) => ({ node: i, next: i.nextSibling }));
253
+ this.drag = {
254
+ column: e,
255
+ pointerId: t.pointerId,
256
+ startX: t.clientX,
257
+ layout: o,
258
+ edgeOffset: o.boundaries[0] - this.contentDOM.getBoundingClientRect().left,
259
+ widths: o.widths,
260
+ document: this.view.state.doc,
261
+ preview: r,
262
+ originals: l,
263
+ style: this.contentDOM.getAttribute("style")
264
+ };
265
+ for (const { node: i } of l)
266
+ this.previewNodes.add(i), i.remove();
267
+ o.widths.forEach((i) => {
268
+ const d = n.createElement("col");
269
+ d.style.width = `${i}px`, this.previewNodes.add(d), r.append(d);
270
+ });
271
+ const h = this.contentDOM.caption;
272
+ this.contentDOM.insertBefore(
273
+ r,
274
+ h ? h.nextSibling : this.contentDOM.firstChild
275
+ ), this.contentDOM.style.cssText = I(
276
+ this.drag.style || "",
277
+ o.widths.reduce((i, d) => i + d, 0),
278
+ n,
279
+ !0
280
+ ), this.dom.classList.add(
281
+ "lunar-table-scroll--fixed",
282
+ "lunar-table-scroll--resizing"
283
+ ), this.controls.children[e]?.classList.add(
284
+ "lunar-table-column-handle--active"
285
+ ), n.addEventListener("pointermove", this.moveDrag, !0), n.addEventListener("pointerup", this.endDrag, !0), n.addEventListener("pointercancel", this.cancelDrag, !0), n.addEventListener("keydown", this.onDragKeyDown, !0), n.defaultView?.addEventListener("blur", this.cancelDrag), this.scheduleLayout();
286
+ };
287
+ moveDrag = (t) => {
288
+ const e = this.drag;
289
+ if (!(!e || t.pointerId !== e.pointerId)) {
290
+ if (!this.view.editable || this.view.state.doc !== e.document) {
291
+ this.finishDrag(!1);
292
+ return;
293
+ }
294
+ t.preventDefault(), e.widths = H(
295
+ e.layout.widths,
296
+ e.column,
297
+ (t.clientX - e.startX) * e.layout.direction
298
+ );
299
+ for (const o of [e.column, e.column + 1])
300
+ e.preview.children[o].style.width = `${e.widths[o]}px`;
301
+ this.scheduleLayout();
302
+ }
303
+ };
304
+ endDrag = (t) => {
305
+ t.pointerId === this.drag?.pointerId && (this.moveDrag(t), this.finishDrag(!0));
306
+ };
307
+ cancelDrag = () => this.finishDrag(!1);
308
+ onDragKeyDown = (t) => {
309
+ t.key === "Escape" && (t.preventDefault(), t.stopPropagation(), this.finishDrag(!1));
310
+ };
311
+ finishDrag(t) {
312
+ const e = this.drag;
313
+ if (!e) return;
314
+ this.drag = void 0;
315
+ const o = this.dom.ownerDocument;
316
+ o.removeEventListener("pointermove", this.moveDrag, !0), o.removeEventListener("pointerup", this.endDrag, !0), o.removeEventListener("pointercancel", this.cancelDrag, !0), o.removeEventListener("keydown", this.onDragKeyDown, !0), o.defaultView?.removeEventListener("blur", this.cancelDrag), e.preview.remove(), [...e.originals].reverse().forEach(({ node: r, next: l }) => this.contentDOM.insertBefore(r, l)), e.style === null ? this.contentDOM.removeAttribute("style") : this.contentDOM.setAttribute("style", e.style), this.dom.classList.remove("lunar-table-scroll--resizing"), this.controls.children[e.column]?.classList.remove(
317
+ "lunar-table-column-handle--active"
318
+ ), this.dom.classList.toggle(
319
+ "lunar-table-scroll--fixed",
320
+ this.contentDOM.style.tableLayout === "fixed"
321
+ );
322
+ const n = this.getPos();
323
+ t && this.view.editable && this.view.state.doc === e.document && n !== void 0 && e.widths.some(
324
+ (r, l) => Math.abs(r - e.layout.widths[l]) >= 0.5
325
+ ) && X(this.view, n, e.widths), this.scheduleLayout();
326
+ }
327
+ onHandleKeyDown = (t) => {
328
+ const e = this.handleColumn(t), o = K(this.contentDOM), n = this.getPos();
329
+ if (!this.view.editable || e === void 0 || !o || e >= o.widths.length - 1 || n === void 0 || !["ArrowLeft", "ArrowRight"].includes(t.key))
330
+ return;
331
+ t.preventDefault();
332
+ const r = (t.key === "ArrowRight" ? 10 : -10) * o.direction, l = H(o.widths, e, r);
333
+ l[e] !== o.widths[e] && X(this.view, n, l), this.scheduleLayout();
334
+ };
335
+ stopEvent(t) {
336
+ return this.controls.contains(t.target);
337
+ }
338
+ ignoreMutation(t) {
339
+ return t.type === "selection" ? !1 : !this.contentDOM.contains(t.target) || t.type === "attributes" && t.target === this.contentDOM || this.previewNodes.has(t.target) ? !0 : t.type === "childList" && t.target === this.contentDOM && [
340
+ ...Array.from(t.addedNodes),
341
+ ...Array.from(t.removedNodes)
342
+ ].every((e) => this.previewNodes.has(e));
343
+ }
344
+ destroy() {
345
+ this.finishDrag(!1), this.observer?.disconnect(), this.dom.ownerDocument.defaultView?.cancelAnimationFrame(this.frame), this.dom.removeEventListener("pointerover", this.showControls), this.dom.removeEventListener("pointerleave", this.hideControls), this.dom.removeEventListener("focusin", this.showControls), this.dom.removeEventListener("focusout", this.hideControls), this.dom.removeEventListener("scroll", this.scheduleLayout), this.controls.removeEventListener("pointerdown", this.startDrag), this.controls.removeEventListener("keydown", this.onHandleKeyDown);
346
+ }
347
+ }
348
+ const lt = (s) => ["table_cell", "table_header"].includes(s.type.name), R = (s) => Math.min(100, Math.max(1, Number.parseInt(String(s), 10) || 1));
349
+ function T(s) {
350
+ for (let t = s.depth; t > 0; t--)
351
+ if (s.node(t).type.name === "table") return { node: s.node(t), pos: s.before(t) };
352
+ }
353
+ function W(s, t) {
354
+ const e = [];
355
+ if (s.forEach((h, i, d) => {
356
+ h.type.name === "table_row" ? e.push({ node: h, section: -1, pos: t + 1 + i }) : ["table_head", "table_body", "table_foot"].includes(h.type.name) && h.forEach((u, f) => e.push({ node: u, section: d, pos: t + 2 + i + f }));
357
+ }), !e.length || e.length > 200) return;
358
+ const o = [], n = [];
359
+ let r = 0, l = !0;
360
+ if (e.forEach((h, i) => {
361
+ let d = 0;
362
+ h.node.forEach((u, f) => {
363
+ if (!lt(u)) {
364
+ l = !1;
365
+ return;
366
+ }
367
+ for (; o[i]?.[d]; ) d++;
368
+ const y = e.findIndex((c, p) => p > i && c.section !== h.section), A = y < 0 ? e.length : y;
369
+ if (Number(u.attrs.htmlAttrs?.colspan) > 100 || Number(u.attrs.htmlAttrs?.rowspan) > 100) {
370
+ l = !1;
371
+ return;
372
+ }
373
+ const m = R(u.attrs.htmlAttrs?.colspan), g = u.attrs.htmlAttrs?.rowspan === "0" ? A - i : R(u.attrs.htmlAttrs?.rowspan);
374
+ if (i + g > A || d + m > 100) {
375
+ l = !1;
376
+ return;
377
+ }
378
+ const a = { node: u, row: i, col: d, rows: g, cols: m, pos: h.pos + 1 + f };
379
+ n.push(a);
380
+ for (let c = i; c < i + g; c++)
381
+ for (let p = d; p < d + m; p++)
382
+ o[c] ??= [], o[c][p] && (l = !1), o[c][p] = a;
383
+ d += m, r = Math.max(r, d);
384
+ });
385
+ }), !(!l || !r || e.some((h, i) => Array.from({ length: r }, (d, u) => !o[i]?.[u]).some(Boolean))))
386
+ return { node: s, pos: t, rows: e, cells: n, matrix: o, width: r };
387
+ }
388
+ function L(s, t) {
389
+ return s.cells.find((e) => t >= e.pos && t < e.pos + e.node.nodeSize);
390
+ }
391
+ function it(s, t, e) {
392
+ const o = {
393
+ top: Math.min(t.row, e.row),
394
+ bottom: Math.max(t.row + t.rows, e.row + e.rows),
395
+ left: Math.min(t.col, e.col),
396
+ right: Math.max(t.col + t.cols, e.col + e.cols)
397
+ };
398
+ let n = !0;
399
+ for (; n; ) {
400
+ n = !1;
401
+ for (const r of s.cells)
402
+ if (j(r, o)) {
403
+ const l = [Math.min(o.top, r.row), Math.max(o.bottom, r.row + r.rows), Math.min(o.left, r.col), Math.max(o.right, r.col + r.cols)];
404
+ l.some((h, i) => h !== [o.top, o.bottom, o.left, o.right][i]) && ([o.top, o.bottom, o.left, o.right] = l, n = !0);
405
+ }
406
+ }
407
+ return o;
408
+ }
409
+ function j(s, t) {
410
+ return s.row < t.bottom && s.row + s.rows > t.top && s.col < t.right && s.col + s.cols > t.left;
411
+ }
412
+ function F(s, t, e) {
413
+ const o = { ...s.attrs.htmlAttrs };
414
+ for (const n of ["id", "rowspan", "colspan", "headers"]) delete o[n];
415
+ return { node: s.type.createAndFill({ ...s.attrs, sourceKey: null, htmlAttrs: o }), row: t, col: e, rows: 1, cols: 1, pos: -1 };
416
+ }
417
+ function at(s, t, e, o) {
418
+ const n = t.map((l, h) => {
419
+ const i = e.filter((u) => u.row === h).sort((u, f) => u.col - f.col).map((u) => {
420
+ const f = { ...u.node.attrs.htmlAttrs };
421
+ return u.rows > 1 ? f.rowspan = String(u.rows) : delete f.rowspan, u.cols > 1 ? f.colspan = String(u.cols) : delete f.colspan, JSON.stringify(f) === JSON.stringify(u.node.attrs.htmlAttrs || {}) ? u.node : u.node.type.create({ ...u.node.attrs, htmlAttrs: f }, u.node.content);
422
+ }), d = E.fromArray(i);
423
+ return l.node.content.eq(d) ? l.node : l.node.copy(d);
424
+ }), r = [];
425
+ return s.node.forEach((l, h, i) => {
426
+ if (l.type.name === "colgroup" && o) {
427
+ const d = o.filter((f) => f.group === i), u = { ...l.attrs.htmlAttrs };
428
+ delete u.span, d.length && r.push(
429
+ l.type.create(
430
+ { ...l.attrs, htmlAttrs: u },
431
+ d.map((f) => f.node)
432
+ )
433
+ );
434
+ } else if (["table_head", "table_body", "table_foot"].includes(l.type.name)) {
435
+ const d = n.filter((u, f) => t[f].section === i);
436
+ d.length && r.push(l.copy(E.fromArray(d)));
437
+ } else l.type.name !== "table_row" && r.push(l);
438
+ }), r.push(...n.filter((l, h) => t[h].section === -1)), s.node.copy(E.fromArray(r));
439
+ }
440
+ function q(s, t, e, o) {
441
+ const n = [];
442
+ if (s.node.forEach((r, l, h) => {
443
+ if (r.type.name !== "colgroup") return;
444
+ const i = { ...r.attrs.htmlAttrs };
445
+ if (!r.childCount)
446
+ for (let d = 0; d < R(i.span); d++) n.push({ node: r.type.schema.nodes.col.create({ htmlAttrs: {}, sourceKey: null }), group: h });
447
+ r.forEach((d) => {
448
+ for (let u = 0; u < R(d.attrs.htmlAttrs?.span); u++) {
449
+ const f = { ...d.attrs.htmlAttrs };
450
+ delete f.span, u && delete f.id, n.push({ node: d.type.create({ ...d.attrs, sourceKey: u ? null : d.attrs.sourceKey, htmlAttrs: f }), group: h });
451
+ }
452
+ });
453
+ }), !!n.length) {
454
+ for (; n.length < s.width; ) n.push({ node: s.node.type.schema.nodes.col.create(), group: n[n.length - 1].group });
455
+ return n.length = s.width, o ? n.splice(t, e) : n.splice(t, 0, { node: s.node.type.schema.nodes.col.create(), group: n[Math.min(t, n.length - 1)].group }), n;
456
+ }
457
+ }
458
+ const M = new Y("lunar-table-selection"), B = {
459
+ available: !1,
460
+ inTable: !1,
461
+ valid: !1,
462
+ inCell: !1,
463
+ rows: 0,
464
+ columns: 0,
465
+ row: 0,
466
+ column: 0,
467
+ selectedCells: 0,
468
+ canMerge: !1,
469
+ canSplit: !1,
470
+ headerRow: !1,
471
+ headerColumn: !1,
472
+ caption: !1,
473
+ textAlign: "left",
474
+ verticalAlign: "top",
475
+ backgroundColor: "#ffffff"
476
+ };
477
+ function _(s) {
478
+ const t = M.getState(s), e = s.selection instanceof Q && s.selection.node.type.name === "table" ? { node: s.selection.node, pos: s.selection.from } : void 0, o = t ? T(s.doc.resolve(t.anchor)) : e || T(s.selection.$anchor);
479
+ if (!o || !t && !e && T(s.selection.$head)?.pos !== o.pos) return;
480
+ const n = W(o.node, o.pos);
481
+ if (!n) return { table: o };
482
+ const r = L(n, t?.anchor ?? s.selection.anchor), l = r || n.cells[0], h = L(n, t?.head ?? s.selection.head) || l, i = e ? { top: 0, bottom: n.rows.length, left: 0, right: n.width } : it(n, l, h);
483
+ return { table: o, grid: n, rect: i, inCell: !!(e || r), cells: n.cells.filter((d) => j(d, i)) };
484
+ }
485
+ function O(s) {
486
+ if (!s) return B;
487
+ const t = s.editable && (!z(s.state) || z(s.state)?.status === "synchronized"), e = _(s.state);
488
+ if (!e) return { ...B, available: t };
489
+ if (!e.grid) return { ...B, available: t, inTable: !0 };
490
+ const { grid: o, rect: n, cells: r } = e, l = o.rows[n.top].section, h = s.dom.ownerDocument.createElement("span").style;
491
+ return h.cssText = r[0].node.attrs.htmlAttrs?.style || "", {
492
+ available: t,
493
+ inTable: !0,
494
+ valid: !0,
495
+ inCell: e.inCell,
496
+ rows: o.rows.length,
497
+ columns: o.width,
498
+ row: n.top + 1,
499
+ column: n.left + 1,
500
+ selectedCells: r.length,
501
+ canMerge: r.length > 1 && o.rows.slice(n.top, n.bottom).every((i) => i.section === l),
502
+ canSplit: r.some((i) => i.rows > 1 || i.cols > 1),
503
+ headerRow: o.cells.filter((i) => i.row < n.bottom && i.row + i.rows > n.top).every((i) => i.node.type.name === "table_header"),
504
+ headerColumn: o.cells.filter((i) => i.col < n.right && i.col + i.cols > n.left).every((i) => i.node.type.name === "table_header"),
505
+ caption: Array.from({ length: o.node.childCount }, (i, d) => o.node.child(d)).some((i) => i.type.name === "caption"),
506
+ textAlign: h.textAlign || "left",
507
+ verticalAlign: h.verticalAlign || "top",
508
+ backgroundColor: h.backgroundColor || "#ffffff"
509
+ };
510
+ }
511
+ function V(s, t) {
512
+ if (!t.docChanged) return !1;
513
+ const e = z(s.state), o = ot(s.state, k(t));
514
+ return e && !st(o.steps).length ? !1 : (s.dispatch(o.scrollIntoView()), s.dispatch(k(s.state.tr)), s.focus(), !0);
515
+ }
516
+ function P(s, t, e) {
517
+ const o = s.state.doc.resolve(e), n = o.parent.canReplace(o.index(), o.index() + 1) ? E.empty : E.from(s.state.schema.nodes.paragraph.createAndFill());
518
+ return V(s, s.state.tr.replaceWith(e, e + t.nodeSize, n));
519
+ }
520
+ function G(s, t, e) {
521
+ const o = e.createElement("span").style;
522
+ o.cssText = s.attrs.htmlAttrs?.style || "";
523
+ for (const [n, r] of Object.entries(t)) o.setProperty(n, r);
524
+ return s.type.create({ ...s.attrs, htmlAttrs: { ...s.attrs.htmlAttrs, style: o.cssText } }, s.content);
525
+ }
526
+ function ct(s, t, e, o) {
527
+ const n = e ? t.bottom : t.top, r = s.rows[e ? t.bottom - 1 : t.top], l = { ...r.node.attrs.htmlAttrs };
528
+ delete l.id;
529
+ const h = { node: r.node.type.create({ ...r.node.attrs, sourceKey: null, htmlAttrs: l }), section: r.section, pos: -1 };
530
+ for (const d of o)
531
+ d.row >= n ? d.row++ : d.row + d.rows > n && d.rows++;
532
+ for (let d = 0; d < s.width; d++)
533
+ o.some((u) => u.row < n && u.row + u.rows > n && d >= u.col && d < u.col + u.cols) || o.push(F(s.matrix[e ? t.bottom - 1 : t.top][d].node, n, d));
534
+ const i = [...s.rows];
535
+ return i.splice(n, 0, h), { rows: i, focusRow: n };
536
+ }
537
+ function $(s, t, e) {
538
+ if (!s || !O(s).available) return !1;
539
+ try {
540
+ if (t.action === "insert") {
541
+ const { rows: a, columns: c } = t;
542
+ if (![a, c].every((x) => Number.isInteger(x) && x >= 1 && x <= 50)) return !1;
543
+ const p = s.state.schema, b = Array.from(
544
+ { length: a },
545
+ (x, S) => p.nodes.table_row.create(
546
+ null,
547
+ Array.from(
548
+ { length: c },
549
+ () => (t.headerRow && S === 0 ? p.nodes.table_header : p.nodes.table_cell).createAndFill({
550
+ htmlAttrs: t.headerRow && S === 0 ? { scope: "col" } : {}
551
+ })
552
+ )
553
+ )
554
+ ), w = p.nodes.table.create({ htmlAttrs: { style: "width: 100%;" } }, p.nodes.table_body.create(null, b)), D = s.state.tr.replaceSelectionWith(w);
555
+ let v = -1;
556
+ return D.doc.descendants((x, S) => {
557
+ v < 0 && x === w && (v = S);
558
+ }), v >= 0 && D.setSelection(C.near(D.doc.resolve(v + 4))), V(s, D);
559
+ }
560
+ const o = _(s.state);
561
+ if (!o) return !1;
562
+ if (t.action === "delete_table") return P(s, o.table.node, o.table.pos);
563
+ if (!o.grid || !o.rect || !o.cells || !o.inCell && !["caption", "table_style"].includes(t.action)) return !1;
564
+ const { grid: n, rect: r } = o;
565
+ let l = n.cells.map((a) => ({ ...a })), h = [...n.rows], i, d = r.top, u = r.left;
566
+ const f = (a) => j(a, r);
567
+ if (t.action === "row_before" || t.action === "row_after") {
568
+ if (h.length >= 200) return !1;
569
+ ({ rows: h, focusRow: d } = ct(n, r, t.action === "row_after", l));
570
+ } else if (t.action === "delete_rows") {
571
+ if (r.bottom - r.top === h.length) return P(s, n.node, n.pos);
572
+ const a = r.bottom - r.top;
573
+ h.splice(r.top, a), l = l.filter((c) => {
574
+ const p = Math.max(0, Math.min(c.row + c.rows, r.bottom) - Math.max(c.row, r.top));
575
+ return p === c.rows ? !1 : (c.row >= r.bottom ? c.row -= a : c.row >= r.top && (c.row = r.top), c.rows -= p, !0);
576
+ });
577
+ } else if (t.action === "column_before" || t.action === "column_after") {
578
+ if (n.width >= 100) return !1;
579
+ const a = t.action === "column_after" ? r.right : r.left;
580
+ for (const c of l)
581
+ c.col >= a ? c.col++ : c.col + c.cols > a && c.cols++;
582
+ for (let c = 0; c < h.length; c++)
583
+ l.some((p) => p.col < a && p.col + p.cols > a && c >= p.row && c < p.row + p.rows) || l.push(F(n.matrix[c][Math.min(r.left, n.width - 1)].node, c, a));
584
+ i = q(n, a, 1, !1), u = a;
585
+ } else if (t.action === "delete_columns") {
586
+ if (r.right - r.left === n.width) return P(s, n.node, n.pos);
587
+ const a = r.right - r.left;
588
+ l = l.filter((c) => {
589
+ const p = Math.max(0, Math.min(c.col + c.cols, r.right) - Math.max(c.col, r.left));
590
+ return p === c.cols ? !1 : (c.col >= r.right ? c.col -= a : c.col >= r.left && (c.col = r.left), c.cols -= p, !0);
591
+ }), i = q(n, r.left, a, !0);
592
+ } else if (t.action === "merge") {
593
+ if (!O(s).canMerge) return !1;
594
+ const a = l.filter(f), c = a[0], p = a.reduce((b, w) => b.append(w.node.content), E.empty);
595
+ c.node = c.node.copy(p), c.rows = r.bottom - r.top, c.cols = r.right - r.left, l = l.filter((b) => b === c || !a.includes(b));
596
+ } else if (t.action === "split")
597
+ for (const a of [...l].filter(f)) {
598
+ const { rows: c, cols: p } = a;
599
+ for (let b = 0; b < c; b++)
600
+ for (let w = 0; w < p; w++)
601
+ (b || w) && l.push(F(a.node, a.row + b, a.col + w));
602
+ a.rows = 1, a.cols = 1;
603
+ }
604
+ else if (t.action === "header_row" || t.action === "header_column") {
605
+ const a = t.action === "header_row", c = a ? !O(s).headerRow : !O(s).headerColumn;
606
+ for (const p of l.filter((b) => a ? b.row < r.bottom && b.row + b.rows > r.top : b.col < r.right && b.col + b.cols > r.left)) {
607
+ const b = { ...p.node.attrs.htmlAttrs };
608
+ c ? b.scope = a ? "col" : "row" : delete b.scope;
609
+ const w = s.state.schema.nodes[c ? "table_header" : "table_cell"];
610
+ p.node = w.create({ ...p.node.attrs, htmlAttrs: b }, p.node.content);
611
+ }
612
+ } else if (t.action === "clear_cells") {
613
+ let a = !0;
614
+ for (const c of l.filter(f))
615
+ c.node = c.node.copy(E.from(s.state.schema.nodes.paragraph.create(null, a && e ? s.state.schema.text(e) : void 0))), a = !1;
616
+ } else if (t.action === "cell_style") {
617
+ const a = {};
618
+ if (t.textAlign && ["left", "center", "right", "justify"].includes(t.textAlign) && (a["text-align"] = t.textAlign), t.verticalAlign && ["top", "middle", "bottom"].includes(t.verticalAlign) && (a["vertical-align"] = t.verticalAlign), t.backgroundColor && /^(#[\da-f]{6}|transparent)$/i.test(t.backgroundColor) && (a["background-color"] = t.backgroundColor), !Object.keys(a).length) return !1;
619
+ for (const c of l.filter(f)) c.node = G(c.node, a, s.dom.ownerDocument);
620
+ }
621
+ let y = at(n, h, l, i);
622
+ if (t.action === "caption") {
623
+ const a = [];
624
+ y.forEach((c) => {
625
+ c.type.name !== "caption" && a.push(c);
626
+ }), O(s).caption || a.unshift(s.state.schema.nodes.caption.createAndFill(null, s.state.schema.nodes.paragraph.create(null, s.state.schema.text("Table caption")))), y = y.copy(E.fromArray(a));
627
+ } else if (t.action === "table_style") {
628
+ const a = {};
629
+ if (t.align && ["left", "center", "right"].includes(t.align) && (a["margin-left"] = t.align === "left" ? "0" : "auto", a["margin-right"] = t.align === "right" ? "0" : "auto", a.float = "none"), t.width && ["25%", "50%", "75%", "100%"].includes(t.width) && (a.width = t.width), !Object.keys(a).length) return !1;
630
+ y = G(y, a, s.dom.ownerDocument);
631
+ }
632
+ if (y.eq(n.node)) return !1;
633
+ const A = s.state.tr.replaceWith(n.pos, n.pos + n.node.nodeSize, y), m = W(y, n.pos);
634
+ if (!m) return !1;
635
+ const g = m?.matrix[Math.min(d, m.rows.length - 1)]?.[Math.min(u, m.width - 1)];
636
+ if (g && A.setSelection(C.near(A.doc.resolve(g.pos + 1))), o.cells.length > 1 && ["cell_style", "table_style", "header_row", "header_column", "caption"].includes(t.action)) {
637
+ const a = m.matrix[r.top][r.left], c = m.matrix[r.bottom - 1][r.right - 1];
638
+ A.setMeta(M, { anchor: a.pos + 1, head: c.pos + 1 });
639
+ }
640
+ return V(s, A);
641
+ } catch {
642
+ return !1;
643
+ }
644
+ }
645
+ function J(s, t) {
646
+ const e = t?.closest?.("td, th");
647
+ if (!e || !s.dom.contains(e)) return;
648
+ const o = s.posAtDOM(e, 0), n = T(s.state.doc.resolve(o)), r = n && W(n.node, n.pos), l = r && L(r, o), h = e.closest("table");
649
+ return r && l && h ? { grid: r, cell: l, tableElement: h } : void 0;
650
+ }
651
+ function ht(s, t, e) {
652
+ const o = s.state.doc.resolve(t);
653
+ if (e >= 3 && o.parent.inlineContent) return C.create(s.state.doc, o.start(), o.end());
654
+ if (e === 2 && o.parent.inlineContent) {
655
+ const n = o.parent.textBetween(0, o.parent.content.size, "", ""), r = o.parent.textBetween(0, o.parentOffset, "", "").length, l = [...new Intl.Segmenter(void 0, { granularity: "word" }).segment(n)].find((h) => r >= h.index && r < h.index + h.segment.length);
656
+ if (l) {
657
+ let h = 0, i, d;
658
+ if (o.parent.descendants((u, f) => {
659
+ if (!u.isText && !u.isLeaf) return;
660
+ const y = u.isText ? u.text.length : 1;
661
+ i === void 0 && l.index < h + y && (i = o.start() + f + Math.max(0, l.index - h));
662
+ const A = l.index + l.segment.length;
663
+ d === void 0 && A <= h + y && (d = o.start() + f + Math.max(0, A - h)), h += y;
664
+ }), i !== void 0 && d !== void 0) return C.create(s.state.doc, i, d);
665
+ }
666
+ }
667
+ return C.near(o);
668
+ }
669
+ class dt {
670
+ drag;
671
+ get active() {
672
+ return !!this.drag?.view.editable;
673
+ }
674
+ start(t, e) {
675
+ if (this.cancel(), !t.editable || e.button !== 0 || e.shiftKey || e.ctrlKey || e.metaKey || e.altKey) return !1;
676
+ const o = J(t, e.target);
677
+ if (!o) return !1;
678
+ const n = t.posAtCoords({ left: e.clientX, top: e.clientY })?.pos, r = n !== void 0 && L(o.grid, n)?.pos === o.cell.pos ? n : o.cell.pos + 1, l = ht(t, r, e.detail);
679
+ this.drag = { view: t, doc: t.state.doc, ...o, anchor: o.cell, head: o.cell, selecting: !1, textFrom: l.from, textTo: l.to }, e.preventDefault(), t.dispatch(t.state.tr.setSelection(l).setMeta(M, { anchor: o.cell.pos + 1, head: o.cell.pos + 1, dragging: !0 })), t.focus();
680
+ const h = t.dom.ownerDocument;
681
+ return h.addEventListener("mousemove", this.move, !0), h.addEventListener("mouseup", this.end, !0), h.defaultView?.addEventListener("blur", this.cancel), !0;
682
+ }
683
+ cancelIfStale(t) {
684
+ this.drag && (this.drag.doc !== t.state.doc || !t.editable) && this.destroy();
685
+ }
686
+ /** Clamp hit-testing to the current table and editor viewport, including on mouseup. */
687
+ point(t, e) {
688
+ const o = e.tableElement.getBoundingClientRect(), n = e.view.dom.getBoundingClientRect(), r = e.view.dom.ownerDocument.defaultView;
689
+ return {
690
+ left: Math.max(Math.max(o.left + 1, n.left + 1, 1), Math.min(t.clientX, o.right - 1, n.right - 1, r.innerWidth - 1)),
691
+ top: Math.max(Math.max(o.top + 1, n.top + 1, 1), Math.min(t.clientY, o.bottom - 1, n.bottom - 1, r.innerHeight - 1))
692
+ };
693
+ }
694
+ move = (t) => {
695
+ const e = this.drag;
696
+ if (!e) return;
697
+ if (!(t.buttons & 1)) {
698
+ this.end();
699
+ return;
700
+ }
701
+ if (e.doc !== e.view.state.doc || !e.view.editable) {
702
+ this.cancel();
703
+ return;
704
+ }
705
+ t.preventDefault();
706
+ const o = this.point(t, e), r = e.view.dom.ownerDocument.elementFromPoint?.(o.left, o.top) ?? t.target, l = J(e.view, r);
707
+ if (!l || l.grid.pos !== e.grid.pos) return;
708
+ const { view: h, anchor: i } = e;
709
+ l.cell.pos !== i.pos && (e.selecting = !0), e.head = l.cell;
710
+ let d;
711
+ if (e.selecting) {
712
+ const f = i.pos <= l.cell.pos;
713
+ d = C.between(
714
+ h.state.doc.resolve(i.pos + (f ? 1 : i.node.nodeSize - 1)),
715
+ h.state.doc.resolve(l.cell.pos + (f ? l.cell.node.nodeSize - 1 : 1))
716
+ );
717
+ } else {
718
+ const f = h.posAtCoords(o)?.pos;
719
+ if (f === void 0 || L(e.grid, f)?.pos !== i.pos) return;
720
+ d = C.between(h.state.doc.resolve(f < e.textFrom ? e.textTo : e.textFrom), h.state.doc.resolve(f));
721
+ }
722
+ const u = M.getState(h.state);
723
+ d.eq(h.state.selection) && u?.head === l.cell.pos + 1 || h.dispatch(h.state.tr.setSelection(d).setMeta(M, { anchor: i.pos + 1, head: l.cell.pos + 1, dragging: !0 }));
724
+ };
725
+ end = () => {
726
+ const t = this.drag;
727
+ this.destroy(), !(!t || t.doc !== t.view.state.doc || t.view.isDestroyed) && t.view.dispatch(t.view.state.tr.setMeta(M, t.selecting ? { anchor: t.anchor.pos + 1, head: t.head.pos + 1 } : null));
728
+ };
729
+ cancel = () => {
730
+ const t = this.drag;
731
+ if (this.destroy(), !t || t.doc !== t.view.state.doc || t.view.isDestroyed) return;
732
+ const e = M.getState(t.view.state);
733
+ e?.dragging && t.view.dispatch(t.view.state.tr.setMeta(M, { ...e, dragging: !1 }));
734
+ };
735
+ destroy = () => {
736
+ const t = this.drag?.view.dom.ownerDocument;
737
+ t?.removeEventListener("mousemove", this.move, !0), t?.removeEventListener("mouseup", this.end, !0), t?.defaultView?.removeEventListener("blur", this.cancel), this.drag = void 0;
738
+ };
739
+ }
740
+ function U(s, t) {
741
+ s.dispatch(s.state.tr.setSelection(C.near(s.state.doc.resolve(t + 1)))), s.focus();
742
+ }
743
+ function ut(s) {
744
+ const t = new dt();
745
+ return new Z({
746
+ key: M,
747
+ state: {
748
+ init: () => null,
749
+ apply(e, o, n) {
750
+ const r = e.getMeta(M);
751
+ return r !== void 0 ? r : e.docChanged || e.selectionSet && !t.active && !e.selection.eq(n.selection) ? null : o;
752
+ }
753
+ },
754
+ props: {
755
+ attributes(e) {
756
+ const o = [];
757
+ return (_(e)?.cells?.length || 0) > 1 && o.push("lunar-table-cell-selection"), M.getState(e)?.dragging && t.active && o.push("lunar-table-dragging"), { class: o.join(" ") };
758
+ },
759
+ decorations(e) {
760
+ const o = _(e);
761
+ return !o?.cells || o.cells.length < 2 ? null : tt.create(
762
+ e.doc,
763
+ o.cells.map((n) => et.node(n.pos, n.pos + n.node.nodeSize, { class: "lunar-table-cell--selected" }))
764
+ );
765
+ },
766
+ handleDOMEvents: {
767
+ keydown(e, o) {
768
+ t.cancel();
769
+ const n = ft(e, o);
770
+ return n && o.preventDefault(), n;
771
+ },
772
+ mousedown(e, o) {
773
+ if (t.cancel(), !e.editable || o.button !== 0) return !1;
774
+ const n = o.target?.closest?.("td, th");
775
+ if (o.shiftKey && n && e.dom.contains(n)) {
776
+ const r = _(e.state), l = e.posAtDOM(n, 0);
777
+ if (r?.grid && T(e.state.doc.resolve(l))?.pos === r.grid.pos) {
778
+ const h = L(r.grid, l), i = L(r.grid, M.getState(e.state)?.anchor ?? e.state.selection.anchor);
779
+ if (i && h)
780
+ return o.preventDefault(), e.dispatch(e.state.tr.setMeta(M, { anchor: i.pos + 1, head: h.pos + 1 })), !0;
781
+ }
782
+ }
783
+ return M.getState(e.state) && e.dispatch(e.state.tr.setMeta(M, null)), t.start(e, o);
784
+ }
785
+ },
786
+ handleTextInput(e, o, n, r) {
787
+ return (_(e.state)?.cells?.length || 0) < 2 ? !1 : $(e, { action: "clear_cells" }, r);
788
+ }
789
+ },
790
+ view(e) {
791
+ let o = "";
792
+ const n = () => {
793
+ const r = O(e), l = JSON.stringify(r);
794
+ o !== l && (o = l, s.onStateChange?.(r));
795
+ };
796
+ return n(), {
797
+ update() {
798
+ t.cancelIfStale(e), n();
799
+ },
800
+ destroy: t.destroy
801
+ };
802
+ }
803
+ });
804
+ }
805
+ function ft(s, t) {
806
+ if (!s.editable) return !1;
807
+ const e = _(s.state);
808
+ if (!e?.grid || !e.rect) return !1;
809
+ if (t.key === "Escape" && (M.getState(s.state) || e.cells.length > 1))
810
+ return s.dispatch(s.state.tr.setSelection(C.near(s.state.selection.$head)).setMeta(M, null)), !0;
811
+ if (t.ctrlKey || t.metaKey || t.altKey) return !1;
812
+ if (["Backspace", "Delete"].includes(t.key) && e.cells.length > 1)
813
+ return $(s, { action: "clear_cells" });
814
+ if (t.key !== "Tab") return !1;
815
+ const o = e.grid, n = L(o, s.state.selection.head);
816
+ if (!n) return !1;
817
+ const r = o.cells.indexOf(n) + (t.shiftKey ? -1 : 1);
818
+ if (r < 0) return !1;
819
+ if (r < o.cells.length)
820
+ return U(s, o.cells[r].pos), !0;
821
+ if (!$(s, { action: "row_after" })) return !1;
822
+ const l = _(s.state)?.grid, h = l?.matrix[l.rows.length - 1]?.[0];
823
+ return h && U(s, h.pos), !0;
824
+ }
825
+ function bt(s = {}) {
826
+ return {
827
+ name: "table-view",
828
+ plugins: () => [ut(s)],
829
+ nodeViews: { table: (t, e, o) => new rt(t, e, o) }
830
+ };
831
+ }
832
+ export {
833
+ B as EMPTY_TABLE_STATE,
834
+ bt as createTableExtension,
835
+ O as getTableState,
836
+ $ as runTableCommand
837
+ };
838
+ //# sourceMappingURL=index.js.map