@mache-hangar/lunar-editor-react 0.0.1 → 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.
- package/README.md +64 -1
- package/dist/extensions/table/index.cjs +1 -1
- package/dist/extensions/table/index.cjs.map +1 -1
- package/dist/extensions/table/index.js +648 -166
- package/dist/extensions/table/index.js.map +1 -1
- package/dist/extensions/table-ui/index.cjs +2 -0
- package/dist/extensions/table-ui/index.cjs.map +1 -0
- package/dist/extensions/table-ui/index.js +531 -0
- package/dist/extensions/table-ui/index.js.map +1 -0
- package/dist/extensions/table-ui.css +1 -0
- package/dist/extensions/table.css +1 -1
- package/dist/types/extensions/table/cell-drag.d.ts +14 -0
- package/dist/types/extensions/table/commands.d.ts +67 -0
- package/dist/types/extensions/table/grid.d.ts +49 -0
- package/dist/types/extensions/table/index.d.ts +5 -1
- package/dist/types/extensions/table/interaction.d.ts +6 -0
- package/dist/types/extensions/table/selection-state.d.ts +3 -0
- package/dist/types/extensions/table-ui/TableProperties.d.ts +9 -0
- package/dist/types/extensions/table-ui/TableTools.d.ts +14 -0
- package/dist/types/extensions/table-ui/index.d.ts +2 -0
- package/dist/types/extensions/table-ui/useTableOverlay.d.ts +24 -0
- package/package.json +23 -5
|
@@ -1,148 +1,151 @@
|
|
|
1
|
-
import { Fragment as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
t.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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);
|
|
17
20
|
}
|
|
18
21
|
});
|
|
19
|
-
let
|
|
20
|
-
for (const
|
|
21
|
-
(
|
|
22
|
+
let d = 0;
|
|
23
|
+
for (const m of Array.from(s.children).filter(
|
|
24
|
+
(g) => g.tagName === "COLGROUP"
|
|
22
25
|
)) {
|
|
23
|
-
const
|
|
24
|
-
for (const
|
|
25
|
-
const c =
|
|
26
|
-
c.width > 0 && (
|
|
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 (
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
const f = Array.from({ length:
|
|
33
|
-
const
|
|
34
|
-
return
|
|
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
|
|
37
|
-
for (;
|
|
38
|
-
let
|
|
39
|
-
for (;
|
|
40
|
-
for (let
|
|
41
|
-
f[
|
|
42
|
-
|
|
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
|
|
45
|
-
return
|
|
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
|
|
48
|
-
const
|
|
49
|
-
n -
|
|
50
|
-
Math.min(e,
|
|
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
|
|
55
|
+
return o[t] += l, o[t + 1] -= l, o;
|
|
53
56
|
}
|
|
54
|
-
function
|
|
57
|
+
function I(s, t, e, o = !1) {
|
|
55
58
|
const n = e.createElement("span").style;
|
|
56
|
-
return 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
|
|
59
|
-
const
|
|
60
|
-
if (!
|
|
61
|
-
const n =
|
|
62
|
-
let
|
|
63
|
-
|
|
64
|
-
|
|
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:
|
|
67
|
-
let
|
|
68
|
-
const f = (
|
|
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:
|
|
71
|
-
width:
|
|
72
|
-
...
|
|
73
|
-
} =
|
|
74
|
-
return c || delete
|
|
75
|
-
sourceKey: c ?
|
|
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
|
-
...
|
|
78
|
-
style:
|
|
80
|
+
...w,
|
|
81
|
+
style: I(w.style, e[u++], n)
|
|
79
82
|
}
|
|
80
83
|
});
|
|
81
84
|
};
|
|
82
|
-
for (const { node:
|
|
85
|
+
for (const { node: a } of r) {
|
|
83
86
|
const c = [];
|
|
84
|
-
if (
|
|
85
|
-
const
|
|
87
|
+
if (a.forEach((v) => {
|
|
88
|
+
const x = Math.min(
|
|
86
89
|
1e3,
|
|
87
|
-
Math.max(1, parseInt(
|
|
90
|
+
Math.max(1, parseInt(v.attrs.htmlAttrs?.span, 10) || 1)
|
|
88
91
|
);
|
|
89
|
-
for (let
|
|
90
|
-
c.push(f(
|
|
91
|
-
}), !
|
|
92
|
-
const
|
|
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(
|
|
97
|
+
Math.max(1, parseInt(a.attrs.htmlAttrs?.span, 10) || 1)
|
|
95
98
|
);
|
|
96
|
-
for (let
|
|
99
|
+
for (let x = 0; x < v && u < e.length; x++)
|
|
97
100
|
c.push(f());
|
|
98
101
|
}
|
|
99
102
|
const {
|
|
100
|
-
span:
|
|
101
|
-
width:
|
|
102
|
-
...
|
|
103
|
-
} =
|
|
104
|
-
|
|
105
|
-
|
|
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 (
|
|
109
|
-
const
|
|
110
|
-
for (;
|
|
111
|
-
if (
|
|
112
|
-
const c =
|
|
113
|
-
|
|
114
|
-
c.content.append(
|
|
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))
|
|
115
118
|
);
|
|
116
|
-
} else
|
|
119
|
+
} else d.push(i.create(null, a));
|
|
117
120
|
}
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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)
|
|
122
125
|
);
|
|
123
|
-
|
|
124
|
-
...
|
|
126
|
+
g.setNodeMarkup(t, void 0, {
|
|
127
|
+
...o.attrs,
|
|
125
128
|
htmlAttrs: {
|
|
126
|
-
...
|
|
127
|
-
style:
|
|
128
|
-
|
|
129
|
-
e.reduce((
|
|
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
|
-
}),
|
|
137
|
+
}), s.dispatch(k(g)), s.dispatch(k(s.state.tr));
|
|
135
138
|
}
|
|
136
|
-
function
|
|
137
|
-
Array.from(
|
|
138
|
-
e.name in t ||
|
|
139
|
-
}), Object.entries(t).forEach(([e,
|
|
140
|
-
|
|
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
|
|
144
|
-
constructor(t, e,
|
|
145
|
-
this.view = e, this.getPos =
|
|
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 ||
|
|
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
|
|
169
|
-
|
|
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
|
|
192
|
+
let o;
|
|
190
193
|
if (e) {
|
|
191
|
-
const
|
|
194
|
+
const h = [t.left + e.edgeOffset];
|
|
192
195
|
e.widths.forEach(
|
|
193
|
-
(
|
|
194
|
-
|
|
196
|
+
(i) => h.push(
|
|
197
|
+
h[h.length - 1] + i * e.layout.direction
|
|
195
198
|
)
|
|
196
|
-
),
|
|
199
|
+
), o = {
|
|
197
200
|
widths: e.widths,
|
|
198
|
-
boundaries:
|
|
201
|
+
boundaries: h,
|
|
199
202
|
direction: e.layout.direction
|
|
200
203
|
};
|
|
201
|
-
} else
|
|
202
|
-
if (!
|
|
204
|
+
} else o = K(this.contentDOM);
|
|
205
|
+
if (!o) {
|
|
203
206
|
this.controls.replaceChildren();
|
|
204
207
|
return;
|
|
205
208
|
}
|
|
206
|
-
const n =
|
|
209
|
+
const n = o.widths.length - 1;
|
|
207
210
|
if (this.controls.childElementCount !== n) {
|
|
208
211
|
this.controls.replaceChildren();
|
|
209
|
-
for (let
|
|
210
|
-
const
|
|
211
|
-
|
|
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
|
|
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((
|
|
219
|
-
const
|
|
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(
|
|
221
224
|
"aria-valuenow",
|
|
222
|
-
String(Math.round(
|
|
223
|
-
),
|
|
225
|
+
String(Math.round(o.widths[i]))
|
|
226
|
+
), d.setAttribute(
|
|
224
227
|
"aria-valuemin",
|
|
225
|
-
String(Math.min(
|
|
226
|
-
),
|
|
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
|
-
|
|
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),
|
|
244
|
-
if (!this.view.editable || t.button !== 0 || e === void 0 || !
|
|
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,
|
|
248
|
-
this.previewNodes.add(
|
|
249
|
-
const
|
|
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:
|
|
255
|
-
edgeOffset:
|
|
256
|
-
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:
|
|
259
|
-
originals:
|
|
261
|
+
preview: r,
|
|
262
|
+
originals: l,
|
|
260
263
|
style: this.contentDOM.getAttribute("style")
|
|
261
264
|
};
|
|
262
|
-
for (const { node:
|
|
263
|
-
this.previewNodes.add(
|
|
264
|
-
|
|
265
|
-
const
|
|
266
|
-
|
|
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
|
|
271
|
+
const h = this.contentDOM.caption;
|
|
269
272
|
this.contentDOM.insertBefore(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
), this.contentDOM.style.cssText =
|
|
273
|
+
r,
|
|
274
|
+
h ? h.nextSibling : this.contentDOM.firstChild
|
|
275
|
+
), this.contentDOM.style.cssText = I(
|
|
273
276
|
this.drag.style || "",
|
|
274
|
-
|
|
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 =
|
|
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
|
|
297
|
-
e.preview.children[
|
|
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
|
|
313
|
-
|
|
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
|
-
(
|
|
322
|
-
) &&
|
|
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),
|
|
326
|
-
if (!this.view.editable || e === void 0 || !
|
|
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
|
|
330
|
-
|
|
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,494 @@ 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
|
-
|
|
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 = {}) {
|
|
346
826
|
return {
|
|
347
827
|
name: "table-view",
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
828
|
+
plugins: () => [ut(s)],
|
|
829
|
+
nodeViews: { table: (t, e, o) => new rt(t, e, o) }
|
|
351
830
|
};
|
|
352
831
|
}
|
|
353
832
|
export {
|
|
354
|
-
B as
|
|
833
|
+
B as EMPTY_TABLE_STATE,
|
|
834
|
+
bt as createTableExtension,
|
|
835
|
+
O as getTableState,
|
|
836
|
+
$ as runTableCommand
|
|
355
837
|
};
|
|
356
838
|
//# sourceMappingURL=index.js.map
|