@mache-hangar/lunar-editor-react 0.0.0-stage → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +128 -2
- package/THIRD_PARTY_NOTICES.md +58 -0
- package/dist/code-editor-Cq33u9jq.cjs +2 -0
- package/dist/code-editor-Cq33u9jq.cjs.map +1 -0
- package/dist/code-editor-um45ekRh.js +78 -0
- package/dist/code-editor-um45ekRh.js.map +1 -0
- package/dist/code-editor.cjs +2 -0
- package/dist/code-editor.cjs.map +1 -0
- package/dist/code-editor.js +8 -0
- package/dist/code-editor.js.map +1 -0
- package/dist/extensions/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Main-Bold.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Main-Italic.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Main-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Math-Italic.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Script-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- package/dist/extensions/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/dist/extensions/math/index.cjs +2 -0
- package/dist/extensions/math/index.cjs.map +1 -0
- package/dist/extensions/math/index.js +285 -0
- package/dist/extensions/math/index.js.map +1 -0
- package/dist/extensions/math.css +1 -0
- package/dist/extensions/table/index.cjs +2 -0
- package/dist/extensions/table/index.cjs.map +1 -0
- package/dist/extensions/table/index.js +838 -0
- package/dist/extensions/table/index.js.map +1 -0
- 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 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/rich-editor.cjs +2 -0
- package/dist/rich-editor.cjs.map +1 -0
- package/dist/rich-editor.js +67 -0
- package/dist/rich-editor.js.map +1 -0
- package/dist/styles.css +1 -0
- package/dist/types/code-editor-types.d.ts +5 -0
- package/dist/types/code-editor.d.ts +6 -0
- package/dist/types/controller-hooks.d.ts +6 -0
- package/dist/types/extensions/math/index.d.ts +3 -0
- package/dist/types/extensions/math/math-input.d.ts +4 -0
- package/dist/types/extensions/math/math-node-view.d.ts +27 -0
- package/dist/types/extensions/math/nodes.d.ts +9 -0
- package/dist/types/extensions/table/cell-drag.d.ts +14 -0
- package/dist/types/extensions/table/column-layout.d.ts +9 -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 +6 -0
- package/dist/types/extensions/table/interaction.d.ts +6 -0
- package/dist/types/extensions/table/resize-transaction.d.ts +4 -0
- package/dist/types/extensions/table/selection-state.d.ts +3 -0
- package/dist/types/extensions/table/table-node-view.d.ts +30 -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/dist/types/index.d.ts +5 -0
- package/dist/types/rich-editor.d.ts +25 -0
- package/package.json +95 -3
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { getDomSourceKey as f } from "@mache-hangar/lunar-editor-core/source/dom-projection";
|
|
2
|
+
import { readHtmlAttributes as g } from "@mache-hangar/lunar-editor-core/schema/dom-attributes";
|
|
3
|
+
import { MathfieldElement as p } from "mathlive";
|
|
4
|
+
import { NodeSelection as d, Selection as y, Plugin as w } from "@mache-hangar/lunar-editor-core/pm";
|
|
5
|
+
function c(e) {
|
|
6
|
+
const t = g(e);
|
|
7
|
+
return delete t["data-type"], delete t["data-display"], delete t["data-latex"], {
|
|
8
|
+
htmlAttrs: t,
|
|
9
|
+
latex: e.dataset.latex || "",
|
|
10
|
+
sourceKey: f(e) || null
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
const u = {
|
|
14
|
+
math_block: {
|
|
15
|
+
attrs: {
|
|
16
|
+
htmlAttrs: { default: {} },
|
|
17
|
+
latex: { default: "" },
|
|
18
|
+
sourceKey: { default: null }
|
|
19
|
+
},
|
|
20
|
+
atom: !0,
|
|
21
|
+
group: "block",
|
|
22
|
+
isolating: !0,
|
|
23
|
+
parseDOM: [
|
|
24
|
+
{
|
|
25
|
+
tag: 'p[data-type="math"][data-display="block"]',
|
|
26
|
+
priority: 100,
|
|
27
|
+
getAttrs: (e) => c(e)
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
toDOM: (e) => [
|
|
31
|
+
"p",
|
|
32
|
+
{
|
|
33
|
+
...e.attrs.htmlAttrs,
|
|
34
|
+
"data-type": "math",
|
|
35
|
+
"data-display": "block",
|
|
36
|
+
"data-latex": e.attrs.latex || ""
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
math_inline: {
|
|
41
|
+
attrs: {
|
|
42
|
+
htmlAttrs: { default: {} },
|
|
43
|
+
latex: { default: "" },
|
|
44
|
+
sourceKey: { default: null }
|
|
45
|
+
},
|
|
46
|
+
atom: !0,
|
|
47
|
+
group: "inline",
|
|
48
|
+
inline: !0,
|
|
49
|
+
isolating: !0,
|
|
50
|
+
parseDOM: [
|
|
51
|
+
{
|
|
52
|
+
tag: 'span[data-type="math"][data-display="inline"]',
|
|
53
|
+
priority: 100,
|
|
54
|
+
getAttrs: (e) => c(e)
|
|
55
|
+
}
|
|
56
|
+
],
|
|
57
|
+
toDOM: (e) => [
|
|
58
|
+
"span",
|
|
59
|
+
{
|
|
60
|
+
...e.attrs.htmlAttrs,
|
|
61
|
+
"data-type": "math",
|
|
62
|
+
"data-display": "inline",
|
|
63
|
+
"data-latex": e.attrs.latex || ""
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
p.soundsDirectory = null;
|
|
69
|
+
function v(e, t) {
|
|
70
|
+
Array.from(e.attributes).forEach((a) => {
|
|
71
|
+
a.name in t || e.removeAttribute(a.name);
|
|
72
|
+
}), Object.entries(t).forEach(([a, s]) => {
|
|
73
|
+
e.setAttribute(a, s);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function b(e, t, a, s) {
|
|
77
|
+
const i = { ...e.attrs.htmlAttrs || {} }, r = new Set(
|
|
78
|
+
String(i.class || "").split(/\s+/).filter(Boolean)
|
|
79
|
+
);
|
|
80
|
+
return r.add("lunar-math-node"), r.add(`lunar-math-${s}`), t && r.add("ProseMirror-selectednode"), a && r.add("lunar-math-node--editing"), {
|
|
81
|
+
...i,
|
|
82
|
+
class: Array.from(r).join(" "),
|
|
83
|
+
"data-type": "math",
|
|
84
|
+
"data-display": s,
|
|
85
|
+
"data-latex": e.attrs.latex || "",
|
|
86
|
+
contenteditable: "false"
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
class m {
|
|
90
|
+
constructor(t, a, s) {
|
|
91
|
+
this.view = a, this.getPos = s, this.node = t, this.display = t.type.name === "math_inline" ? "inline" : "block", this.dom = document.createElement(this.display === "inline" ? "span" : "p"), this.mathfield = new p(), this.mathfield.className = `lunar-math-field lunar-math-field--${this.display}`, this.mathfield.smartFence = !0, this.mathfield.mathVirtualKeyboardPolicy = "auto", this.mathfield.setAttribute(
|
|
92
|
+
"aria-label",
|
|
93
|
+
this.display === "inline" ? "Inline math formula" : "Math formula"
|
|
94
|
+
), this.mathfield.addEventListener("change", this.handleChange), this.mathfield.addEventListener("focus", this.handleFocus), this.mathfield.addEventListener("blur", this.handleBlur), this.mathfield.addEventListener("keydown", this.handleKeyDown), this.dom.addEventListener("pointerdown", this.handlePointerDown, !0), this.dom.appendChild(this.mathfield), this.update(t);
|
|
95
|
+
}
|
|
96
|
+
view;
|
|
97
|
+
getPos;
|
|
98
|
+
dom;
|
|
99
|
+
node;
|
|
100
|
+
mathfield;
|
|
101
|
+
display;
|
|
102
|
+
selected = !1;
|
|
103
|
+
editing = !1;
|
|
104
|
+
handlePointerDown = () => {
|
|
105
|
+
if (!this.view.editable) {
|
|
106
|
+
this.setEditing(!1);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
this.setEditing(!0);
|
|
110
|
+
const t = this.getPos();
|
|
111
|
+
if (t === void 0)
|
|
112
|
+
return;
|
|
113
|
+
const a = this.view.state.selection;
|
|
114
|
+
(!(a instanceof d) || a.from !== t) && this.view.dispatch(
|
|
115
|
+
this.view.state.tr.setSelection(
|
|
116
|
+
d.create(this.view.state.doc, t)
|
|
117
|
+
)
|
|
118
|
+
), window.requestAnimationFrame(() => {
|
|
119
|
+
this.editing && this.view.editable && this.mathfield.focus();
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
handleFocus = () => {
|
|
123
|
+
this.setEditing(!0);
|
|
124
|
+
};
|
|
125
|
+
handleBlur = () => {
|
|
126
|
+
this.setEditing(!1);
|
|
127
|
+
};
|
|
128
|
+
setEditing(t) {
|
|
129
|
+
this.editing = t && this.view.editable, this.dom.classList.toggle("lunar-math-node--editing", this.editing), this.mathfield.readOnly = !this.view.editable;
|
|
130
|
+
}
|
|
131
|
+
handleChange = () => {
|
|
132
|
+
const t = this.getPos();
|
|
133
|
+
if (t === void 0 || !this.view.editable)
|
|
134
|
+
return;
|
|
135
|
+
const a = this.view.state.doc.nodeAt(t), s = this.mathfield.value;
|
|
136
|
+
a?.type !== this.node.type || a.attrs.latex === s || this.view.dispatch(
|
|
137
|
+
this.view.state.tr.setNodeMarkup(t, void 0, {
|
|
138
|
+
...a.attrs,
|
|
139
|
+
latex: s
|
|
140
|
+
})
|
|
141
|
+
);
|
|
142
|
+
};
|
|
143
|
+
handleKeyDown = (t) => {
|
|
144
|
+
if ((t.key === "Backspace" || t.key === "Delete") && this.mathfield.value.trim() === "") {
|
|
145
|
+
t.preventDefault(), t.stopPropagation(), this.deleteEmptyBlock(t.key === "Backspace" ? -1 : 1);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
t.key === "Escape" && (t.preventDefault(), t.stopPropagation(), this.mathfield.blur(), this.view.focus());
|
|
149
|
+
};
|
|
150
|
+
deleteEmptyBlock(t) {
|
|
151
|
+
const a = this.getPos();
|
|
152
|
+
if (a === void 0 || !this.view.editable)
|
|
153
|
+
return;
|
|
154
|
+
const s = this.view.state.doc.nodeAt(a);
|
|
155
|
+
if (s?.type !== this.node.type)
|
|
156
|
+
return;
|
|
157
|
+
const i = this.view.state.tr.delete(
|
|
158
|
+
a,
|
|
159
|
+
a + s.nodeSize
|
|
160
|
+
), r = Math.min(a, i.doc.content.size);
|
|
161
|
+
i.setSelection(
|
|
162
|
+
y.near(i.doc.resolve(r), t)
|
|
163
|
+
), this.view.dispatch(i), window.requestAnimationFrame(() => {
|
|
164
|
+
this.view.focus();
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
update(t) {
|
|
168
|
+
if (t.type.name !== `math_${this.display}`)
|
|
169
|
+
return !1;
|
|
170
|
+
this.node = t, v(
|
|
171
|
+
this.dom,
|
|
172
|
+
b(t, this.selected, this.editing, this.display)
|
|
173
|
+
), this.mathfield.readOnly = !this.view.editable;
|
|
174
|
+
const a = t.attrs.latex || "";
|
|
175
|
+
return this.mathfield.value !== a && this.mathfield.setValue(a, { silenceNotifications: !0 }), !0;
|
|
176
|
+
}
|
|
177
|
+
selectNode() {
|
|
178
|
+
if (this.selected = !0, this.dom.classList.add("ProseMirror-selectednode"), !this.view.editable) {
|
|
179
|
+
this.setEditing(!1);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
this.setEditing(!0), window.requestAnimationFrame(() => {
|
|
183
|
+
this.mathfield.focus();
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
deselectNode() {
|
|
187
|
+
this.selected = !1, this.dom.classList.remove("ProseMirror-selectednode"), this.setEditing(!1);
|
|
188
|
+
}
|
|
189
|
+
stopEvent(t) {
|
|
190
|
+
return t.target instanceof Node && this.dom.contains(t.target);
|
|
191
|
+
}
|
|
192
|
+
ignoreMutation() {
|
|
193
|
+
return !0;
|
|
194
|
+
}
|
|
195
|
+
destroy() {
|
|
196
|
+
this.mathfield.removeEventListener("change", this.handleChange), this.mathfield.removeEventListener("focus", this.handleFocus), this.mathfield.removeEventListener("blur", this.handleBlur), this.mathfield.removeEventListener("keydown", this.handleKeyDown), this.dom.removeEventListener("pointerdown", this.handlePointerDown, !0);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function E(e, t, a, s) {
|
|
200
|
+
const i = e.state.doc.resolve(t), r = e.state.doc.resolve(a);
|
|
201
|
+
if (i.parent !== r.parent || i.parent.type !== e.state.schema.nodes.paragraph)
|
|
202
|
+
return;
|
|
203
|
+
const n = i.parent, o = n.textBetween(0, i.parentOffset, "", ""), l = n.textBetween(
|
|
204
|
+
r.parentOffset,
|
|
205
|
+
n.content.size,
|
|
206
|
+
"",
|
|
207
|
+
""
|
|
208
|
+
);
|
|
209
|
+
return {
|
|
210
|
+
paragraph: n,
|
|
211
|
+
text: `${o}${s}${l}`,
|
|
212
|
+
paragraphStart: i.before(),
|
|
213
|
+
paragraphEnd: i.after()
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function x(e, t, a, s) {
|
|
217
|
+
if (!s.includes("$"))
|
|
218
|
+
return !1;
|
|
219
|
+
const i = E(e, t, a, s), r = e.state.schema.nodes.math_block;
|
|
220
|
+
if (!i || i.text !== "$$" || !r)
|
|
221
|
+
return !1;
|
|
222
|
+
const n = { ...i.paragraph.attrs.htmlAttrs || {} };
|
|
223
|
+
delete n["data-type"], delete n["data-display"], delete n["data-latex"];
|
|
224
|
+
const o = e.state.tr.replaceWith(
|
|
225
|
+
i.paragraphStart,
|
|
226
|
+
i.paragraphEnd,
|
|
227
|
+
r.create({
|
|
228
|
+
htmlAttrs: n,
|
|
229
|
+
latex: "",
|
|
230
|
+
sourceKey: i.paragraph.attrs.sourceKey || null
|
|
231
|
+
})
|
|
232
|
+
);
|
|
233
|
+
return o.setSelection(
|
|
234
|
+
d.create(o.doc, i.paragraphStart)
|
|
235
|
+
), e.dispatch(o.scrollIntoView()), !0;
|
|
236
|
+
}
|
|
237
|
+
function A(e, t, a, s) {
|
|
238
|
+
if (s !== "$" || t !== a)
|
|
239
|
+
return !1;
|
|
240
|
+
const i = e.state.doc.resolve(t);
|
|
241
|
+
if (i.parent.type !== e.state.schema.nodes.paragraph || i.parentOffset < 2)
|
|
242
|
+
return !1;
|
|
243
|
+
const r = i.parentOffset - 2, n = i.parent.textBetween(
|
|
244
|
+
r,
|
|
245
|
+
i.parentOffset,
|
|
246
|
+
"",
|
|
247
|
+
""
|
|
248
|
+
), o = e.state.schema.nodes.math_inline;
|
|
249
|
+
if (n !== "$." || !o)
|
|
250
|
+
return !1;
|
|
251
|
+
const l = t - 2, h = e.state.tr.replaceWith(
|
|
252
|
+
l,
|
|
253
|
+
a,
|
|
254
|
+
o.create({ htmlAttrs: {}, latex: "" })
|
|
255
|
+
);
|
|
256
|
+
return h.setSelection(d.create(h.doc, l)), e.dispatch(h.scrollIntoView()), !0;
|
|
257
|
+
}
|
|
258
|
+
function k() {
|
|
259
|
+
return new w({
|
|
260
|
+
props: {
|
|
261
|
+
handleTextInput: (e, t, a, s) => A(e, t, a, s) || x(e, t, a, s)
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
function B() {
|
|
266
|
+
return {
|
|
267
|
+
name: "math",
|
|
268
|
+
nodes: {
|
|
269
|
+
math_block: { ...u.math_block, sourceUnit: !0 },
|
|
270
|
+
math_inline: { ...u.math_inline, sourceUnit: !0 }
|
|
271
|
+
},
|
|
272
|
+
plugins: () => [k()],
|
|
273
|
+
nodeViews: {
|
|
274
|
+
math_block: (e, t, a) => new m(e, t, a),
|
|
275
|
+
math_inline: (e, t, a) => new m(e, t, a)
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
export {
|
|
280
|
+
B as createMathExtension,
|
|
281
|
+
k as createMathInputPlugin,
|
|
282
|
+
x as insertBlockMathFromDollarInput,
|
|
283
|
+
A as insertInlineMathFromDotDollarInput
|
|
284
|
+
};
|
|
285
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/extensions/math/nodes.ts","../../../src/extensions/math/math-node-view.ts","../../../src/extensions/math/math-input.ts","../../../src/extensions/math/index.ts"],"sourcesContent":["import type { NodeSpec, DOMOutputSpec } from '@mache-hangar/lunar-editor-core/pm';\nimport { getDomSourceKey } from '@mache-hangar/lunar-editor-core/source/dom-projection';\nimport { readHtmlAttributes } from '@mache-hangar/lunar-editor-core/schema/dom-attributes';\nexport function parseMathAttrs(element: HTMLElement) {\n const htmlAttrs = readHtmlAttributes(element);\n delete htmlAttrs['data-type'];\n delete htmlAttrs['data-display'];\n delete htmlAttrs['data-latex'];\n\n return {\n htmlAttrs,\n latex: element.dataset.latex || '',\n sourceKey: getDomSourceKey(element) || null,\n };\n}\n\nexport const mathNodes: Record<string, NodeSpec> = {\n math_block: {\n attrs: {\n htmlAttrs: { default: {} },\n latex: { default: '' },\n sourceKey: { default: null },\n },\n atom: true,\n group: 'block',\n isolating: true,\n parseDOM: [\n {\n tag: 'p[data-type=\"math\"][data-display=\"block\"]',\n priority: 100,\n getAttrs: (element) => parseMathAttrs(element as HTMLElement),\n },\n ],\n toDOM: (node): DOMOutputSpec => [\n 'p',\n {\n ...node.attrs.htmlAttrs,\n 'data-type': 'math',\n 'data-display': 'block',\n 'data-latex': node.attrs.latex || '',\n },\n ],\n },\n math_inline: {\n attrs: {\n htmlAttrs: { default: {} },\n latex: { default: '' },\n sourceKey: { default: null },\n },\n atom: true,\n group: 'inline',\n inline: true,\n isolating: true,\n parseDOM: [\n {\n tag: 'span[data-type=\"math\"][data-display=\"inline\"]',\n priority: 100,\n getAttrs: (element) => parseMathAttrs(element as HTMLElement),\n },\n ],\n toDOM: (node): DOMOutputSpec => [\n 'span',\n {\n ...node.attrs.htmlAttrs,\n 'data-type': 'math',\n 'data-display': 'inline',\n 'data-latex': node.attrs.latex || '',\n },\n ],\n },\n};\n","import { MathfieldElement } from 'mathlive';\nimport {\n type Node as ProseMirrorNode,\n NodeSelection,\n Selection,\n type EditorView,\n type NodeView,\n} from '@mache-hangar/lunar-editor-core/pm';\n\ntype HtmlAttributes = Record<string, string>;\ntype GetPos = () => number | undefined;\ntype MathDisplay = 'block' | 'inline';\n\nMathfieldElement.soundsDirectory = null;\n\nfunction updateHtmlAttributes(\n element: HTMLElement,\n attributes: HtmlAttributes\n) {\n Array.from(element.attributes).forEach((attribute) => {\n if (!(attribute.name in attributes)) {\n element.removeAttribute(attribute.name);\n }\n });\n\n Object.entries(attributes).forEach(([name, value]) => {\n element.setAttribute(name, value);\n });\n}\n\nfunction mathNodeAttributes(\n node: ProseMirrorNode,\n selected: boolean,\n editing: boolean,\n display: MathDisplay\n): HtmlAttributes {\n const htmlAttrs = { ...(node.attrs.htmlAttrs || {}) };\n const classNames = new Set(\n String(htmlAttrs.class || '')\n .split(/\\s+/)\n .filter(Boolean)\n );\n classNames.add('lunar-math-node');\n classNames.add(`lunar-math-${display}`);\n if (selected) {\n classNames.add('ProseMirror-selectednode');\n }\n if (editing) {\n classNames.add('lunar-math-node--editing');\n }\n\n return {\n ...htmlAttrs,\n class: Array.from(classNames).join(' '),\n 'data-type': 'math',\n 'data-display': display,\n 'data-latex': node.attrs.latex || '',\n contenteditable: 'false',\n };\n}\n\nexport class MathNodeView implements NodeView {\n readonly dom: HTMLElement;\n\n private node: ProseMirrorNode;\n private readonly mathfield: MathfieldElement;\n private readonly display: MathDisplay;\n private selected = false;\n private editing = false;\n\n constructor(\n node: ProseMirrorNode,\n private readonly view: EditorView,\n private readonly getPos: GetPos\n ) {\n this.node = node;\n this.display = node.type.name === 'math_inline' ? 'inline' : 'block';\n this.dom = document.createElement(this.display === 'inline' ? 'span' : 'p');\n this.mathfield = new MathfieldElement();\n this.mathfield.className = `lunar-math-field lunar-math-field--${this.display}`;\n this.mathfield.smartFence = true;\n this.mathfield.mathVirtualKeyboardPolicy = 'auto';\n this.mathfield.setAttribute(\n 'aria-label',\n this.display === 'inline' ? 'Inline math formula' : 'Math formula'\n );\n this.mathfield.addEventListener('change', this.handleChange);\n this.mathfield.addEventListener('focus', this.handleFocus);\n this.mathfield.addEventListener('blur', this.handleBlur);\n this.mathfield.addEventListener('keydown', this.handleKeyDown);\n this.dom.addEventListener('pointerdown', this.handlePointerDown, true);\n this.dom.appendChild(this.mathfield);\n this.update(node);\n }\n\n private handlePointerDown = () => {\n if (!this.view.editable) {\n this.setEditing(false);\n return;\n }\n\n this.setEditing(true);\n\n const position = this.getPos();\n if (position === undefined) {\n return;\n }\n\n const selection = this.view.state.selection;\n if (!(selection instanceof NodeSelection) || selection.from !== position) {\n this.view.dispatch(\n this.view.state.tr.setSelection(\n NodeSelection.create(this.view.state.doc, position)\n )\n );\n }\n\n window.requestAnimationFrame(() => {\n if (this.editing && this.view.editable) {\n this.mathfield.focus();\n }\n });\n };\n\n private handleFocus = () => {\n this.setEditing(true);\n };\n\n private handleBlur = () => {\n this.setEditing(false);\n };\n\n private setEditing(editing: boolean) {\n this.editing = editing && this.view.editable;\n this.dom.classList.toggle('lunar-math-node--editing', this.editing);\n this.mathfield.readOnly = !this.view.editable;\n }\n\n private handleChange = () => {\n const position = this.getPos();\n if (position === undefined || !this.view.editable) {\n return;\n }\n\n const currentNode = this.view.state.doc.nodeAt(position);\n const latex = this.mathfield.value;\n if (\n currentNode?.type !== this.node.type ||\n currentNode.attrs.latex === latex\n ) {\n return;\n }\n\n this.view.dispatch(\n this.view.state.tr.setNodeMarkup(position, undefined, {\n ...currentNode.attrs,\n latex,\n })\n );\n };\n\n private handleKeyDown = (event: KeyboardEvent) => {\n if (\n (event.key === 'Backspace' || event.key === 'Delete') &&\n this.mathfield.value.trim() === ''\n ) {\n event.preventDefault();\n event.stopPropagation();\n this.deleteEmptyBlock(event.key === 'Backspace' ? -1 : 1);\n return;\n }\n\n if (event.key !== 'Escape') {\n return;\n }\n\n event.preventDefault();\n event.stopPropagation();\n this.mathfield.blur();\n this.view.focus();\n };\n\n private deleteEmptyBlock(direction: -1 | 1) {\n const position = this.getPos();\n if (position === undefined || !this.view.editable) {\n return;\n }\n\n const currentNode = this.view.state.doc.nodeAt(position);\n if (currentNode?.type !== this.node.type) {\n return;\n }\n\n const transaction = this.view.state.tr.delete(\n position,\n position + currentNode.nodeSize\n );\n const selectionPosition = Math.min(position, transaction.doc.content.size);\n transaction.setSelection(\n Selection.near(transaction.doc.resolve(selectionPosition), direction)\n );\n this.view.dispatch(transaction);\n\n window.requestAnimationFrame(() => {\n this.view.focus();\n });\n }\n\n update(node: ProseMirrorNode) {\n if (node.type.name !== `math_${this.display}`) {\n return false;\n }\n\n this.node = node;\n updateHtmlAttributes(\n this.dom,\n mathNodeAttributes(node, this.selected, this.editing, this.display)\n );\n this.mathfield.readOnly = !this.view.editable;\n\n const latex = node.attrs.latex || '';\n if (this.mathfield.value !== latex) {\n this.mathfield.setValue(latex, { silenceNotifications: true });\n }\n return true;\n }\n\n selectNode() {\n this.selected = true;\n this.dom.classList.add('ProseMirror-selectednode');\n if (!this.view.editable) {\n this.setEditing(false);\n return;\n }\n\n this.setEditing(true);\n\n window.requestAnimationFrame(() => {\n this.mathfield.focus();\n });\n }\n\n deselectNode() {\n this.selected = false;\n this.dom.classList.remove('ProseMirror-selectednode');\n this.setEditing(false);\n }\n\n stopEvent(event: Event) {\n return event.target instanceof Node && this.dom.contains(event.target);\n }\n\n ignoreMutation() {\n return true;\n }\n\n destroy() {\n this.mathfield.removeEventListener('change', this.handleChange);\n this.mathfield.removeEventListener('focus', this.handleFocus);\n this.mathfield.removeEventListener('blur', this.handleBlur);\n this.mathfield.removeEventListener('keydown', this.handleKeyDown);\n this.dom.removeEventListener('pointerdown', this.handlePointerDown, true);\n }\n}\n","import {\n NodeSelection,\n Plugin,\n type EditorView,\n} from '@mache-hangar/lunar-editor-core/pm';\n\nfunction getParagraphTextWithInput(\n view: EditorView,\n from: number,\n to: number,\n text: string\n) {\n const resolvedFrom = view.state.doc.resolve(from);\n const resolvedTo = view.state.doc.resolve(to);\n\n if (\n resolvedFrom.parent !== resolvedTo.parent ||\n resolvedFrom.parent.type !== view.state.schema.nodes.paragraph\n ) {\n return undefined;\n }\n\n const paragraph = resolvedFrom.parent;\n const before = paragraph.textBetween(0, resolvedFrom.parentOffset, '', '');\n const after = paragraph.textBetween(\n resolvedTo.parentOffset,\n paragraph.content.size,\n '',\n ''\n );\n\n return {\n paragraph,\n text: `${before}${text}${after}`,\n paragraphStart: resolvedFrom.before(),\n paragraphEnd: resolvedFrom.after(),\n };\n}\n\nexport function insertBlockMathFromDollarInput(\n view: EditorView,\n from: number,\n to: number,\n text: string\n) {\n if (!text.includes('$')) {\n return false;\n }\n\n const match = getParagraphTextWithInput(view, from, to, text);\n const mathBlock = view.state.schema.nodes.math_block;\n if (!match || match.text !== '$$' || !mathBlock) {\n return false;\n }\n\n const htmlAttrs = { ...(match.paragraph.attrs.htmlAttrs || {}) };\n delete htmlAttrs['data-type'];\n delete htmlAttrs['data-display'];\n delete htmlAttrs['data-latex'];\n\n const transaction = view.state.tr.replaceWith(\n match.paragraphStart,\n match.paragraphEnd,\n mathBlock.create({\n htmlAttrs,\n latex: '',\n sourceKey: match.paragraph.attrs.sourceKey || null,\n })\n );\n transaction.setSelection(\n NodeSelection.create(transaction.doc, match.paragraphStart)\n );\n view.dispatch(transaction.scrollIntoView());\n return true;\n}\n\nexport function insertInlineMathFromDotDollarInput(\n view: EditorView,\n from: number,\n to: number,\n text: string\n) {\n if (text !== '$' || from !== to) {\n return false;\n }\n\n const resolvedFrom = view.state.doc.resolve(from);\n if (\n resolvedFrom.parent.type !== view.state.schema.nodes.paragraph ||\n resolvedFrom.parentOffset < 2\n ) {\n return false;\n }\n\n const triggerStartOffset = resolvedFrom.parentOffset - 2;\n const triggerPrefix = resolvedFrom.parent.textBetween(\n triggerStartOffset,\n resolvedFrom.parentOffset,\n '',\n ''\n );\n const mathInline = view.state.schema.nodes.math_inline;\n if (triggerPrefix !== '$.' || !mathInline) {\n return false;\n }\n\n const triggerStart = from - 2;\n const transaction = view.state.tr.replaceWith(\n triggerStart,\n to,\n mathInline.create({ htmlAttrs: {}, latex: '' })\n );\n transaction.setSelection(NodeSelection.create(transaction.doc, triggerStart));\n view.dispatch(transaction.scrollIntoView());\n return true;\n}\n\nexport function createMathInputPlugin() {\n return new Plugin({\n props: {\n handleTextInput: (view, from, to, text) =>\n insertInlineMathFromDotDollarInput(view, from, to, text) ||\n insertBlockMathFromDollarInput(view, from, to, text),\n },\n });\n}\n","import type { EditorExtension } from '@mache-hangar/lunar-editor-core/extensions';\nimport { mathNodes } from './nodes';\nimport { MathNodeView } from './math-node-view';\nimport { createMathInputPlugin } from './math-input';\nexport * from './math-input';\nexport function createMathExtension(): EditorExtension {\n return {\n name: 'math',\n nodes: {\n math_block: { ...mathNodes.math_block, sourceUnit: true },\n math_inline: { ...mathNodes.math_inline, sourceUnit: true },\n },\n plugins: () => [createMathInputPlugin()],\n nodeViews: {\n math_block: (node, view, getPos) => new MathNodeView(node, view, getPos),\n math_inline: (node, view, getPos) => new MathNodeView(node, view, getPos),\n },\n };\n}\n"],"names":["parseMathAttrs","element","htmlAttrs","readHtmlAttributes","getDomSourceKey","mathNodes","node","MathfieldElement","updateHtmlAttributes","attributes","attribute","name","value","mathNodeAttributes","selected","editing","display","classNames","MathNodeView","view","getPos","position","selection","NodeSelection","currentNode","latex","event","direction","transaction","selectionPosition","Selection","getParagraphTextWithInput","from","to","text","resolvedFrom","resolvedTo","paragraph","before","after","insertBlockMathFromDollarInput","match","mathBlock","insertInlineMathFromDotDollarInput","triggerStartOffset","triggerPrefix","mathInline","triggerStart","createMathInputPlugin","Plugin","createMathExtension"],"mappings":";;;;AAGO,SAASA,EAAeC,GAAsB;AACnD,QAAMC,IAAYC,EAAmBF,CAAO;AAC5C,gBAAOC,EAAU,WAAW,GAC5B,OAAOA,EAAU,cAAc,GAC/B,OAAOA,EAAU,YAAY,GAEtB;AAAA,IACL,WAAAA;AAAA,IACA,OAAOD,EAAQ,QAAQ,SAAS;AAAA,IAChC,WAAWG,EAAgBH,CAAO,KAAK;AAAA,EAAA;AAE3C;AAEO,MAAMI,IAAsC;AAAA,EACjD,YAAY;AAAA,IACV,OAAO;AAAA,MACL,WAAW,EAAE,SAAS,GAAC;AAAA,MACvB,OAAO,EAAE,SAAS,GAAA;AAAA,MAClB,WAAW,EAAE,SAAS,KAAA;AAAA,IAAK;AAAA,IAE7B,MAAM;AAAA,IACN,OAAO;AAAA,IACP,WAAW;AAAA,IACX,UAAU;AAAA,MACR;AAAA,QACE,KAAK;AAAA,QACL,UAAU;AAAA,QACV,UAAU,CA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
1
|
+
@font-face{font-display:"swap";font-family:KaTeX_AMS;font-style:normal;font-weight:400;src:url(fonts/KaTeX_AMS-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Caligraphic;font-style:normal;font-weight:700;src:url(fonts/KaTeX_Caligraphic-Bold.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Caligraphic;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Caligraphic-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Fraktur;font-style:normal;font-weight:700;src:url(fonts/KaTeX_Fraktur-Bold.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Fraktur;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Fraktur-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Main;font-style:italic;font-weight:700;src:url(fonts/KaTeX_Main-BoldItalic.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Main;font-style:normal;font-weight:700;src:url(fonts/KaTeX_Main-Bold.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Main;font-style:italic;font-weight:400;src:url(fonts/KaTeX_Main-Italic.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Main;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Main-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Math;font-style:italic;font-weight:700;src:url(fonts/KaTeX_Math-BoldItalic.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Math;font-style:italic;font-weight:400;src:url(fonts/KaTeX_Math-Italic.woff2) format("woff2")}@font-face{font-display:"swap";font-family:"KaTeX_SansSerif";font-style:normal;font-weight:700;src:url(fonts/KaTeX_SansSerif-Bold.woff2) format("woff2")}@font-face{font-display:"swap";font-family:"KaTeX_SansSerif";font-style:italic;font-weight:400;src:url(fonts/KaTeX_SansSerif-Italic.woff2) format("woff2")}@font-face{font-display:"swap";font-family:"KaTeX_SansSerif";font-style:normal;font-weight:400;src:url(fonts/KaTeX_SansSerif-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Script;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Script-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Size1;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Size1-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Size2;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Size2-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Size3;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Size3-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Size4;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Size4-Regular.woff2) format("woff2")}@font-face{font-display:"swap";font-family:KaTeX_Typewriter;font-style:normal;font-weight:400;src:url(fonts/KaTeX_Typewriter-Regular.woff2) format("woff2")}:root{--ML__static-fonts:true}.lunar-editor__document .lunar-math-block{box-sizing:border-box;display:flex;min-height:52px;align-items:center;justify-content:center;padding:8px 12px;border:1px solid rgba(0,0,0,0);border-radius:6px;white-space:normal}.lunar-editor__document .lunar-math-block:hover,.lunar-editor__document .lunar-math-block.ProseMirror-selectednode{border-color:#84a9ff;outline:none}.lunar-editor__document .lunar-math-inline{box-sizing:border-box;display:inline-flex;align-items:center;padding:0 2px;border:1px solid rgba(0,0,0,0);border-radius:4px;vertical-align:middle;white-space:normal}.lunar-editor__document .lunar-math-inline:hover,.lunar-editor__document .lunar-math-inline.ProseMirror-selectednode{border-color:#84a9ff;outline:none}.lunar-editor__document .lunar-math-field{display:inline-block;min-width:120px;max-width:100%;padding:4px 8px;border:0;background:rgba(0,0,0,0);font-size:1.1em;outline:none}.lunar-editor__document .lunar-math-field--inline{min-width:1.5em;padding:0 2px;font-size:1em}.lunar-editor__document .lunar-math-node:not(.lunar-math-node--editing) .lunar-math-field::part(virtual-keyboard-toggle),.lunar-editor__document .lunar-math-node:not(.lunar-math-node--editing) .lunar-math-field::part(menu-toggle){display:none}.lunar-editor__document .lunar-math-node.lunar-math-node--editing .lunar-math-field::part(virtual-keyboard-toggle),.lunar-editor__document .lunar-math-node.lunar-math-node--editing .lunar-math-field::part(menu-toggle){display:flex !important}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("@mache-hangar/lunar-editor-core/pm"),G=require("@mache-hangar/lunar-editor-core/source/compile-transaction"),P=require("@mache-hangar/lunar-editor-core/source/source-map-plugin"),J=require("@mache-hangar/lunar-editor-core/source/source-mutation-step"),k=48;function K(s){const t=Array.from(s.rows),e=[],o=[],n=s.ownerDocument.defaultView.getComputedStyle(s).direction==="rtl",r=n?-1:1;let l=0;const h=(g,m)=>(o[g]??=[]).push(m),i=new Map;t.forEach((g,m)=>i.set(g.parentElement,m+1)),t.forEach((g,m)=>{let a=0;for(const c of Array.from(g.cells)){for(;e[m]?.[a];)a++;const p=c.colSpan,w=i.get(g.parentElement),x=c.rowSpan||w-m;for(let E=m;E<Math.min(m+x,w);E++)for(let D=a;D<a+p;D++)(e[E]??=[])[D]=!0;const v=c.getBoundingClientRect();h(a,n?v.right:v.left),h(a+p,n?v.left:v.right),a+=p,l=Math.max(l,a)}});let d=0;for(const g of Array.from(s.children).filter(m=>m.tagName==="COLGROUP")){const m=Array.from(g.children);for(const a of m.length?m:[g]){const c=a.getBoundingClientRect();c.width>0&&(h(d,n?c.right:c.left),h(d+a.span,n?c.left:c.right)),d+=a.span}}if(l=Math.max(l,d),l<2||s.getBoundingClientRect().width<=0)return;const u=s.getBoundingClientRect();o[0]??=[n?u.right:u.left],o[l]??=[n?u.left:u.right];const f=Array.from({length:l+1},(g,m)=>{const a=o[m];return a?a.reduce((c,p)=>c+p,0)/a.length:NaN});let b=0;for(;b<l;){let g=b+1;for(;g<l&&!Number.isFinite(f[g]);)g++;for(let m=b+1;m<g;m++)f[m]=f[b]+(f[g]-f[b])*(m-b)/(g-b);b=g}const A=f.slice(1).map((g,m)=>Math.abs(g-f[m]));return A.every(g=>Number.isFinite(g)&&g>0)?{widths:A,boundaries:f,direction:r}:void 0}function H(s,t,e){const o=[...s],n=Math.min(k,s[t]),r=Math.min(k,s[t+1]),l=Math.max(n-s[t],Math.min(e,s[t+1]-r));return o[t]+=l,o[t+1]-=l,o}function B(s,t,e,o=!1){const n=e.createElement("span").style;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}function q(s,t,e){const o=s.state.doc.nodeAt(t);if(!s.editable||o?.type.name!=="table")return;const n=s.dom.ownerDocument,r=[];let l=t+1;o.forEach((a,c)=>{a.type.name==="caption"&&(l=t+1+c+a.nodeSize),a.type.name==="colgroup"&&r.push({node:a,position:t+1+c})});const{col:h,colgroup:i}=s.state.schema.nodes,d=[];let u=0;const f=(a,c=!1)=>{const{span:p,width:w,...x}=a?.attrs.htmlAttrs||{};return c||delete x.id,h.create({sourceKey:c?a?.attrs.sourceKey:null,htmlAttrs:{...x,style:B(x.style,e[u++],n)}})};for(const{node:a}of r){const c=[];if(a.forEach(E=>{const D=Math.min(1e3,Math.max(1,parseInt(E.attrs.htmlAttrs?.span,10)||1));for(let C=0;C<D&&u<e.length;C++)c.push(f(E,C===0))}),!a.childCount){const E=Math.min(1e3,Math.max(1,parseInt(a.attrs.htmlAttrs?.span,10)||1));for(let D=0;D<E&&u<e.length;D++)c.push(f())}const{span:p,width:w,...x}=a.attrs.htmlAttrs||{},v=n.createElement("span").style;v.cssText=x.style||"",v.removeProperty("width"),v.cssText?x.style=v.cssText:delete x.style,d.push(i.create({...a.attrs,htmlAttrs:x},c))}if(u<e.length){const a=[];for(;u<e.length;)a.push(f());if(d.length){const c=d[d.length-1];d[d.length-1]=c.copy(c.content.append(y.Fragment.fromArray(a)))}else d.push(i.create(null,a))}const b=r[0]?.position??l,A=r[r.length-1],g=A?A.position+A.node.nodeSize:b,m=s.state.tr.replaceWith(b,g,y.Fragment.fromArray(d));m.setNodeMarkup(t,void 0,{...o.attrs,htmlAttrs:{...o.attrs.htmlAttrs,style:B(o.attrs.htmlAttrs?.style,e.reduce((a,c)=>a+c,0),n,!0)}}),s.dispatch(y.closeHistory(m)),s.dispatch(y.closeHistory(s.state.tr))}function U(s,t){Array.from(s.attributes).forEach(e=>{e.name in t||s.removeAttribute(e.name)}),Object.entries(t).forEach(([e,o])=>{s.setAttribute(e,o)})}class Q{constructor(t,e,o){this.view=e,this.getPos=o;const n=e.dom.ownerDocument;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)}view;getPos;dom;contentDOM;controls;node;observer;frame=0;previewNodes=new WeakSet;drag;update(t){return t.type.name!=="table"?!1:(this.drag&&(t!==this.node||!this.view.editable)&&this.finishDrag(!1),this.node=t,this.drag||U(this.contentDOM,t.attrs.htmlAttrs||{}),this.dom.classList.toggle("lunar-table-scroll--fixed",this.contentDOM.style.tableLayout==="fixed"),this.controls.childElementCount&&this.scheduleLayout(),!0)}showControls=t=>{if(!this.view.editable||this.drag||this.controls.childElementCount||t.target.closest?.(".lunar-table-scroll")!==this.dom)return;this.refreshControls();const o=this.dom.ownerDocument.defaultView?.ResizeObserver;o&&!this.observer&&(this.observer=new o(this.scheduleLayout),this.observer.observe(this.contentDOM))};hideControls=t=>{if(this.drag)return;const e=t.relatedTarget;e&&this.dom.contains(e)||t.type==="pointerleave"&&this.controls.contains(this.dom.ownerDocument.activeElement)||(this.controls.replaceChildren(),this.observer?.disconnect(),this.observer=void 0)};scheduleLayout=()=>{this.frame||(this.frame=this.dom.ownerDocument.defaultView.requestAnimationFrame(()=>{this.frame=0,this.controls.childElementCount&&this.refreshControls()}))};refreshControls(){if(!this.view.editable){this.controls.replaceChildren();return}const t=this.contentDOM.getBoundingClientRect(),e=this.drag;let o;if(e){const h=[t.left+e.edgeOffset];e.widths.forEach(i=>h.push(h[h.length-1]+i*e.layout.direction)),o={widths:e.widths,boundaries:h,direction:e.layout.direction}}else o=K(this.contentDOM);if(!o){this.controls.replaceChildren();return}const n=o.widths.length-1;if(this.controls.childElementCount!==n){this.controls.replaceChildren();for(let h=0;h<n;h++){const i=this.dom.ownerDocument.createElement("div");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)}}const r=this.controls.getBoundingClientRect(),l=Math.max(t.top,this.contentDOM.rows[0]?.getBoundingClientRect().top??t.top);Array.from(this.controls.children).forEach((h,i)=>{const d=h;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("aria-valuenow",String(Math.round(o.widths[i]))),d.setAttribute("aria-valuemin",String(Math.min(k,Math.round(o.widths[i])))),d.setAttribute("aria-valuemax",String(Math.round(o.widths[i]+o.widths[i+1]-Math.min(k,o.widths[i+1]))))})}handleColumn(t){const e=t.target.closest(".lunar-table-column-handle");return e&&this.controls.contains(e)?Number(e.dataset.column):void 0}startDrag=t=>{const e=this.handleColumn(t),o=K(this.contentDOM);if(!this.view.editable||t.button!==0||e===void 0||!o||e>=o.widths.length-1||this.drag)return;t.preventDefault(),t.stopPropagation();const n=this.dom.ownerDocument,r=n.createElement("colgroup");this.previewNodes.add(r);const l=Array.from(this.contentDOM.children).filter(i=>i.tagName==="COLGROUP").map(i=>({node:i,next:i.nextSibling}));this.drag={column:e,pointerId:t.pointerId,startX:t.clientX,layout:o,edgeOffset:o.boundaries[0]-this.contentDOM.getBoundingClientRect().left,widths:o.widths,document:this.view.state.doc,preview:r,originals:l,style:this.contentDOM.getAttribute("style")};for(const{node:i}of l)this.previewNodes.add(i),i.remove();o.widths.forEach(i=>{const d=n.createElement("col");d.style.width=`${i}px`,this.previewNodes.add(d),r.append(d)});const h=this.contentDOM.caption;this.contentDOM.insertBefore(r,h?h.nextSibling:this.contentDOM.firstChild),this.contentDOM.style.cssText=B(this.drag.style||"",o.widths.reduce((i,d)=>i+d,0),n,!0),this.dom.classList.add("lunar-table-scroll--fixed","lunar-table-scroll--resizing"),this.controls.children[e]?.classList.add("lunar-table-column-handle--active"),n.addEventListener("pointermove",this.moveDrag,!0),n.addEventListener("pointerup",this.endDrag,!0),n.addEventListener("pointercancel",this.cancelDrag,!0),n.addEventListener("keydown",this.onDragKeyDown,!0),n.defaultView?.addEventListener("blur",this.cancelDrag),this.scheduleLayout()};moveDrag=t=>{const e=this.drag;if(!(!e||t.pointerId!==e.pointerId)){if(!this.view.editable||this.view.state.doc!==e.document){this.finishDrag(!1);return}t.preventDefault(),e.widths=H(e.layout.widths,e.column,(t.clientX-e.startX)*e.layout.direction);for(const o of[e.column,e.column+1])e.preview.children[o].style.width=`${e.widths[o]}px`;this.scheduleLayout()}};endDrag=t=>{t.pointerId===this.drag?.pointerId&&(this.moveDrag(t),this.finishDrag(!0))};cancelDrag=()=>this.finishDrag(!1);onDragKeyDown=t=>{t.key==="Escape"&&(t.preventDefault(),t.stopPropagation(),this.finishDrag(!1))};finishDrag(t){const e=this.drag;if(!e)return;this.drag=void 0;const o=this.dom.ownerDocument;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("lunar-table-column-handle--active"),this.dom.classList.toggle("lunar-table-scroll--fixed",this.contentDOM.style.tableLayout==="fixed");const n=this.getPos();t&&this.view.editable&&this.view.state.doc===e.document&&n!==void 0&&e.widths.some((r,l)=>Math.abs(r-e.layout.widths[l])>=.5)&&q(this.view,n,e.widths),this.scheduleLayout()}onHandleKeyDown=t=>{const e=this.handleColumn(t),o=K(this.contentDOM),n=this.getPos();if(!this.view.editable||e===void 0||!o||e>=o.widths.length-1||n===void 0||!["ArrowLeft","ArrowRight"].includes(t.key))return;t.preventDefault();const r=(t.key==="ArrowRight"?10:-10)*o.direction,l=H(o.widths,e,r);l[e]!==o.widths[e]&&q(this.view,n,l),this.scheduleLayout()};stopEvent(t){return this.controls.contains(t.target)}ignoreMutation(t){return t.type==="selection"?!1:!this.contentDOM.contains(t.target)||t.type==="attributes"&&t.target===this.contentDOM||this.previewNodes.has(t.target)?!0:t.type==="childList"&&t.target===this.contentDOM&&[...Array.from(t.addedNodes),...Array.from(t.removedNodes)].every(e=>this.previewNodes.has(e))}destroy(){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)}}const Z=s=>["table_cell","table_header"].includes(s.type.name),N=s=>Math.min(100,Math.max(1,Number.parseInt(String(s),10)||1));function T(s){for(let t=s.depth;t>0;t--)if(s.node(t).type.name==="table")return{node:s.node(t),pos:s.before(t)}}function V(s,t){const e=[];if(s.forEach((h,i,d)=>{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}))}),!e.length||e.length>200)return;const o=[],n=[];let r=0,l=!0;if(e.forEach((h,i)=>{let d=0;h.node.forEach((u,f)=>{if(!Z(u)){l=!1;return}for(;o[i]?.[d];)d++;const b=e.findIndex((c,p)=>p>i&&c.section!==h.section),A=b<0?e.length:b;if(Number(u.attrs.htmlAttrs?.colspan)>100||Number(u.attrs.htmlAttrs?.rowspan)>100){l=!1;return}const g=N(u.attrs.htmlAttrs?.colspan),m=u.attrs.htmlAttrs?.rowspan==="0"?A-i:N(u.attrs.htmlAttrs?.rowspan);if(i+m>A||d+g>100){l=!1;return}const a={node:u,row:i,col:d,rows:m,cols:g,pos:h.pos+1+f};n.push(a);for(let c=i;c<i+m;c++)for(let p=d;p<d+g;p++)o[c]??=[],o[c][p]&&(l=!1),o[c][p]=a;d+=g,r=Math.max(r,d)})}),!(!l||!r||e.some((h,i)=>Array.from({length:r},(d,u)=>!o[i]?.[u]).some(Boolean))))return{node:s,pos:t,rows:e,cells:n,matrix:o,width:r}}function _(s,t){return s.cells.find(e=>t>=e.pos&&t<e.pos+e.node.nodeSize)}function tt(s,t,e){const o={top:Math.min(t.row,e.row),bottom:Math.max(t.row+t.rows,e.row+e.rows),left:Math.min(t.col,e.col),right:Math.max(t.col+t.cols,e.col+e.cols)};let n=!0;for(;n;){n=!1;for(const r of s.cells)if($(r,o)){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)];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)}}return o}function $(s,t){return s.row<t.bottom&&s.row+s.rows>t.top&&s.col<t.right&&s.col+s.cols>t.left}function z(s,t,e){const o={...s.attrs.htmlAttrs};for(const n of["id","rowspan","colspan","headers"])delete o[n];return{node:s.type.createAndFill({...s.attrs,sourceKey:null,htmlAttrs:o}),row:t,col:e,rows:1,cols:1,pos:-1}}function et(s,t,e,o){const n=t.map((l,h)=>{const i=e.filter(u=>u.row===h).sort((u,f)=>u.col-f.col).map(u=>{const f={...u.node.attrs.htmlAttrs};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)}),d=y.Fragment.fromArray(i);return l.node.content.eq(d)?l.node:l.node.copy(d)}),r=[];return s.node.forEach((l,h,i)=>{if(l.type.name==="colgroup"&&o){const d=o.filter(f=>f.group===i),u={...l.attrs.htmlAttrs};delete u.span,d.length&&r.push(l.type.create({...l.attrs,htmlAttrs:u},d.map(f=>f.node)))}else if(["table_head","table_body","table_foot"].includes(l.type.name)){const d=n.filter((u,f)=>t[f].section===i);d.length&&r.push(l.copy(y.Fragment.fromArray(d)))}else l.type.name!=="table_row"&&r.push(l)}),r.push(...n.filter((l,h)=>t[h].section===-1)),s.node.copy(y.Fragment.fromArray(r))}function W(s,t,e,o){const n=[];if(s.node.forEach((r,l,h)=>{if(r.type.name!=="colgroup")return;const i={...r.attrs.htmlAttrs};if(!r.childCount)for(let d=0;d<N(i.span);d++)n.push({node:r.type.schema.nodes.col.create({htmlAttrs:{},sourceKey:null}),group:h});r.forEach(d=>{for(let u=0;u<N(d.attrs.htmlAttrs?.span);u++){const f={...d.attrs.htmlAttrs};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})}})}),!!n.length){for(;n.length<s.width;)n.push({node:s.node.type.schema.nodes.col.create(),group:n[n.length-1].group});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}}const M=new y.PluginKey("lunar-table-selection"),O={available:!1,inTable:!1,valid:!1,inCell:!1,rows:0,columns:0,row:0,column:0,selectedCells:0,canMerge:!1,canSplit:!1,headerRow:!1,headerColumn:!1,caption:!1,textAlign:"left",verticalAlign:"top",backgroundColor:"#ffffff"};function S(s){const t=M.getState(s),e=s.selection instanceof y.NodeSelection&&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);if(!o||!t&&!e&&T(s.selection.$head)?.pos!==o.pos)return;const n=V(o.node,o.pos);if(!n)return{table:o};const r=_(n,t?.anchor??s.selection.anchor),l=r||n.cells[0],h=_(n,t?.head??s.selection.head)||l,i=e?{top:0,bottom:n.rows.length,left:0,right:n.width}:tt(n,l,h);return{table:o,grid:n,rect:i,inCell:!!(e||r),cells:n.cells.filter(d=>$(d,i))}}function L(s){if(!s)return O;const t=s.editable&&(!P.getLunarSourceState(s.state)||P.getLunarSourceState(s.state)?.status==="synchronized"),e=S(s.state);if(!e)return{...O,available:t};if(!e.grid)return{...O,available:t,inTable:!0};const{grid:o,rect:n,cells:r}=e,l=o.rows[n.top].section,h=s.dom.ownerDocument.createElement("span").style;return h.cssText=r[0].node.attrs.htmlAttrs?.style||"",{available:t,inTable:!0,valid:!0,inCell:e.inCell,rows:o.rows.length,columns:o.width,row:n.top+1,column:n.left+1,selectedCells:r.length,canMerge:r.length>1&&o.rows.slice(n.top,n.bottom).every(i=>i.section===l),canSplit:r.some(i=>i.rows>1||i.cols>1),headerRow:o.cells.filter(i=>i.row<n.bottom&&i.row+i.rows>n.top).every(i=>i.node.type.name==="table_header"),headerColumn:o.cells.filter(i=>i.col<n.right&&i.col+i.cols>n.left).every(i=>i.node.type.name==="table_header"),caption:Array.from({length:o.node.childCount},(i,d)=>o.node.child(d)).some(i=>i.type.name==="caption"),textAlign:h.textAlign||"left",verticalAlign:h.verticalAlign||"top",backgroundColor:h.backgroundColor||"#ffffff"}}function I(s,t){if(!t.docChanged)return!1;const e=P.getLunarSourceState(s.state),o=G.compileLunarSourceTransaction(s.state,y.closeHistory(t));return e&&!J.getSourceMutationSteps(o.steps).length?!1:(s.dispatch(o.scrollIntoView()),s.dispatch(y.closeHistory(s.state.tr)),s.focus(),!0)}function F(s,t,e){const o=s.state.doc.resolve(e),n=o.parent.canReplace(o.index(),o.index()+1)?y.Fragment.empty:y.Fragment.from(s.state.schema.nodes.paragraph.createAndFill());return I(s,s.state.tr.replaceWith(e,e+t.nodeSize,n))}function j(s,t,e){const o=e.createElement("span").style;o.cssText=s.attrs.htmlAttrs?.style||"";for(const[n,r]of Object.entries(t))o.setProperty(n,r);return s.type.create({...s.attrs,htmlAttrs:{...s.attrs.htmlAttrs,style:o.cssText}},s.content)}function ot(s,t,e,o){const n=e?t.bottom:t.top,r=s.rows[e?t.bottom-1:t.top],l={...r.node.attrs.htmlAttrs};delete l.id;const h={node:r.node.type.create({...r.node.attrs,sourceKey:null,htmlAttrs:l}),section:r.section,pos:-1};for(const d of o)d.row>=n?d.row++:d.row+d.rows>n&&d.rows++;for(let d=0;d<s.width;d++)o.some(u=>u.row<n&&u.row+u.rows>n&&d>=u.col&&d<u.col+u.cols)||o.push(z(s.matrix[e?t.bottom-1:t.top][d].node,n,d));const i=[...s.rows];return i.splice(n,0,h),{rows:i,focusRow:n}}function R(s,t,e){if(!s||!L(s).available)return!1;try{if(t.action==="insert"){const{rows:a,columns:c}=t;if(![a,c].every(D=>Number.isInteger(D)&&D>=1&&D<=50))return!1;const p=s.state.schema,w=Array.from({length:a},(D,C)=>p.nodes.table_row.create(null,Array.from({length:c},()=>(t.headerRow&&C===0?p.nodes.table_header:p.nodes.table_cell).createAndFill({htmlAttrs:t.headerRow&&C===0?{scope:"col"}:{}})))),x=p.nodes.table.create({htmlAttrs:{style:"width: 100%;"}},p.nodes.table_body.create(null,w)),v=s.state.tr.replaceSelectionWith(x);let E=-1;return v.doc.descendants((D,C)=>{E<0&&D===x&&(E=C)}),E>=0&&v.setSelection(y.TextSelection.near(v.doc.resolve(E+4))),I(s,v)}const o=S(s.state);if(!o)return!1;if(t.action==="delete_table")return F(s,o.table.node,o.table.pos);if(!o.grid||!o.rect||!o.cells||!o.inCell&&!["caption","table_style"].includes(t.action))return!1;const{grid:n,rect:r}=o;let l=n.cells.map(a=>({...a})),h=[...n.rows],i,d=r.top,u=r.left;const f=a=>$(a,r);if(t.action==="row_before"||t.action==="row_after"){if(h.length>=200)return!1;({rows:h,focusRow:d}=ot(n,r,t.action==="row_after",l))}else if(t.action==="delete_rows"){if(r.bottom-r.top===h.length)return F(s,n.node,n.pos);const a=r.bottom-r.top;h.splice(r.top,a),l=l.filter(c=>{const p=Math.max(0,Math.min(c.row+c.rows,r.bottom)-Math.max(c.row,r.top));return p===c.rows?!1:(c.row>=r.bottom?c.row-=a:c.row>=r.top&&(c.row=r.top),c.rows-=p,!0)})}else if(t.action==="column_before"||t.action==="column_after"){if(n.width>=100)return!1;const a=t.action==="column_after"?r.right:r.left;for(const c of l)c.col>=a?c.col++:c.col+c.cols>a&&c.cols++;for(let c=0;c<h.length;c++)l.some(p=>p.col<a&&p.col+p.cols>a&&c>=p.row&&c<p.row+p.rows)||l.push(z(n.matrix[c][Math.min(r.left,n.width-1)].node,c,a));i=W(n,a,1,!1),u=a}else if(t.action==="delete_columns"){if(r.right-r.left===n.width)return F(s,n.node,n.pos);const a=r.right-r.left;l=l.filter(c=>{const p=Math.max(0,Math.min(c.col+c.cols,r.right)-Math.max(c.col,r.left));return p===c.cols?!1:(c.col>=r.right?c.col-=a:c.col>=r.left&&(c.col=r.left),c.cols-=p,!0)}),i=W(n,r.left,a,!0)}else if(t.action==="merge"){if(!L(s).canMerge)return!1;const a=l.filter(f),c=a[0],p=a.reduce((w,x)=>w.append(x.node.content),y.Fragment.empty);c.node=c.node.copy(p),c.rows=r.bottom-r.top,c.cols=r.right-r.left,l=l.filter(w=>w===c||!a.includes(w))}else if(t.action==="split")for(const a of[...l].filter(f)){const{rows:c,cols:p}=a;for(let w=0;w<c;w++)for(let x=0;x<p;x++)(w||x)&&l.push(z(a.node,a.row+w,a.col+x));a.rows=1,a.cols=1}else if(t.action==="header_row"||t.action==="header_column"){const a=t.action==="header_row",c=a?!L(s).headerRow:!L(s).headerColumn;for(const p of l.filter(w=>a?w.row<r.bottom&&w.row+w.rows>r.top:w.col<r.right&&w.col+w.cols>r.left)){const w={...p.node.attrs.htmlAttrs};c?w.scope=a?"col":"row":delete w.scope;const x=s.state.schema.nodes[c?"table_header":"table_cell"];p.node=x.create({...p.node.attrs,htmlAttrs:w},p.node.content)}}else if(t.action==="clear_cells"){let a=!0;for(const c of l.filter(f))c.node=c.node.copy(y.Fragment.from(s.state.schema.nodes.paragraph.create(null,a&&e?s.state.schema.text(e):void 0))),a=!1}else if(t.action==="cell_style"){const a={};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;for(const c of l.filter(f))c.node=j(c.node,a,s.dom.ownerDocument)}let b=et(n,h,l,i);if(t.action==="caption"){const a=[];b.forEach(c=>{c.type.name!=="caption"&&a.push(c)}),L(s).caption||a.unshift(s.state.schema.nodes.caption.createAndFill(null,s.state.schema.nodes.paragraph.create(null,s.state.schema.text("Table caption")))),b=b.copy(y.Fragment.fromArray(a))}else if(t.action==="table_style"){const a={};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;b=j(b,a,s.dom.ownerDocument)}if(b.eq(n.node))return!1;const A=s.state.tr.replaceWith(n.pos,n.pos+n.node.nodeSize,b),g=V(b,n.pos);if(!g)return!1;const m=g?.matrix[Math.min(d,g.rows.length-1)]?.[Math.min(u,g.width-1)];if(m&&A.setSelection(y.TextSelection.near(A.doc.resolve(m.pos+1))),o.cells.length>1&&["cell_style","table_style","header_row","header_column","caption"].includes(t.action)){const a=g.matrix[r.top][r.left],c=g.matrix[r.bottom-1][r.right-1];A.setMeta(M,{anchor:a.pos+1,head:c.pos+1})}return I(s,A)}catch{return!1}}function X(s,t){const e=t?.closest?.("td, th");if(!e||!s.dom.contains(e))return;const o=s.posAtDOM(e,0),n=T(s.state.doc.resolve(o)),r=n&&V(n.node,n.pos),l=r&&_(r,o),h=e.closest("table");return r&&l&&h?{grid:r,cell:l,tableElement:h}:void 0}function st(s,t,e){const o=s.state.doc.resolve(t);if(e>=3&&o.parent.inlineContent)return y.TextSelection.create(s.state.doc,o.start(),o.end());if(e===2&&o.parent.inlineContent){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);if(l){let h=0,i,d;if(o.parent.descendants((u,f)=>{if(!u.isText&&!u.isLeaf)return;const b=u.isText?u.text.length:1;i===void 0&&l.index<h+b&&(i=o.start()+f+Math.max(0,l.index-h));const A=l.index+l.segment.length;d===void 0&&A<=h+b&&(d=o.start()+f+Math.max(0,A-h)),h+=b}),i!==void 0&&d!==void 0)return y.TextSelection.create(s.state.doc,i,d)}}return y.TextSelection.near(o)}class nt{drag;get active(){return!!this.drag?.view.editable}start(t,e){if(this.cancel(),!t.editable||e.button!==0||e.shiftKey||e.ctrlKey||e.metaKey||e.altKey)return!1;const o=X(t,e.target);if(!o)return!1;const n=t.posAtCoords({left:e.clientX,top:e.clientY})?.pos,r=n!==void 0&&_(o.grid,n)?.pos===o.cell.pos?n:o.cell.pos+1,l=st(t,r,e.detail);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();const h=t.dom.ownerDocument;return h.addEventListener("mousemove",this.move,!0),h.addEventListener("mouseup",this.end,!0),h.defaultView?.addEventListener("blur",this.cancel),!0}cancelIfStale(t){this.drag&&(this.drag.doc!==t.state.doc||!t.editable)&&this.destroy()}point(t,e){const o=e.tableElement.getBoundingClientRect(),n=e.view.dom.getBoundingClientRect(),r=e.view.dom.ownerDocument.defaultView;return{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)),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))}}move=t=>{const e=this.drag;if(!e)return;if(!(t.buttons&1)){this.end();return}if(e.doc!==e.view.state.doc||!e.view.editable){this.cancel();return}t.preventDefault();const o=this.point(t,e),r=e.view.dom.ownerDocument.elementFromPoint?.(o.left,o.top)??t.target,l=X(e.view,r);if(!l||l.grid.pos!==e.grid.pos)return;const{view:h,anchor:i}=e;l.cell.pos!==i.pos&&(e.selecting=!0),e.head=l.cell;let d;if(e.selecting){const f=i.pos<=l.cell.pos;d=y.TextSelection.between(h.state.doc.resolve(i.pos+(f?1:i.node.nodeSize-1)),h.state.doc.resolve(l.cell.pos+(f?l.cell.node.nodeSize-1:1)))}else{const f=h.posAtCoords(o)?.pos;if(f===void 0||_(e.grid,f)?.pos!==i.pos)return;d=y.TextSelection.between(h.state.doc.resolve(f<e.textFrom?e.textTo:e.textFrom),h.state.doc.resolve(f))}const u=M.getState(h.state);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}))};end=()=>{const t=this.drag;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))};cancel=()=>{const t=this.drag;if(this.destroy(),!t||t.doc!==t.view.state.doc||t.view.isDestroyed)return;const e=M.getState(t.view.state);e?.dragging&&t.view.dispatch(t.view.state.tr.setMeta(M,{...e,dragging:!1}))};destroy=()=>{const t=this.drag?.view.dom.ownerDocument;t?.removeEventListener("mousemove",this.move,!0),t?.removeEventListener("mouseup",this.end,!0),t?.defaultView?.removeEventListener("blur",this.cancel),this.drag=void 0}}function Y(s,t){s.dispatch(s.state.tr.setSelection(y.TextSelection.near(s.state.doc.resolve(t+1)))),s.focus()}function rt(s){const t=new nt;return new y.Plugin({key:M,state:{init:()=>null,apply(e,o,n){const r=e.getMeta(M);return r!==void 0?r:e.docChanged||e.selectionSet&&!t.active&&!e.selection.eq(n.selection)?null:o}},props:{attributes(e){const o=[];return(S(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(" ")}},decorations(e){const o=S(e);return!o?.cells||o.cells.length<2?null:y.DecorationSet.create(e.doc,o.cells.map(n=>y.Decoration.node(n.pos,n.pos+n.node.nodeSize,{class:"lunar-table-cell--selected"})))},handleDOMEvents:{keydown(e,o){t.cancel();const n=lt(e,o);return n&&o.preventDefault(),n},mousedown(e,o){if(t.cancel(),!e.editable||o.button!==0)return!1;const n=o.target?.closest?.("td, th");if(o.shiftKey&&n&&e.dom.contains(n)){const r=S(e.state),l=e.posAtDOM(n,0);if(r?.grid&&T(e.state.doc.resolve(l))?.pos===r.grid.pos){const h=_(r.grid,l),i=_(r.grid,M.getState(e.state)?.anchor??e.state.selection.anchor);if(i&&h)return o.preventDefault(),e.dispatch(e.state.tr.setMeta(M,{anchor:i.pos+1,head:h.pos+1})),!0}}return M.getState(e.state)&&e.dispatch(e.state.tr.setMeta(M,null)),t.start(e,o)}},handleTextInput(e,o,n,r){return(S(e.state)?.cells?.length||0)<2?!1:R(e,{action:"clear_cells"},r)}},view(e){let o="";const n=()=>{const r=L(e),l=JSON.stringify(r);o!==l&&(o=l,s.onStateChange?.(r))};return n(),{update(){t.cancelIfStale(e),n()},destroy:t.destroy}}})}function lt(s,t){if(!s.editable)return!1;const e=S(s.state);if(!e?.grid||!e.rect)return!1;if(t.key==="Escape"&&(M.getState(s.state)||e.cells.length>1))return s.dispatch(s.state.tr.setSelection(y.TextSelection.near(s.state.selection.$head)).setMeta(M,null)),!0;if(t.ctrlKey||t.metaKey||t.altKey)return!1;if(["Backspace","Delete"].includes(t.key)&&e.cells.length>1)return R(s,{action:"clear_cells"});if(t.key!=="Tab")return!1;const o=e.grid,n=_(o,s.state.selection.head);if(!n)return!1;const r=o.cells.indexOf(n)+(t.shiftKey?-1:1);if(r<0)return!1;if(r<o.cells.length)return Y(s,o.cells[r].pos),!0;if(!R(s,{action:"row_after"}))return!1;const l=S(s.state)?.grid,h=l?.matrix[l.rows.length-1]?.[0];return h&&Y(s,h.pos),!0}function it(s={}){return{name:"table-view",plugins:()=>[rt(s)],nodeViews:{table:(t,e,o)=>new Q(t,e,o)}}}exports.EMPTY_TABLE_STATE=O;exports.createTableExtension=it;exports.getTableState=L;exports.runTableCommand=R;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|