@p10node/office-engine 0.1.1 → 0.2.0

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.
@@ -46,16 +46,16 @@ function p(e, t) {
46
46
  function ee(e, t) {
47
47
  for (let n of c.get(e)?.localFns ?? []) n(t);
48
48
  }
49
- function m(e, t) {
49
+ function te(e, t) {
50
50
  let n = l(e);
51
51
  return n.localFns.add(t), () => n.localFns.delete(t);
52
52
  }
53
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) {
54
+ var m = (e) => e.t === "text" ? e.text.length : 1, h = (e) => e.reduce((e, t) => e + m(t), 0), g = (e) => e.a.pa === e.f.pa && e.a.o === e.f.o, _ = (e, t) => e.replace(/(\d+)$/, (e) => String(Number(e) + t));
55
+ function v(e) {
56
56
  return e.map((e) => e.t === "text" ? e.text : "").join("");
57
57
  }
58
- function b(e, t) {
58
+ function y(e, t) {
59
59
  if (e === t) return !0;
60
60
  if (!e || !t) return !e && !t;
61
61
  let n = Object.keys(e);
@@ -63,23 +63,23 @@ function b(e, t) {
63
63
  for (let r of n) if (e[r] !== t[r]) return !1;
64
64
  return !0;
65
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) {
66
+ var ne = (e, t) => e.t === "text" && t.t === "text" && e.href === t.href && e.lk === t.lk && y(e.s, t.s) && y(e.d, t.d);
67
+ function b(e) {
68
68
  let t = [];
69
69
  for (let n of e) {
70
70
  if (n.t === "text" && !n.text) continue;
71
71
  let e = t[t.length - 1];
72
- e && te(e, n) ? t[t.length - 1] = {
72
+ e && ne(e, n) ? t[t.length - 1] = {
73
73
  ...e,
74
74
  text: e.text + n.text
75
75
  } : t.push(n);
76
76
  }
77
77
  return t;
78
78
  }
79
- function S(e, t) {
79
+ function x(e, t) {
80
80
  let n = [], r = [], i = 0;
81
81
  for (let a of e) {
82
- let e = h(a);
82
+ let e = m(a);
83
83
  if (i + e <= t) n.push(a);
84
84
  else if (i >= t) r.push(a);
85
85
  else {
@@ -96,14 +96,14 @@ function S(e, t) {
96
96
  }
97
97
  return [n, r];
98
98
  }
99
- function C(e, t) {
100
- let [n, r] = S(e, t);
99
+ function S(e, t) {
100
+ let [n, r] = x(e, t);
101
101
  for (let e = n.length - 1; e >= 0; e--) if (n[e].t === "text") return n[e];
102
102
  return r.find((e) => e.t === "text");
103
103
  }
104
- function ne(e, t, n, r, i) {
104
+ function re(e, t, n, r, i) {
105
105
  if (!n) return e;
106
- let [a, o] = S(e, t), s = C(e, t), c = {
106
+ let [a, o] = x(e, t), s = S(e, t), c = {
107
107
  t: "text",
108
108
  ...s ?? i ?? {},
109
109
  ...r ?? {},
@@ -115,17 +115,17 @@ function ne(e, t, n, r, i) {
115
115
  }), (r?.d || s?.d) && (c.d = {
116
116
  ...s?.d ?? {},
117
117
  ...r?.d ?? {}
118
- }), c.d && !Object.keys(c.d).length && delete c.d, x([
118
+ }), c.d && !Object.keys(c.d).length && delete c.d, b([
119
119
  ...a,
120
120
  c,
121
121
  ...o
122
122
  ]);
123
123
  }
124
- function w(e, t, n) {
125
- return n <= t ? e : x([...S(e, t)[0], ...S(e, n)[1]]);
124
+ function C(e, t, n) {
125
+ return n <= t ? e : b([...x(e, t)[0], ...x(e, n)[1]]);
126
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" ? {
127
+ function ie(e, t, n, r, i) {
128
+ let [a, o] = x(e, t), [s, c] = x(o, n - t), l = s.map((e) => e.t === "text" ? {
129
129
  ...e,
130
130
  s: {
131
131
  ...e.s ?? {},
@@ -136,13 +136,13 @@ function re(e, t, n, r, i) {
136
136
  ...i
137
137
  }
138
138
  } : e);
139
- return x([
139
+ return b([
140
140
  ...a,
141
141
  ...l,
142
142
  ...c
143
143
  ]);
144
144
  }
145
- function ie(e, t) {
145
+ function ae(e, t) {
146
146
  if (t <= 0) return 0;
147
147
  let n = Intl.Segmenter;
148
148
  if (n) {
@@ -153,7 +153,7 @@ function ie(e, t) {
153
153
  let r = e.charCodeAt(t - 1);
154
154
  return r >= 56320 && r <= 57343 && t >= 2 ? t - 2 : t - 1;
155
155
  }
156
- function ae(e, t) {
156
+ function oe(e, t) {
157
157
  if (t >= e.length) return e.length;
158
158
  let n = Intl.Segmenter;
159
159
  if (n) for (let r of new n(void 0, { granularity: "grapheme" }).segment(e.slice(t))) return t + r.segment.length;
@@ -161,39 +161,39 @@ function ae(e, t) {
161
161
  return r >= 55296 && r <= 56319 ? t + 2 : t + 1;
162
162
  }
163
163
  // src/react/edit/input.ts
164
- function T(e, t) {
164
+ function w(e, t) {
165
165
  let n = e.cmp(t.a.pa, t.f.pa);
166
166
  return n < 0 || n === 0 && t.a.o <= t.f.o ? [t.a, t.f] : [t.f, t.a];
167
167
  }
168
- var E = (e) => ({
168
+ var T = (e) => ({
169
169
  a: e,
170
170
  f: e
171
171
  });
172
- function D(e, t) {
173
- let [n, r] = T(e, t);
172
+ function E(e, t) {
173
+ let [n, r] = w(e, t);
174
174
  if (n.pa === r.pa) {
175
175
  let t = e.para(n.pa);
176
176
  return t ? {
177
177
  pas: [n.pa],
178
- paras: [e.withRuns(t, w(t.runs, n.o, r.o))],
178
+ paras: [e.withRuns(t, C(t.runs, n.o, r.o))],
179
179
  at: n
180
180
  } : null;
181
181
  }
182
182
  let i = e.span(n.pa, r.pa);
183
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]]);
184
+ let a = e.para(i[0]), o = e.para(i[i.length - 1]), s = b([...x(a.runs, n.o)[0], ...x(o.runs, r.o)[1]]);
185
185
  return {
186
186
  pas: i,
187
187
  paras: [e.join(a, o, s)],
188
188
  at: n
189
189
  };
190
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);
191
+ function D(e, t, n) {
192
+ let r = E(e, t);
193
+ r && e.replace(r.pas, r.paras, T(r.at), n);
194
194
  }
195
- function k(e, t) {
196
- if (!_(t)) return D(e, t);
195
+ function O(e, t) {
196
+ if (!g(t)) return E(e, t);
197
197
  let n = e.para(t.a.pa);
198
198
  return n ? {
199
199
  pas: [t.a.pa],
@@ -201,53 +201,53 @@ function k(e, t) {
201
201
  at: t.a
202
202
  } : null;
203
203
  }
204
- function A(e, t, n, r = {}, i) {
205
- let a = k(e, t);
204
+ function k(e, t, n, r = {}, i) {
205
+ let a = O(e, t);
206
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 ? {
207
+ let o = a.paras[0], s = a.at.o, [c, l] = x(o.runs, s), u = S(o.runs, s) ?? e.defaultRun?.(o), d = (e) => e.map((e) => e.t === "text" && !e.s && u ? {
208
208
  ...u,
209
209
  ...e,
210
210
  s: u.s,
211
211
  src: u.src
212
212
  } : e);
213
213
  if (n.length === 1) {
214
- let t = d(n[0]), i = x([
214
+ let t = d(n[0]), i = b([
215
215
  ...c,
216
216
  ...t,
217
217
  ...l
218
218
  ]);
219
- e.replace(a.pas, [e.withRuns(o, i)], E({
219
+ e.replace(a.pas, [e.withRuns(o, i)], T({
220
220
  pa: a.at.pa,
221
- o: s + g(t)
221
+ o: s + h(t)
222
222
  }), {
223
223
  label: "paste",
224
224
  ...r
225
225
  });
226
226
  return;
227
227
  }
228
- let f = [], p = e.withRuns(o, x([...c, ...d(n[0])]));
228
+ let f = [], p = e.withRuns(o, b([...c, ...d(n[0])]));
229
229
  i?.[0] && (p = i[0](p));
230
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);
231
+ let r = t === n.length - 1, a = d(n[t]), [o, s] = e.split(p, p.runs, b([...a, ...r ? l : []]), !r);
232
232
  f.push(o), p = i?.[t] ? i[t](s) : s;
233
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]))
234
+ f.push(p), e.replace(a.pas, f, T({
235
+ pa: _(a.at.pa, f.length - 1),
236
+ o: h(d(n[n.length - 1]))
237
237
  }), {
238
238
  label: "paste",
239
239
  ...r
240
240
  });
241
241
  }
242
- function j(e, t, n, r) {
243
- let i = k(e, t);
242
+ function A(e, t, n, r) {
243
+ let i = O(e, t);
244
244
  if (!i) return;
245
245
  let a = e.pendingStyle?.();
246
246
  e.clearPending?.();
247
247
  let o = i.paras[0], s = i.at.o, c = n.replace(/\r\n?/g, "\n").split("\n");
248
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({
249
+ let t = re(o.runs, s, n, a, e.defaultRun?.(o));
250
+ e.replace(i.pas, [e.withRuns(o, t)], T({
251
251
  pa: i.at.pa,
252
252
  o: s + n.length
253
253
  }), {
@@ -257,42 +257,42 @@ function j(e, t, n, r) {
257
257
  return;
258
258
  }
259
259
  let l = a ? {
260
- ...C(o.runs, s) ?? e.defaultRun?.(o) ?? {},
260
+ ...S(o.runs, s) ?? e.defaultRun?.(o) ?? {},
261
261
  ...a
262
262
  } : void 0;
263
- A(e, t, c.map((e) => e ? [{
263
+ k(e, t, c.map((e) => e ? [{
264
264
  ...l ?? {},
265
265
  t: "text",
266
266
  text: e
267
267
  }] : []), r);
268
268
  }
269
- function M(e, t) {
270
- let n = _(t) ? (() => {
269
+ function se(e, t) {
270
+ let n = g(t) ? (() => {
271
271
  let n = e.para(t.a.pa);
272
272
  return n ? {
273
273
  pas: [t.a.pa],
274
274
  paras: [n],
275
275
  at: t.a
276
276
  } : null;
277
- })() : D(e, t);
277
+ })() : E(e, t);
278
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),
279
+ let r = n.paras[0], [i, a] = x(r.runs, n.at.o), [o, s] = e.split(r, i, a, n.at.o >= h(r.runs));
280
+ e.replace(n.pas, [o, s], T({
281
+ pa: _(n.at.pa, 1),
282
282
  o: 0
283
283
  }), { label: "enter" });
284
284
  }
285
- function N(e, t, n = !1) {
286
- if (!_(t)) return O(e, t);
285
+ function j(e, t, n = !1) {
286
+ if (!g(t)) return D(e, t);
287
287
  let { pa: r, o: i } = t.a, a = e.para(r);
288
288
  if (!a) return;
289
289
  if (i > 0) {
290
- let t = y(a.runs), o = ie(t, i);
290
+ let t = v(a.runs), o = ae(t, i);
291
291
  if (n) {
292
292
  let e = /(\S+\s*|\s+)$/.exec(t.slice(0, i));
293
293
  e && (o = i - e[0].length);
294
294
  }
295
- e.replace([r], [e.withRuns(a, w(a.runs, o, i))], E({
295
+ e.replace([r], [e.withRuns(a, C(a.runs, o, i))], T({
296
296
  pa: r,
297
297
  o
298
298
  }), { label: "delete" });
@@ -301,23 +301,23 @@ function N(e, t, n = !1) {
301
301
  if (e.backspaceAtStart?.(r, a)) return;
302
302
  let o = e.prev(r), s = o ? e.para(o) : void 0;
303
303
  if (!o || !s) return;
304
- let c = e.join(s, a, x([...s.runs, ...a.runs]));
305
- e.replace([o, r], [c], E({
304
+ let c = e.join(s, a, b([...s.runs, ...a.runs]));
305
+ e.replace([o, r], [c], T({
306
306
  pa: o,
307
- o: g(s.runs)
307
+ o: h(s.runs)
308
308
  }), { label: "merge" });
309
309
  }
310
- function P(e, t, n = !1) {
311
- if (!_(t)) return O(e, t);
310
+ function M(e, t, n = !1) {
311
+ if (!g(t)) return D(e, t);
312
312
  let { pa: r, o: i } = t.a, a = e.para(r);
313
313
  if (!a) return;
314
- if (i < g(a.runs)) {
315
- let t = y(a.runs), o = ae(t, i);
314
+ if (i < h(a.runs)) {
315
+ let t = v(a.runs), o = oe(t, i);
316
316
  if (n) {
317
317
  let e = /^(\s*\S+|\s+)/.exec(t.slice(i));
318
318
  e && (o = i + e[0].length);
319
319
  }
320
- e.replace([r], [e.withRuns(a, w(a.runs, i, o))], E({
320
+ e.replace([r], [e.withRuns(a, C(a.runs, i, o))], T({
321
321
  pa: r,
322
322
  o: i
323
323
  }), { label: "delete" });
@@ -325,36 +325,36 @@ function P(e, t, n = !1) {
325
325
  }
326
326
  let o = e.next(r), s = o ? e.para(o) : void 0;
327
327
  if (!o || !s) return;
328
- let c = e.join(a, s, x([...a.runs, ...s.runs]));
329
- e.replace([r, o], [c], E({
328
+ let c = e.join(a, s, b([...a.runs, ...s.runs]));
329
+ e.replace([r, o], [c], T({
330
330
  pa: r,
331
331
  o: i
332
332
  }), { label: "merge" });
333
333
  }
334
- function F(e, t) {
334
+ function N(e, t) {
335
335
  let n = (e && (e.nodeType === 1 ? e : e.parentElement))?.closest("[data-o]");
336
336
  return n && t.contains(n) ? n : null;
337
337
  }
338
- function I(e) {
338
+ function P(e) {
339
339
  let t = Number(e.dataset.o);
340
340
  return e.hasAttribute("data-obj") ? t + 1 : e.hasAttribute("data-empty") ? t : t + (e.textContent?.length ?? 0);
341
341
  }
342
- function L(e, t, n) {
342
+ function F(e, t, n) {
343
343
  let r = (t.nodeType === 1 ? t : t.parentElement)?.closest("[data-pa]");
344
344
  if (!r || !e.contains(r)) return null;
345
345
  let i = r.dataset.pa;
346
- if (!F(t, r)) {
346
+ if (!N(t, r)) {
347
347
  let e = document.createRange();
348
348
  e.setStart(t, n), e.collapse(!0);
349
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);
350
+ for (let t of Array.from(r.querySelectorAll("[data-o]"))) if (e.comparePoint(t, t.childNodes.length) < 0) a = P(t);
351
351
  else break;
352
352
  return {
353
353
  pa: i,
354
354
  o: a
355
355
  };
356
356
  }
357
- let a = F(t, r), o = Number(a.dataset.o);
357
+ let a = N(t, r), o = Number(a.dataset.o);
358
358
  if (a.hasAttribute("data-empty")) return {
359
359
  pa: i,
360
360
  o
@@ -384,14 +384,14 @@ function L(e, t, n) {
384
384
  o: o + s
385
385
  };
386
386
  }
387
- function R(e, t) {
387
+ function I(e, t) {
388
388
  let n = e.querySelector(`[data-pa="${t.pa}"]`);
389
389
  if (!n) return null;
390
390
  let r = Array.from(n.querySelectorAll("[data-o]"));
391
391
  if (!r.length) return [n, n.childNodes.length];
392
392
  let i;
393
393
  for (let e of r) {
394
- let n = Number(e.dataset.o), r = I(e);
394
+ let n = Number(e.dataset.o), r = P(e);
395
395
  if (t.o >= n && t.o <= r && (i = e, t.o < r || !e.hasAttribute("data-obj"))) break;
396
396
  }
397
397
  i ??= r[r.length - 1];
@@ -412,12 +412,12 @@ function R(e, t) {
412
412
  }
413
413
  return c ? [c, c.textContent?.length ?? 0] : [i, 0];
414
414
  }
415
- function z(e) {
415
+ function L(e) {
416
416
  let t = e.getRootNode();
417
417
  return typeof ShadowRoot < "u" && t instanceof ShadowRoot && typeof t.getSelection == "function" ? t.getSelection() : (e.ownerDocument ?? document).getSelection();
418
418
  }
419
- function oe(e) {
420
- let t = e.getRootNode(), n = z(e);
419
+ function ce(e) {
420
+ let t = e.getRootNode(), n = L(e);
421
421
  if (!n) return null;
422
422
  if (typeof ShadowRoot < "u" && t instanceof ShadowRoot && n.anchorNode && !e.contains(n.anchorNode)) {
423
423
  let e = n.getComposedRanges?.call(n, { shadowRoots: [t] })[0];
@@ -430,7 +430,7 @@ function oe(e) {
430
430
  }
431
431
  return n;
432
432
  }
433
- var se = class {
433
+ var le = class {
434
434
  host;
435
435
  root = null;
436
436
  composing = null;
@@ -451,9 +451,9 @@ var se = class {
451
451
  return !!this.composing;
452
452
  }
453
453
  readSel() {
454
- let e = this.root, t = e ? oe(e) : null;
454
+ let e = this.root, t = e ? ce(e) : null;
455
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);
456
+ let n = F(e, t.anchorNode, t.anchorOffset), r = F(e, t.focusNode, t.focusOffset);
457
457
  return n && r ? {
458
458
  a: n,
459
459
  f: r
@@ -465,11 +465,11 @@ var se = class {
465
465
  restore(e, t = !0) {
466
466
  let n = this.root;
467
467
  if (!n || !e) return !1;
468
- let r = R(n, e.a), i = R(n, e.f);
468
+ let r = I(n, e.a), i = I(n, e.f);
469
469
  if (!r || !i) return this.lastSel = e, !1;
470
470
  let a = n.getRootNode().activeElement;
471
471
  t && a !== n && !n.contains(a) && n.focus({ preventScroll: !0 });
472
- let o = z(n);
472
+ let o = L(n);
473
473
  try {
474
474
  o?.setBaseAndExtent(r[0], r[1], i[0], i[1]);
475
475
  } catch {}
@@ -477,7 +477,7 @@ var se = class {
477
477
  }
478
478
  rangeSel(e) {
479
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);
480
+ let t = F(this.root, e.startContainer, e.startOffset), n = F(this.root, e.endContainer, e.endOffset);
481
481
  return t && n ? {
482
482
  a: t,
483
483
  f: n
@@ -489,7 +489,7 @@ var se = class {
489
489
  if (t === "deleteCompositionText" || t === "insertFromComposition") {
490
490
  if (e.cancelable && e.preventDefault(), t === "insertFromComposition" && e.data && e.data !== this.justComposed) {
491
491
  let t = this.sel();
492
- t && j(this.host, t, e.data, { remount: !0 });
492
+ t && A(this.host, t, e.data, { remount: !0 });
493
493
  }
494
494
  this.justComposed = "";
495
495
  return;
@@ -502,34 +502,34 @@ var se = class {
502
502
  case "insertText":
503
503
  case "insertReplacementText": {
504
504
  let a = e.data ?? e.dataTransfer?.getData("text/plain") ?? "";
505
- j(i, t === "insertReplacementText" && n ? n : r, a);
505
+ A(i, t === "insertReplacementText" && n ? n : r, a);
506
506
  break;
507
507
  }
508
508
  case "insertParagraph":
509
- M(i, r);
509
+ se(i, r);
510
510
  break;
511
511
  case "insertLineBreak":
512
- j(i, r, "\n", { label: "linebreak" });
512
+ A(i, r, "\n", { label: "linebreak" });
513
513
  break;
514
514
  case "insertFromPaste":
515
515
  case "insertFromDrop":
516
516
  case "insertFromYank": {
517
517
  if (e.dataTransfer && i.pasteRich?.(t === "insertFromDrop" && n ? n : r, e.dataTransfer)) break;
518
518
  let a = e.dataTransfer?.getData("text/plain") ?? e.data ?? "";
519
- a && j(i, r, a);
519
+ a && A(i, r, a);
520
520
  break;
521
521
  }
522
522
  case "deleteContentBackward":
523
- N(i, r);
523
+ j(i, r);
524
524
  break;
525
525
  case "deleteWordBackward":
526
- N(i, r, !0);
526
+ j(i, r, !0);
527
527
  break;
528
528
  case "deleteContentForward":
529
- P(i, r);
529
+ M(i, r);
530
530
  break;
531
531
  case "deleteWordForward":
532
- P(i, r, !0);
532
+ M(i, r, !0);
533
533
  break;
534
534
  case "deleteSoftLineBackward":
535
535
  case "deleteHardLineBackward":
@@ -539,7 +539,7 @@ var se = class {
539
539
  case "deleteByDrag":
540
540
  case "deleteContent":
541
541
  case "deleteEntireSoftLine":
542
- _(r) ? n && !_(n) && O(i, n) : O(i, r);
542
+ g(r) ? n && !g(n) && D(i, n) : D(i, r);
543
543
  break;
544
544
  case "historyUndo":
545
545
  i.undo();
@@ -561,7 +561,7 @@ var se = class {
561
561
  };
562
562
  onCompEnd = (e) => {
563
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));
564
+ this.composing = null, t && (this.justComposed = e.data ?? "", e.data ? A(this.host, t, e.data, { remount: !0 }) : A(this.host, t, "", { remount: !0 }), setTimeout(() => this.justComposed = "", 0));
565
565
  };
566
566
  onKeyDown = (e) => {
567
567
  if (e.isComposing || this.composing) return;
@@ -586,7 +586,7 @@ var se = class {
586
586
  e.preventDefault();
587
587
  let t = this.sel();
588
588
  if (!t) return;
589
- !this.host.tab?.(t, e.shiftKey) && !e.shiftKey && j(this.host, t, " ");
589
+ !this.host.tab?.(t, e.shiftKey) && !e.shiftKey && A(this.host, t, " ");
590
590
  }
591
591
  }
592
592
  };
@@ -597,35 +597,35 @@ var se = class {
597
597
  e && (this.lastSel = e), this.host.onSelection?.(e);
598
598
  });
599
599
  };
600
- }, ce = /* @__PURE__ */ new Set([
600
+ }, ue = /* @__PURE__ */ new Set([
601
601
  "open",
602
602
  "part",
603
603
  "docx.start",
604
604
  "save",
605
605
  "dirty"
606
- ]), B = (e) => e.t === "p" ? e.pid === void 0 ? null : { p: e.pid } : e.t === "tbl" ? {
606
+ ]), R = (e) => e.t === "p" ? e.pid === void 0 ? null : { p: e.pid } : e.t === "tbl" ? {
607
607
  tid: e.tid,
608
608
  item: e.item
609
609
  } : null;
610
- function V(e, t) {
610
+ function z(e, t) {
611
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
612
  }
613
- var le = (e) => [...new Set(e)];
614
- function H(e, t = []) {
613
+ var de = (e) => [...new Set(e)];
614
+ function B(e, t = []) {
615
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);
616
+ else if (n.t === "tbl") for (let e of n.rows) for (let n of e.cells) B(n.blocks, t);
617
617
  return t;
618
618
  }
619
- function U(e) {
619
+ function fe(e) {
620
620
  if (e.t !== "text") return JSON.stringify(e);
621
621
  let { text: t, ...n } = e;
622
622
  return JSON.stringify(n);
623
623
  }
624
- function ue(e, t) {
624
+ function pe(e, t) {
625
625
  let n = (e) => {
626
626
  let t = [];
627
627
  for (let n of e) {
628
- let e = U(n);
628
+ let e = fe(n);
629
629
  if (n.t === "text") for (let r = 0; r < n.text.length; r++) t.push(e + "\0" + n.text[r]);
630
630
  else t.push(e);
631
631
  }
@@ -635,25 +635,25 @@ function ue(e, t) {
635
635
  let o = 0;
636
636
  for (; o < r.length - a && o < i.length - a && r[r.length - 1 - o] === i[i.length - 1 - o];) o++;
637
637
  if (a === r.length && a === i.length) return null;
638
- let [, s] = S(t, a), [c] = S(s, i.length - a - o);
638
+ let [, s] = x(t, a), [c] = x(s, i.length - a - o);
639
639
  return {
640
640
  at: a,
641
641
  del: r.length - a - o,
642
642
  ins: c
643
643
  };
644
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);
645
+ function me(e, t, n, r) {
646
+ let i = h(e), a = Math.max(0, Math.min(t, i)), o = Math.max(0, Math.min(n, i - a)), [s, c] = x(e, a), [, l] = x(c, o);
647
647
  return [
648
648
  ...s,
649
649
  ...r,
650
650
  ...l
651
651
  ];
652
652
  }
653
- function W(e, t, n, r, i) {
653
+ function V(e, t, n, r, i) {
654
654
  return e < t || e === t && (n > 0 || i) ? e : e >= t + n ? e - n + r : t + r;
655
655
  }
656
- function G(e, t, n) {
656
+ function he(e, t, n) {
657
657
  if (t === n) return e;
658
658
  let r = 0;
659
659
  for (; r < t.length && r < n.length && t[r] === n[r];) r++;
@@ -661,7 +661,7 @@ function G(e, t, n) {
661
661
  for (; i < t.length - r && i < n.length - r && t[t.length - 1 - i] === n[n.length - 1 - i];) i++;
662
662
  return e <= r ? e : e >= t.length - i ? n.length - (t.length - e) : n.length - i;
663
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 {
664
+ var ge = (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), _e = class {
665
665
  client;
666
666
  host;
667
667
  suppress = 0;
@@ -701,7 +701,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
701
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
702
  }
703
703
  captures(e) {
704
- return !this.suppress && !ce.has(e) && e !== "docx.edit" && e !== "docx.table" && e.startsWith("docx.");
704
+ return !this.suppress && !ue.has(e) && e !== "docx.edit" && e !== "docx.table" && e.startsWith("docx.");
705
705
  }
706
706
  intercept(e, t) {
707
707
  if (this.submit && this.captures(e)) {
@@ -756,7 +756,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
756
756
  }), t;
757
757
  }
758
758
  topIndex(e) {
759
- return this.st.blocks.findIndex((t) => V(t, e));
759
+ return this.st.blocks.findIndex((t) => z(t, e));
760
760
  }
761
761
  region(e) {
762
762
  let t = new Set(e);
@@ -841,8 +841,8 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
841
841
  if (this.suppress || !this.push) return;
842
842
  if (e.kind === "paras") {
843
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);
844
+ if (t.length === 1 && n.length === 1 && t[0].pid !== void 0 && ge(t[0], n[0]) && this.locate(t[0].pid).length === 1) {
845
+ let e = pe(t[0].runs, n[0].runs);
846
846
  if (!e) return;
847
847
  this.push({
848
848
  t: "text",
@@ -868,10 +868,10 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
868
868
  }]);
869
869
  return;
870
870
  }
871
- let t = e.bi > 0 ? B(this.st.blocks[e.bi - 1]) : null;
871
+ let t = e.bi > 0 ? R(this.st.blocks[e.bi - 1]) : null;
872
872
  this.push({
873
873
  t: "blocks",
874
- before: e.before.map(B).filter((e) => !!e),
874
+ before: e.before.map(R).filter((e) => !!e),
875
875
  prev: t,
876
876
  next: e.next.map((e) => this.tag(e))
877
877
  }, [{
@@ -884,14 +884,14 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
884
884
  transform(e, t) {
885
885
  if (e.t !== "text") return e;
886
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;
887
+ let n = h(t.ins), r = V(e.at, t.at, t.del, n, !1), i = e.del ? Math.max(r, V(e.at + e.del, t.at, t.del, n, !0)) : r;
888
888
  return r === e.at && i - r === e.del ? e : {
889
889
  ...e,
890
890
  at: r,
891
891
  del: i - r
892
892
  };
893
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;
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) && !B(t.next).includes(e.pid) ? null : e;
895
895
  }
896
896
  touched(e) {
897
897
  switch (e.t) {
@@ -901,9 +901,9 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
901
901
  let t = [
902
902
  ...e.before,
903
903
  ...e.prev ? [e.prev] : [],
904
- ...e.next.map(B).filter((e) => !!e)
904
+ ...e.next.map(R).filter((e) => !!e)
905
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);
906
+ for (let n of B(e.next)) t.push("p" + n);
907
907
  return t;
908
908
  }
909
909
  default: return [];
@@ -950,7 +950,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
950
950
  if (n.length !== 1 || !t.para(n[0].pa)) return null;
951
951
  let r = n[0].p, i = {
952
952
  ...r,
953
- runs: de(r.runs, e.at, e.del, e.ins.map((e) => this.remap(e))),
953
+ runs: me(r.runs, e.at, e.del, e.ins.map((e) => this.remap(e))),
954
954
  edited: !0
955
955
  };
956
956
  return t.replace([n[0].pa], [i], this.sel(), {
@@ -966,7 +966,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
966
966
  let n = e.next.map((e) => this.remap(e)), r = new Set(n.map((e) => e.pid)), i = this.region(e.before);
967
967
  if (!i || !i.length) return null;
968
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()) {
969
+ for (let n of de([...e.before, ...r])) if (!o.has(n)) for (let e of this.locate(n).reverse()) {
970
970
  if (!t.para(e.pa)) continue;
971
971
  let n = (t) => {
972
972
  let n = e.list[t];
@@ -1006,14 +1006,14 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1006
1006
  if (!t) return null;
1007
1007
  r = this.topIndex(t);
1008
1008
  }
1009
- for (; r + i < this.st.blocks.length && e.before.some((e) => V(this.st.blocks[r + i], e));) i++;
1009
+ for (; r + i < this.st.blocks.length && e.before.some((e) => z(this.st.blocks[r + i], e));) i++;
1010
1010
  } else if (r = e.prev ? this.topIndex(e.prev) + 1 : 0, e.prev && r === 0) return null;
1011
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
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
1013
  t: "blocks",
1014
1014
  before: a,
1015
1015
  next: n,
1016
- prev: r > 0 ? B(this.st.blocks[r - 1]) : null
1016
+ prev: r > 0 ? R(this.st.blocks[r - 1]) : null
1017
1017
  }];
1018
1018
  }
1019
1019
  return null;
@@ -1055,7 +1055,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1055
1055
  });
1056
1056
  return;
1057
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;
1058
+ let n = e.next.length ? R(e.next[0]) : null, r = n ? this.topIndex(n) : e.prev ? this.topIndex(e.prev) + 1 : 0;
1059
1059
  if (r < 0) return;
1060
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
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);
@@ -1074,7 +1074,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1074
1074
  let t = this.locate(e.pid), n = t[Math.min(e.k, t.length - 1)];
1075
1075
  return n ? {
1076
1076
  pa: n.pa,
1077
- o: Math.min(e.o, g(n.p.runs))
1077
+ o: Math.min(e.o, h(n.p.runs))
1078
1078
  } : null;
1079
1079
  }
1080
1080
  capture() {
@@ -1084,7 +1084,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1084
1084
  let i = /* @__PURE__ */ new Map();
1085
1085
  for (let e of [n, r]) {
1086
1086
  let t = this.locate(e.pid)[e.k];
1087
- t && i.set(`${e.pid}:${e.k}`, y(t.p.runs));
1087
+ t && i.set(`${e.pid}:${e.k}`, v(t.p.runs));
1088
1088
  }
1089
1089
  this.snap = {
1090
1090
  a: n,
@@ -1105,10 +1105,10 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1105
1105
  let n = (n) => {
1106
1106
  let r = this.locate(n.pid)[n.k] ?? this.locate(n.pid)[0];
1107
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);
1108
+ let i = e.text.get(`${n.pid}:${n.k}`), a = v(r.p.runs);
1109
1109
  return {
1110
1110
  pa: r.pa,
1111
- o: i === void 0 ? Math.min(n.o, a.length) : G(n.o, i, a)
1111
+ o: i === void 0 ? Math.min(n.o, a.length) : he(n.o, i, a)
1112
1112
  };
1113
1113
  };
1114
1114
  return {
@@ -1151,7 +1151,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1151
1151
  caret: null,
1152
1152
  sel: []
1153
1153
  };
1154
- let a = R(n, i), o = null;
1154
+ let a = I(n, i), o = null;
1155
1155
  if (a) {
1156
1156
  let e = document.createRange();
1157
1157
  try {
@@ -1160,7 +1160,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1160
1160
  o = null;
1161
1161
  }
1162
1162
  }
1163
- let s = [], c = R(n, r);
1163
+ let s = [], c = I(n, r);
1164
1164
  if (c && a && (r.pa !== i.pa || r.o !== i.o)) {
1165
1165
  let e = document.createRange();
1166
1166
  try {
@@ -1173,7 +1173,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1173
1173
  sel: s
1174
1174
  };
1175
1175
  }
1176
- }, K = (e) => e !== null, q = class {
1176
+ }, H = (e) => e !== null, U = class {
1177
1177
  transport;
1178
1178
  adapter;
1179
1179
  opts;
@@ -1195,13 +1195,18 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1195
1195
  presenceAt = 0;
1196
1196
  closed = !1;
1197
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({
1198
+ this.transport = e, this.adapter = t, this.opts = n, this.rev = n.base ?? 0, 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(this.joined ? {
1200
1200
  t: "hello",
1201
1201
  room: n.room,
1202
1202
  user: n.user,
1203
- have: this.joined ? this.rev : void 0,
1204
- site: this.joined ? this.site : void 0
1203
+ have: this.rev,
1204
+ site: this.site
1205
+ } : {
1206
+ t: "hello",
1207
+ room: n.room,
1208
+ user: n.user,
1209
+ base: n.base || void 0
1205
1210
  })) : t === "closed" && (this.online = !1, this.peers.clear(), this.emit("peers", []), this.status !== "error" && this.setStatus(this.joined ? "offline" : "connecting"));
1206
1211
  }, e.connect();
1207
1212
  }
@@ -1256,6 +1261,9 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1256
1261
  let n = this.listeners.get(e);
1257
1262
  return n || this.listeners.set(e, n = /* @__PURE__ */ new Set()), n.add(t), () => n.delete(t);
1258
1263
  }
1264
+ context(e = this.opts.logWindow ?? 5e3) {
1265
+ return e > 0 ? this.log.slice(-e) : [];
1266
+ }
1259
1267
  idle() {
1260
1268
  return this.queue;
1261
1269
  }
@@ -1281,7 +1289,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1281
1289
  case "welcome": {
1282
1290
  if (e.reset) return this.fail("The shared document history was reset; reopen the document");
1283
1291
  let t = !this.joined;
1284
- this.site = e.site, this.joined = !0, t && this.setStatus("syncing"), this.peers.clear();
1292
+ this.site = e.site, this.joined = !0, t && (this.log = e.ctx ?? [], this.setStatus("syncing")), this.peers.clear();
1285
1293
  for (let t of e.peers) this.peers.set(t.site, t);
1286
1294
  this.emit("peers", [...this.peers.values()]);
1287
1295
  for (let t of e.log) await this.onBatch(t);
@@ -1289,7 +1297,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1289
1297
  t: "ops",
1290
1298
  id: this.inflight.id,
1291
1299
  base: this.rev,
1292
- ops: this.inflight.items.map((e) => e.op).filter(K)
1300
+ ops: this.inflight.items.map((e) => e.op).filter(H)
1293
1301
  })), this.flush(), this.presence !== void 0 && this.transport.send({
1294
1302
  t: "presence",
1295
1303
  data: this.presence
@@ -1335,7 +1343,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1335
1343
  let t = e.ops.map((e) => e);
1336
1344
  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
1345
  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);
1346
+ return t.filter(H);
1339
1347
  }
1340
1348
  async revertAll(e) {
1341
1349
  for (let t = e.length - 1; t >= 0; t--) {
@@ -1394,7 +1402,7 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1394
1402
  this.log.push({
1395
1403
  seq: e.seq,
1396
1404
  site: e.site,
1397
- ops: t.map((e) => e.op).filter(K)
1405
+ ops: t.map((e) => e.op).filter(H)
1398
1406
  });
1399
1407
  }
1400
1408
  flush() {
@@ -1410,28 +1418,28 @@ var fe = (e, t) => e.pid === t.pid && e.from === t.from && e.sid === t.sid && !!
1410
1418
  t: "ops",
1411
1419
  id: t,
1412
1420
  base: this.rev,
1413
- ops: e.map((e) => e.op).filter(K)
1421
+ ops: e.map((e) => e.op).filter(H)
1414
1422
  });
1415
1423
  }
1416
- }, J = "$b64";
1417
- function Y(e) {
1424
+ }, W = "$b64";
1425
+ function ve(e) {
1418
1426
  let t = "";
1419
1427
  for (let n = 0; n < e.length; n += 32768) t += String.fromCharCode(...e.subarray(n, n + 32768));
1420
1428
  return btoa(t);
1421
1429
  }
1422
- function me(e) {
1430
+ function ye(e) {
1423
1431
  let t = atob(e), n = new Uint8Array(t.length);
1424
1432
  for (let e = 0; e < t.length; e++) n[e] = t.charCodeAt(e);
1425
1433
  return n;
1426
1434
  }
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);
1435
+ function G(e) {
1436
+ return JSON.stringify(e, (e, t) => t instanceof Uint8Array ? { [W]: ve(t) } : t instanceof ArrayBuffer ? { [W]: ve(new Uint8Array(t)) } : t);
1429
1437
  }
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);
1438
+ function K(e) {
1439
+ return JSON.parse(e, (e, t) => t && typeof t == "object" && typeof t[W] == "string" && Object.keys(t).length === 1 ? ye(t[W]) : t);
1432
1440
  }
1433
1441
  // src/collab/transport.ts
1434
- var Q = class {
1442
+ var be = class {
1435
1443
  url;
1436
1444
  protocols;
1437
1445
  onMessage;
@@ -1453,7 +1461,7 @@ var Q = class {
1453
1461
  e.onopen = () => {
1454
1462
  this.retry = 0, this.onStatus?.("open");
1455
1463
  }, e.onmessage = (e) => {
1456
- typeof e.data == "string" && this.onMessage?.(Z(e.data));
1464
+ typeof e.data == "string" && this.onMessage?.(K(e.data));
1457
1465
  }, e.onclose = () => {
1458
1466
  if (this.ws !== e || (this.ws = void 0, this.onStatus?.("closed"), this.closed)) return;
1459
1467
  let t = Math.min(1e4, 300 * 2 ** this.retry++) * (.75 + Math.random() * .5);
@@ -1461,14 +1469,14 @@ var Q = class {
1461
1469
  }, e.onerror = () => e.close();
1462
1470
  }
1463
1471
  send(e) {
1464
- this.ws?.readyState === WebSocket.OPEN && this.ws.send(X(e));
1472
+ this.ws?.readyState === WebSocket.OPEN && this.ws.send(G(e));
1465
1473
  }
1466
1474
  close() {
1467
1475
  this.closed = !0, clearTimeout(this.timer);
1468
1476
  let e = this.ws;
1469
1477
  this.ws = void 0, e?.close(), this.onStatus?.("closed");
1470
1478
  }
1471
- }, he = class {
1479
+ }, xe = class {
1472
1480
  hub;
1473
1481
  latency;
1474
1482
  onMessage;
@@ -1487,9 +1495,9 @@ var Q = class {
1487
1495
  if (this.conn) return;
1488
1496
  this.onStatus?.("connecting");
1489
1497
  let e = this.conn = this.hub.connect((t) => {
1490
- let n = X(t);
1498
+ let n = G(t);
1491
1499
  this.down = this.down.then(() => this.delay()).then(() => {
1492
- this.conn === e && this.onMessage?.(Z(n));
1500
+ this.conn === e && this.onMessage?.(K(n));
1493
1501
  });
1494
1502
  });
1495
1503
  queueMicrotask(() => this.onStatus?.("open"));
@@ -1497,9 +1505,9 @@ var Q = class {
1497
1505
  send(e) {
1498
1506
  let t = this.conn;
1499
1507
  if (!t) return;
1500
- let n = X(e);
1508
+ let n = G(e);
1501
1509
  this.up = this.up.then(() => this.delay()).then(() => {
1502
- this.conn === t && t.receive(Z(n));
1510
+ this.conn === t && t.receive(K(n));
1503
1511
  });
1504
1512
  }
1505
1513
  drop() {
@@ -1509,7 +1517,7 @@ var Q = class {
1509
1517
  close() {
1510
1518
  this.drop();
1511
1519
  }
1512
- }, ge = /* @__PURE__ */ new Set([
1520
+ }, Se = /* @__PURE__ */ new Set([
1513
1521
  "xlsx.set",
1514
1522
  "xlsx.format",
1515
1523
  "xlsx.sheetOp",
@@ -1517,7 +1525,7 @@ var Q = class {
1517
1525
  "xlsx.bookOp",
1518
1526
  "xlsx.undo",
1519
1527
  "xlsx.redo"
1520
- ]), _e = class {
1528
+ ]), Ce = class {
1521
1529
  client;
1522
1530
  submit;
1523
1531
  nextId;
@@ -1526,7 +1534,7 @@ var Q = class {
1526
1534
  redoStack = [];
1527
1535
  offLocal;
1528
1536
  constructor(e) {
1529
- this.client = e, this.offLocal = m(e, (e) => {
1537
+ this.client = e, this.offLocal = te(e, (e) => {
1530
1538
  let t = e, n = this.path(t.sheet);
1531
1539
  n && this.onPresence?.({
1532
1540
  k: "xlsx",
@@ -1550,7 +1558,7 @@ var Q = class {
1550
1558
  return this.nextId();
1551
1559
  }
1552
1560
  captures(e) {
1553
- return ge.has(e);
1561
+ return Se.has(e);
1554
1562
  }
1555
1563
  intercept(e, t) {
1556
1564
  if (!this.submit) return;
@@ -1676,22 +1684,22 @@ var Q = class {
1676
1684
  }
1677
1685
  };
1678
1686
  // src/collab/pptx-ops.ts
1679
- function ve(e, t) {
1687
+ function we(e, t) {
1680
1688
  return t.t === "slides" && t.op.op === "delete" && "slide" in e && e.slide === t.op.path ? null : e;
1681
1689
  }
1682
- function ye(e, t) {
1690
+ function Te(e, t) {
1683
1691
  if (t.some((e) => e.t === "slides" || e.t === "revert") || e.some((e) => e.t === "revert")) return !0;
1684
1692
  let n = new Set(e.flatMap((e) => "slide" in e && e.t !== "image" ? [e.slide] : []));
1685
1693
  return t.some((e) => "slide" in e && e.t !== "image" && n.has(e.slide));
1686
1694
  }
1687
1695
  // src/collab/pptx.ts
1688
- var be = /* @__PURE__ */ new Set([
1696
+ var Ee = /* @__PURE__ */ new Set([
1689
1697
  "pptx.setText",
1690
1698
  "pptx.shapes",
1691
1699
  "pptx.slides",
1692
1700
  "pptx.image",
1693
1701
  "pptx.undo"
1694
- ]), xe = class {
1702
+ ]), De = class {
1695
1703
  client;
1696
1704
  submit;
1697
1705
  nextId;
@@ -1701,7 +1709,7 @@ var be = /* @__PURE__ */ new Set([
1701
1709
  ridKey = /* @__PURE__ */ new Map();
1702
1710
  offLocal;
1703
1711
  constructor(e) {
1704
- this.client = e, this.offLocal = m(e, (e) => {
1712
+ this.client = e, this.offLocal = te(e, (e) => {
1705
1713
  let t = e, n = this.path(t.slide);
1706
1714
  n && this.onPresence?.({
1707
1715
  k: "pptx",
@@ -1723,7 +1731,7 @@ var be = /* @__PURE__ */ new Set([
1723
1731
  return this.nextId();
1724
1732
  }
1725
1733
  captures(e) {
1726
- return be.has(e);
1734
+ return Ee.has(e);
1727
1735
  }
1728
1736
  intercept(e, t) {
1729
1737
  if (!this.submit) return;
@@ -1817,10 +1825,10 @@ var be = /* @__PURE__ */ new Set([
1817
1825
  } : null;
1818
1826
  }
1819
1827
  transform(e, t) {
1820
- return ve(e, t);
1828
+ return we(e, t);
1821
1829
  }
1822
1830
  conflicts(e, t) {
1823
- return ye(e, t);
1831
+ return Te(e, t);
1824
1832
  }
1825
1833
  pessimistic(e) {
1826
1834
  return e.t === "slides";
@@ -1838,7 +1846,7 @@ var be = /* @__PURE__ */ new Set([
1838
1846
  async revert(e) {
1839
1847
  typeof e == "number" && this.client.notify("collab:pptx", await this.client.direct("collab.pptx.revert", e));
1840
1848
  }
1841
- }, $ = [
1849
+ }, q = [
1842
1850
  "#1a73e8",
1843
1851
  "#e8710a",
1844
1852
  "#188038",
@@ -1849,28 +1857,22 @@ var be = /* @__PURE__ */ new Set([
1849
1857
  "#b06000",
1850
1858
  "#3f51b5",
1851
1859
  "#e52592"
1852
- ], Se = class {
1860
+ ], Oe = class {
1853
1861
  opts;
1854
1862
  rooms = /* @__PURE__ */ new Map();
1863
+ loading = /* @__PURE__ */ new Map();
1855
1864
  constructor(e = {}) {
1856
1865
  this.opts = e;
1857
1866
  }
1858
1867
  room(e) {
1859
1868
  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;
1869
+ if (t) return Promise.resolve(t);
1870
+ let n = this.loading.get(e);
1871
+ return n || (n = Promise.resolve(this.opts.loadLog?.(e)).then((t) => {
1872
+ if (this.loading.get(e) !== n) return this.room(e);
1873
+ let r = this.rooms.get(e) ?? ke(e, t);
1874
+ return this.rooms.set(e, r), r;
1875
+ }), n.finally(() => this.loading.get(e) === n && this.loading.delete(e)).catch(() => {}), this.loading.set(e, n)), n;
1874
1876
  }
1875
1877
  reset(e) {
1876
1878
  let t = this.rooms.get(e);
@@ -1878,67 +1880,100 @@ var be = /* @__PURE__ */ new Set([
1878
1880
  t: "error",
1879
1881
  message: "Room was reset"
1880
1882
  });
1881
- this.rooms.delete(e);
1883
+ this.rooms.delete(e), this.loading.delete(e);
1884
+ }
1885
+ unload(e) {
1886
+ return !this.rooms.get(e)?.peers.size && (this.rooms.delete(e), !0);
1887
+ }
1888
+ snapshot(e, t) {
1889
+ let n = this.rooms.get(e);
1890
+ if (!n || t.seq > n.seq || t.seq < n.start) return !1;
1891
+ n.snaps.set(t.seq, t.ctx);
1892
+ let r = [...n.snaps.keys()].sort((e, t) => e - t);
1893
+ for (let e of r.splice(0, Math.max(0, r.length - (this.opts.keep ?? 3)))) n.snaps.delete(e);
1894
+ let i = Math.min(r[0], r[r.length - 1] - (this.opts.retain ?? 1e3));
1895
+ if (i > n.start) {
1896
+ let e = 0;
1897
+ for (; e < n.log.length && n.log[e].seq <= i;) n.ids.delete(n.log[e++].id);
1898
+ n.log.splice(0, e), n.start = i;
1899
+ }
1900
+ return !0;
1882
1901
  }
1883
1902
  connect(e) {
1884
- let t, n = 0, r = (e, n) => {
1903
+ let t, n = 0, r = !1, i = null, a = (e, n) => {
1885
1904
  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") {
1905
+ }, o = (r, i) => {
1906
+ t = r, n = i.site && i.site < r.nextSite && !r.peers.has(i.site) ? i.site : r.nextSite++;
1907
+ let o = new Set([...r.peers.values()].map((e) => e.info.color)), s = i.user.color || q.find((e) => !o.has(e)) || q[n % q.length], c = {
1908
+ ...i.user,
1909
+ site: n,
1910
+ color: s
1911
+ }, l, u, d;
1912
+ i.have === void 0 ? (l = i.base ?? 0, u = l === 0 ? void 0 : r.snaps.get(l), d = l > r.seq || l < r.start || l > 0 && !u) : (l = i.have, d = l > r.seq || l < r.start), e({
1913
+ t: "welcome",
1914
+ site: n,
1915
+ seq: r.seq,
1916
+ from: l,
1917
+ reset: d,
1918
+ log: d ? [] : r.log.filter((e) => e.seq > l),
1919
+ peers: [...r.peers.values()].map((e) => e.info),
1920
+ ...u && !d ? { ctx: u } : {}
1921
+ }), r.peers.set(n, {
1922
+ info: c,
1923
+ send: e
1924
+ }), a({
1925
+ t: "join",
1926
+ peer: c
1927
+ }, n);
1928
+ }, s = (c) => {
1929
+ if (!r) {
1930
+ if (i) return void i.push(c);
1931
+ if (c.t === "hello") {
1890
1932
  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);
1933
+ i = [], this.room(c.room).then((e) => {
1934
+ let t = i;
1935
+ i = null, r || (o(e, c), t.forEach(s));
1936
+ }, (t) => {
1937
+ i = null, e({
1938
+ t: "error",
1939
+ message: `Room failed to load: ${t.message}`
1940
+ });
1941
+ });
1912
1942
  } else {
1913
1943
  if (!t) return e({
1914
1944
  t: "error",
1915
1945
  message: "Not joined"
1916
1946
  });
1917
- if (i.t === "ops") {
1918
- if (t.ids.has(i.id)) return;
1919
- let e = {
1947
+ if (c.t === "ops") {
1948
+ if (t.ids.has(c.id)) return;
1949
+ let e = t.peers.get(n), r = {
1920
1950
  seq: ++t.seq,
1921
1951
  site: n,
1922
- base: Math.min(i.base, t.seq - 1),
1923
- id: i.id,
1924
- ops: i.ops
1952
+ base: Math.min(c.base, t.seq - 1),
1953
+ id: c.id,
1954
+ ops: c.ops,
1955
+ time: Date.now(),
1956
+ user: e?.info.id ?? e?.info.name
1925
1957
  };
1926
- t.log.push(e), t.ids.add(e.id), this.opts.onBatch?.(t.id, e), r({
1958
+ t.log.push(r), t.ids.add(r.id), this.opts.onBatch?.(t.id, r), a({
1927
1959
  t: "batch",
1928
- b: e
1960
+ b: r
1929
1961
  });
1930
- } else if (i.t === "presence") {
1962
+ } else if (c.t === "presence") {
1931
1963
  let e = t.peers.get(n);
1932
- e && (e.info.presence = i.data), r({
1964
+ e && (e.info.presence = c.data), a({
1933
1965
  t: "presence",
1934
1966
  site: n,
1935
- data: i.data
1967
+ data: c.data
1936
1968
  }, n);
1937
1969
  }
1938
1970
  }
1939
- },
1971
+ }
1972
+ };
1973
+ return {
1974
+ receive: s,
1940
1975
  close: () => {
1941
- t && t.peers.has(n) && (t.peers.delete(n), r({
1976
+ r = !0, t && t.peers.has(n) && (t.peers.delete(n), a({
1942
1977
  t: "leave",
1943
1978
  site: n
1944
1979
  }), t.peers.size || this.opts.onIdle?.(t.id));
@@ -1946,11 +1981,273 @@ var be = /* @__PURE__ */ new Set([
1946
1981
  };
1947
1982
  }
1948
1983
  };
1984
+ function ke(e, t) {
1985
+ let n = Array.isArray(t) ? { log: t } : t ?? { log: [] }, r = n.snapshot, i = [...n.log].sort((e, t) => e.seq - t.seq);
1986
+ for (let e = i.length - 1; e > 0; e--) if (i[e - 1].seq !== i[e].seq - 1) {
1987
+ i = i.slice(e);
1988
+ break;
1989
+ }
1990
+ let a = i.length ? i[i.length - 1].seq : 0, o = i.length ? i[0].seq - 1 : 0;
1991
+ r && (r.seq > a || r.seq < o) && (a = o = r.seq, i = []);
1992
+ let s = Math.max(1, r?.sites ?? 1);
1993
+ for (let e of i) s = Math.max(s, e.site + 1);
1994
+ for (let e of r?.ctx ?? []) s = Math.max(s, e.site + 1);
1995
+ return {
1996
+ id: e,
1997
+ seq: a,
1998
+ log: i,
1999
+ start: o,
2000
+ nextSite: s,
2001
+ ids: new Set(i.map((e) => e.id)),
2002
+ peers: /* @__PURE__ */ new Map(),
2003
+ snaps: new Map(r ? [[r.seq, r.ctx]] : [])
2004
+ };
2005
+ }
2006
+ // src/collab/compact.ts
2007
+ var Ae = class {
2008
+ welcome;
2009
+ onMessage;
2010
+ onStatus;
2011
+ constructor(e) {
2012
+ this.welcome = e;
2013
+ }
2014
+ connect() {
2015
+ queueMicrotask(() => this.onStatus?.("open"));
2016
+ }
2017
+ send(e) {
2018
+ e.t === "hello" && queueMicrotask(() => this.onMessage?.(this.welcome));
2019
+ }
2020
+ close() {}
2021
+ };
2022
+ async function je(e, t) {
2023
+ if (e.kind === "docx" && !o(e, "docx")) throw Error("compact: DOCX needs its editor host registered (registerHost)");
2024
+ let n = t.from ?? {
2025
+ seq: 0,
2026
+ ctx: []
2027
+ }, r = t.batches.filter((e) => e.seq > n.seq).sort((e, t) => e.seq - t.seq);
2028
+ r.forEach((e, t) => {
2029
+ if (e.seq !== n.seq + t + 1) throw Error(`compact: batch ${n.seq + t + 1} is missing`);
2030
+ });
2031
+ let i = n.seq + r.length, a = $(e, {
2032
+ transport: new Ae({
2033
+ t: "welcome",
2034
+ site: 0,
2035
+ seq: i,
2036
+ from: n.seq,
2037
+ log: r,
2038
+ peers: [],
2039
+ ctx: n.ctx
2040
+ }),
2041
+ room: "",
2042
+ user: { name: "compact" },
2043
+ base: n.seq
2044
+ });
2045
+ try {
2046
+ if (await new Promise((e, t) => {
2047
+ let n = () => {
2048
+ a.status === "live" ? e() : a.status === "error" && t(Error(a.session.error ?? "compact failed"));
2049
+ };
2050
+ a.on("status", n), n();
2051
+ }), await a.session.idle(), a.session.rev !== i) throw Error(`compact: stopped at ${a.session.rev} of ${i}`);
2052
+ return {
2053
+ seq: i,
2054
+ ctx: a.session.context(t.window ?? 1e3)
2055
+ };
2056
+ } finally {
2057
+ a.close();
2058
+ }
2059
+ }
2060
+ // src/collab/store.ts
2061
+ var Me = class {
2062
+ data = /* @__PURE__ */ new Map();
2063
+ async get(e) {
2064
+ return this.data.get(e) ?? null;
2065
+ }
2066
+ async put(e, t) {
2067
+ this.data.set(e, t.slice());
2068
+ }
2069
+ async list(e) {
2070
+ return [...this.data.keys()].filter((t) => t.startsWith(e));
2071
+ }
2072
+ async delete(e) {
2073
+ this.data.delete(e);
2074
+ }
2075
+ }, J = (e) => String(e).padStart(12, "0"), Y = new TextEncoder(), X = new TextDecoder(), Z = (e) => Y.encode(JSON.stringify(e)), Q = (e) => e ? JSON.parse(X.decode(e)) : null, Ne = class {
2076
+ blobs;
2077
+ opts;
2078
+ heads = /* @__PURE__ */ new Map();
2079
+ buffers = /* @__PURE__ */ new Map();
2080
+ queues = /* @__PURE__ */ new Map();
2081
+ constructor(e, t = {}) {
2082
+ this.blobs = e, this.opts = t;
2083
+ }
2084
+ dir(e) {
2085
+ return `${this.opts.prefix ?? ""}${encodeURIComponent(e).replace(/\./g, "%2E")}/`;
2086
+ }
2087
+ epochDir(e, t) {
2088
+ return `${this.dir(e)}${t}/`;
2089
+ }
2090
+ run(e, t) {
2091
+ let n = (this.queues.get(e) ?? Promise.resolve()).then(t, t), r = n.catch(() => {});
2092
+ return this.queues.set(e, r), r.then(() => this.queues.get(e) === r && this.queues.delete(e)), n;
2093
+ }
2094
+ async readHead(e) {
2095
+ if (this.heads.has(e)) return this.heads.get(e);
2096
+ let t = Q(await this.blobs.get(this.dir(e) + "head.json"));
2097
+ return this.heads.set(e, t), t;
2098
+ }
2099
+ create(e, t, n) {
2100
+ let r = this.buffers.get(e);
2101
+ return r && (clearTimeout(r.timer), this.buffers.delete(e)), this.run(e, async () => {
2102
+ let r = await this.readHead(e), i = {
2103
+ epoch: Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
2104
+ seq: 0,
2105
+ size: t.length,
2106
+ time: Date.now(),
2107
+ ...n
2108
+ }, a = this.epochDir(e, i.epoch);
2109
+ if (await this.blobs.put(`${a}snap/${J(0)}.bin`, t), await this.blobs.put(`${a}snap/${J(0)}.json`, Z({
2110
+ seq: 0,
2111
+ size: t.length,
2112
+ time: i.time
2113
+ })), await this.blobs.put(this.dir(e) + "head.json", Z(i)), this.heads.set(e, i), r) for (let t of await this.blobs.list(this.epochDir(e, r.epoch))) await this.blobs.delete(t);
2114
+ return i;
2115
+ });
2116
+ }
2117
+ head(e) {
2118
+ return this.run(e, () => this.readHead(e));
2119
+ }
2120
+ async file(e, t, n) {
2121
+ return n ??= (await this.head(e))?.epoch, n ? this.blobs.get(`${this.epochDir(e, n)}snap/${J(t)}.bin`) : null;
2122
+ }
2123
+ async snapshot(e, t, n) {
2124
+ if (n ??= (await this.head(e))?.epoch, !n) return null;
2125
+ let r = `${this.epochDir(e, n)}snap/${J(t)}`, i = Q(await this.blobs.get(r + ".json"));
2126
+ if (!i) return null;
2127
+ let a = await this.blobs.get(r + ".ctx");
2128
+ return {
2129
+ ...i,
2130
+ ctx: a ? K(X.decode(a)) : []
2131
+ };
2132
+ }
2133
+ async versions(e, t) {
2134
+ if (t ??= (await this.head(e))?.epoch, !t) return [];
2135
+ let n = `${this.epochDir(e, t)}snap/`, r = (await this.blobs.list(n)).filter((e) => e.endsWith(".json")).sort(), i = [];
2136
+ for (let e of r) {
2137
+ let t = Q(await this.blobs.get(e));
2138
+ t && i.push({
2139
+ seq: t.seq,
2140
+ size: t.size,
2141
+ time: t.time
2142
+ });
2143
+ }
2144
+ return i;
2145
+ }
2146
+ async batches(e, t, n = Infinity, r) {
2147
+ if (r ??= (await this.head(e))?.epoch, !r) return [];
2148
+ let i = (await this.blobs.list(`${this.epochDir(e, r)}ops/`)).map((e) => {
2149
+ let t = /(\d+)-(\d+)\.jsonl$/.exec(e);
2150
+ return t ? {
2151
+ k: e,
2152
+ first: Number(t[1]),
2153
+ last: Number(t[2])
2154
+ } : null;
2155
+ }).filter((e) => e !== null && e.last > t && e.first <= n).sort((e, t) => e.first - t.first), a = /* @__PURE__ */ new Set(), o = [];
2156
+ for (let e of i) {
2157
+ let r = X.decode(await this.blobs.get(e.k) ?? /* @__PURE__ */ new Uint8Array());
2158
+ for (let e of r.split("\n")) {
2159
+ if (!e) continue;
2160
+ let r = K(e);
2161
+ r.seq > t && r.seq <= n && !a.has(r.seq) && (a.add(r.seq), o.push(r));
2162
+ }
2163
+ }
2164
+ return o.sort((e, t) => e.seq - t.seq);
2165
+ }
2166
+ append(e, t) {
2167
+ let n = this.buffers.get(e);
2168
+ n || this.buffers.set(e, n = {
2169
+ batches: [],
2170
+ retry: 0
2171
+ }), n.batches.push(t), n.timer || (n.timer = setTimeout(() => void this.flushRoom(e), this.opts.flushMs ?? 250));
2172
+ }
2173
+ async flush(e) {
2174
+ let t = e === void 0 ? [...this.buffers.keys()] : [e];
2175
+ await Promise.all(t.map((e) => this.flushRoom(e)));
2176
+ }
2177
+ flushRoom(e) {
2178
+ let t = this.buffers.get(e);
2179
+ return t && (clearTimeout(t.timer), t.timer = void 0), this.run(e, async () => {
2180
+ let t = this.buffers.get(e);
2181
+ if (!t?.batches.length) return;
2182
+ let n = t.batches;
2183
+ t.batches = [];
2184
+ let r = await this.readHead(e);
2185
+ if (!r) return;
2186
+ let i = `${this.epochDir(e, r.epoch)}ops/${J(n[0].seq)}-${J(n[n.length - 1].seq)}.jsonl`;
2187
+ try {
2188
+ await this.blobs.put(i, Y.encode(n.map((e) => G(e)).join("\n") + "\n")), t.retry = 0;
2189
+ } catch (r) {
2190
+ if (this.opts.onError?.(r, e), this.buffers.get(e) !== t) return;
2191
+ t.batches.unshift(...n), t.timer ||= setTimeout(() => void this.flushRoom(e), Math.min(3e4, 500 * 2 ** t.retry++));
2192
+ }
2193
+ });
2194
+ }
2195
+ load(e) {
2196
+ return this.flushRoom(e).then(() => this.run(e, async () => {
2197
+ this.heads.delete(e);
2198
+ let t = await this.readHead(e);
2199
+ if (!t) return;
2200
+ let n = await this.snapshot(e, t.seq, t.epoch);
2201
+ return {
2202
+ log: await this.batches(e, Math.max(0, t.seq - (this.opts.retain ?? 1e3)), Infinity, t.epoch),
2203
+ snapshot: n ?? {
2204
+ seq: t.seq,
2205
+ ctx: []
2206
+ }
2207
+ };
2208
+ }));
2209
+ }
2210
+ save(e, t, n, r) {
2211
+ return this.run(e, async () => {
2212
+ let i = await this.readHead(e);
2213
+ if (!i || i.epoch !== t.epoch || n.seq <= i.seq) return null;
2214
+ let a = this.epochDir(e, i.epoch), o = Date.now();
2215
+ await Promise.all([
2216
+ this.blobs.put(`${a}snap/${J(n.seq)}.bin`, r),
2217
+ this.blobs.put(`${a}snap/${J(n.seq)}.ctx`, Y.encode(G(n.ctx))),
2218
+ this.blobs.put(`${a}snap/${J(n.seq)}.json`, Z({
2219
+ seq: n.seq,
2220
+ size: r.length,
2221
+ time: o,
2222
+ sites: n.sites
2223
+ }))
2224
+ ]);
2225
+ let s = {
2226
+ ...i,
2227
+ seq: n.seq,
2228
+ size: r.length,
2229
+ time: o
2230
+ };
2231
+ return await this.blobs.put(this.dir(e) + "head.json", Z(s)), this.heads.set(e, s), this.opts.keep && await this.prune(e, s), s;
2232
+ });
2233
+ }
2234
+ async prune(e, t) {
2235
+ let n = `${this.epochDir(e, t.epoch)}snap/`, r = [...new Set((await this.blobs.list(n)).map((e) => Number(/(\d+)\.\w+$/.exec(e)?.[1])).filter((e) => !Number.isNaN(e)))].sort((e, t) => e - t), i = r.slice(0, Math.max(0, r.length - this.opts.keep));
2236
+ for (let e of i) for (let t of [
2237
+ "bin",
2238
+ "json",
2239
+ "ctx"
2240
+ ]) await this.blobs.delete(`${n}${J(e)}.${t}`);
2241
+ let a = r[i.length], o = Math.min(a, t.seq - (this.opts.retain ?? 1e3));
2242
+ for (let n of await this.blobs.list(`${this.epochDir(e, t.epoch)}ops/`)) Number(/-(\d+)\.jsonl$/.exec(n)?.[1]) <= o && await this.blobs.delete(n);
2243
+ }
2244
+ };
1949
2245
  // 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, {
2246
+ function $(e, t) {
2247
+ let n = typeof t.transport == "string" ? new be(t.transport) : t.transport, r = e.kind === "docx" ? new _e(e) : void 0, i = r ?? (e.kind === "xlsx" ? new Ce(e) : new De(e)), a = new U(n, i, {
1952
2248
  room: t.room,
1953
- user: t.user
2249
+ user: t.user,
2250
+ base: t.base
1954
2251
  });
1955
2252
  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
2253
  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);
@@ -1979,6 +2276,6 @@ function Ce(e, t) {
1979
2276
  }
1980
2277
  };
1981
2278
  }
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 };
2279
+ export { v as C, ee as D, f as E, a as O, S, x as T, F as _, Oe as a, b, K as c, le as d, I as f, w as g, k as h, je as i, p as k, G as l, A as m, Me as n, xe as o, L as p, Ne as r, be as s, $ as t, U as u, g as v, _ as w, h as x, ie as y };
1983
2280
 
1984
- //# sourceMappingURL=collab-BzN1ZI-z.js.map
2281
+ //# sourceMappingURL=collab-BvjXvfJ9.js.map