@p10node/office-engine 0.0.0 → 0.1.1
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 +65 -1
- package/dist/DocViewer-kfAVEXOO.js +10533 -0
- package/dist/DocViewer-kfAVEXOO.js.map +1 -0
- package/dist/THIRD_PARTY_LICENSES.txt +79 -0
- package/dist/assets/worker-DbLPJnSo.js.map +1 -0
- package/dist/collab-BzN1ZI-z.js +1984 -0
- package/dist/collab-BzN1ZI-z.js.map +1 -0
- package/dist/collab.js +2 -0
- package/dist/core.js +2 -0
- package/dist/default-worker-f546VALp.js +9196 -0
- package/dist/default-worker-f546VALp.js.map +1 -0
- package/dist/embed.js +91 -0
- package/dist/embed.js.map +1 -0
- package/dist/index.js +5 -0
- package/dist/office-engine.embed.js +81 -0
- package/dist/office-engine.worker.js +9092 -0
- package/dist/types/collab/codec.d.ts +3 -0
- package/dist/types/collab/docx.d.ts +166 -0
- package/dist/types/collab/hosts.d.ts +24 -0
- package/dist/types/collab/hub.d.ts +41 -0
- package/dist/types/collab/index.d.ts +43 -0
- package/dist/types/collab/pptx-ops.d.ts +60 -0
- package/dist/types/collab/pptx-worker.d.ts +35 -0
- package/dist/types/collab/pptx.d.ts +47 -0
- package/dist/types/collab/protocol.d.ts +76 -0
- package/dist/types/collab/react.d.ts +23 -0
- package/dist/types/collab/session.d.ts +115 -0
- package/dist/types/collab/transport.d.ts +47 -0
- package/dist/types/collab/xlsx-ops.d.ts +72 -0
- package/dist/types/collab/xlsx-worker.d.ts +29 -0
- package/dist/types/collab/xlsx.d.ts +51 -0
- package/dist/types/core/color.d.ts +25 -0
- package/dist/types/core/convert.d.ts +29 -0
- package/dist/types/core/default-worker.d.ts +1 -0
- package/dist/types/core/docx/numbering.d.ts +33 -0
- package/dist/types/core/docx/parser.d.ts +126 -0
- package/dist/types/core/docx/spec.d.ts +4 -0
- package/dist/types/core/docx/styles.d.ts +63 -0
- package/dist/types/core/docx/types.d.ts +325 -0
- package/dist/types/core/docx/writer.d.ts +35 -0
- package/dist/types/core/fonts.d.ts +1 -0
- package/dist/types/core/index.d.ts +15 -0
- package/dist/types/core/opc.d.ts +56 -0
- package/dist/types/core/pptx/edit.d.ts +10 -0
- package/dist/types/core/pptx/parser.d.ts +52 -0
- package/dist/types/core/pptx/types.d.ts +238 -0
- package/dist/types/core/raw.d.ts +40 -0
- package/dist/types/core/source.d.ts +65 -0
- package/dist/types/core/theme.d.ts +11 -0
- package/dist/types/core/units.d.ts +7 -0
- package/dist/types/core/worker/client.d.ts +48 -0
- package/dist/types/core/worker/engine.d.ts +27 -0
- package/dist/types/core/worker/protocol.d.ts +112 -0
- package/dist/types/core/worker/worker.d.ts +1 -0
- package/dist/types/core/xlsx/calc.d.ts +31 -0
- package/dist/types/core/xlsx/editor.d.ts +95 -0
- package/dist/types/core/xlsx/formula/assist.d.ts +35 -0
- package/dist/types/core/xlsx/formula/catalog.d.ts +14 -0
- package/dist/types/core/xlsx/formula/dates.d.ts +23 -0
- package/dist/types/core/xlsx/formula/evaluate.d.ts +33 -0
- package/dist/types/core/xlsx/formula/fn-date.d.ts +2 -0
- package/dist/types/core/xlsx/formula/fn-logic.d.ts +3 -0
- package/dist/types/core/xlsx/formula/fn-lookup.d.ts +3 -0
- package/dist/types/core/xlsx/formula/fn-math.d.ts +3 -0
- package/dist/types/core/xlsx/formula/fn-stats.d.ts +3 -0
- package/dist/types/core/xlsx/formula/fn-text.d.ts +3 -0
- package/dist/types/core/xlsx/formula/fn.d.ts +64 -0
- package/dist/types/core/xlsx/formula/format.d.ts +5 -0
- package/dist/types/core/xlsx/formula/functions.d.ts +4 -0
- package/dist/types/core/xlsx/formula/index.d.ts +17 -0
- package/dist/types/core/xlsx/formula/lexer.d.ts +54 -0
- package/dist/types/core/xlsx/formula/parser.d.ts +53 -0
- package/dist/types/core/xlsx/formula/refs.d.ts +23 -0
- package/dist/types/core/xlsx/formula/transform.d.ts +21 -0
- package/dist/types/core/xlsx/formula/types.d.ts +59 -0
- package/dist/types/core/xlsx/formula/values.d.ts +49 -0
- package/dist/types/core/xlsx/numfmt.d.ts +11 -0
- package/dist/types/core/xlsx/structure.d.ts +31 -0
- package/dist/types/core/xlsx/styles.d.ts +17 -0
- package/dist/types/core/xlsx/types.d.ts +193 -0
- package/dist/types/core/xlsx/workbook.d.ts +94 -0
- package/dist/types/core/xml.d.ts +80 -0
- package/dist/types/core/zip.d.ts +35 -0
- package/dist/types/core/zipwrite.d.ts +11 -0
- package/dist/types/embed.d.ts +30 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/react/DocViewer.d.ts +61 -0
- package/dist/types/react/common/ContextMenu.d.ts +21 -0
- package/dist/types/react/common/Dialog.d.ts +35 -0
- package/dist/types/react/common/Slots.d.ts +38 -0
- package/dist/types/react/common/Status.d.ts +6 -0
- package/dist/types/react/common/ZoomControl.d.ts +6 -0
- package/dist/types/react/common/hooks.d.ts +20 -0
- package/dist/types/react/docx/DocxEditor.d.ts +172 -0
- package/dist/types/react/docx/DocxToolbar.d.ts +19 -0
- package/dist/types/react/docx/DocxView.d.ts +8 -0
- package/dist/types/react/docx/FindBar.d.ts +22 -0
- package/dist/types/react/docx/html.d.ts +8 -0
- package/dist/types/react/docx/render.d.ts +24 -0
- package/dist/types/react/docx/tables.d.ts +20 -0
- package/dist/types/react/edit/Toolbar.d.ts +134 -0
- package/dist/types/react/edit/input.d.ts +81 -0
- package/dist/types/react/edit/rich.d.ts +40 -0
- package/dist/types/react/file/DetailsDialog.d.ts +7 -0
- package/dist/types/react/file/FileMenu.d.ts +48 -0
- package/dist/types/react/file/importFile.d.ts +10 -0
- package/dist/types/react/file/print.d.ts +16 -0
- package/dist/types/react/file/text.d.ts +10 -0
- package/dist/types/react/injectStyles.d.ts +4 -0
- package/dist/types/react/pptx/PptxEditor.d.ts +95 -0
- package/dist/types/react/pptx/PptxToolbar.d.ts +36 -0
- package/dist/types/react/pptx/PptxView.d.ts +24 -0
- package/dist/types/react/pptx/Presenter.d.ts +9 -0
- package/dist/types/react/pptx/Slide.d.ts +28 -0
- package/dist/types/react/pptx/geometry.d.ts +5 -0
- package/dist/types/react/useDocument.d.ts +11 -0
- package/dist/types/react/xlsx/XlsxToolbar.d.ts +27 -0
- package/dist/types/react/xlsx/XlsxView.d.ts +18 -0
- package/dist/types/react/xlsx/axis.d.ts +19 -0
- package/dist/types/react/xlsx/print.d.ts +17 -0
- package/dist/xlsx-ops-CsjNpMI0.js +244 -0
- package/dist/xlsx-ops-CsjNpMI0.js.map +1 -0
- package/package.json +75 -2
|
@@ -0,0 +1,1984 @@
|
|
|
1
|
+
|
|
2
|
+
import { i as e, n as t, r as n } from "./xlsx-ops-CsjNpMI0.js";
|
|
3
|
+
// src/collab/hosts.ts
|
|
4
|
+
var r = /* @__PURE__ */ new WeakMap(), i = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
function a(e, t, n) {
|
|
6
|
+
let a = r.get(e);
|
|
7
|
+
a || r.set(e, a = /* @__PURE__ */ new Map()), a.set(t, n);
|
|
8
|
+
for (let r of i.get(e)?.get(t) ?? []) r(n);
|
|
9
|
+
return () => {
|
|
10
|
+
a.get(t) === n && a.delete(t);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function o(e, t) {
|
|
14
|
+
return r.get(e)?.get(t);
|
|
15
|
+
}
|
|
16
|
+
function s(e, t, n) {
|
|
17
|
+
let r = i.get(e);
|
|
18
|
+
r || i.set(e, r = /* @__PURE__ */ new Map());
|
|
19
|
+
let a = r.get(t);
|
|
20
|
+
a || r.set(t, a = /* @__PURE__ */ new Set()), a.add(n);
|
|
21
|
+
let s = o(e, t);
|
|
22
|
+
return s && n(s), () => a.delete(n);
|
|
23
|
+
}
|
|
24
|
+
var c = /* @__PURE__ */ new WeakMap();
|
|
25
|
+
function l(e) {
|
|
26
|
+
let t = c.get(e);
|
|
27
|
+
return t || c.set(e, t = {
|
|
28
|
+
peers: d,
|
|
29
|
+
peerFns: /* @__PURE__ */ new Set(),
|
|
30
|
+
localFns: /* @__PURE__ */ new Set()
|
|
31
|
+
}), t;
|
|
32
|
+
}
|
|
33
|
+
function u(e, t) {
|
|
34
|
+
let n = l(e);
|
|
35
|
+
n.peers = t;
|
|
36
|
+
for (let e of n.peerFns) e();
|
|
37
|
+
}
|
|
38
|
+
var d = [];
|
|
39
|
+
function f(e) {
|
|
40
|
+
return c.get(e)?.peers ?? d;
|
|
41
|
+
}
|
|
42
|
+
function p(e, t) {
|
|
43
|
+
let n = l(e);
|
|
44
|
+
return n.peerFns.add(t), () => n.peerFns.delete(t);
|
|
45
|
+
}
|
|
46
|
+
function ee(e, t) {
|
|
47
|
+
for (let n of c.get(e)?.localFns ?? []) n(t);
|
|
48
|
+
}
|
|
49
|
+
function m(e, t) {
|
|
50
|
+
let n = l(e);
|
|
51
|
+
return n.localFns.add(t), () => n.localFns.delete(t);
|
|
52
|
+
}
|
|
53
|
+
// src/react/edit/rich.ts
|
|
54
|
+
var h = (e) => e.t === "text" ? e.text.length : 1, g = (e) => e.reduce((e, t) => e + h(t), 0), _ = (e) => e.a.pa === e.f.pa && e.a.o === e.f.o, v = (e, t) => e.replace(/(\d+)$/, (e) => String(Number(e) + t));
|
|
55
|
+
function y(e) {
|
|
56
|
+
return e.map((e) => e.t === "text" ? e.text : "").join("");
|
|
57
|
+
}
|
|
58
|
+
function b(e, t) {
|
|
59
|
+
if (e === t) return !0;
|
|
60
|
+
if (!e || !t) return !e && !t;
|
|
61
|
+
let n = Object.keys(e);
|
|
62
|
+
if (n.length !== Object.keys(t).length) return !1;
|
|
63
|
+
for (let r of n) if (e[r] !== t[r]) return !1;
|
|
64
|
+
return !0;
|
|
65
|
+
}
|
|
66
|
+
var te = (e, t) => e.t === "text" && t.t === "text" && e.href === t.href && e.lk === t.lk && b(e.s, t.s) && b(e.d, t.d);
|
|
67
|
+
function x(e) {
|
|
68
|
+
let t = [];
|
|
69
|
+
for (let n of e) {
|
|
70
|
+
if (n.t === "text" && !n.text) continue;
|
|
71
|
+
let e = t[t.length - 1];
|
|
72
|
+
e && te(e, n) ? t[t.length - 1] = {
|
|
73
|
+
...e,
|
|
74
|
+
text: e.text + n.text
|
|
75
|
+
} : t.push(n);
|
|
76
|
+
}
|
|
77
|
+
return t;
|
|
78
|
+
}
|
|
79
|
+
function S(e, t) {
|
|
80
|
+
let n = [], r = [], i = 0;
|
|
81
|
+
for (let a of e) {
|
|
82
|
+
let e = h(a);
|
|
83
|
+
if (i + e <= t) n.push(a);
|
|
84
|
+
else if (i >= t) r.push(a);
|
|
85
|
+
else {
|
|
86
|
+
let e = t - i;
|
|
87
|
+
n.push({
|
|
88
|
+
...a,
|
|
89
|
+
text: a.text.slice(0, e)
|
|
90
|
+
}), r.push({
|
|
91
|
+
...a,
|
|
92
|
+
text: a.text.slice(e)
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
i += e;
|
|
96
|
+
}
|
|
97
|
+
return [n, r];
|
|
98
|
+
}
|
|
99
|
+
function C(e, t) {
|
|
100
|
+
let [n, r] = S(e, t);
|
|
101
|
+
for (let e = n.length - 1; e >= 0; e--) if (n[e].t === "text") return n[e];
|
|
102
|
+
return r.find((e) => e.t === "text");
|
|
103
|
+
}
|
|
104
|
+
function ne(e, t, n, r, i) {
|
|
105
|
+
if (!n) return e;
|
|
106
|
+
let [a, o] = S(e, t), s = C(e, t), c = {
|
|
107
|
+
t: "text",
|
|
108
|
+
...s ?? i ?? {},
|
|
109
|
+
...r ?? {},
|
|
110
|
+
text: n
|
|
111
|
+
};
|
|
112
|
+
return r?.s && s?.s && (c.s = {
|
|
113
|
+
...s.s,
|
|
114
|
+
...r.s
|
|
115
|
+
}), (r?.d || s?.d) && (c.d = {
|
|
116
|
+
...s?.d ?? {},
|
|
117
|
+
...r?.d ?? {}
|
|
118
|
+
}), c.d && !Object.keys(c.d).length && delete c.d, x([
|
|
119
|
+
...a,
|
|
120
|
+
c,
|
|
121
|
+
...o
|
|
122
|
+
]);
|
|
123
|
+
}
|
|
124
|
+
function w(e, t, n) {
|
|
125
|
+
return n <= t ? e : x([...S(e, t)[0], ...S(e, n)[1]]);
|
|
126
|
+
}
|
|
127
|
+
function re(e, t, n, r, i) {
|
|
128
|
+
let [a, o] = S(e, t), [s, c] = S(o, n - t), l = s.map((e) => e.t === "text" ? {
|
|
129
|
+
...e,
|
|
130
|
+
s: {
|
|
131
|
+
...e.s ?? {},
|
|
132
|
+
...r
|
|
133
|
+
},
|
|
134
|
+
d: {
|
|
135
|
+
...e.d ?? {},
|
|
136
|
+
...i
|
|
137
|
+
}
|
|
138
|
+
} : e);
|
|
139
|
+
return x([
|
|
140
|
+
...a,
|
|
141
|
+
...l,
|
|
142
|
+
...c
|
|
143
|
+
]);
|
|
144
|
+
}
|
|
145
|
+
function ie(e, t) {
|
|
146
|
+
if (t <= 0) return 0;
|
|
147
|
+
let n = Intl.Segmenter;
|
|
148
|
+
if (n) {
|
|
149
|
+
let r = 0;
|
|
150
|
+
for (let i of new n(void 0, { granularity: "grapheme" }).segment(e.slice(0, t))) r = i.index;
|
|
151
|
+
return r;
|
|
152
|
+
}
|
|
153
|
+
let r = e.charCodeAt(t - 1);
|
|
154
|
+
return r >= 56320 && r <= 57343 && t >= 2 ? t - 2 : t - 1;
|
|
155
|
+
}
|
|
156
|
+
function ae(e, t) {
|
|
157
|
+
if (t >= e.length) return e.length;
|
|
158
|
+
let n = Intl.Segmenter;
|
|
159
|
+
if (n) for (let r of new n(void 0, { granularity: "grapheme" }).segment(e.slice(t))) return t + r.segment.length;
|
|
160
|
+
let r = e.charCodeAt(t);
|
|
161
|
+
return r >= 55296 && r <= 56319 ? t + 2 : t + 1;
|
|
162
|
+
}
|
|
163
|
+
// src/react/edit/input.ts
|
|
164
|
+
function T(e, t) {
|
|
165
|
+
let n = e.cmp(t.a.pa, t.f.pa);
|
|
166
|
+
return n < 0 || n === 0 && t.a.o <= t.f.o ? [t.a, t.f] : [t.f, t.a];
|
|
167
|
+
}
|
|
168
|
+
var E = (e) => ({
|
|
169
|
+
a: e,
|
|
170
|
+
f: e
|
|
171
|
+
});
|
|
172
|
+
function D(e, t) {
|
|
173
|
+
let [n, r] = T(e, t);
|
|
174
|
+
if (n.pa === r.pa) {
|
|
175
|
+
let t = e.para(n.pa);
|
|
176
|
+
return t ? {
|
|
177
|
+
pas: [n.pa],
|
|
178
|
+
paras: [e.withRuns(t, w(t.runs, n.o, r.o))],
|
|
179
|
+
at: n
|
|
180
|
+
} : null;
|
|
181
|
+
}
|
|
182
|
+
let i = e.span(n.pa, r.pa);
|
|
183
|
+
if (!i) return null;
|
|
184
|
+
let a = e.para(i[0]), o = e.para(i[i.length - 1]), s = x([...S(a.runs, n.o)[0], ...S(o.runs, r.o)[1]]);
|
|
185
|
+
return {
|
|
186
|
+
pas: i,
|
|
187
|
+
paras: [e.join(a, o, s)],
|
|
188
|
+
at: n
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function O(e, t, n) {
|
|
192
|
+
let r = D(e, t);
|
|
193
|
+
r && e.replace(r.pas, r.paras, E(r.at), n);
|
|
194
|
+
}
|
|
195
|
+
function k(e, t) {
|
|
196
|
+
if (!_(t)) return D(e, t);
|
|
197
|
+
let n = e.para(t.a.pa);
|
|
198
|
+
return n ? {
|
|
199
|
+
pas: [t.a.pa],
|
|
200
|
+
paras: [n],
|
|
201
|
+
at: t.a
|
|
202
|
+
} : null;
|
|
203
|
+
}
|
|
204
|
+
function A(e, t, n, r = {}, i) {
|
|
205
|
+
let a = k(e, t);
|
|
206
|
+
if (!a) return;
|
|
207
|
+
let o = a.paras[0], s = a.at.o, [c, l] = S(o.runs, s), u = C(o.runs, s) ?? e.defaultRun?.(o), d = (e) => e.map((e) => e.t === "text" && !e.s && u ? {
|
|
208
|
+
...u,
|
|
209
|
+
...e,
|
|
210
|
+
s: u.s,
|
|
211
|
+
src: u.src
|
|
212
|
+
} : e);
|
|
213
|
+
if (n.length === 1) {
|
|
214
|
+
let t = d(n[0]), i = x([
|
|
215
|
+
...c,
|
|
216
|
+
...t,
|
|
217
|
+
...l
|
|
218
|
+
]);
|
|
219
|
+
e.replace(a.pas, [e.withRuns(o, i)], E({
|
|
220
|
+
pa: a.at.pa,
|
|
221
|
+
o: s + g(t)
|
|
222
|
+
}), {
|
|
223
|
+
label: "paste",
|
|
224
|
+
...r
|
|
225
|
+
});
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
let f = [], p = e.withRuns(o, x([...c, ...d(n[0])]));
|
|
229
|
+
i?.[0] && (p = i[0](p));
|
|
230
|
+
for (let t = 1; t < n.length; t++) {
|
|
231
|
+
let r = t === n.length - 1, a = d(n[t]), [o, s] = e.split(p, p.runs, x([...a, ...r ? l : []]), !r);
|
|
232
|
+
f.push(o), p = i?.[t] ? i[t](s) : s;
|
|
233
|
+
}
|
|
234
|
+
f.push(p), e.replace(a.pas, f, E({
|
|
235
|
+
pa: v(a.at.pa, f.length - 1),
|
|
236
|
+
o: g(d(n[n.length - 1]))
|
|
237
|
+
}), {
|
|
238
|
+
label: "paste",
|
|
239
|
+
...r
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
function j(e, t, n, r) {
|
|
243
|
+
let i = k(e, t);
|
|
244
|
+
if (!i) return;
|
|
245
|
+
let a = e.pendingStyle?.();
|
|
246
|
+
e.clearPending?.();
|
|
247
|
+
let o = i.paras[0], s = i.at.o, c = n.replace(/\r\n?/g, "\n").split("\n");
|
|
248
|
+
if (c.length === 1 || r?.label === "linebreak") {
|
|
249
|
+
let t = ne(o.runs, s, n, a, e.defaultRun?.(o));
|
|
250
|
+
e.replace(i.pas, [e.withRuns(o, t)], E({
|
|
251
|
+
pa: i.at.pa,
|
|
252
|
+
o: s + n.length
|
|
253
|
+
}), {
|
|
254
|
+
label: "type",
|
|
255
|
+
...r
|
|
256
|
+
});
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
let l = a ? {
|
|
260
|
+
...C(o.runs, s) ?? e.defaultRun?.(o) ?? {},
|
|
261
|
+
...a
|
|
262
|
+
} : void 0;
|
|
263
|
+
A(e, t, c.map((e) => e ? [{
|
|
264
|
+
...l ?? {},
|
|
265
|
+
t: "text",
|
|
266
|
+
text: e
|
|
267
|
+
}] : []), r);
|
|
268
|
+
}
|
|
269
|
+
function M(e, t) {
|
|
270
|
+
let n = _(t) ? (() => {
|
|
271
|
+
let n = e.para(t.a.pa);
|
|
272
|
+
return n ? {
|
|
273
|
+
pas: [t.a.pa],
|
|
274
|
+
paras: [n],
|
|
275
|
+
at: t.a
|
|
276
|
+
} : null;
|
|
277
|
+
})() : D(e, t);
|
|
278
|
+
if (!n) return;
|
|
279
|
+
let r = n.paras[0], [i, a] = S(r.runs, n.at.o), [o, s] = e.split(r, i, a, n.at.o >= g(r.runs));
|
|
280
|
+
e.replace(n.pas, [o, s], E({
|
|
281
|
+
pa: v(n.at.pa, 1),
|
|
282
|
+
o: 0
|
|
283
|
+
}), { label: "enter" });
|
|
284
|
+
}
|
|
285
|
+
function N(e, t, n = !1) {
|
|
286
|
+
if (!_(t)) return O(e, t);
|
|
287
|
+
let { pa: r, o: i } = t.a, a = e.para(r);
|
|
288
|
+
if (!a) return;
|
|
289
|
+
if (i > 0) {
|
|
290
|
+
let t = y(a.runs), o = ie(t, i);
|
|
291
|
+
if (n) {
|
|
292
|
+
let e = /(\S+\s*|\s+)$/.exec(t.slice(0, i));
|
|
293
|
+
e && (o = i - e[0].length);
|
|
294
|
+
}
|
|
295
|
+
e.replace([r], [e.withRuns(a, w(a.runs, o, i))], E({
|
|
296
|
+
pa: r,
|
|
297
|
+
o
|
|
298
|
+
}), { label: "delete" });
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (e.backspaceAtStart?.(r, a)) return;
|
|
302
|
+
let o = e.prev(r), s = o ? e.para(o) : void 0;
|
|
303
|
+
if (!o || !s) return;
|
|
304
|
+
let c = e.join(s, a, x([...s.runs, ...a.runs]));
|
|
305
|
+
e.replace([o, r], [c], E({
|
|
306
|
+
pa: o,
|
|
307
|
+
o: g(s.runs)
|
|
308
|
+
}), { label: "merge" });
|
|
309
|
+
}
|
|
310
|
+
function P(e, t, n = !1) {
|
|
311
|
+
if (!_(t)) return O(e, t);
|
|
312
|
+
let { pa: r, o: i } = t.a, a = e.para(r);
|
|
313
|
+
if (!a) return;
|
|
314
|
+
if (i < g(a.runs)) {
|
|
315
|
+
let t = y(a.runs), o = ae(t, i);
|
|
316
|
+
if (n) {
|
|
317
|
+
let e = /^(\s*\S+|\s+)/.exec(t.slice(i));
|
|
318
|
+
e && (o = i + e[0].length);
|
|
319
|
+
}
|
|
320
|
+
e.replace([r], [e.withRuns(a, w(a.runs, i, o))], E({
|
|
321
|
+
pa: r,
|
|
322
|
+
o: i
|
|
323
|
+
}), { label: "delete" });
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
let o = e.next(r), s = o ? e.para(o) : void 0;
|
|
327
|
+
if (!o || !s) return;
|
|
328
|
+
let c = e.join(a, s, x([...a.runs, ...s.runs]));
|
|
329
|
+
e.replace([r, o], [c], E({
|
|
330
|
+
pa: r,
|
|
331
|
+
o: i
|
|
332
|
+
}), { label: "merge" });
|
|
333
|
+
}
|
|
334
|
+
function F(e, t) {
|
|
335
|
+
let n = (e && (e.nodeType === 1 ? e : e.parentElement))?.closest("[data-o]");
|
|
336
|
+
return n && t.contains(n) ? n : null;
|
|
337
|
+
}
|
|
338
|
+
function I(e) {
|
|
339
|
+
let t = Number(e.dataset.o);
|
|
340
|
+
return e.hasAttribute("data-obj") ? t + 1 : e.hasAttribute("data-empty") ? t : t + (e.textContent?.length ?? 0);
|
|
341
|
+
}
|
|
342
|
+
function L(e, t, n) {
|
|
343
|
+
let r = (t.nodeType === 1 ? t : t.parentElement)?.closest("[data-pa]");
|
|
344
|
+
if (!r || !e.contains(r)) return null;
|
|
345
|
+
let i = r.dataset.pa;
|
|
346
|
+
if (!F(t, r)) {
|
|
347
|
+
let e = document.createRange();
|
|
348
|
+
e.setStart(t, n), e.collapse(!0);
|
|
349
|
+
let a = 0;
|
|
350
|
+
for (let t of Array.from(r.querySelectorAll("[data-o]"))) if (e.comparePoint(t, t.childNodes.length) < 0) a = I(t);
|
|
351
|
+
else break;
|
|
352
|
+
return {
|
|
353
|
+
pa: i,
|
|
354
|
+
o: a
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
let a = F(t, r), o = Number(a.dataset.o);
|
|
358
|
+
if (a.hasAttribute("data-empty")) return {
|
|
359
|
+
pa: i,
|
|
360
|
+
o
|
|
361
|
+
};
|
|
362
|
+
if (a.hasAttribute("data-obj")) return {
|
|
363
|
+
pa: i,
|
|
364
|
+
o: n > 0 || t !== a ? o + 1 : o
|
|
365
|
+
};
|
|
366
|
+
if (t.nodeType === 3) {
|
|
367
|
+
let e = 0, r = document.createTreeWalker(a, NodeFilter.SHOW_TEXT);
|
|
368
|
+
for (let a = r.nextNode(); a; a = r.nextNode()) {
|
|
369
|
+
if (a === t) return {
|
|
370
|
+
pa: i,
|
|
371
|
+
o: o + e + Math.min(n, (a.textContent ?? "").length)
|
|
372
|
+
};
|
|
373
|
+
e += a.textContent?.length ?? 0;
|
|
374
|
+
}
|
|
375
|
+
return {
|
|
376
|
+
pa: i,
|
|
377
|
+
o: o + e
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
let s = 0;
|
|
381
|
+
for (let e = 0; e < Math.min(n, a.childNodes.length); e++) s += a.childNodes[e].textContent?.length ?? 0;
|
|
382
|
+
return {
|
|
383
|
+
pa: i,
|
|
384
|
+
o: o + s
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
function R(e, t) {
|
|
388
|
+
let n = e.querySelector(`[data-pa="${t.pa}"]`);
|
|
389
|
+
if (!n) return null;
|
|
390
|
+
let r = Array.from(n.querySelectorAll("[data-o]"));
|
|
391
|
+
if (!r.length) return [n, n.childNodes.length];
|
|
392
|
+
let i;
|
|
393
|
+
for (let e of r) {
|
|
394
|
+
let n = Number(e.dataset.o), r = I(e);
|
|
395
|
+
if (t.o >= n && t.o <= r && (i = e, t.o < r || !e.hasAttribute("data-obj"))) break;
|
|
396
|
+
}
|
|
397
|
+
i ??= r[r.length - 1];
|
|
398
|
+
let a = Number(i.dataset.o);
|
|
399
|
+
if (i.hasAttribute("data-obj") || i.hasAttribute("data-empty")) {
|
|
400
|
+
let e = i.parentNode, n = Array.prototype.indexOf.call(e.childNodes, i);
|
|
401
|
+
if (i.hasAttribute("data-empty")) {
|
|
402
|
+
let t = i.firstChild;
|
|
403
|
+
return t ? [t, 0] : [e, n];
|
|
404
|
+
}
|
|
405
|
+
return [e, t.o > a ? n + 1 : n];
|
|
406
|
+
}
|
|
407
|
+
let o = Math.max(0, t.o - a), s = document.createTreeWalker(i, NodeFilter.SHOW_TEXT), c = null;
|
|
408
|
+
for (let e = s.nextNode(); e; e = s.nextNode()) {
|
|
409
|
+
let t = e.textContent?.length ?? 0;
|
|
410
|
+
if (o <= t) return [e, o];
|
|
411
|
+
o -= t, c = e;
|
|
412
|
+
}
|
|
413
|
+
return c ? [c, c.textContent?.length ?? 0] : [i, 0];
|
|
414
|
+
}
|
|
415
|
+
function z(e) {
|
|
416
|
+
let t = e.getRootNode();
|
|
417
|
+
return typeof ShadowRoot < "u" && t instanceof ShadowRoot && typeof t.getSelection == "function" ? t.getSelection() : (e.ownerDocument ?? document).getSelection();
|
|
418
|
+
}
|
|
419
|
+
function oe(e) {
|
|
420
|
+
let t = e.getRootNode(), n = z(e);
|
|
421
|
+
if (!n) return null;
|
|
422
|
+
if (typeof ShadowRoot < "u" && t instanceof ShadowRoot && n.anchorNode && !e.contains(n.anchorNode)) {
|
|
423
|
+
let e = n.getComposedRanges?.call(n, { shadowRoots: [t] })[0];
|
|
424
|
+
if (e) return {
|
|
425
|
+
anchorNode: e.startContainer,
|
|
426
|
+
anchorOffset: e.startOffset,
|
|
427
|
+
focusNode: e.endContainer,
|
|
428
|
+
focusOffset: e.endOffset
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
return n;
|
|
432
|
+
}
|
|
433
|
+
var se = class {
|
|
434
|
+
host;
|
|
435
|
+
root = null;
|
|
436
|
+
composing = null;
|
|
437
|
+
justComposed = "";
|
|
438
|
+
raf = 0;
|
|
439
|
+
lastSel = null;
|
|
440
|
+
constructor(e) {
|
|
441
|
+
this.host = e;
|
|
442
|
+
}
|
|
443
|
+
attach(e) {
|
|
444
|
+
this.root !== e && (this.detach(), this.root = e, e.addEventListener("beforeinput", this.onBeforeInput), e.addEventListener("compositionstart", this.onCompStart), e.addEventListener("compositionend", this.onCompEnd), e.addEventListener("keydown", this.onKeyDown), document.addEventListener("selectionchange", this.onSelChange));
|
|
445
|
+
}
|
|
446
|
+
detach() {
|
|
447
|
+
let e = this.root;
|
|
448
|
+
e && (e.removeEventListener("beforeinput", this.onBeforeInput), e.removeEventListener("compositionstart", this.onCompStart), e.removeEventListener("compositionend", this.onCompEnd), e.removeEventListener("keydown", this.onKeyDown), document.removeEventListener("selectionchange", this.onSelChange), cancelAnimationFrame(this.raf), this.root = null);
|
|
449
|
+
}
|
|
450
|
+
get isComposing() {
|
|
451
|
+
return !!this.composing;
|
|
452
|
+
}
|
|
453
|
+
readSel() {
|
|
454
|
+
let e = this.root, t = e ? oe(e) : null;
|
|
455
|
+
if (!e || !t || !t.anchorNode || !t.focusNode || !e.contains(t.anchorNode) || !e.contains(t.focusNode)) return null;
|
|
456
|
+
let n = L(e, t.anchorNode, t.anchorOffset), r = L(e, t.focusNode, t.focusOffset);
|
|
457
|
+
return n && r ? {
|
|
458
|
+
a: n,
|
|
459
|
+
f: r
|
|
460
|
+
} : null;
|
|
461
|
+
}
|
|
462
|
+
sel() {
|
|
463
|
+
return this.readSel() ?? this.lastSel;
|
|
464
|
+
}
|
|
465
|
+
restore(e, t = !0) {
|
|
466
|
+
let n = this.root;
|
|
467
|
+
if (!n || !e) return !1;
|
|
468
|
+
let r = R(n, e.a), i = R(n, e.f);
|
|
469
|
+
if (!r || !i) return this.lastSel = e, !1;
|
|
470
|
+
let a = n.getRootNode().activeElement;
|
|
471
|
+
t && a !== n && !n.contains(a) && n.focus({ preventScroll: !0 });
|
|
472
|
+
let o = z(n);
|
|
473
|
+
try {
|
|
474
|
+
o?.setBaseAndExtent(r[0], r[1], i[0], i[1]);
|
|
475
|
+
} catch {}
|
|
476
|
+
return this.lastSel = e, !0;
|
|
477
|
+
}
|
|
478
|
+
rangeSel(e) {
|
|
479
|
+
if (!e || !this.root) return null;
|
|
480
|
+
let t = L(this.root, e.startContainer, e.startOffset), n = L(this.root, e.endContainer, e.endOffset);
|
|
481
|
+
return t && n ? {
|
|
482
|
+
a: t,
|
|
483
|
+
f: n
|
|
484
|
+
} : null;
|
|
485
|
+
}
|
|
486
|
+
onBeforeInput = (e) => {
|
|
487
|
+
let t = e.inputType;
|
|
488
|
+
if (this.composing || t === "insertCompositionText") return;
|
|
489
|
+
if (t === "deleteCompositionText" || t === "insertFromComposition") {
|
|
490
|
+
if (e.cancelable && e.preventDefault(), t === "insertFromComposition" && e.data && e.data !== this.justComposed) {
|
|
491
|
+
let t = this.sel();
|
|
492
|
+
t && j(this.host, t, e.data, { remount: !0 });
|
|
493
|
+
}
|
|
494
|
+
this.justComposed = "";
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
e.preventDefault();
|
|
498
|
+
let n = this.rangeSel(e.getTargetRanges?.()[0]), r = this.readSel() ?? this.lastSel;
|
|
499
|
+
if (!r) return;
|
|
500
|
+
let i = this.host;
|
|
501
|
+
switch (t) {
|
|
502
|
+
case "insertText":
|
|
503
|
+
case "insertReplacementText": {
|
|
504
|
+
let a = e.data ?? e.dataTransfer?.getData("text/plain") ?? "";
|
|
505
|
+
j(i, t === "insertReplacementText" && n ? n : r, a);
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
case "insertParagraph":
|
|
509
|
+
M(i, r);
|
|
510
|
+
break;
|
|
511
|
+
case "insertLineBreak":
|
|
512
|
+
j(i, r, "\n", { label: "linebreak" });
|
|
513
|
+
break;
|
|
514
|
+
case "insertFromPaste":
|
|
515
|
+
case "insertFromDrop":
|
|
516
|
+
case "insertFromYank": {
|
|
517
|
+
if (e.dataTransfer && i.pasteRich?.(t === "insertFromDrop" && n ? n : r, e.dataTransfer)) break;
|
|
518
|
+
let a = e.dataTransfer?.getData("text/plain") ?? e.data ?? "";
|
|
519
|
+
a && j(i, r, a);
|
|
520
|
+
break;
|
|
521
|
+
}
|
|
522
|
+
case "deleteContentBackward":
|
|
523
|
+
N(i, r);
|
|
524
|
+
break;
|
|
525
|
+
case "deleteWordBackward":
|
|
526
|
+
N(i, r, !0);
|
|
527
|
+
break;
|
|
528
|
+
case "deleteContentForward":
|
|
529
|
+
P(i, r);
|
|
530
|
+
break;
|
|
531
|
+
case "deleteWordForward":
|
|
532
|
+
P(i, r, !0);
|
|
533
|
+
break;
|
|
534
|
+
case "deleteSoftLineBackward":
|
|
535
|
+
case "deleteHardLineBackward":
|
|
536
|
+
case "deleteSoftLineForward":
|
|
537
|
+
case "deleteHardLineForward":
|
|
538
|
+
case "deleteByCut":
|
|
539
|
+
case "deleteByDrag":
|
|
540
|
+
case "deleteContent":
|
|
541
|
+
case "deleteEntireSoftLine":
|
|
542
|
+
_(r) ? n && !_(n) && O(i, n) : O(i, r);
|
|
543
|
+
break;
|
|
544
|
+
case "historyUndo":
|
|
545
|
+
i.undo();
|
|
546
|
+
break;
|
|
547
|
+
case "historyRedo":
|
|
548
|
+
i.redo();
|
|
549
|
+
break;
|
|
550
|
+
case "formatBold":
|
|
551
|
+
i.toggle?.("b");
|
|
552
|
+
break;
|
|
553
|
+
case "formatItalic":
|
|
554
|
+
i.toggle?.("i");
|
|
555
|
+
break;
|
|
556
|
+
case "formatUnderline": i.toggle?.("u");
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
onCompStart = () => {
|
|
560
|
+
this.composing = this.readSel() ?? this.lastSel;
|
|
561
|
+
};
|
|
562
|
+
onCompEnd = (e) => {
|
|
563
|
+
let t = this.composing;
|
|
564
|
+
this.composing = null, t && (this.justComposed = e.data ?? "", e.data ? j(this.host, t, e.data, { remount: !0 }) : j(this.host, t, "", { remount: !0 }), setTimeout(() => this.justComposed = "", 0));
|
|
565
|
+
};
|
|
566
|
+
onKeyDown = (e) => {
|
|
567
|
+
if (e.isComposing || this.composing) return;
|
|
568
|
+
let t = e.metaKey || e.ctrlKey, n = e.key.toLowerCase();
|
|
569
|
+
if (this.host.shortcut?.(e)) e.preventDefault();
|
|
570
|
+
else {
|
|
571
|
+
if (t && !e.altKey) {
|
|
572
|
+
if (n === "z") {
|
|
573
|
+
e.preventDefault(), e.shiftKey ? this.host.redo() : this.host.undo();
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
if (n === "y") {
|
|
577
|
+
e.preventDefault(), this.host.redo();
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (n === "b" || n === "i" || n === "u") {
|
|
581
|
+
e.preventDefault(), this.host.toggle?.(n);
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
if (e.key === "Tab" && !t) {
|
|
586
|
+
e.preventDefault();
|
|
587
|
+
let t = this.sel();
|
|
588
|
+
if (!t) return;
|
|
589
|
+
!this.host.tab?.(t, e.shiftKey) && !e.shiftKey && j(this.host, t, " ");
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
onSelChange = () => {
|
|
594
|
+
this.raf ||= requestAnimationFrame(() => {
|
|
595
|
+
this.raf = 0;
|
|
596
|
+
let e = this.readSel();
|
|
597
|
+
e && (this.lastSel = e), this.host.onSelection?.(e);
|
|
598
|
+
});
|
|
599
|
+
};
|
|
600
|
+
}, ce = /* @__PURE__ */ new Set([
|
|
601
|
+
"open",
|
|
602
|
+
"part",
|
|
603
|
+
"docx.start",
|
|
604
|
+
"save",
|
|
605
|
+
"dirty"
|
|
606
|
+
]), B = (e) => e.t === "p" ? e.pid === void 0 ? null : { p: e.pid } : e.t === "tbl" ? {
|
|
607
|
+
tid: e.tid,
|
|
608
|
+
item: e.item
|
|
609
|
+
} : null;
|
|
610
|
+
function V(e, t) {
|
|
611
|
+
return e ? "p" in t ? e.t === "p" && e.pid === t.p : e.t === "tbl" && (t.tid !== void 0 && e.tid === t.tid || t.item !== void 0 && e.item === t.item) : !1;
|
|
612
|
+
}
|
|
613
|
+
var le = (e) => [...new Set(e)];
|
|
614
|
+
function H(e, t = []) {
|
|
615
|
+
for (let n of e) if (n.t === "p" && n.pid !== void 0) t.push(n.pid);
|
|
616
|
+
else if (n.t === "tbl") for (let e of n.rows) for (let n of e.cells) H(n.blocks, t);
|
|
617
|
+
return t;
|
|
618
|
+
}
|
|
619
|
+
function U(e) {
|
|
620
|
+
if (e.t !== "text") return JSON.stringify(e);
|
|
621
|
+
let { text: t, ...n } = e;
|
|
622
|
+
return JSON.stringify(n);
|
|
623
|
+
}
|
|
624
|
+
function ue(e, t) {
|
|
625
|
+
let n = (e) => {
|
|
626
|
+
let t = [];
|
|
627
|
+
for (let n of e) {
|
|
628
|
+
let e = U(n);
|
|
629
|
+
if (n.t === "text") for (let r = 0; r < n.text.length; r++) t.push(e + "\0" + n.text[r]);
|
|
630
|
+
else t.push(e);
|
|
631
|
+
}
|
|
632
|
+
return t;
|
|
633
|
+
}, r = n(e), i = n(t), a = 0;
|
|
634
|
+
for (; a < r.length && a < i.length && r[a] === i[a];) a++;
|
|
635
|
+
let o = 0;
|
|
636
|
+
for (; o < r.length - a && o < i.length - a && r[r.length - 1 - o] === i[i.length - 1 - o];) o++;
|
|
637
|
+
if (a === r.length && a === i.length) return null;
|
|
638
|
+
let [, s] = S(t, a), [c] = S(s, i.length - a - o);
|
|
639
|
+
return {
|
|
640
|
+
at: a,
|
|
641
|
+
del: r.length - a - o,
|
|
642
|
+
ins: c
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function de(e, t, n, r) {
|
|
646
|
+
let i = g(e), a = Math.max(0, Math.min(t, i)), o = Math.max(0, Math.min(n, i - a)), [s, c] = S(e, a), [, l] = S(c, o);
|
|
647
|
+
return [
|
|
648
|
+
...s,
|
|
649
|
+
...r,
|
|
650
|
+
...l
|
|
651
|
+
];
|
|
652
|
+
}
|
|
653
|
+
function W(e, t, n, r, i) {
|
|
654
|
+
return e < t || e === t && (n > 0 || i) ? e : e >= t + n ? e - n + r : t + r;
|
|
655
|
+
}
|
|
656
|
+
function G(e, t, n) {
|
|
657
|
+
if (t === n) return e;
|
|
658
|
+
let r = 0;
|
|
659
|
+
for (; r < t.length && r < n.length && t[r] === n[r];) r++;
|
|
660
|
+
let i = 0;
|
|
661
|
+
for (; i < t.length - r && i < n.length - r && t[t.length - 1 - i] === n[n.length - 1 - i];) i++;
|
|
662
|
+
return e <= r ? e : e >= t.length - i ? n.length - (t.length - e) : n.length - i;
|
|
663
|
+
}
|
|
664
|
+
var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!e.pb == !!t.pb && e.keepSect === t.keepSect && JSON.stringify(e.pd ?? null) === JSON.stringify(t.pd ?? null) && JSON.stringify(e.ps) === JSON.stringify(t.ps) && JSON.stringify(e.orig ?? null) === JSON.stringify(t.orig ?? null), pe = class {
|
|
665
|
+
client;
|
|
666
|
+
host;
|
|
667
|
+
suppress = 0;
|
|
668
|
+
res = /* @__PURE__ */ new Map();
|
|
669
|
+
resKey = /* @__PURE__ */ new Map();
|
|
670
|
+
results = /* @__PURE__ */ new WeakMap();
|
|
671
|
+
snap = null;
|
|
672
|
+
touchedModel = !1;
|
|
673
|
+
site = 0;
|
|
674
|
+
counter = 0;
|
|
675
|
+
offHost;
|
|
676
|
+
onPresence;
|
|
677
|
+
lastPresence = "";
|
|
678
|
+
raf = 0;
|
|
679
|
+
push;
|
|
680
|
+
submit;
|
|
681
|
+
constructor(e) {
|
|
682
|
+
this.client = e;
|
|
683
|
+
}
|
|
684
|
+
ready() {
|
|
685
|
+
return new Promise((e) => {
|
|
686
|
+
this.offHost = s(this.client, "docx", (t) => {
|
|
687
|
+
this.host?.editor !== t.editor && (this.host && (this.host.editor.onCommit = void 0), this.host = t, t.editor.onCommit = (e) => this.onCommit(e), this.allocPids(), e());
|
|
688
|
+
}), typeof document < "u" && document.addEventListener("selectionchange", this.onSel);
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
setSite(e) {
|
|
692
|
+
this.site = e, this.allocPids();
|
|
693
|
+
}
|
|
694
|
+
allocPids() {
|
|
695
|
+
let e = this.host?.editor;
|
|
696
|
+
if (!e || !this.site) return;
|
|
697
|
+
let t = 2e9 + this.site * 1e7;
|
|
698
|
+
(e.nextPid < t || e.nextPid >= t + 1e7) && (e.nextPid = t);
|
|
699
|
+
}
|
|
700
|
+
dispose() {
|
|
701
|
+
this.offHost?.(), this.host && (this.host.editor.onCommit = void 0), typeof document < "u" && document.removeEventListener("selectionchange", this.onSel), this.raf && cancelAnimationFrame(this.raf);
|
|
702
|
+
}
|
|
703
|
+
captures(e) {
|
|
704
|
+
return !this.suppress && !ce.has(e) && e !== "docx.edit" && e !== "docx.table" && e.startsWith("docx.");
|
|
705
|
+
}
|
|
706
|
+
intercept(e, t) {
|
|
707
|
+
if (this.submit && this.captures(e)) {
|
|
708
|
+
switch (e) {
|
|
709
|
+
case "docx.image": {
|
|
710
|
+
let e = new Uint8Array(t[0].slice(0));
|
|
711
|
+
return this.submit({
|
|
712
|
+
t: "image",
|
|
713
|
+
key: this.key(),
|
|
714
|
+
bytes: e,
|
|
715
|
+
ext: t[1]
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
case "docx.link": return this.submit({
|
|
719
|
+
t: "link",
|
|
720
|
+
key: this.key(),
|
|
721
|
+
url: t[0]
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
return this.submit({
|
|
725
|
+
t: "call",
|
|
726
|
+
method: e,
|
|
727
|
+
args: t
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
key() {
|
|
732
|
+
return `${this.site}:${++this.counter}:${Math.random().toString(36).slice(2, 7)}`;
|
|
733
|
+
}
|
|
734
|
+
get st() {
|
|
735
|
+
return this.host.st;
|
|
736
|
+
}
|
|
737
|
+
get ed() {
|
|
738
|
+
return this.host.editor;
|
|
739
|
+
}
|
|
740
|
+
locate(e) {
|
|
741
|
+
let t = [];
|
|
742
|
+
return this.st.blocks.forEach((n, r) => {
|
|
743
|
+
n.t === "p" ? n.pid === e && t.push({
|
|
744
|
+
list: this.st.blocks,
|
|
745
|
+
index: r,
|
|
746
|
+
pa: String(r),
|
|
747
|
+
p: n
|
|
748
|
+
}) : n.t === "tbl" && n.rows.forEach((n, i) => n.cells.forEach((n, a) => n.blocks.forEach((o, s) => {
|
|
749
|
+
o.t === "p" && o.pid === e && t.push({
|
|
750
|
+
list: n.blocks,
|
|
751
|
+
index: s,
|
|
752
|
+
pa: `${r}.${i}.${a}.${s}`,
|
|
753
|
+
p: o
|
|
754
|
+
});
|
|
755
|
+
})));
|
|
756
|
+
}), t;
|
|
757
|
+
}
|
|
758
|
+
topIndex(e) {
|
|
759
|
+
return this.st.blocks.findIndex((t) => V(t, e));
|
|
760
|
+
}
|
|
761
|
+
region(e) {
|
|
762
|
+
let t = new Set(e);
|
|
763
|
+
for (let n of e) {
|
|
764
|
+
let e = this.locate(n)[0];
|
|
765
|
+
if (!e) continue;
|
|
766
|
+
let r = [];
|
|
767
|
+
for (let n = e.index; n < e.list.length; n++) {
|
|
768
|
+
let i = e.list[n];
|
|
769
|
+
if (i.t !== "p" || i.pid === void 0 || !t.has(i.pid)) break;
|
|
770
|
+
r.push(e.pa.replace(/\d+$/, String(n)));
|
|
771
|
+
}
|
|
772
|
+
return r;
|
|
773
|
+
}
|
|
774
|
+
return null;
|
|
775
|
+
}
|
|
776
|
+
remap(e) {
|
|
777
|
+
if (e.t === "img") {
|
|
778
|
+
let t = e.ck, n = t ? this.res.get(t) : void 0;
|
|
779
|
+
return n && typeof n == "object" ? {
|
|
780
|
+
...e,
|
|
781
|
+
rid: n.rid,
|
|
782
|
+
target: n.path
|
|
783
|
+
} : e;
|
|
784
|
+
}
|
|
785
|
+
if (e.t === "text") {
|
|
786
|
+
let t = e.lck, n = t ? this.res.get(t) : void 0;
|
|
787
|
+
return typeof n == "string" ? {
|
|
788
|
+
...e,
|
|
789
|
+
lk: n
|
|
790
|
+
} : e;
|
|
791
|
+
}
|
|
792
|
+
return e.t === "p" ? {
|
|
793
|
+
...e,
|
|
794
|
+
runs: e.runs.map((e) => this.remap(e))
|
|
795
|
+
} : e.t === "tbl" ? {
|
|
796
|
+
...e,
|
|
797
|
+
rows: e.rows.map((e) => ({
|
|
798
|
+
...e,
|
|
799
|
+
cells: e.cells.map((e) => ({
|
|
800
|
+
...e,
|
|
801
|
+
blocks: e.blocks.map((e) => this.remap(e))
|
|
802
|
+
}))
|
|
803
|
+
}))
|
|
804
|
+
} : e;
|
|
805
|
+
}
|
|
806
|
+
tag(e) {
|
|
807
|
+
return e.t === "img" && e.rid && this.resKey.has(e.rid) && !e.ck ? {
|
|
808
|
+
...e,
|
|
809
|
+
ck: this.resKey.get(e.rid)
|
|
810
|
+
} : e.t === "text" && e.lk && this.resKey.has(e.lk) && !e.lck ? {
|
|
811
|
+
...e,
|
|
812
|
+
lck: this.resKey.get(e.lk)
|
|
813
|
+
} : e.t === "p" ? {
|
|
814
|
+
...e,
|
|
815
|
+
runs: e.runs.map((e) => this.tag(e)),
|
|
816
|
+
caret: void 0
|
|
817
|
+
} : e.t === "tbl" ? {
|
|
818
|
+
...e,
|
|
819
|
+
rows: e.rows.map((e) => ({
|
|
820
|
+
...e,
|
|
821
|
+
cells: e.cells.map((e) => ({
|
|
822
|
+
...e,
|
|
823
|
+
blocks: e.blocks.map((e) => this.tag(e))
|
|
824
|
+
}))
|
|
825
|
+
}))
|
|
826
|
+
} : e;
|
|
827
|
+
}
|
|
828
|
+
async fetchRes(e) {
|
|
829
|
+
if (e.t === "image" && !this.res.has(e.key)) {
|
|
830
|
+
let t = await this.client.direct("docx.image", e.bytes.slice().buffer, e.ext);
|
|
831
|
+
this.res.set(e.key, t), this.resKey.set(t.rid, e.key);
|
|
832
|
+
} else if (e.t === "link" && !this.res.has(e.key)) {
|
|
833
|
+
let t = await this.client.direct("docx.link", e.url);
|
|
834
|
+
this.res.set(e.key, t), this.resKey.set(t, e.key);
|
|
835
|
+
} else if (e.t === "call" && !this.results.has(e)) {
|
|
836
|
+
let t = this.client.direct;
|
|
837
|
+
this.results.set(e, await t(e.method, ...e.args));
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
onCommit(e) {
|
|
841
|
+
if (this.suppress || !this.push) return;
|
|
842
|
+
if (e.kind === "paras") {
|
|
843
|
+
let t = e.before, n = e.next;
|
|
844
|
+
if (t.length === 1 && n.length === 1 && t[0].pid !== void 0 && fe(t[0], n[0]) && this.locate(t[0].pid).length === 1) {
|
|
845
|
+
let e = ue(t[0].runs, n[0].runs);
|
|
846
|
+
if (!e) return;
|
|
847
|
+
this.push({
|
|
848
|
+
t: "text",
|
|
849
|
+
pid: t[0].pid,
|
|
850
|
+
at: e.at,
|
|
851
|
+
del: e.del,
|
|
852
|
+
ins: e.ins.map((e) => this.tag(e))
|
|
853
|
+
}, [{
|
|
854
|
+
t: "paras",
|
|
855
|
+
before: t,
|
|
856
|
+
next: n
|
|
857
|
+
}]);
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
this.push({
|
|
861
|
+
t: "paras",
|
|
862
|
+
before: t.map((e) => e.pid),
|
|
863
|
+
next: n.map((e) => this.tag(e))
|
|
864
|
+
}, [{
|
|
865
|
+
t: "paras",
|
|
866
|
+
before: t,
|
|
867
|
+
next: n
|
|
868
|
+
}]);
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
let t = e.bi > 0 ? B(this.st.blocks[e.bi - 1]) : null;
|
|
872
|
+
this.push({
|
|
873
|
+
t: "blocks",
|
|
874
|
+
before: e.before.map(B).filter((e) => !!e),
|
|
875
|
+
prev: t,
|
|
876
|
+
next: e.next.map((e) => this.tag(e))
|
|
877
|
+
}, [{
|
|
878
|
+
t: "blocks",
|
|
879
|
+
before: e.before,
|
|
880
|
+
next: e.next,
|
|
881
|
+
prev: t
|
|
882
|
+
}]);
|
|
883
|
+
}
|
|
884
|
+
transform(e, t) {
|
|
885
|
+
if (e.t !== "text") return e;
|
|
886
|
+
if (t.t === "text" && t.pid === e.pid) {
|
|
887
|
+
let n = g(t.ins), r = W(e.at, t.at, t.del, n, !1), i = e.del ? Math.max(r, W(e.at + e.del, t.at, t.del, n, !0)) : r;
|
|
888
|
+
return r === e.at && i - r === e.del ? e : {
|
|
889
|
+
...e,
|
|
890
|
+
at: r,
|
|
891
|
+
del: i - r
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
return t.t === "paras" && (t.before.includes(e.pid) || t.next.some((t) => t.pid === e.pid)) ? t.next.some((t) => t.pid === e.pid) ? e : null : t.t === "blocks" && t.before.some((t) => "p" in t && t.p === e.pid) && !H(t.next).includes(e.pid) ? null : e;
|
|
895
|
+
}
|
|
896
|
+
touched(e) {
|
|
897
|
+
switch (e.t) {
|
|
898
|
+
case "text": return ["p" + e.pid];
|
|
899
|
+
case "paras": return [...e.before, ...e.next.map((e) => e.pid)].map((e) => "p" + e);
|
|
900
|
+
case "blocks": {
|
|
901
|
+
let t = [
|
|
902
|
+
...e.before,
|
|
903
|
+
...e.prev ? [e.prev] : [],
|
|
904
|
+
...e.next.map(B).filter((e) => !!e)
|
|
905
|
+
].map((e) => "p" in e ? "p" + e.p : "t" + (e.item ?? "n" + e.tid));
|
|
906
|
+
for (let n of H(e.next)) t.push("p" + n);
|
|
907
|
+
return t;
|
|
908
|
+
}
|
|
909
|
+
default: return [];
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
conflicts(e, t) {
|
|
913
|
+
let n = new Set(e.flatMap((e) => this.touched(e)));
|
|
914
|
+
return t.some((e) => this.touched(e).some((e) => n.has(e)));
|
|
915
|
+
}
|
|
916
|
+
pessimistic(e) {
|
|
917
|
+
return e.t === "call";
|
|
918
|
+
}
|
|
919
|
+
async prepare(e) {
|
|
920
|
+
for (; this.host?.editor.input.isComposing;) await new Promise((e) => setTimeout(e, 50));
|
|
921
|
+
for (let t of e) await this.fetchRes(t);
|
|
922
|
+
this.capture();
|
|
923
|
+
}
|
|
924
|
+
async apply(e, t) {
|
|
925
|
+
switch (e.t) {
|
|
926
|
+
case "image":
|
|
927
|
+
case "link": return await this.fetchRes(e), { result: this.res.get(e.key) };
|
|
928
|
+
case "call": {
|
|
929
|
+
await this.fetchRes(e);
|
|
930
|
+
let t = this.results.get(e);
|
|
931
|
+
if (e.method === "docx.style" && t && typeof t == "object" && this.host) {
|
|
932
|
+
let e = t.styles;
|
|
933
|
+
this.ed.styles = e, this.ed.onStyles?.(e);
|
|
934
|
+
}
|
|
935
|
+
return { result: t };
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
if (!this.host) throw Error("Document editor not ready");
|
|
939
|
+
(t === "remote" || t === "replay") && this.capture(), this.touchedModel = !0, this.suppress++;
|
|
940
|
+
try {
|
|
941
|
+
return { undo: this.run(e) };
|
|
942
|
+
} finally {
|
|
943
|
+
this.suppress--;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
run(e) {
|
|
947
|
+
let t = this.ed;
|
|
948
|
+
if (e.t === "text") {
|
|
949
|
+
let n = this.locate(e.pid);
|
|
950
|
+
if (n.length !== 1 || !t.para(n[0].pa)) return null;
|
|
951
|
+
let r = n[0].p, i = {
|
|
952
|
+
...r,
|
|
953
|
+
runs: de(r.runs, e.at, e.del, e.ins.map((e) => this.remap(e))),
|
|
954
|
+
edited: !0
|
|
955
|
+
};
|
|
956
|
+
return t.replace([n[0].pa], [i], this.sel(), {
|
|
957
|
+
history: !1,
|
|
958
|
+
label: "collab"
|
|
959
|
+
}), [{
|
|
960
|
+
t: "paras",
|
|
961
|
+
before: [r],
|
|
962
|
+
next: [i]
|
|
963
|
+
}];
|
|
964
|
+
}
|
|
965
|
+
if (e.t === "paras") {
|
|
966
|
+
let n = e.next.map((e) => this.remap(e)), r = new Set(n.map((e) => e.pid)), i = this.region(e.before);
|
|
967
|
+
if (!i || !i.length) return null;
|
|
968
|
+
let a = [], o = new Set(i.map((e) => t.para(e)?.pid ?? -1));
|
|
969
|
+
for (let n of le([...e.before, ...r])) if (!o.has(n)) for (let e of this.locate(n).reverse()) {
|
|
970
|
+
if (!t.para(e.pa)) continue;
|
|
971
|
+
let n = (t) => {
|
|
972
|
+
let n = e.list[t];
|
|
973
|
+
return n?.t === "p" && n.pid !== void 0 ? {
|
|
974
|
+
pid: n.pid,
|
|
975
|
+
k: this.locate(n.pid).findIndex((n) => n.list === e.list && n.index === t)
|
|
976
|
+
} : void 0;
|
|
977
|
+
}, r = {
|
|
978
|
+
t: "paras",
|
|
979
|
+
before: [e.p],
|
|
980
|
+
next: [],
|
|
981
|
+
prev: n(e.index - 1),
|
|
982
|
+
after: n(e.index + 1)
|
|
983
|
+
};
|
|
984
|
+
t.replace([e.pa], [], this.sel(), {
|
|
985
|
+
history: !1,
|
|
986
|
+
label: "collab"
|
|
987
|
+
}), a.push(r);
|
|
988
|
+
}
|
|
989
|
+
let s = this.region(e.before);
|
|
990
|
+
if (!s?.length) return a;
|
|
991
|
+
let c = s.map((e) => t.para(e)).filter(Boolean);
|
|
992
|
+
return c.length === s.length && (t.replace(s, n, this.sel(), {
|
|
993
|
+
history: !1,
|
|
994
|
+
label: "collab"
|
|
995
|
+
}), a.push({
|
|
996
|
+
t: "paras",
|
|
997
|
+
before: c,
|
|
998
|
+
next: n
|
|
999
|
+
})), a;
|
|
1000
|
+
}
|
|
1001
|
+
if (e.t === "blocks") {
|
|
1002
|
+
let n = e.next.map((e) => this.remap(e)), r, i = 0;
|
|
1003
|
+
if (e.before.length) {
|
|
1004
|
+
if (r = this.topIndex(e.before[0]), r < 0) {
|
|
1005
|
+
let t = e.before.find((e) => this.topIndex(e) >= 0);
|
|
1006
|
+
if (!t) return null;
|
|
1007
|
+
r = this.topIndex(t);
|
|
1008
|
+
}
|
|
1009
|
+
for (; r + i < this.st.blocks.length && e.before.some((e) => V(this.st.blocks[r + i], e));) i++;
|
|
1010
|
+
} else if (r = e.prev ? this.topIndex(e.prev) + 1 : 0, e.prev && r === 0) return null;
|
|
1011
|
+
let a = this.st.blocks.slice(r, r + i), o = new Map(a.flatMap((e) => e.t === "p" && e.pid !== void 0 ? [[e.pid, e]] : []));
|
|
1012
|
+
return n = n.map((e) => e.t === "p" && e.pid !== void 0 && o.has(e.pid) ? o.get(e.pid) : e), t.replaceBlocks(r, i, n, null, !1), [{
|
|
1013
|
+
t: "blocks",
|
|
1014
|
+
before: a,
|
|
1015
|
+
next: n,
|
|
1016
|
+
prev: r > 0 ? B(this.st.blocks[r - 1]) : null
|
|
1017
|
+
}];
|
|
1018
|
+
}
|
|
1019
|
+
return null;
|
|
1020
|
+
}
|
|
1021
|
+
revert(e) {
|
|
1022
|
+
let t = e;
|
|
1023
|
+
if (t && this.host) {
|
|
1024
|
+
this.capture(), this.touchedModel = !0, this.suppress++;
|
|
1025
|
+
try {
|
|
1026
|
+
for (let e = t.length - 1; e >= 0; e--) this.undoStep(t[e]);
|
|
1027
|
+
} finally {
|
|
1028
|
+
this.suppress--;
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
undoStep(e) {
|
|
1033
|
+
let t = this.ed;
|
|
1034
|
+
if (e.t === "paras") {
|
|
1035
|
+
if (!e.next.length) {
|
|
1036
|
+
let n = (e) => e ? this.locate(e.pid)[e.k] : void 0, r = n(e.prev);
|
|
1037
|
+
if (r && t.para(r.pa)) return t.replace([r.pa], [r.p, ...e.before], this.sel(), {
|
|
1038
|
+
history: !1,
|
|
1039
|
+
label: "collab"
|
|
1040
|
+
});
|
|
1041
|
+
let i = n(e.after);
|
|
1042
|
+
i && t.para(i.pa) && t.replace([i.pa], [...e.before, i.p], this.sel(), {
|
|
1043
|
+
history: !1,
|
|
1044
|
+
label: "collab"
|
|
1045
|
+
});
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
let n = this.locate(e.next[0].pid)[0];
|
|
1049
|
+
if (!n) return;
|
|
1050
|
+
let r = e.next.map((e, t) => n.pa.replace(/\d+$/, String(n.index + t)));
|
|
1051
|
+
if (r.some((n, r) => t.para(n)?.pid !== e.next[r].pid)) return;
|
|
1052
|
+
t.replace(r, e.before, this.sel(), {
|
|
1053
|
+
history: !1,
|
|
1054
|
+
label: "collab"
|
|
1055
|
+
});
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
let n = e.next.length ? B(e.next[0]) : null, r = n ? this.topIndex(n) : e.prev ? this.topIndex(e.prev) + 1 : 0;
|
|
1059
|
+
if (r < 0) return;
|
|
1060
|
+
let i = new Map(this.st.blocks.slice(r, r + e.next.length).flatMap((e) => e.t === "p" && e.pid !== void 0 ? [[e.pid, e]] : []));
|
|
1061
|
+
t.replaceBlocks(r, e.next.length, e.before.map((e) => e.t === "p" && e.pid !== void 0 && i.has(e.pid) ? i.get(e.pid) : e), null, !1);
|
|
1062
|
+
}
|
|
1063
|
+
toD(e) {
|
|
1064
|
+
let t = this.ed.para(e.pa);
|
|
1065
|
+
if (!t || t.pid === void 0) return null;
|
|
1066
|
+
let n = this.locate(t.pid);
|
|
1067
|
+
return {
|
|
1068
|
+
pid: t.pid,
|
|
1069
|
+
k: Math.max(0, n.findIndex((t) => t.pa === e.pa)),
|
|
1070
|
+
o: e.o
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
fromD(e) {
|
|
1074
|
+
let t = this.locate(e.pid), n = t[Math.min(e.k, t.length - 1)];
|
|
1075
|
+
return n ? {
|
|
1076
|
+
pa: n.pa,
|
|
1077
|
+
o: Math.min(e.o, g(n.p.runs))
|
|
1078
|
+
} : null;
|
|
1079
|
+
}
|
|
1080
|
+
capture() {
|
|
1081
|
+
if (this.snap || !this.host) return;
|
|
1082
|
+
let e = !!this.ed.input.readSel(), t = this.ed.input.sel(), n = t && this.toD(t.a), r = t && this.toD(t.f);
|
|
1083
|
+
if (!n || !r) return;
|
|
1084
|
+
let i = /* @__PURE__ */ new Map();
|
|
1085
|
+
for (let e of [n, r]) {
|
|
1086
|
+
let t = this.locate(e.pid)[e.k];
|
|
1087
|
+
t && i.set(`${e.pid}:${e.k}`, y(t.p.runs));
|
|
1088
|
+
}
|
|
1089
|
+
this.snap = {
|
|
1090
|
+
a: n,
|
|
1091
|
+
f: r,
|
|
1092
|
+
text: i,
|
|
1093
|
+
focused: e
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
sel() {
|
|
1097
|
+
let e = this.snap, t = {
|
|
1098
|
+
pa: "0",
|
|
1099
|
+
o: 0
|
|
1100
|
+
};
|
|
1101
|
+
if (!e) return this.ed.input.lastSel ?? {
|
|
1102
|
+
a: t,
|
|
1103
|
+
f: t
|
|
1104
|
+
};
|
|
1105
|
+
let n = (n) => {
|
|
1106
|
+
let r = this.locate(n.pid)[n.k] ?? this.locate(n.pid)[0];
|
|
1107
|
+
if (!r) return this.ed.input.lastSel?.f ?? t;
|
|
1108
|
+
let i = e.text.get(`${n.pid}:${n.k}`), a = y(r.p.runs);
|
|
1109
|
+
return {
|
|
1110
|
+
pa: r.pa,
|
|
1111
|
+
o: i === void 0 ? Math.min(n.o, a.length) : G(n.o, i, a)
|
|
1112
|
+
};
|
|
1113
|
+
};
|
|
1114
|
+
return {
|
|
1115
|
+
a: n(e.a),
|
|
1116
|
+
f: n(e.f)
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
settled() {
|
|
1120
|
+
let e = this.snap, t = this.touchedModel;
|
|
1121
|
+
if (this.touchedModel = !1, !this.host) return;
|
|
1122
|
+
if (!e) {
|
|
1123
|
+
t && (this.ed.pendingSel = null);
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
let n = this.sel();
|
|
1127
|
+
this.snap = null, this.ed.pendingSel = e.focused ? n : null, e.focused || (this.ed.input.lastSel = n);
|
|
1128
|
+
}
|
|
1129
|
+
onSel = () => {
|
|
1130
|
+
!this.raf && this.host && (this.raf = requestAnimationFrame(() => {
|
|
1131
|
+
this.raf = 0;
|
|
1132
|
+
let e = this.host && this.ed.input.readSel();
|
|
1133
|
+
if (!e) return;
|
|
1134
|
+
let t = this.toD(e.a), n = this.toD(e.f);
|
|
1135
|
+
if (!t || !n) return;
|
|
1136
|
+
let r = {
|
|
1137
|
+
k: "docx",
|
|
1138
|
+
a: t,
|
|
1139
|
+
f: n
|
|
1140
|
+
}, i = JSON.stringify(r);
|
|
1141
|
+
i !== this.lastPresence && (this.lastPresence = i, this.onPresence?.(r));
|
|
1142
|
+
}));
|
|
1143
|
+
};
|
|
1144
|
+
rects(e, t) {
|
|
1145
|
+
if (!this.host) return {
|
|
1146
|
+
caret: null,
|
|
1147
|
+
sel: []
|
|
1148
|
+
};
|
|
1149
|
+
let n = e.querySelector(".de-pages"), r = this.fromD(t.a), i = this.fromD(t.f);
|
|
1150
|
+
if (!n || !r || !i) return {
|
|
1151
|
+
caret: null,
|
|
1152
|
+
sel: []
|
|
1153
|
+
};
|
|
1154
|
+
let a = R(n, i), o = null;
|
|
1155
|
+
if (a) {
|
|
1156
|
+
let e = document.createRange();
|
|
1157
|
+
try {
|
|
1158
|
+
e.setStart(a[0], a[1]), e.collapse(!0), o = e.getClientRects()[0] ?? (a[0].nodeType === 1 ? a[0].getBoundingClientRect() : e.getBoundingClientRect());
|
|
1159
|
+
} catch {
|
|
1160
|
+
o = null;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
let s = [], c = R(n, r);
|
|
1164
|
+
if (c && a && (r.pa !== i.pa || r.o !== i.o)) {
|
|
1165
|
+
let e = document.createRange();
|
|
1166
|
+
try {
|
|
1167
|
+
this.ed.cmp(r.pa, i.pa) < 0 || r.pa === i.pa && r.o < i.o ? (e.setStart(c[0], c[1]), e.setEnd(a[0], a[1])) : (e.setStart(a[0], a[1]), e.setEnd(c[0], c[1]));
|
|
1168
|
+
for (let t of Array.from(e.getClientRects())) t.width > .5 && s.push(t);
|
|
1169
|
+
} catch {}
|
|
1170
|
+
}
|
|
1171
|
+
return {
|
|
1172
|
+
caret: o,
|
|
1173
|
+
sel: s
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
}, K = (e) => e !== null, q = class {
|
|
1177
|
+
transport;
|
|
1178
|
+
adapter;
|
|
1179
|
+
opts;
|
|
1180
|
+
site = 0;
|
|
1181
|
+
rev = 0;
|
|
1182
|
+
status = "connecting";
|
|
1183
|
+
error;
|
|
1184
|
+
peers = /* @__PURE__ */ new Map();
|
|
1185
|
+
log = [];
|
|
1186
|
+
inflight = null;
|
|
1187
|
+
buffer = [];
|
|
1188
|
+
queue;
|
|
1189
|
+
joined = !1;
|
|
1190
|
+
online = !1;
|
|
1191
|
+
counter = 0;
|
|
1192
|
+
listeners = /* @__PURE__ */ new Map();
|
|
1193
|
+
presence;
|
|
1194
|
+
presenceTimer;
|
|
1195
|
+
presenceAt = 0;
|
|
1196
|
+
closed = !1;
|
|
1197
|
+
constructor(e, t, n) {
|
|
1198
|
+
this.transport = e, this.adapter = t, this.opts = n, this.queue = Promise.resolve(t.ready?.()).catch((e) => this.fail(e.message)), e.onMessage = (e) => this.enqueue(() => this.onMessage(e)), e.onStatus = (t) => {
|
|
1199
|
+
t === "open" ? (this.online = !0, e.send({
|
|
1200
|
+
t: "hello",
|
|
1201
|
+
room: n.room,
|
|
1202
|
+
user: n.user,
|
|
1203
|
+
have: this.joined ? this.rev : void 0,
|
|
1204
|
+
site: this.joined ? this.site : void 0
|
|
1205
|
+
})) : t === "closed" && (this.online = !1, this.peers.clear(), this.emit("peers", []), this.status !== "error" && this.setStatus(this.joined ? "offline" : "connecting"));
|
|
1206
|
+
}, e.connect();
|
|
1207
|
+
}
|
|
1208
|
+
get pending() {
|
|
1209
|
+
return (this.inflight?.items.length ?? 0) + this.buffer.length;
|
|
1210
|
+
}
|
|
1211
|
+
nextId() {
|
|
1212
|
+
return `${this.site}.${++this.counter}.${Math.random().toString(36).slice(2, 6)}`;
|
|
1213
|
+
}
|
|
1214
|
+
submit(e) {
|
|
1215
|
+
return new Promise((t, n) => this.enqueue(async () => {
|
|
1216
|
+
if (this.status === "error") return n(Error(this.error ?? "Collaboration stopped"));
|
|
1217
|
+
if (this.adapter.pessimistic?.(e)) this.buffer.push({
|
|
1218
|
+
op: e,
|
|
1219
|
+
applied: !1,
|
|
1220
|
+
done: t
|
|
1221
|
+
});
|
|
1222
|
+
else {
|
|
1223
|
+
let r;
|
|
1224
|
+
try {
|
|
1225
|
+
r = await this.adapter.apply(e, "local");
|
|
1226
|
+
} catch (e) {
|
|
1227
|
+
return n(e);
|
|
1228
|
+
}
|
|
1229
|
+
this.buffer.push({
|
|
1230
|
+
op: e,
|
|
1231
|
+
applied: !0,
|
|
1232
|
+
undo: r.undo
|
|
1233
|
+
}), t(r.result);
|
|
1234
|
+
}
|
|
1235
|
+
this.flush();
|
|
1236
|
+
}));
|
|
1237
|
+
}
|
|
1238
|
+
push(e, t) {
|
|
1239
|
+
this.buffer.push({
|
|
1240
|
+
op: e,
|
|
1241
|
+
applied: !0,
|
|
1242
|
+
undo: t
|
|
1243
|
+
}), this.enqueue(async () => this.flush());
|
|
1244
|
+
}
|
|
1245
|
+
setPresence(e) {
|
|
1246
|
+
if (this.presence = e, this.presenceTimer) return;
|
|
1247
|
+
let t = Math.max(0, 80 - (Date.now() - this.presenceAt));
|
|
1248
|
+
this.presenceTimer = setTimeout(() => {
|
|
1249
|
+
this.presenceTimer = void 0, this.presenceAt = Date.now(), this.online && this.joined && this.transport.send({
|
|
1250
|
+
t: "presence",
|
|
1251
|
+
data: this.presence
|
|
1252
|
+
});
|
|
1253
|
+
}, t);
|
|
1254
|
+
}
|
|
1255
|
+
on(e, t) {
|
|
1256
|
+
let n = this.listeners.get(e);
|
|
1257
|
+
return n || this.listeners.set(e, n = /* @__PURE__ */ new Set()), n.add(t), () => n.delete(t);
|
|
1258
|
+
}
|
|
1259
|
+
idle() {
|
|
1260
|
+
return this.queue;
|
|
1261
|
+
}
|
|
1262
|
+
close() {
|
|
1263
|
+
this.closed = !0, clearTimeout(this.presenceTimer), this.transport.close(), this.listeners.clear();
|
|
1264
|
+
}
|
|
1265
|
+
emit(e, t) {
|
|
1266
|
+
for (let n of this.listeners.get(e) ?? []) n(t);
|
|
1267
|
+
}
|
|
1268
|
+
setStatus(e) {
|
|
1269
|
+
this.status !== e && (this.status = e, this.emit("status", e));
|
|
1270
|
+
}
|
|
1271
|
+
fail(e) {
|
|
1272
|
+
this.error = e, this.setStatus("error"), this.emit("error", Error(e));
|
|
1273
|
+
}
|
|
1274
|
+
enqueue(e) {
|
|
1275
|
+
this.queue = this.queue.then(e).catch((e) => {
|
|
1276
|
+
console.error("[office-engine collab]", e), this.emit("error", e);
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
async onMessage(e) {
|
|
1280
|
+
if (!(this.closed || this.status === "error")) switch (e.t) {
|
|
1281
|
+
case "welcome": {
|
|
1282
|
+
if (e.reset) return this.fail("The shared document history was reset; reopen the document");
|
|
1283
|
+
let t = !this.joined;
|
|
1284
|
+
this.site = e.site, this.joined = !0, t && this.setStatus("syncing"), this.peers.clear();
|
|
1285
|
+
for (let t of e.peers) this.peers.set(t.site, t);
|
|
1286
|
+
this.emit("peers", [...this.peers.values()]);
|
|
1287
|
+
for (let t of e.log) await this.onBatch(t);
|
|
1288
|
+
this.inflight && (this.inflight.base = this.rev, this.transport.send({
|
|
1289
|
+
t: "ops",
|
|
1290
|
+
id: this.inflight.id,
|
|
1291
|
+
base: this.rev,
|
|
1292
|
+
ops: this.inflight.items.map((e) => e.op).filter(K)
|
|
1293
|
+
})), this.flush(), this.presence !== void 0 && this.transport.send({
|
|
1294
|
+
t: "presence",
|
|
1295
|
+
data: this.presence
|
|
1296
|
+
}), this.setStatus("live");
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
case "batch": return this.onBatch(e.b);
|
|
1300
|
+
case "presence": {
|
|
1301
|
+
let t = this.peers.get(e.site);
|
|
1302
|
+
t && (this.peers.set(e.site, {
|
|
1303
|
+
...t,
|
|
1304
|
+
presence: e.data
|
|
1305
|
+
}), this.emit("peers", [...this.peers.values()]));
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
case "join":
|
|
1309
|
+
this.peers.set(e.peer.site, e.peer), this.emit("peers", [...this.peers.values()]);
|
|
1310
|
+
return;
|
|
1311
|
+
case "leave":
|
|
1312
|
+
this.peers.delete(e.site), this.emit("peers", [...this.peers.values()]);
|
|
1313
|
+
return;
|
|
1314
|
+
case "error": return this.fail(e.message);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
async onBatch(e) {
|
|
1318
|
+
if (e.seq <= this.rev) return;
|
|
1319
|
+
if (e.seq !== this.rev + 1) return this.fail(`Missed operations (${this.rev + 1}…${e.seq - 1})`);
|
|
1320
|
+
if (this.inflight && e.id === this.inflight.id) await this.ack(e);
|
|
1321
|
+
else {
|
|
1322
|
+
let t = this.canonical(e);
|
|
1323
|
+
if (t === null) return;
|
|
1324
|
+
await this.remote(t), this.log.push({
|
|
1325
|
+
seq: e.seq,
|
|
1326
|
+
site: e.site,
|
|
1327
|
+
ops: t
|
|
1328
|
+
});
|
|
1329
|
+
}
|
|
1330
|
+
this.rev = e.seq;
|
|
1331
|
+
let t = this.opts.logWindow ?? 5e3;
|
|
1332
|
+
this.log.length > t && this.log.splice(0, this.log.length - t), this.flush(), this.adapter.settled?.(), this.emit("change", void 0);
|
|
1333
|
+
}
|
|
1334
|
+
canonical(e) {
|
|
1335
|
+
let t = e.ops.map((e) => e);
|
|
1336
|
+
if (e.base < e.seq - 1 && (this.log[0]?.seq ?? Infinity) > e.base + 1 && this.rev > e.base) return this.fail("Operation is too old to merge; reopen the document"), null;
|
|
1337
|
+
for (let n of this.log) if (!(n.seq <= e.base || n.site === e.site)) for (let e of n.ops) t = t.map((t) => t === null ? null : this.adapter.transform(t, e));
|
|
1338
|
+
return t.filter(K);
|
|
1339
|
+
}
|
|
1340
|
+
async revertAll(e) {
|
|
1341
|
+
for (let t = e.length - 1; t >= 0; t--) {
|
|
1342
|
+
let n = e[t];
|
|
1343
|
+
if (n.applied) {
|
|
1344
|
+
try {
|
|
1345
|
+
await this.adapter.revert(n.undo);
|
|
1346
|
+
} catch (e) {
|
|
1347
|
+
console.error("[office-engine collab] revert failed", e);
|
|
1348
|
+
}
|
|
1349
|
+
n.applied = !1, n.undo = void 0;
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
async replay(e, t) {
|
|
1354
|
+
if (e.op) try {
|
|
1355
|
+
let n = await this.adapter.apply(e.op, t);
|
|
1356
|
+
e.undo = n.undo, e.applied = !0, e.done &&= (e.done(n.result), void 0);
|
|
1357
|
+
} catch (e) {
|
|
1358
|
+
console.error("[office-engine collab] replay failed", e);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
async remote(e) {
|
|
1362
|
+
await this.adapter.prepare?.(e);
|
|
1363
|
+
let t = [...this.inflight?.items ?? [], ...this.buffer];
|
|
1364
|
+
if (!t.length) {
|
|
1365
|
+
for (let t of e) await this.applyRemote(t);
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
let n = t.map((t) => {
|
|
1369
|
+
let n = t.op;
|
|
1370
|
+
for (let t of e) n !== null && (n = this.adapter.transform(n, t));
|
|
1371
|
+
return n;
|
|
1372
|
+
}), r = t.filter((e) => e.applied && e.op).map((e) => e.op), i = n.some((e, n) => e !== t[n].op) || r.length > 0 && (this.adapter.conflicts?.(r, e) ?? !0);
|
|
1373
|
+
i && await this.revertAll(t);
|
|
1374
|
+
for (let t of e) await this.applyRemote(t);
|
|
1375
|
+
if (t.forEach((e, t) => e.op = n[t]), i) for (let e of t) !e.op || this.adapter.pessimistic?.(e.op) && !e.applied && e.done || await this.replay(e, "replay");
|
|
1376
|
+
}
|
|
1377
|
+
async applyRemote(e) {
|
|
1378
|
+
try {
|
|
1379
|
+
await this.adapter.apply(e, "remote");
|
|
1380
|
+
} catch (t) {
|
|
1381
|
+
console.error("[office-engine collab] remote op failed", t, e), this.emit("error", t);
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
async ack(e) {
|
|
1385
|
+
let t = this.inflight.items;
|
|
1386
|
+
this.inflight = null;
|
|
1387
|
+
let n = t.findIndex((e) => e.op && !e.applied);
|
|
1388
|
+
if (n >= 0) {
|
|
1389
|
+
let e = [...t.slice(n), ...this.buffer];
|
|
1390
|
+
await this.adapter.prepare?.(t.slice(n).filter((e) => e.op && !e.applied).map((e) => e.op)), await this.revertAll(e);
|
|
1391
|
+
for (let n of e) !t.includes(n) && n.op && this.adapter.pessimistic?.(n.op) && n.done || await this.replay(n, n.done ? "local" : "replay");
|
|
1392
|
+
}
|
|
1393
|
+
for (let e of t) e.done && e.done(null);
|
|
1394
|
+
this.log.push({
|
|
1395
|
+
seq: e.seq,
|
|
1396
|
+
site: e.site,
|
|
1397
|
+
ops: t.map((e) => e.op).filter(K)
|
|
1398
|
+
});
|
|
1399
|
+
}
|
|
1400
|
+
flush() {
|
|
1401
|
+
if (this.inflight || !this.buffer.length || !this.online || !this.joined || this.status === "error") return;
|
|
1402
|
+
let e = this.buffer;
|
|
1403
|
+
this.buffer = [];
|
|
1404
|
+
let t = `${this.site}-${Date.now().toString(36)}-${++this.counter}`;
|
|
1405
|
+
this.inflight = {
|
|
1406
|
+
id: t,
|
|
1407
|
+
base: this.rev,
|
|
1408
|
+
items: e
|
|
1409
|
+
}, this.transport.send({
|
|
1410
|
+
t: "ops",
|
|
1411
|
+
id: t,
|
|
1412
|
+
base: this.rev,
|
|
1413
|
+
ops: e.map((e) => e.op).filter(K)
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
}, J = "$b64";
|
|
1417
|
+
function Y(e) {
|
|
1418
|
+
let t = "";
|
|
1419
|
+
for (let n = 0; n < e.length; n += 32768) t += String.fromCharCode(...e.subarray(n, n + 32768));
|
|
1420
|
+
return btoa(t);
|
|
1421
|
+
}
|
|
1422
|
+
function me(e) {
|
|
1423
|
+
let t = atob(e), n = new Uint8Array(t.length);
|
|
1424
|
+
for (let e = 0; e < t.length; e++) n[e] = t.charCodeAt(e);
|
|
1425
|
+
return n;
|
|
1426
|
+
}
|
|
1427
|
+
function X(e) {
|
|
1428
|
+
return JSON.stringify(e, (e, t) => t instanceof Uint8Array ? { [J]: Y(t) } : t instanceof ArrayBuffer ? { [J]: Y(new Uint8Array(t)) } : t);
|
|
1429
|
+
}
|
|
1430
|
+
function Z(e) {
|
|
1431
|
+
return JSON.parse(e, (e, t) => t && typeof t == "object" && typeof t[J] == "string" && Object.keys(t).length === 1 ? me(t[J]) : t);
|
|
1432
|
+
}
|
|
1433
|
+
// src/collab/transport.ts
|
|
1434
|
+
var Q = class {
|
|
1435
|
+
url;
|
|
1436
|
+
protocols;
|
|
1437
|
+
onMessage;
|
|
1438
|
+
onStatus;
|
|
1439
|
+
ws;
|
|
1440
|
+
closed = !1;
|
|
1441
|
+
retry = 0;
|
|
1442
|
+
timer;
|
|
1443
|
+
constructor(e, t) {
|
|
1444
|
+
this.url = e, this.protocols = t;
|
|
1445
|
+
}
|
|
1446
|
+
connect() {
|
|
1447
|
+
this.closed = !1, this.open();
|
|
1448
|
+
}
|
|
1449
|
+
open() {
|
|
1450
|
+
if (this.closed) return;
|
|
1451
|
+
this.onStatus?.("connecting");
|
|
1452
|
+
let e = this.ws = new WebSocket(this.url, this.protocols);
|
|
1453
|
+
e.onopen = () => {
|
|
1454
|
+
this.retry = 0, this.onStatus?.("open");
|
|
1455
|
+
}, e.onmessage = (e) => {
|
|
1456
|
+
typeof e.data == "string" && this.onMessage?.(Z(e.data));
|
|
1457
|
+
}, e.onclose = () => {
|
|
1458
|
+
if (this.ws !== e || (this.ws = void 0, this.onStatus?.("closed"), this.closed)) return;
|
|
1459
|
+
let t = Math.min(1e4, 300 * 2 ** this.retry++) * (.75 + Math.random() * .5);
|
|
1460
|
+
this.timer = setTimeout(() => this.open(), t);
|
|
1461
|
+
}, e.onerror = () => e.close();
|
|
1462
|
+
}
|
|
1463
|
+
send(e) {
|
|
1464
|
+
this.ws?.readyState === WebSocket.OPEN && this.ws.send(X(e));
|
|
1465
|
+
}
|
|
1466
|
+
close() {
|
|
1467
|
+
this.closed = !0, clearTimeout(this.timer);
|
|
1468
|
+
let e = this.ws;
|
|
1469
|
+
this.ws = void 0, e?.close(), this.onStatus?.("closed");
|
|
1470
|
+
}
|
|
1471
|
+
}, he = class {
|
|
1472
|
+
hub;
|
|
1473
|
+
latency;
|
|
1474
|
+
onMessage;
|
|
1475
|
+
onStatus;
|
|
1476
|
+
conn;
|
|
1477
|
+
up = Promise.resolve();
|
|
1478
|
+
down = Promise.resolve();
|
|
1479
|
+
constructor(e, t = 0) {
|
|
1480
|
+
this.hub = e, this.latency = t;
|
|
1481
|
+
}
|
|
1482
|
+
delay() {
|
|
1483
|
+
let e = typeof this.latency == "function" ? this.latency() : this.latency;
|
|
1484
|
+
return new Promise((t) => e > 0 ? setTimeout(t, e) : queueMicrotask(t));
|
|
1485
|
+
}
|
|
1486
|
+
connect() {
|
|
1487
|
+
if (this.conn) return;
|
|
1488
|
+
this.onStatus?.("connecting");
|
|
1489
|
+
let e = this.conn = this.hub.connect((t) => {
|
|
1490
|
+
let n = X(t);
|
|
1491
|
+
this.down = this.down.then(() => this.delay()).then(() => {
|
|
1492
|
+
this.conn === e && this.onMessage?.(Z(n));
|
|
1493
|
+
});
|
|
1494
|
+
});
|
|
1495
|
+
queueMicrotask(() => this.onStatus?.("open"));
|
|
1496
|
+
}
|
|
1497
|
+
send(e) {
|
|
1498
|
+
let t = this.conn;
|
|
1499
|
+
if (!t) return;
|
|
1500
|
+
let n = X(e);
|
|
1501
|
+
this.up = this.up.then(() => this.delay()).then(() => {
|
|
1502
|
+
this.conn === t && t.receive(Z(n));
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
drop() {
|
|
1506
|
+
let e = this.conn;
|
|
1507
|
+
this.conn = void 0, e?.close(), this.onStatus?.("closed");
|
|
1508
|
+
}
|
|
1509
|
+
close() {
|
|
1510
|
+
this.drop();
|
|
1511
|
+
}
|
|
1512
|
+
}, ge = /* @__PURE__ */ new Set([
|
|
1513
|
+
"xlsx.set",
|
|
1514
|
+
"xlsx.format",
|
|
1515
|
+
"xlsx.sheetOp",
|
|
1516
|
+
"xlsx.replace",
|
|
1517
|
+
"xlsx.bookOp",
|
|
1518
|
+
"xlsx.undo",
|
|
1519
|
+
"xlsx.redo"
|
|
1520
|
+
]), _e = class {
|
|
1521
|
+
client;
|
|
1522
|
+
submit;
|
|
1523
|
+
nextId;
|
|
1524
|
+
onPresence;
|
|
1525
|
+
undoStack = [];
|
|
1526
|
+
redoStack = [];
|
|
1527
|
+
offLocal;
|
|
1528
|
+
constructor(e) {
|
|
1529
|
+
this.client = e, this.offLocal = m(e, (e) => {
|
|
1530
|
+
let t = e, n = this.path(t.sheet);
|
|
1531
|
+
n && this.onPresence?.({
|
|
1532
|
+
k: "xlsx",
|
|
1533
|
+
sheet: n,
|
|
1534
|
+
range: t.range,
|
|
1535
|
+
ar: t.ar,
|
|
1536
|
+
ac: t.ac
|
|
1537
|
+
});
|
|
1538
|
+
});
|
|
1539
|
+
}
|
|
1540
|
+
dispose() {
|
|
1541
|
+
this.offLocal();
|
|
1542
|
+
}
|
|
1543
|
+
get meta() {
|
|
1544
|
+
return this.client.info.xlsx;
|
|
1545
|
+
}
|
|
1546
|
+
path(e) {
|
|
1547
|
+
return this.meta.sheets[e]?.path ?? "";
|
|
1548
|
+
}
|
|
1549
|
+
id() {
|
|
1550
|
+
return this.nextId();
|
|
1551
|
+
}
|
|
1552
|
+
captures(e) {
|
|
1553
|
+
return ge.has(e);
|
|
1554
|
+
}
|
|
1555
|
+
intercept(e, t) {
|
|
1556
|
+
if (!this.submit) return;
|
|
1557
|
+
let n = t;
|
|
1558
|
+
switch (e) {
|
|
1559
|
+
case "xlsx.set": return this.local({
|
|
1560
|
+
t: "set",
|
|
1561
|
+
id: this.id(),
|
|
1562
|
+
sheet: this.path(n[0]),
|
|
1563
|
+
cells: n[1]
|
|
1564
|
+
});
|
|
1565
|
+
case "xlsx.format": return this.local({
|
|
1566
|
+
t: "fmt",
|
|
1567
|
+
id: this.id(),
|
|
1568
|
+
sheet: this.path(n[0]),
|
|
1569
|
+
range: n[1],
|
|
1570
|
+
patch: n[2]
|
|
1571
|
+
});
|
|
1572
|
+
case "xlsx.sheetOp": return this.local({
|
|
1573
|
+
t: "sheet",
|
|
1574
|
+
id: this.id(),
|
|
1575
|
+
sheet: this.path(n[0]),
|
|
1576
|
+
op: n[1]
|
|
1577
|
+
});
|
|
1578
|
+
case "xlsx.replace": return this.local({
|
|
1579
|
+
t: "replace",
|
|
1580
|
+
id: this.id(),
|
|
1581
|
+
sheet: this.path(n[0]),
|
|
1582
|
+
query: n[1],
|
|
1583
|
+
by: n[2],
|
|
1584
|
+
opts: n[3],
|
|
1585
|
+
at: n[4]
|
|
1586
|
+
});
|
|
1587
|
+
case "xlsx.bookOp": return this.local({
|
|
1588
|
+
t: "book",
|
|
1589
|
+
id: this.id(),
|
|
1590
|
+
op: this.pbook(n[0])
|
|
1591
|
+
});
|
|
1592
|
+
case "xlsx.undo": return this.history(this.undoStack, this.redoStack, "prev");
|
|
1593
|
+
case "xlsx.redo": return this.history(this.redoStack, this.undoStack, "next");
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
pbook(e) {
|
|
1597
|
+
switch (e.op) {
|
|
1598
|
+
case "add": return {
|
|
1599
|
+
op: "add",
|
|
1600
|
+
name: e.name,
|
|
1601
|
+
after: e.after === void 0 ? void 0 : this.path(e.after)
|
|
1602
|
+
};
|
|
1603
|
+
case "rename": return {
|
|
1604
|
+
op: "rename",
|
|
1605
|
+
path: this.path(e.index),
|
|
1606
|
+
name: e.name
|
|
1607
|
+
};
|
|
1608
|
+
case "delete": return {
|
|
1609
|
+
op: "delete",
|
|
1610
|
+
path: this.path(e.index)
|
|
1611
|
+
};
|
|
1612
|
+
case "move": return {
|
|
1613
|
+
op: "move",
|
|
1614
|
+
path: this.path(e.from),
|
|
1615
|
+
to: e.to
|
|
1616
|
+
};
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
local(e) {
|
|
1620
|
+
if (e.t === "sheet" && (e.op.op === "insertRows" || e.op.op === "insertCols")) {
|
|
1621
|
+
let t = {
|
|
1622
|
+
op: e.op.op === "insertRows" ? "deleteRows" : "deleteCols",
|
|
1623
|
+
at: e.op.at,
|
|
1624
|
+
n: e.op.n
|
|
1625
|
+
};
|
|
1626
|
+
this.undoStack.push({
|
|
1627
|
+
kind: "inverse",
|
|
1628
|
+
sheet: e.sheet,
|
|
1629
|
+
undo: t,
|
|
1630
|
+
redo: e.op
|
|
1631
|
+
});
|
|
1632
|
+
} else n(e) || this.undoStack.push({
|
|
1633
|
+
kind: "op",
|
|
1634
|
+
id: e.id
|
|
1635
|
+
});
|
|
1636
|
+
return this.undoStack.length > 200 && this.undoStack.shift(), this.redoStack = [], this.submit(e);
|
|
1637
|
+
}
|
|
1638
|
+
history(e, t, n) {
|
|
1639
|
+
let r = e.pop();
|
|
1640
|
+
return r ? (t.push(r), r.kind === "op" ? this.submit({
|
|
1641
|
+
t: "revert",
|
|
1642
|
+
id: this.id(),
|
|
1643
|
+
target: r.id,
|
|
1644
|
+
which: n
|
|
1645
|
+
}) : this.submit({
|
|
1646
|
+
t: "sheet",
|
|
1647
|
+
id: this.id(),
|
|
1648
|
+
sheet: r.sheet,
|
|
1649
|
+
op: n === "prev" ? r.undo : r.redo
|
|
1650
|
+
})) : Promise.resolve(null);
|
|
1651
|
+
}
|
|
1652
|
+
transform(t, n) {
|
|
1653
|
+
return e(t, n);
|
|
1654
|
+
}
|
|
1655
|
+
conflicts(e, n) {
|
|
1656
|
+
return t(e, n);
|
|
1657
|
+
}
|
|
1658
|
+
pessimistic(e) {
|
|
1659
|
+
return n(e);
|
|
1660
|
+
}
|
|
1661
|
+
async apply(e, t) {
|
|
1662
|
+
let { res: r, rec: i } = await this.client.direct("collab.xlsx.apply", e);
|
|
1663
|
+
if (n(e)) {
|
|
1664
|
+
let e = (e) => e.kind === "inverse" && t === "local";
|
|
1665
|
+
this.undoStack = this.undoStack.filter(e), this.redoStack = this.redoStack.filter(e);
|
|
1666
|
+
}
|
|
1667
|
+
return t !== "local" && r && this.client.notify("collab:xlsx", r), {
|
|
1668
|
+
result: r,
|
|
1669
|
+
undo: i
|
|
1670
|
+
};
|
|
1671
|
+
}
|
|
1672
|
+
async revert(e) {
|
|
1673
|
+
if (typeof e != "number") return;
|
|
1674
|
+
let t = await this.client.direct("collab.xlsx.revert", e);
|
|
1675
|
+
t && this.client.notify("collab:xlsx", t);
|
|
1676
|
+
}
|
|
1677
|
+
};
|
|
1678
|
+
// src/collab/pptx-ops.ts
|
|
1679
|
+
function ve(e, t) {
|
|
1680
|
+
return t.t === "slides" && t.op.op === "delete" && "slide" in e && e.slide === t.op.path ? null : e;
|
|
1681
|
+
}
|
|
1682
|
+
function ye(e, t) {
|
|
1683
|
+
if (t.some((e) => e.t === "slides" || e.t === "revert") || e.some((e) => e.t === "revert")) return !0;
|
|
1684
|
+
let n = new Set(e.flatMap((e) => "slide" in e && e.t !== "image" ? [e.slide] : []));
|
|
1685
|
+
return t.some((e) => "slide" in e && e.t !== "image" && n.has(e.slide));
|
|
1686
|
+
}
|
|
1687
|
+
// src/collab/pptx.ts
|
|
1688
|
+
var be = /* @__PURE__ */ new Set([
|
|
1689
|
+
"pptx.setText",
|
|
1690
|
+
"pptx.shapes",
|
|
1691
|
+
"pptx.slides",
|
|
1692
|
+
"pptx.image",
|
|
1693
|
+
"pptx.undo"
|
|
1694
|
+
]), xe = class {
|
|
1695
|
+
client;
|
|
1696
|
+
submit;
|
|
1697
|
+
nextId;
|
|
1698
|
+
onPresence;
|
|
1699
|
+
undoStack = [];
|
|
1700
|
+
redoStack = [];
|
|
1701
|
+
ridKey = /* @__PURE__ */ new Map();
|
|
1702
|
+
offLocal;
|
|
1703
|
+
constructor(e) {
|
|
1704
|
+
this.client = e, this.offLocal = m(e, (e) => {
|
|
1705
|
+
let t = e, n = this.path(t.slide);
|
|
1706
|
+
n && this.onPresence?.({
|
|
1707
|
+
k: "pptx",
|
|
1708
|
+
slide: n,
|
|
1709
|
+
top: t.top
|
|
1710
|
+
});
|
|
1711
|
+
});
|
|
1712
|
+
}
|
|
1713
|
+
dispose() {
|
|
1714
|
+
this.offLocal();
|
|
1715
|
+
}
|
|
1716
|
+
get meta() {
|
|
1717
|
+
return this.client.info.pptx;
|
|
1718
|
+
}
|
|
1719
|
+
path(e) {
|
|
1720
|
+
return this.meta.slides[e]?.path ?? "";
|
|
1721
|
+
}
|
|
1722
|
+
id() {
|
|
1723
|
+
return this.nextId();
|
|
1724
|
+
}
|
|
1725
|
+
captures(e) {
|
|
1726
|
+
return be.has(e);
|
|
1727
|
+
}
|
|
1728
|
+
intercept(e, t) {
|
|
1729
|
+
if (!this.submit) return;
|
|
1730
|
+
let n = t;
|
|
1731
|
+
switch (e) {
|
|
1732
|
+
case "pptx.setText": return this.local({
|
|
1733
|
+
t: "text",
|
|
1734
|
+
id: this.id(),
|
|
1735
|
+
slide: this.path(n[0]),
|
|
1736
|
+
sid: n[1],
|
|
1737
|
+
paras: n[2]
|
|
1738
|
+
}).then((e) => e.model);
|
|
1739
|
+
case "pptx.shapes": {
|
|
1740
|
+
let e = n[1].map((e) => e.op === "insert" && e.rid && this.ridKey.has(e.rid) ? {
|
|
1741
|
+
...e,
|
|
1742
|
+
rid: void 0,
|
|
1743
|
+
key: this.ridKey.get(e.rid)
|
|
1744
|
+
} : e);
|
|
1745
|
+
return this.local({
|
|
1746
|
+
t: "shapes",
|
|
1747
|
+
id: this.id(),
|
|
1748
|
+
slide: this.path(n[0]),
|
|
1749
|
+
ops: e
|
|
1750
|
+
}).then((e) => e.model);
|
|
1751
|
+
}
|
|
1752
|
+
case "pptx.image": {
|
|
1753
|
+
let e = `${this.id()}`, t = new Uint8Array(n[1].slice(0));
|
|
1754
|
+
return this.submit({
|
|
1755
|
+
t: "image",
|
|
1756
|
+
id: e,
|
|
1757
|
+
key: e,
|
|
1758
|
+
slide: this.path(n[0]),
|
|
1759
|
+
bytes: t,
|
|
1760
|
+
ext: n[2]
|
|
1761
|
+
}).then((t) => {
|
|
1762
|
+
let n = t.rid;
|
|
1763
|
+
return n && this.ridKey.set(n, e), n;
|
|
1764
|
+
});
|
|
1765
|
+
}
|
|
1766
|
+
case "pptx.slides": return this.submit({
|
|
1767
|
+
t: "slides",
|
|
1768
|
+
id: this.id(),
|
|
1769
|
+
op: this.pslide(n[0])
|
|
1770
|
+
}).then((e) => e?.meta ?? this.meta);
|
|
1771
|
+
case "pptx.undo": return this.history(n[0] ? this.redoStack : this.undoStack, n[0] ? this.undoStack : this.redoStack, n[0] ? "next" : "prev");
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
pslide(e) {
|
|
1775
|
+
switch (e.op) {
|
|
1776
|
+
case "add": return {
|
|
1777
|
+
op: "add",
|
|
1778
|
+
after: e.after >= 0 && this.path(e.after) || null,
|
|
1779
|
+
layout: e.layout
|
|
1780
|
+
};
|
|
1781
|
+
case "duplicate": return {
|
|
1782
|
+
op: "duplicate",
|
|
1783
|
+
path: this.path(e.index)
|
|
1784
|
+
};
|
|
1785
|
+
case "delete": return {
|
|
1786
|
+
op: "delete",
|
|
1787
|
+
path: this.path(e.index)
|
|
1788
|
+
};
|
|
1789
|
+
case "move": return {
|
|
1790
|
+
op: "move",
|
|
1791
|
+
path: this.path(e.from),
|
|
1792
|
+
to: e.to
|
|
1793
|
+
};
|
|
1794
|
+
case "hide": return {
|
|
1795
|
+
op: "hide",
|
|
1796
|
+
path: this.path(e.index),
|
|
1797
|
+
hidden: e.hidden
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
local(e) {
|
|
1802
|
+
return this.undoStack.push(e.id), this.undoStack.length > 200 && this.undoStack.shift(), this.redoStack = [], this.submit(e);
|
|
1803
|
+
}
|
|
1804
|
+
async history(e, t, n) {
|
|
1805
|
+
let r = e.pop();
|
|
1806
|
+
if (!r) return null;
|
|
1807
|
+
t.push(r);
|
|
1808
|
+
let i = await this.submit({
|
|
1809
|
+
t: "revert",
|
|
1810
|
+
id: this.id(),
|
|
1811
|
+
target: r,
|
|
1812
|
+
which: n
|
|
1813
|
+
});
|
|
1814
|
+
return i.model && i.slide !== void 0 ? {
|
|
1815
|
+
index: i.slide,
|
|
1816
|
+
model: i.model
|
|
1817
|
+
} : null;
|
|
1818
|
+
}
|
|
1819
|
+
transform(e, t) {
|
|
1820
|
+
return ve(e, t);
|
|
1821
|
+
}
|
|
1822
|
+
conflicts(e, t) {
|
|
1823
|
+
return ye(e, t);
|
|
1824
|
+
}
|
|
1825
|
+
pessimistic(e) {
|
|
1826
|
+
return e.t === "slides";
|
|
1827
|
+
}
|
|
1828
|
+
prepare(e) {
|
|
1829
|
+
e.some((e) => e.t === "slides") && o(this.client, "pptx")?.endEditing();
|
|
1830
|
+
}
|
|
1831
|
+
async apply(e, t) {
|
|
1832
|
+
let { res: n, rec: r } = await this.client.direct("collab.pptx.apply", e);
|
|
1833
|
+
return e.t === "slides" && (this.undoStack = [], this.redoStack = [], n.meta && Object.assign(this.meta, n.meta)), t !== "local" && this.client.notify("collab:pptx", n), {
|
|
1834
|
+
result: n,
|
|
1835
|
+
undo: r
|
|
1836
|
+
};
|
|
1837
|
+
}
|
|
1838
|
+
async revert(e) {
|
|
1839
|
+
typeof e == "number" && this.client.notify("collab:pptx", await this.client.direct("collab.pptx.revert", e));
|
|
1840
|
+
}
|
|
1841
|
+
}, $ = [
|
|
1842
|
+
"#1a73e8",
|
|
1843
|
+
"#e8710a",
|
|
1844
|
+
"#188038",
|
|
1845
|
+
"#d93025",
|
|
1846
|
+
"#9334e6",
|
|
1847
|
+
"#c5221f",
|
|
1848
|
+
"#007b83",
|
|
1849
|
+
"#b06000",
|
|
1850
|
+
"#3f51b5",
|
|
1851
|
+
"#e52592"
|
|
1852
|
+
], Se = class {
|
|
1853
|
+
opts;
|
|
1854
|
+
rooms = /* @__PURE__ */ new Map();
|
|
1855
|
+
constructor(e = {}) {
|
|
1856
|
+
this.opts = e;
|
|
1857
|
+
}
|
|
1858
|
+
room(e) {
|
|
1859
|
+
let t = this.rooms.get(e);
|
|
1860
|
+
if (!t) {
|
|
1861
|
+
let n = this.opts.loadLog?.(e) ?? [];
|
|
1862
|
+
t = {
|
|
1863
|
+
id: e,
|
|
1864
|
+
seq: n.length ? n[n.length - 1].seq : 0,
|
|
1865
|
+
log: n,
|
|
1866
|
+
ids: new Set(n.map((e) => e.id)),
|
|
1867
|
+
peers: /* @__PURE__ */ new Map(),
|
|
1868
|
+
nextSite: 1
|
|
1869
|
+
};
|
|
1870
|
+
for (let e of n) t.nextSite = Math.max(t.nextSite, e.site + 1);
|
|
1871
|
+
this.rooms.set(e, t);
|
|
1872
|
+
}
|
|
1873
|
+
return t;
|
|
1874
|
+
}
|
|
1875
|
+
reset(e) {
|
|
1876
|
+
let t = this.rooms.get(e);
|
|
1877
|
+
if (t) for (let e of t.peers.values()) e.send({
|
|
1878
|
+
t: "error",
|
|
1879
|
+
message: "Room was reset"
|
|
1880
|
+
});
|
|
1881
|
+
this.rooms.delete(e);
|
|
1882
|
+
}
|
|
1883
|
+
connect(e) {
|
|
1884
|
+
let t, n = 0, r = (e, n) => {
|
|
1885
|
+
if (t) for (let [r, i] of t.peers) r !== n && i.send(e);
|
|
1886
|
+
};
|
|
1887
|
+
return {
|
|
1888
|
+
receive: (i) => {
|
|
1889
|
+
if (i.t === "hello") {
|
|
1890
|
+
if (t) return;
|
|
1891
|
+
t = this.room(i.room), n = i.site && i.site < t.nextSite && !t.peers.has(i.site) ? i.site : t.nextSite++;
|
|
1892
|
+
let a = new Set([...t.peers.values()].map((e) => e.info.color)), o = i.user.color || $.find((e) => !a.has(e)) || $[n % $.length], s = {
|
|
1893
|
+
...i.user,
|
|
1894
|
+
site: n,
|
|
1895
|
+
color: o
|
|
1896
|
+
}, c = i.have !== void 0 && i.have > t.seq, l = i.have !== void 0 && !c ? i.have : 0;
|
|
1897
|
+
e({
|
|
1898
|
+
t: "welcome",
|
|
1899
|
+
site: n,
|
|
1900
|
+
seq: t.seq,
|
|
1901
|
+
from: l,
|
|
1902
|
+
reset: c,
|
|
1903
|
+
log: t.log.filter((e) => e.seq > l),
|
|
1904
|
+
peers: [...t.peers.values()].map((e) => e.info)
|
|
1905
|
+
}), t.peers.set(n, {
|
|
1906
|
+
info: s,
|
|
1907
|
+
send: e
|
|
1908
|
+
}), r({
|
|
1909
|
+
t: "join",
|
|
1910
|
+
peer: s
|
|
1911
|
+
}, n);
|
|
1912
|
+
} else {
|
|
1913
|
+
if (!t) return e({
|
|
1914
|
+
t: "error",
|
|
1915
|
+
message: "Not joined"
|
|
1916
|
+
});
|
|
1917
|
+
if (i.t === "ops") {
|
|
1918
|
+
if (t.ids.has(i.id)) return;
|
|
1919
|
+
let e = {
|
|
1920
|
+
seq: ++t.seq,
|
|
1921
|
+
site: n,
|
|
1922
|
+
base: Math.min(i.base, t.seq - 1),
|
|
1923
|
+
id: i.id,
|
|
1924
|
+
ops: i.ops
|
|
1925
|
+
};
|
|
1926
|
+
t.log.push(e), t.ids.add(e.id), this.opts.onBatch?.(t.id, e), r({
|
|
1927
|
+
t: "batch",
|
|
1928
|
+
b: e
|
|
1929
|
+
});
|
|
1930
|
+
} else if (i.t === "presence") {
|
|
1931
|
+
let e = t.peers.get(n);
|
|
1932
|
+
e && (e.info.presence = i.data), r({
|
|
1933
|
+
t: "presence",
|
|
1934
|
+
site: n,
|
|
1935
|
+
data: i.data
|
|
1936
|
+
}, n);
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
},
|
|
1940
|
+
close: () => {
|
|
1941
|
+
t && t.peers.has(n) && (t.peers.delete(n), r({
|
|
1942
|
+
t: "leave",
|
|
1943
|
+
site: n
|
|
1944
|
+
}), t.peers.size || this.opts.onIdle?.(t.id));
|
|
1945
|
+
}
|
|
1946
|
+
};
|
|
1947
|
+
}
|
|
1948
|
+
};
|
|
1949
|
+
// src/collab/index.ts
|
|
1950
|
+
function Ce(e, t) {
|
|
1951
|
+
let n = typeof t.transport == "string" ? new Q(t.transport) : t.transport, r = e.kind === "docx" ? new pe(e) : void 0, i = r ?? (e.kind === "xlsx" ? new _e(e) : new xe(e)), a = new q(n, i, {
|
|
1952
|
+
room: t.room,
|
|
1953
|
+
user: t.user
|
|
1954
|
+
});
|
|
1955
|
+
i.submit = (e) => a.submit(e), i.push = (e, t) => a.push(e, t), i.nextId = () => a.nextId(), i.onPresence = (e) => a.setPresence(e), e.interceptor = (e, t) => {
|
|
1956
|
+
if (i.captures(e)) return a.status === "connecting" || a.status === "syncing" || a.status === "error" ? Promise.reject(Error(a.status === "error" ? a.error ?? "Collaboration stopped" : "Document is still syncing")) : i.intercept(e, t);
|
|
1957
|
+
};
|
|
1958
|
+
let o = [a.on("peers", (t) => u(e, t)), a.on("status", (e) => {
|
|
1959
|
+
e === "live" && i.setSite?.(a.site);
|
|
1960
|
+
})];
|
|
1961
|
+
return {
|
|
1962
|
+
session: a,
|
|
1963
|
+
get status() {
|
|
1964
|
+
return a.status;
|
|
1965
|
+
},
|
|
1966
|
+
get peers() {
|
|
1967
|
+
return [...a.peers.values()];
|
|
1968
|
+
},
|
|
1969
|
+
get pending() {
|
|
1970
|
+
return a.pending;
|
|
1971
|
+
},
|
|
1972
|
+
on: (e, t) => a.on(e, t),
|
|
1973
|
+
rects: (e, t) => {
|
|
1974
|
+
let n = t.presence;
|
|
1975
|
+
return r && n?.k === "docx" ? r.rects(e, n) : null;
|
|
1976
|
+
},
|
|
1977
|
+
close() {
|
|
1978
|
+
o.forEach((e) => e()), a.close(), e.interceptor &&= void 0, i.dispose?.(), u(e, []);
|
|
1979
|
+
}
|
|
1980
|
+
};
|
|
1981
|
+
}
|
|
1982
|
+
export { f as C, p as E, S, a as T, x as _, Z as a, y as b, se as c, j as d, A as f, re as g, _ as h, Q as i, R as l, L as m, Se as n, X as o, T as p, he as r, q as s, Ce as t, z as u, g as v, ee as w, v as x, C as y };
|
|
1983
|
+
|
|
1984
|
+
//# sourceMappingURL=collab-BzN1ZI-z.js.map
|