@mache-hangar/lunar-editor-react 0.0.1 → 0.0.4

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.
@@ -1,148 +1,151 @@
1
- import { Fragment as x, closeHistory as N } from "@mache-hangar/lunar-editor-core/pm";
2
- const L = 48;
3
- function A(i) {
4
- const t = Array.from(i.rows), e = [], s = [], n = i.ownerDocument.defaultView.getComputedStyle(i).direction === "rtl", u = n ? -1 : 1;
5
- let a = 0;
6
- const m = (h, d) => (s[h] ??= []).push(d), r = /* @__PURE__ */ new Map();
7
- t.forEach((h, d) => r.set(h.parentElement, d + 1)), t.forEach((h, d) => {
8
- let o = 0;
9
- for (const c of Array.from(h.cells)) {
10
- for (; e[d]?.[o]; ) o++;
11
- const b = c.colSpan, M = r.get(h.parentElement), v = c.rowSpan || M - d;
12
- for (let y = d; y < Math.min(d + v, M); y++)
13
- for (let D = o; D < o + b; D++)
14
- (e[y] ??= [])[D] = !0;
15
- const w = c.getBoundingClientRect();
16
- m(o, n ? w.right : w.left), m(o + b, n ? w.left : w.right), o += b, a = Math.max(a, o);
1
+ import { Fragment as C, closeHistory as k, PluginKey as Y, TextSelection as E, 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);
17
20
  }
18
21
  });
19
- let l = 0;
20
- for (const h of Array.from(i.children).filter(
21
- (d) => d.tagName === "COLGROUP"
22
+ let d = 0;
23
+ for (const m of Array.from(s.children).filter(
24
+ (g) => g.tagName === "COLGROUP"
22
25
  )) {
23
- const d = Array.from(h.children);
24
- for (const o of d.length ? d : [h]) {
25
- const c = o.getBoundingClientRect();
26
- c.width > 0 && (m(l, n ? c.right : c.left), m(l + o.span, n ? c.left : c.right)), l += o.span;
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;
27
30
  }
28
31
  }
29
- if (a = Math.max(a, l), a < 2 || i.getBoundingClientRect().width <= 0) return;
30
- const g = i.getBoundingClientRect();
31
- s[0] ??= [n ? g.right : g.left], s[a] ??= [n ? g.left : g.right];
32
- const f = Array.from({ length: a + 1 }, (h, d) => {
33
- const o = s[d];
34
- return o ? o.reduce((c, b) => c + b, 0) / o.length : NaN;
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;
35
38
  });
36
- let p = 0;
37
- for (; p < a; ) {
38
- let h = p + 1;
39
- for (; h < a && !Number.isFinite(f[h]); ) h++;
40
- for (let d = p + 1; d < h; d++)
41
- f[d] = f[p] + (f[h] - f[p]) * (d - p) / (h - p);
42
- p = h;
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;
43
46
  }
44
- const E = f.slice(1).map((h, d) => Math.abs(h - f[d]));
45
- return E.every((h) => Number.isFinite(h) && h > 0) ? { widths: E, boundaries: f, direction: u } : void 0;
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;
46
49
  }
47
- function R(i, t, e) {
48
- const s = [...i], n = Math.min(L, i[t]), u = Math.min(L, i[t + 1]), a = Math.max(
49
- n - i[t],
50
- Math.min(e, i[t + 1] - u)
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)
51
54
  );
52
- return s[t] += a, s[t + 1] -= a, s;
55
+ return o[t] += l, o[t + 1] -= l, o;
53
56
  }
54
- function O(i, t, e, s = !1) {
57
+ function I(s, t, e, o = !1) {
55
58
  const n = e.createElement("span").style;
56
- return n.cssText = i || "", n.setProperty("width", `${Math.round(t * 100) / 100}px`), s && (n.setProperty("min-width", "0"), n.setProperty("max-width", "none"), n.setProperty("table-layout", "fixed")), n.cssText;
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;
57
60
  }
58
- function P(i, t, e) {
59
- const s = i.state.doc.nodeAt(t);
60
- if (!i.editable || s?.type.name !== "table") return;
61
- const n = i.dom.ownerDocument, u = [];
62
- let a = t + 1;
63
- s.forEach((o, c) => {
64
- o.type.name === "caption" && (a = t + 1 + c + o.nodeSize), o.type.name === "colgroup" && u.push({ node: o, position: t + 1 + c });
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 });
65
68
  });
66
- const { col: m, colgroup: r } = i.state.schema.nodes, l = [];
67
- let g = 0;
68
- const f = (o, c = !1) => {
69
+ const { col: h, colgroup: i } = s.state.schema.nodes, d = [];
70
+ let u = 0;
71
+ const f = (a, c = !1) => {
69
72
  const {
70
- span: b,
71
- width: M,
72
- ...v
73
- } = o?.attrs.htmlAttrs || {};
74
- return c || delete v.id, m.create({
75
- sourceKey: c ? o?.attrs.sourceKey : null,
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,
76
79
  htmlAttrs: {
77
- ...v,
78
- style: O(v.style, e[g++], n)
80
+ ...w,
81
+ style: I(w.style, e[u++], n)
79
82
  }
80
83
  });
81
84
  };
82
- for (const { node: o } of u) {
85
+ for (const { node: a } of r) {
83
86
  const c = [];
84
- if (o.forEach((y) => {
85
- const D = Math.min(
87
+ if (a.forEach((v) => {
88
+ const x = Math.min(
86
89
  1e3,
87
- Math.max(1, parseInt(y.attrs.htmlAttrs?.span, 10) || 1)
90
+ Math.max(1, parseInt(v.attrs.htmlAttrs?.span, 10) || 1)
88
91
  );
89
- for (let C = 0; C < D && g < e.length; C++)
90
- c.push(f(y, C === 0));
91
- }), !o.childCount) {
92
- const y = Math.min(
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(
93
96
  1e3,
94
- Math.max(1, parseInt(o.attrs.htmlAttrs?.span, 10) || 1)
97
+ Math.max(1, parseInt(a.attrs.htmlAttrs?.span, 10) || 1)
95
98
  );
96
- for (let D = 0; D < y && g < e.length; D++)
99
+ for (let x = 0; x < v && u < e.length; x++)
97
100
  c.push(f());
98
101
  }
99
102
  const {
100
- span: b,
101
- width: M,
102
- ...v
103
- } = o.attrs.htmlAttrs || {}, w = n.createElement("span").style;
104
- w.cssText = v.style || "", w.removeProperty("width"), w.cssText ? v.style = w.cssText : delete v.style, l.push(
105
- r.create({ ...o.attrs, htmlAttrs: v }, c)
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)
106
109
  );
107
110
  }
108
- if (g < e.length) {
109
- const o = [];
110
- for (; g < e.length; ) o.push(f());
111
- if (l.length) {
112
- const c = l[l.length - 1];
113
- l[l.length - 1] = c.copy(
114
- c.content.append(x.fromArray(o))
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(C.fromArray(a))
115
118
  );
116
- } else l.push(r.create(null, o));
119
+ } else d.push(i.create(null, a));
117
120
  }
118
- const p = u[0]?.position ?? a, E = u[u.length - 1], h = E ? E.position + E.node.nodeSize : p, d = i.state.tr.replaceWith(
119
- p,
120
- h,
121
- x.fromArray(l)
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
+ C.fromArray(d)
122
125
  );
123
- d.setNodeMarkup(t, void 0, {
124
- ...s.attrs,
126
+ g.setNodeMarkup(t, void 0, {
127
+ ...o.attrs,
125
128
  htmlAttrs: {
126
- ...s.attrs.htmlAttrs,
127
- style: O(
128
- s.attrs.htmlAttrs?.style,
129
- e.reduce((o, c) => o + c, 0),
129
+ ...o.attrs.htmlAttrs,
130
+ style: I(
131
+ o.attrs.htmlAttrs?.style,
132
+ e.reduce((a, c) => a + c, 0),
130
133
  n,
131
134
  !0
132
135
  )
133
136
  }
134
- }), i.dispatch(N(d)), i.dispatch(N(i.state.tr));
137
+ }), s.dispatch(k(g)), s.dispatch(k(s.state.tr));
135
138
  }
136
- function S(i, t) {
137
- Array.from(i.attributes).forEach((e) => {
138
- e.name in t || i.removeAttribute(e.name);
139
- }), Object.entries(t).forEach(([e, s]) => {
140
- i.setAttribute(e, s);
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);
141
144
  });
142
145
  }
143
- class k {
144
- constructor(t, e, s) {
145
- this.view = e, this.getPos = s;
146
+ class rt {
147
+ constructor(t, e, o) {
148
+ this.view = e, this.getPos = o;
146
149
  const n = e.dom.ownerDocument;
147
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);
148
151
  }
@@ -157,7 +160,7 @@ class k {
157
160
  previewNodes = /* @__PURE__ */ new WeakSet();
158
161
  drag;
159
162
  update(t) {
160
- return t.type.name !== "table" ? !1 : (this.drag && (t !== this.node || !this.view.editable) && this.finishDrag(!1), this.node = t, this.drag || S(this.contentDOM, t.attrs.htmlAttrs || {}), this.dom.classList.toggle(
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(
161
164
  "lunar-table-scroll--fixed",
162
165
  this.contentDOM.style.tableLayout === "fixed"
163
166
  ), this.controls.childElementCount && this.scheduleLayout(), !0);
@@ -165,8 +168,8 @@ class k {
165
168
  showControls = (t) => {
166
169
  if (!this.view.editable || this.drag || this.controls.childElementCount || t.target.closest?.(".lunar-table-scroll") !== this.dom) return;
167
170
  this.refreshControls();
168
- const s = this.dom.ownerDocument.defaultView?.ResizeObserver;
169
- s && !this.observer && (this.observer = new s(this.scheduleLayout), this.observer.observe(this.contentDOM));
171
+ const o = this.dom.ownerDocument.defaultView?.ResizeObserver;
172
+ o && !this.observer && (this.observer = new o(this.scheduleLayout), this.observer.observe(this.contentDOM));
170
173
  };
171
174
  hideControls = (t) => {
172
175
  if (this.drag) return;
@@ -186,48 +189,48 @@ class k {
186
189
  return;
187
190
  }
188
191
  const t = this.contentDOM.getBoundingClientRect(), e = this.drag;
189
- let s;
192
+ let o;
190
193
  if (e) {
191
- const m = [t.left + e.edgeOffset];
194
+ const h = [t.left + e.edgeOffset];
192
195
  e.widths.forEach(
193
- (r) => m.push(
194
- m[m.length - 1] + r * e.layout.direction
196
+ (i) => h.push(
197
+ h[h.length - 1] + i * e.layout.direction
195
198
  )
196
- ), s = {
199
+ ), o = {
197
200
  widths: e.widths,
198
- boundaries: m,
201
+ boundaries: h,
199
202
  direction: e.layout.direction
200
203
  };
201
- } else s = A(this.contentDOM);
202
- if (!s) {
204
+ } else o = K(this.contentDOM);
205
+ if (!o) {
203
206
  this.controls.replaceChildren();
204
207
  return;
205
208
  }
206
- const n = s.widths.length - 1;
209
+ const n = o.widths.length - 1;
207
210
  if (this.controls.childElementCount !== n) {
208
211
  this.controls.replaceChildren();
209
- for (let m = 0; m < n; m++) {
210
- const r = this.dom.ownerDocument.createElement("div");
211
- r.className = "lunar-table-column-handle", r.dataset.column = String(m), r.setAttribute("role", "separator"), r.setAttribute("aria-orientation", "vertical"), r.setAttribute("aria-label", `Resize column ${m + 1}`), r.title = "Drag to resize column · Arrow keys to adjust · Esc to cancel", r.tabIndex = 0, this.controls.append(r);
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);
212
215
  }
213
216
  }
214
- const u = this.controls.getBoundingClientRect(), a = Math.max(
217
+ const r = this.controls.getBoundingClientRect(), l = Math.max(
215
218
  t.top,
216
219
  this.contentDOM.rows[0]?.getBoundingClientRect().top ?? t.top
217
220
  );
218
- Array.from(this.controls.children).forEach((m, r) => {
219
- const l = m;
220
- l.style.left = `${s.boundaries[r + 1] - u.left}px`, l.style.top = `${a - u.top}px`, l.style.height = `${t.bottom - a}px`, l.setAttribute(
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(
221
224
  "aria-valuenow",
222
- String(Math.round(s.widths[r]))
223
- ), l.setAttribute(
225
+ String(Math.round(o.widths[i]))
226
+ ), d.setAttribute(
224
227
  "aria-valuemin",
225
- String(Math.min(L, Math.round(s.widths[r])))
226
- ), l.setAttribute(
228
+ String(Math.min(N, Math.round(o.widths[i])))
229
+ ), d.setAttribute(
227
230
  "aria-valuemax",
228
231
  String(
229
232
  Math.round(
230
- s.widths[r] + s.widths[r + 1] - Math.min(L, s.widths[r + 1])
233
+ o.widths[i] + o.widths[i + 1] - Math.min(N, o.widths[i + 1])
231
234
  )
232
235
  )
233
236
  );
@@ -240,38 +243,38 @@ class k {
240
243
  return e && this.controls.contains(e) ? Number(e.dataset.column) : void 0;
241
244
  }
242
245
  startDrag = (t) => {
243
- const e = this.handleColumn(t), s = A(this.contentDOM);
244
- if (!this.view.editable || t.button !== 0 || e === void 0 || !s || e >= s.widths.length - 1 || this.drag)
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)
245
248
  return;
246
249
  t.preventDefault(), t.stopPropagation();
247
- const n = this.dom.ownerDocument, u = n.createElement("colgroup");
248
- this.previewNodes.add(u);
249
- const a = Array.from(this.contentDOM.children).filter((r) => r.tagName === "COLGROUP").map((r) => ({ node: r, next: r.nextSibling }));
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 }));
250
253
  this.drag = {
251
254
  column: e,
252
255
  pointerId: t.pointerId,
253
256
  startX: t.clientX,
254
- layout: s,
255
- edgeOffset: s.boundaries[0] - this.contentDOM.getBoundingClientRect().left,
256
- widths: s.widths,
257
+ layout: o,
258
+ edgeOffset: o.boundaries[0] - this.contentDOM.getBoundingClientRect().left,
259
+ widths: o.widths,
257
260
  document: this.view.state.doc,
258
- preview: u,
259
- originals: a,
261
+ preview: r,
262
+ originals: l,
260
263
  style: this.contentDOM.getAttribute("style")
261
264
  };
262
- for (const { node: r } of a)
263
- this.previewNodes.add(r), r.remove();
264
- s.widths.forEach((r) => {
265
- const l = n.createElement("col");
266
- l.style.width = `${r}px`, this.previewNodes.add(l), u.append(l);
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);
267
270
  });
268
- const m = this.contentDOM.caption;
271
+ const h = this.contentDOM.caption;
269
272
  this.contentDOM.insertBefore(
270
- u,
271
- m ? m.nextSibling : this.contentDOM.firstChild
272
- ), this.contentDOM.style.cssText = O(
273
+ r,
274
+ h ? h.nextSibling : this.contentDOM.firstChild
275
+ ), this.contentDOM.style.cssText = I(
273
276
  this.drag.style || "",
274
- s.widths.reduce((r, l) => r + l, 0),
277
+ o.widths.reduce((i, d) => i + d, 0),
275
278
  n,
276
279
  !0
277
280
  ), this.dom.classList.add(
@@ -288,13 +291,13 @@ class k {
288
291
  this.finishDrag(!1);
289
292
  return;
290
293
  }
291
- t.preventDefault(), e.widths = R(
294
+ t.preventDefault(), e.widths = H(
292
295
  e.layout.widths,
293
296
  e.column,
294
297
  (t.clientX - e.startX) * e.layout.direction
295
298
  );
296
- for (const s of [e.column, e.column + 1])
297
- e.preview.children[s].style.width = `${e.widths[s]}px`;
299
+ for (const o of [e.column, e.column + 1])
300
+ e.preview.children[o].style.width = `${e.widths[o]}px`;
298
301
  this.scheduleLayout();
299
302
  }
300
303
  };
@@ -309,8 +312,8 @@ class k {
309
312
  const e = this.drag;
310
313
  if (!e) return;
311
314
  this.drag = void 0;
312
- const s = this.dom.ownerDocument;
313
- s.removeEventListener("pointermove", this.moveDrag, !0), s.removeEventListener("pointerup", this.endDrag, !0), s.removeEventListener("pointercancel", this.cancelDrag, !0), s.removeEventListener("keydown", this.onDragKeyDown, !0), s.defaultView?.removeEventListener("blur", this.cancelDrag), e.preview.remove(), [...e.originals].reverse().forEach(({ node: u, next: a }) => this.contentDOM.insertBefore(u, a)), 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(
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(
314
317
  "lunar-table-column-handle--active"
315
318
  ), this.dom.classList.toggle(
316
319
  "lunar-table-scroll--fixed",
@@ -318,16 +321,16 @@ class k {
318
321
  );
319
322
  const n = this.getPos();
320
323
  t && this.view.editable && this.view.state.doc === e.document && n !== void 0 && e.widths.some(
321
- (u, a) => Math.abs(u - e.layout.widths[a]) >= 0.5
322
- ) && P(this.view, n, e.widths), this.scheduleLayout();
324
+ (r, l) => Math.abs(r - e.layout.widths[l]) >= 0.5
325
+ ) && X(this.view, n, e.widths), this.scheduleLayout();
323
326
  }
324
327
  onHandleKeyDown = (t) => {
325
- const e = this.handleColumn(t), s = A(this.contentDOM), n = this.getPos();
326
- if (!this.view.editable || e === void 0 || !s || e >= s.widths.length - 1 || n === void 0 || !["ArrowLeft", "ArrowRight"].includes(t.key))
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))
327
330
  return;
328
331
  t.preventDefault();
329
- const u = (t.key === "ArrowRight" ? 10 : -10) * s.direction, a = R(s.widths, e, u);
330
- a[e] !== s.widths[e] && P(this.view, n, a), this.scheduleLayout();
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();
331
334
  };
332
335
  stopEvent(t) {
333
336
  return this.controls.contains(t.target);
@@ -342,15 +345,505 @@ class k {
342
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);
343
346
  }
344
347
  }
345
- function B() {
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 = C.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(C.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(C.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) ? C.empty : C.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(E.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), C.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(C.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(C.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(E.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) {
652
+ let e;
653
+ return s.node.descendants((o, n) => {
654
+ if (o.isAtom) return !1;
655
+ if (!o.inlineContent) return;
656
+ const r = s.pos + n + 2, l = Math.max(r, Math.min(t, r + o.content.size));
657
+ return (e === void 0 || Math.abs(l - t) < Math.abs(e - t) || Math.abs(l - t) === Math.abs(e - t) && l > e) && (e = l), !1;
658
+ }), e;
659
+ }
660
+ function dt(s, t, e) {
661
+ const o = s.state.doc.resolve(t);
662
+ if (e >= 3 && o.parent.inlineContent) return E.create(s.state.doc, o.start(), o.end());
663
+ if (e === 2 && o.parent.inlineContent) {
664
+ 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);
665
+ if (l) {
666
+ let h = 0, i, d;
667
+ if (o.parent.descendants((u, f) => {
668
+ if (!u.isText && !u.isLeaf) return;
669
+ const y = u.isText ? u.text.length : 1;
670
+ i === void 0 && l.index < h + y && (i = o.start() + f + Math.max(0, l.index - h));
671
+ const A = l.index + l.segment.length;
672
+ d === void 0 && A <= h + y && (d = o.start() + f + Math.max(0, A - h)), h += y;
673
+ }), i !== void 0 && d !== void 0) return E.create(s.state.doc, i, d);
674
+ }
675
+ }
676
+ return E.create(s.state.doc, t);
677
+ }
678
+ class ut {
679
+ drag;
680
+ get active() {
681
+ return !!this.drag?.view.editable;
682
+ }
683
+ start(t, e) {
684
+ if (this.cancel(), !t.editable || e.button !== 0 || e.shiftKey || e.ctrlKey || e.metaKey || e.altKey) return !1;
685
+ const o = J(t, e.target);
686
+ if (!o) return !1;
687
+ const n = t.posAtCoords({ left: e.clientX, top: e.clientY })?.pos, r = ht(o.cell, n !== void 0 && L(o.grid, n)?.pos === o.cell.pos ? n : o.cell.pos + 1);
688
+ if (r === void 0) return !1;
689
+ const l = dt(t, r, e.detail);
690
+ 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();
691
+ const h = t.dom.ownerDocument;
692
+ return h.addEventListener("mousemove", this.move, !0), h.addEventListener("mouseup", this.end, !0), h.defaultView?.addEventListener("blur", this.cancel), !0;
693
+ }
694
+ cancelIfStale(t) {
695
+ this.drag && (this.drag.doc !== t.state.doc || !t.editable) && this.destroy();
696
+ }
697
+ /** Clamp hit-testing to the current table and editor viewport, including on mouseup. */
698
+ point(t, e) {
699
+ const o = e.tableElement.getBoundingClientRect(), n = e.view.dom.getBoundingClientRect(), r = e.view.dom.ownerDocument.defaultView;
700
+ return {
701
+ 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)),
702
+ 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))
703
+ };
704
+ }
705
+ move = (t) => {
706
+ const e = this.drag;
707
+ if (!e) return;
708
+ if (!(t.buttons & 1)) {
709
+ this.end();
710
+ return;
711
+ }
712
+ if (e.doc !== e.view.state.doc || !e.view.editable) {
713
+ this.cancel();
714
+ return;
715
+ }
716
+ t.preventDefault();
717
+ const o = this.point(t, e), r = e.view.dom.ownerDocument.elementFromPoint?.(o.left, o.top) ?? t.target, l = J(e.view, r);
718
+ if (!l || l.grid.pos !== e.grid.pos) return;
719
+ const { view: h, anchor: i } = e;
720
+ l.cell.pos !== i.pos && (e.selecting = !0), e.head = l.cell;
721
+ let d;
722
+ if (e.selecting) {
723
+ const f = i.pos <= l.cell.pos;
724
+ d = E.between(
725
+ h.state.doc.resolve(i.pos + (f ? 1 : i.node.nodeSize - 1)),
726
+ h.state.doc.resolve(l.cell.pos + (f ? l.cell.node.nodeSize - 1 : 1))
727
+ );
728
+ } else {
729
+ const f = h.posAtCoords(o)?.pos;
730
+ if (f === void 0 || L(e.grid, f)?.pos !== i.pos) return;
731
+ d = E.between(h.state.doc.resolve(f < e.textFrom ? e.textTo : e.textFrom), h.state.doc.resolve(f));
732
+ }
733
+ const u = M.getState(h.state);
734
+ 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 }));
735
+ };
736
+ end = () => {
737
+ const t = this.drag;
738
+ 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));
739
+ };
740
+ cancel = () => {
741
+ const t = this.drag;
742
+ if (this.destroy(), !t || t.doc !== t.view.state.doc || t.view.isDestroyed) return;
743
+ const e = M.getState(t.view.state);
744
+ e?.dragging && t.view.dispatch(t.view.state.tr.setMeta(M, { ...e, dragging: !1 }));
745
+ };
746
+ destroy = () => {
747
+ const t = this.drag?.view.dom.ownerDocument;
748
+ t?.removeEventListener("mousemove", this.move, !0), t?.removeEventListener("mouseup", this.end, !0), t?.defaultView?.removeEventListener("blur", this.cancel), this.drag = void 0;
749
+ };
750
+ }
751
+ function U(s, t) {
752
+ s.dispatch(s.state.tr.setSelection(E.near(s.state.doc.resolve(t + 1)))), s.focus();
753
+ }
754
+ function ft(s) {
755
+ const t = new ut();
756
+ return new Z({
757
+ key: M,
758
+ state: {
759
+ init: () => null,
760
+ apply(e, o, n) {
761
+ const r = e.getMeta(M);
762
+ return r !== void 0 ? r : e.docChanged || e.selectionSet && !t.active && !e.selection.eq(n.selection) ? null : o;
763
+ }
764
+ },
765
+ props: {
766
+ attributes(e) {
767
+ const o = [];
768
+ 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(" ") };
769
+ },
770
+ decorations(e) {
771
+ const o = _(e);
772
+ return !o?.cells || o.cells.length < 2 ? null : tt.create(
773
+ e.doc,
774
+ o.cells.map((n) => et.node(n.pos, n.pos + n.node.nodeSize, { class: "lunar-table-cell--selected" }))
775
+ );
776
+ },
777
+ handleDOMEvents: {
778
+ keydown(e, o) {
779
+ t.cancel();
780
+ const n = pt(e, o);
781
+ return n && o.preventDefault(), n;
782
+ },
783
+ mousedown(e, o) {
784
+ if (t.cancel(), !e.editable || o.button !== 0) return !1;
785
+ const n = o.target?.closest?.("td, th");
786
+ if (o.shiftKey && n && e.dom.contains(n)) {
787
+ const r = _(e.state), l = e.posAtDOM(n, 0);
788
+ if (r?.grid && T(e.state.doc.resolve(l))?.pos === r.grid.pos) {
789
+ const h = L(r.grid, l), i = L(r.grid, M.getState(e.state)?.anchor ?? e.state.selection.anchor);
790
+ if (i && h)
791
+ return o.preventDefault(), e.dispatch(e.state.tr.setMeta(M, { anchor: i.pos + 1, head: h.pos + 1 })), !0;
792
+ }
793
+ }
794
+ return M.getState(e.state) && e.dispatch(e.state.tr.setMeta(M, null)), t.start(e, o);
795
+ }
796
+ },
797
+ handleTextInput(e, o, n, r) {
798
+ return (_(e.state)?.cells?.length || 0) < 2 ? !1 : $(e, { action: "clear_cells" }, r);
799
+ }
800
+ },
801
+ view(e) {
802
+ let o = "";
803
+ const n = () => {
804
+ const r = O(e), l = JSON.stringify(r);
805
+ o !== l && (o = l, s.onStateChange?.(r));
806
+ };
807
+ return n(), {
808
+ update() {
809
+ t.cancelIfStale(e), n();
810
+ },
811
+ destroy: t.destroy
812
+ };
813
+ }
814
+ });
815
+ }
816
+ function pt(s, t) {
817
+ if (!s.editable) return !1;
818
+ const e = _(s.state);
819
+ if (!e?.grid || !e.rect) return !1;
820
+ if (t.key === "Escape" && (M.getState(s.state) || e.cells.length > 1))
821
+ return s.dispatch(s.state.tr.setSelection(E.near(s.state.selection.$head)).setMeta(M, null)), !0;
822
+ if (t.ctrlKey || t.metaKey || t.altKey) return !1;
823
+ if (["Backspace", "Delete"].includes(t.key) && e.cells.length > 1)
824
+ return $(s, { action: "clear_cells" });
825
+ if (t.key !== "Tab") return !1;
826
+ const o = e.grid, n = L(o, s.state.selection.head);
827
+ if (!n) return !1;
828
+ const r = o.cells.indexOf(n) + (t.shiftKey ? -1 : 1);
829
+ if (r < 0) return !1;
830
+ if (r < o.cells.length)
831
+ return U(s, o.cells[r].pos), !0;
832
+ if (!$(s, { action: "row_after" })) return !1;
833
+ const l = _(s.state)?.grid, h = l?.matrix[l.rows.length - 1]?.[0];
834
+ return h && U(s, h.pos), !0;
835
+ }
836
+ function wt(s = {}) {
346
837
  return {
347
838
  name: "table-view",
348
- nodeViews: {
349
- table: (i, t, e) => new k(i, t, e)
350
- }
839
+ plugins: () => [ft(s)],
840
+ nodeViews: { table: (t, e, o) => new rt(t, e, o) }
351
841
  };
352
842
  }
353
843
  export {
354
- B as createTableExtension
844
+ B as EMPTY_TABLE_STATE,
845
+ wt as createTableExtension,
846
+ O as getTableState,
847
+ $ as runTableCommand
355
848
  };
356
849
  //# sourceMappingURL=index.js.map