markloom 0.6.0 → 0.7.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.
Files changed (59) hide show
  1. package/collab.md +25 -13
  2. package/dist/ai/index.cjs +5 -4
  3. package/dist/ai/index.d.cts +5 -5
  4. package/dist/ai/index.d.ts +5 -5
  5. package/dist/ai/index.js +4 -3
  6. package/dist/{chunk-KYZJIOOL.js → chunk-4Q7PAR2T.js} +3671 -904
  7. package/dist/chunk-75EJ4ARB.cjs +642 -0
  8. package/dist/{chunk-U3JZDFL4.cjs → chunk-BMPCVT3B.cjs} +2 -34
  9. package/dist/{chunk-VSBIMMI7.js → chunk-EFYT2223.js} +3 -35
  10. package/dist/chunk-HXABDVLZ.cjs +297 -0
  11. package/dist/{chunk-I5VK4U7J.cjs → chunk-I2LDEVLA.cjs} +434 -80
  12. package/dist/{chunk-YBAT343I.js → chunk-MQLRZQZL.js} +263 -119
  13. package/dist/chunk-OVHASRID.cjs +4279 -0
  14. package/dist/chunk-PCSVBPOR.cjs +40 -0
  15. package/dist/{chunk-TB2OY3D4.js → chunk-PSAXWBNM.js} +413 -59
  16. package/dist/{chunk-QEVME6ZU.cjs → chunk-PZRWTLLB.cjs} +260 -161
  17. package/dist/chunk-QO7JEZDB.js +4279 -0
  18. package/dist/{chunk-EJSFZHGT.js → chunk-WSJUXPAG.js} +18 -2
  19. package/dist/{chunk-ACFYBYZJ.cjs → chunk-Y4IXQFWL.cjs} +4212 -1445
  20. package/dist/{chunk-CGYCJORO.js → chunk-YLVSMDIO.js} +220 -121
  21. package/dist/chunk-ZL6WT6OY.js +297 -0
  22. package/dist/{client-CLYq17SW.d.ts → client-BvvyHjhh.d.ts} +57 -7
  23. package/dist/{client-DAth8pPq.d.cts → client-Ciy7Fa_k.d.cts} +57 -7
  24. package/dist/collab/index.cjs +5 -4
  25. package/dist/collab/index.d.cts +13 -15
  26. package/dist/collab/index.d.ts +13 -15
  27. package/dist/collab/index.js +4 -3
  28. package/dist/{controller-BIOO_XKJ.d.cts → controller-CHznEhAR.d.cts} +186 -27
  29. package/dist/{controller-6AJ36G52.d.ts → controller-DLqI8_jm.d.ts} +186 -27
  30. package/dist/index.cjs +69 -7
  31. package/dist/index.d.cts +127 -8
  32. package/dist/index.d.ts +127 -8
  33. package/dist/index.js +80 -18
  34. package/dist/io/index.cjs +58 -59
  35. package/dist/io/index.d.cts +1 -1
  36. package/dist/io/index.d.ts +1 -1
  37. package/dist/io/index.js +15 -16
  38. package/dist/react/index.cjs +9 -8
  39. package/dist/react/index.d.cts +5 -5
  40. package/dist/react/index.d.ts +5 -5
  41. package/dist/react/index.js +8 -7
  42. package/dist/{rebase-batch-DlerAcFN.d.ts → rebase-batch-CSnv8FKp.d.ts} +1 -1
  43. package/dist/{rebase-batch-PjENQ742.d.cts → rebase-batch-DO1ygkx9.d.cts} +1 -1
  44. package/dist/{types-COUweEav.d.cts → types-BkHlmUFL.d.cts} +5 -1
  45. package/dist/{types-COUweEav.d.ts → types-BkHlmUFL.d.ts} +5 -1
  46. package/dist/{versions-9mG1epfB.d.cts → versions-DNFf8zVz.d.cts} +1 -1
  47. package/dist/{versions-Bubyo4pt.d.ts → versions-DOkOyh2J.d.ts} +1 -1
  48. package/dist/view/index.cjs +215 -17
  49. package/dist/view/index.d.cts +82 -9
  50. package/dist/view/index.d.ts +82 -9
  51. package/dist/view/index.js +212 -14
  52. package/dist/view/theme.css +349 -75
  53. package/dist/{suggest-Bk4zLzuv.d.ts → word-count-BPw9G2Bg.d.ts} +15 -5
  54. package/dist/{suggest-BkGGiPKA.d.cts → word-count-DNLn1h_t.d.cts} +15 -5
  55. package/package.json +11 -6
  56. package/dist/chunk-EADZKEU4.cjs +0 -498
  57. package/dist/chunk-SNFF6X45.js +0 -2347
  58. package/dist/chunk-TWD5T4YC.cjs +0 -2347
  59. package/dist/chunk-ZKQSQSSM.cjs +0 -24
@@ -0,0 +1,642 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;
2
+
3
+
4
+ var _chunkBMPCVT3Bcjs = require('./chunk-BMPCVT3B.cjs');
5
+
6
+
7
+ var _chunkHXABDVLZcjs = require('./chunk-HXABDVLZ.cjs');
8
+
9
+ // src/collab/colors.ts
10
+ var COLLAB_PALETTE = [
11
+ { soft: "#FFE2E0", solid: "#E5484D" },
12
+ // 红
13
+ { soft: "#FFE8D1", solid: "#F76B15" },
14
+ // 橙
15
+ { soft: "#FFF1B8", solid: "#D97706" },
16
+ // 琥珀
17
+ { soft: "#D9F5E5", solid: "#30A46C" },
18
+ // 绿
19
+ { soft: "#D4F4F4", solid: "#12A594" },
20
+ // 青
21
+ { soft: "#D6EBFF", solid: "#3B82F6" },
22
+ // 蓝
23
+ { soft: "#E8E1FF", solid: "#7C5CFC" },
24
+ // 紫
25
+ { soft: "#FCE1F0", solid: "#E14BAF" }
26
+ // 粉
27
+ ];
28
+ var COLLAB_COLORS = COLLAB_PALETTE.map((p) => p.soft);
29
+ var COLLAB_CARET_COLORS = COLLAB_PALETTE.map((p) => p.solid);
30
+ function slot(siteId) {
31
+ return Math.abs((siteId || 1) - 1) % COLLAB_PALETTE.length;
32
+ }
33
+ function collabColor(siteId, explicit) {
34
+ const base = COLLAB_PALETTE[slot(siteId)].soft;
35
+ if (!explicit)
36
+ return base;
37
+ const rgb = parseHex(explicit);
38
+ if (!rgb)
39
+ return base;
40
+ if (collabLuma(explicit) > 0.78)
41
+ return explicit;
42
+ if (collabLuma(explicit) < 0.55)
43
+ return soften(explicit);
44
+ return base;
45
+ }
46
+ function collabCaretColor(siteId, explicit) {
47
+ const base = COLLAB_PALETTE[slot(siteId)].solid;
48
+ if (!explicit)
49
+ return base;
50
+ const rgb = parseHex(explicit);
51
+ if (!rgb)
52
+ return base;
53
+ if (collabLuma(explicit) <= 0.55)
54
+ return explicit;
55
+ return base;
56
+ }
57
+ function solidify(hex) {
58
+ const rgb = parseHex(hex);
59
+ if (!rgb)
60
+ return hex;
61
+ const [r, g, b] = rgb;
62
+ const luma = collabLuma(hex);
63
+ if (luma <= 0.48)
64
+ return hex;
65
+ const target = 0.42;
66
+ const scale = target / Math.max(luma, 0.01);
67
+ const mid = (r + g + b) / 3;
68
+ const sat = 1.35;
69
+ const punch = (c) => {
70
+ const away = mid + (c - mid) * sat;
71
+ return Math.max(0, Math.min(255, Math.round(away * scale)));
72
+ };
73
+ return toHex(punch(r), punch(g), punch(b));
74
+ }
75
+ function soften(hex) {
76
+ const rgb = parseHex(hex);
77
+ if (!rgb)
78
+ return hex;
79
+ const [r, g, b] = rgb;
80
+ const luma = collabLuma(hex);
81
+ if (luma >= 0.82)
82
+ return hex;
83
+ const t = 0.72;
84
+ return toHex(
85
+ Math.round(r + (255 - r) * t),
86
+ Math.round(g + (255 - g) * t),
87
+ Math.round(b + (255 - b) * t)
88
+ );
89
+ }
90
+ function parseHex(hex) {
91
+ const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
92
+ if (!m)
93
+ return null;
94
+ const n = Number.parseInt(m[1], 16);
95
+ return [n >> 16 & 255, n >> 8 & 255, n & 255];
96
+ }
97
+ function toHex(r, g, b) {
98
+ return `#${[r, g, b].map((c) => c.toString(16).padStart(2, "0")).join("")}`;
99
+ }
100
+ function collabLuma(hex) {
101
+ const rgb = parseHex(hex);
102
+ if (!rgb)
103
+ return 0.5;
104
+ const [r, g, b] = rgb;
105
+ return 0.2126 * (r / 255) + 0.7152 * (g / 255) + 0.0722 * (b / 255);
106
+ }
107
+ function collabFg(bg) {
108
+ return collabLuma(bg) > 0.62 ? "rgba(0,0,0,0.78)" : "#fff";
109
+ }
110
+ function collabInitial(name, siteId) {
111
+ const n = (name || "").trim();
112
+ if (n) {
113
+ const ch = n[0];
114
+ return /[a-z]/i.test(ch) ? ch.toUpperCase() : ch;
115
+ }
116
+ return String(_nullishCoalesce(siteId, () => ( "?")));
117
+ }
118
+
119
+ // src/collab/client.ts
120
+ function wireToSel(w) {
121
+ const toPos = (p) => ({
122
+ path: p.path.concat(p.offset)
123
+ });
124
+ return { anchor: toPos(w.anchor), head: toPos(w.head) };
125
+ }
126
+ function selToWire(sel) {
127
+ const split = (p) => {
128
+ const path = p.path;
129
+ if (path.length < 1)
130
+ return { path: [], offset: 0 };
131
+ return { path: path.slice(0, -1), offset: path[path.length - 1] };
132
+ };
133
+ return { anchor: split(sel.anchor), head: split(sel.head) };
134
+ }
135
+ function toWsUrl(url, room) {
136
+ let u = url.replace(/\/+$/, "");
137
+ if (u.startsWith("https://"))
138
+ u = `wss://${u.slice(8)}`;
139
+ else if (u.startsWith("http://"))
140
+ u = `ws://${u.slice(7)}`;
141
+ return `${u}/room/${encodeURIComponent(room)}`;
142
+ }
143
+ function retryDelay(attempt) {
144
+ const base = Math.min(1e3 * 2 ** Math.min(attempt, 5), 3e4);
145
+ const jitter = base * 0.5 * Math.random();
146
+ return base * 0.75 + jitter;
147
+ }
148
+ var CollabClient = (_class = class {
149
+
150
+
151
+ __init() {this.ws = null}
152
+ __init2() {this.closed = false}
153
+ __init3() {this.retry = 0}
154
+ __init4() {this.reconnectTimer = null}
155
+ __init5() {this.pingTimer = null}
156
+ __init6() {this.lastPong = 0}
157
+ __init7() {this.authFailed = false}
158
+ __init8() {this.currentStatus = "connecting"}
159
+ __init9() {this.notifiedStatus = null}
160
+
161
+ __init10() {this.conflictValue = null}
162
+ __init11() {this.conflictAckedSeq = 0}
163
+ __init12() {this.conflictWelcome = null}
164
+ __init13() {this.siteId = 0}
165
+
166
+
167
+
168
+
169
+ __init14() {this.peers = /* @__PURE__ */ new Map()}
170
+ __init15() {this.joined = false}
171
+ constructor(cfg, handlers) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);_class.prototype.__init7.call(this);_class.prototype.__init8.call(this);_class.prototype.__init9.call(this);_class.prototype.__init10.call(this);_class.prototype.__init11.call(this);_class.prototype.__init12.call(this);_class.prototype.__init13.call(this);_class.prototype.__init14.call(this);_class.prototype.__init15.call(this);
172
+ this.cfg = cfg;
173
+ this.handlers = handlers;
174
+ this.clientId = _nullishCoalesce(_optionalChain([cfg, 'access', _ => _.replica, 'optionalAccess', _2 => _2.clientId]), () => ( crypto.randomUUID()));
175
+ this.version = _nullishCoalesce(_optionalChain([cfg, 'access', _3 => _3.replica, 'optionalAccess', _4 => _4.version]), () => ( 0));
176
+ this.clientSeq = _nullishCoalesce(_optionalChain([cfg, 'access', _5 => _5.replica, 'optionalAccess', _6 => _6.clientSeq]), () => ( 0));
177
+ this.synced = _nullishCoalesce(_optionalChain([cfg, 'access', _7 => _7.replica, 'optionalAccess', _8 => _8.synced]), () => ( false));
178
+ this.pending = (_nullishCoalesce(_optionalChain([cfg, 'access', _9 => _9.replica, 'optionalAccess', _10 => _10.pending]), () => ( []))).map((item) => ({
179
+ clientSeq: item.clientSeq,
180
+ baseVersion: this.version,
181
+ steps: structuredClone(item.steps),
182
+ sent: false
183
+ }));
184
+ this.publishReplica();
185
+ this.connect();
186
+ }
187
+ get peersList() {
188
+ return [...this.peers.values()];
189
+ }
190
+ get status() {
191
+ return this.currentStatus;
192
+ }
193
+ get conflict() {
194
+ return this.conflictValue ? structuredClone(this.conflictValue) : null;
195
+ }
196
+ setStatus(status) {
197
+ this.currentStatus = status;
198
+ if (this.notifiedStatus === status)
199
+ return;
200
+ this.notifiedStatus = status;
201
+ _optionalChain([this, 'access', _11 => _11.handlers, 'access', _12 => _12.onStatus, 'optionalCall', _13 => _13(status)]);
202
+ }
203
+ get replica() {
204
+ return {
205
+ clientId: this.clientId,
206
+ version: this.version,
207
+ clientSeq: this.clientSeq,
208
+ synced: this.synced,
209
+ pending: this.pending.map((item) => ({
210
+ clientSeq: item.clientSeq,
211
+ steps: structuredClone(item.steps)
212
+ }))
213
+ };
214
+ }
215
+ publishReplica() {
216
+ _optionalChain([this, 'access', _14 => _14.cfg, 'access', _15 => _15.onReplica, 'optionalCall', _16 => _16(this.replica)]);
217
+ }
218
+ enterConflict(conflict, ackedSeq, welcome = null) {
219
+ this.joined = false;
220
+ this.conflictValue = structuredClone(conflict);
221
+ this.conflictAckedSeq = ackedSeq;
222
+ this.conflictWelcome = welcome ? structuredClone(welcome) : null;
223
+ this.setStatus("conflict");
224
+ _optionalChain([this, 'access', _17 => _17.handlers, 'access', _18 => _18.onConflict, 'optionalCall', _19 => _19(structuredClone(conflict))]);
225
+ }
226
+ connect() {
227
+ if (this.closed || this.authFailed)
228
+ return;
229
+ this.setStatus("connecting");
230
+ const url = toWsUrl(this.cfg.url, this.cfg.room);
231
+ let ws;
232
+ try {
233
+ ws = new WebSocket(url);
234
+ } catch (e) {
235
+ _optionalChain([this, 'access', _20 => _20.handlers, 'access', _21 => _21.onError, 'optionalCall', _22 => _22(e instanceof Error ? e.message : String(e))]);
236
+ this.scheduleReconnect();
237
+ return;
238
+ }
239
+ this.ws = ws;
240
+ ws.onopen = () => {
241
+ this.retry = 0;
242
+ this.lastPong = Date.now();
243
+ this.startHeartbeat();
244
+ void this.sendJoin();
245
+ };
246
+ ws.onclose = () => {
247
+ this.joined = false;
248
+ for (const item of this.pending)
249
+ item.sent = false;
250
+ this.publishReplica();
251
+ this.stopHeartbeat();
252
+ if (this.authFailed) {
253
+ this.setStatus("auth-error");
254
+ return;
255
+ }
256
+ this.setStatus("closed");
257
+ if (!this.closed)
258
+ this.scheduleReconnect();
259
+ };
260
+ ws.onerror = () => {
261
+ try {
262
+ ws.close();
263
+ } catch (e2) {
264
+ }
265
+ };
266
+ ws.onmessage = (ev) => {
267
+ try {
268
+ const msg = JSON.parse(String(ev.data));
269
+ this.onServer(msg);
270
+ } catch (e3) {
271
+ }
272
+ };
273
+ }
274
+ async sendJoin() {
275
+ let token;
276
+ try {
277
+ const t = this.cfg.token;
278
+ if (typeof t === "function")
279
+ token = await t();
280
+ else if (typeof t === "string")
281
+ token = t;
282
+ } catch (e) {
283
+ _optionalChain([this, 'access', _23 => _23.handlers, 'access', _24 => _24.onError, 'optionalCall', _25 => _25(e instanceof Error ? e.message : String(e))]);
284
+ this.authFailed = true;
285
+ this.setStatus("auth-error");
286
+ try {
287
+ _optionalChain([this, 'access', _26 => _26.ws, 'optionalAccess', _27 => _27.close, 'call', _28 => _28()]);
288
+ } catch (e4) {
289
+ }
290
+ return;
291
+ }
292
+ this.send({
293
+ type: "join",
294
+ room: this.cfg.room,
295
+ clientId: this.clientId,
296
+ ...this.synced ? { version: this.version } : {},
297
+ name: this.cfg.name,
298
+ color: this.cfg.color,
299
+ seed: this.cfg.seed,
300
+ ...token ? { token } : {}
301
+ });
302
+ }
303
+ startHeartbeat() {
304
+ this.stopHeartbeat();
305
+ this.pingTimer = setInterval(() => {
306
+ if (_optionalChain([this, 'access', _29 => _29.ws, 'optionalAccess', _30 => _30.readyState]) !== WebSocket.OPEN)
307
+ return;
308
+ if (this.lastPong && Date.now() - this.lastPong > 45e3) {
309
+ try {
310
+ this.ws.close();
311
+ } catch (e5) {
312
+ }
313
+ return;
314
+ }
315
+ this.send({ type: "ping" });
316
+ }, 2e4);
317
+ }
318
+ stopHeartbeat() {
319
+ if (this.pingTimer) {
320
+ clearInterval(this.pingTimer);
321
+ this.pingTimer = null;
322
+ }
323
+ }
324
+ scheduleReconnect() {
325
+ if (this.reconnectTimer || this.closed || this.authFailed)
326
+ return;
327
+ const delay = retryDelay(this.retry++);
328
+ this.reconnectTimer = setTimeout(() => {
329
+ this.reconnectTimer = null;
330
+ this.connect();
331
+ }, delay);
332
+ }
333
+ send(msg) {
334
+ if (_optionalChain([this, 'access', _31 => _31.ws, 'optionalAccess', _32 => _32.readyState]) === WebSocket.OPEN)
335
+ this.ws.send(JSON.stringify(msg));
336
+ }
337
+ onServer(msg) {
338
+ switch (msg.type) {
339
+ case "welcome": {
340
+ this.siteId = msg.siteId;
341
+ if (!this.synced && !msg.seeded && (this.pending.length || this.cfg.bootstrap !== "remote")) {
342
+ _optionalChain([this, 'access', _33 => _33.handlers, 'access', _34 => _34.onError, 'optionalCall', _35 => _35(this.pending.length ? "local replica diverged before its first remote snapshot" : "remote bootstrap was not explicitly allowed")]);
343
+ this.enterConflict({
344
+ reason: this.pending.length ? "local-diverged" : "remote-bootstrap-required",
345
+ remote: { doc: msg.doc, version: msg.version }
346
+ }, msg.ackedSeq, msg);
347
+ return;
348
+ }
349
+ if (!this.cfg.color)
350
+ this.cfg.color = collabColor(msg.siteId);
351
+ this.peers.clear();
352
+ for (const p of msg.peers) {
353
+ this.peers.set(p.siteId, {
354
+ ...p,
355
+ color: collabColor(p.siteId, p.color)
356
+ });
357
+ }
358
+ if (!this.synced) {
359
+ if (!this.pending.length)
360
+ this.handlers.onDoc(msg.doc, msg.version);
361
+ this.version = msg.version;
362
+ this.synced = true;
363
+ } else {
364
+ for (const op of msg.ops)
365
+ this.applyServerOp(op);
366
+ this.pending = this.pending.filter((item) => item.clientSeq > msg.ackedSeq);
367
+ this.version = msg.version;
368
+ }
369
+ this.joined = true;
370
+ this.setStatus("open");
371
+ _optionalChain([this, 'access', _36 => _36.handlers, 'access', _37 => _37.onWelcome, 'optionalCall', _38 => _38({
372
+ siteId: msg.siteId,
373
+ version: msg.version,
374
+ name: this.cfg.name,
375
+ color: this.cfg.color
376
+ })]);
377
+ _optionalChain([this, 'access', _39 => _39.handlers, 'access', _40 => _40.onPeers, 'optionalCall', _41 => _41(this.peersList)]);
378
+ if (msg.versions)
379
+ _optionalChain([this, 'access', _42 => _42.handlers, 'access', _43 => _43.onVersions, 'optionalCall', _44 => _44(msg.versions)]);
380
+ this.publishReplica();
381
+ this.flushPending();
382
+ break;
383
+ }
384
+ case "versions": {
385
+ _optionalChain([this, 'access', _45 => _45.handlers, 'access', _46 => _46.onVersions, 'optionalCall', _47 => _47(msg.versions)]);
386
+ break;
387
+ }
388
+ case "resync": {
389
+ if (this.pending.length) {
390
+ _optionalChain([this, 'access', _48 => _48.handlers, 'access', _49 => _49.onError, 'optionalCall', _50 => _50("resync refused: local changes are still unacknowledged")]);
391
+ this.enterConflict({
392
+ reason: "resync-with-pending",
393
+ remote: { doc: msg.doc, version: msg.version }
394
+ }, this.pending[0].clientSeq - 1);
395
+ break;
396
+ }
397
+ this.version = msg.version;
398
+ this.setStatus("resyncing");
399
+ _optionalChain([this, 'access', _51 => _51.handlers, 'access', _52 => _52.onResync, 'optionalCall', _53 => _53(msg.version)]);
400
+ this.handlers.onDoc(msg.doc, msg.version);
401
+ this.setStatus("open");
402
+ this.publishReplica();
403
+ break;
404
+ }
405
+ case "op": {
406
+ if (!this.joined) {
407
+ if (this.conflictValue && msg.version > this.conflictValue.remote.version) {
408
+ try {
409
+ this.conflictValue.remote.doc = _chunkHXABDVLZcjs.applyAll.call(void 0, this.conflictValue.remote.doc, msg.steps);
410
+ this.conflictValue.remote.version = msg.version;
411
+ _optionalChain([this, 'access', _54 => _54.handlers, 'access', _55 => _55.onConflict, 'optionalCall', _56 => _56(structuredClone(this.conflictValue))]);
412
+ } catch (error) {
413
+ _optionalChain([this, 'access', _57 => _57.handlers, 'access', _58 => _58.onError, 'optionalCall', _59 => _59(error instanceof Error ? error.message : String(error))]);
414
+ }
415
+ }
416
+ return;
417
+ }
418
+ this.applyServerOp(msg);
419
+ this.publishReplica();
420
+ this.flushPending();
421
+ break;
422
+ }
423
+ case "presence": {
424
+ const cur = _nullishCoalesce(this.peers.get(msg.siteId), () => ( { siteId: msg.siteId }));
425
+ this.peers.set(msg.siteId, {
426
+ ...cur,
427
+ name: _nullishCoalesce(msg.name, () => ( cur.name)),
428
+ color: collabColor(msg.siteId, _nullishCoalesce(msg.color, () => ( cur.color))),
429
+ sel: msg.sel === void 0 ? cur.sel : msg.sel
430
+ });
431
+ _optionalChain([this, 'access', _60 => _60.handlers, 'access', _61 => _61.onPeers, 'optionalCall', _62 => _62(this.peersList)]);
432
+ break;
433
+ }
434
+ case "peer-join": {
435
+ this.peers.set(msg.siteId, {
436
+ siteId: msg.siteId,
437
+ name: msg.name,
438
+ color: collabColor(msg.siteId, msg.color)
439
+ });
440
+ _optionalChain([this, 'access', _63 => _63.handlers, 'access', _64 => _64.onPeers, 'optionalCall', _65 => _65(this.peersList)]);
441
+ break;
442
+ }
443
+ case "peer-leave": {
444
+ this.peers.delete(msg.siteId);
445
+ _optionalChain([this, 'access', _66 => _66.handlers, 'access', _67 => _67.onPeers, 'optionalCall', _68 => _68(this.peersList)]);
446
+ break;
447
+ }
448
+ case "error": {
449
+ _optionalChain([this, 'access', _69 => _69.handlers, 'access', _70 => _70.onError, 'optionalCall', _71 => _71(msg.message)]);
450
+ if (msg.code === "unauthorized") {
451
+ this.authFailed = true;
452
+ this.setStatus("auth-error");
453
+ try {
454
+ _optionalChain([this, 'access', _72 => _72.ws, 'optionalAccess', _73 => _73.close, 'call', _74 => _74()]);
455
+ } catch (e6) {
456
+ }
457
+ }
458
+ break;
459
+ }
460
+ case "pong":
461
+ this.lastPong = Date.now();
462
+ break;
463
+ }
464
+ }
465
+ /** 本地已 apply 的 steps 推上服务器 */
466
+ push(steps) {
467
+ if (!steps.length)
468
+ return;
469
+ const clientSeq = ++this.clientSeq;
470
+ this.pending.push({ clientSeq, baseVersion: this.version, steps, sent: false });
471
+ this.publishReplica();
472
+ this.flushPending();
473
+ }
474
+ flushPending() {
475
+ const item = this.pending[0];
476
+ if (!item || item.sent || !this.joined || this.siteId <= 0)
477
+ return;
478
+ item.baseVersion = this.version;
479
+ item.sent = true;
480
+ this.send({
481
+ type: "op",
482
+ clientId: this.clientId,
483
+ baseVersion: item.baseVersion,
484
+ clientSeq: item.clientSeq,
485
+ steps: item.steps
486
+ });
487
+ }
488
+ applyServerOp(msg) {
489
+ if (msg.clientId === this.clientId) {
490
+ this.pending = this.pending.filter((item) => item.clientSeq > msg.clientSeq);
491
+ this.version = msg.version;
492
+ return;
493
+ }
494
+ const pendingAs = this.pending.map((item) => ({ siteId: this.siteId, steps: item.steps }));
495
+ const transformed = _chunkBMPCVT3Bcjs.transformRemoteAgainstPending.call(void 0,
496
+ msg.steps,
497
+ msg.siteId,
498
+ pendingAs,
499
+ this.siteId
500
+ );
501
+ this.pending = this.pending.map((item, i) => ({
502
+ ...item,
503
+ steps: _nullishCoalesce(_optionalChain([transformed, 'access', _75 => _75.pending, 'access', _76 => _76[i], 'optionalAccess', _77 => _77.steps]), () => ( item.steps))
504
+ }));
505
+ this.version = msg.version;
506
+ if (!transformed.remote.length)
507
+ return;
508
+ if (!this.peers.has(msg.siteId)) {
509
+ this.peers.set(msg.siteId, {
510
+ siteId: msg.siteId,
511
+ color: collabColor(msg.siteId)
512
+ });
513
+ }
514
+ this.mapPeerSelsThrough(transformed.remote);
515
+ this.ensureAuthorCaret(msg.siteId, transformed.remote);
516
+ this.handlers.onRemote(transformed.remote, msg.siteId);
517
+ _optionalChain([this, 'access', _78 => _78.handlers, 'access', _79 => _79.onPeers, 'optionalCall', _80 => _80(this.peersList)]);
518
+ }
519
+ /** 命名版本上行(侧信道,不进 OT) */
520
+ pushVersion(v) {
521
+ if (!this.joined || this.siteId <= 0)
522
+ return;
523
+ this.send({ type: "version-save", version: v });
524
+ }
525
+ removeVersion(id) {
526
+ if (!this.joined || this.siteId <= 0)
527
+ return;
528
+ this.send({ type: "version-remove", id });
529
+ }
530
+ acceptRemote() {
531
+ const conflict = this.conflictValue;
532
+ if (!conflict || this.currentStatus !== "conflict" || _optionalChain([this, 'access', _81 => _81.ws, 'optionalAccess', _82 => _82.readyState]) !== WebSocket.OPEN)
533
+ return false;
534
+ this.pending = [];
535
+ this.clientSeq = this.conflictAckedSeq;
536
+ this.version = conflict.remote.version;
537
+ this.synced = true;
538
+ this.joined = true;
539
+ this.conflictValue = null;
540
+ const welcome = this.conflictWelcome;
541
+ this.conflictWelcome = null;
542
+ if (welcome) {
543
+ if (!this.cfg.color)
544
+ this.cfg.color = collabColor(welcome.siteId);
545
+ this.peers.clear();
546
+ for (const peer of welcome.peers) {
547
+ this.peers.set(peer.siteId, {
548
+ ...peer,
549
+ color: collabColor(peer.siteId, peer.color)
550
+ });
551
+ }
552
+ _optionalChain([this, 'access', _83 => _83.handlers, 'access', _84 => _84.onWelcome, 'optionalCall', _85 => _85({
553
+ siteId: welcome.siteId,
554
+ version: conflict.remote.version,
555
+ name: this.cfg.name,
556
+ color: this.cfg.color
557
+ })]);
558
+ _optionalChain([this, 'access', _86 => _86.handlers, 'access', _87 => _87.onPeers, 'optionalCall', _88 => _88(this.peersList)]);
559
+ if (welcome.versions)
560
+ _optionalChain([this, 'access', _89 => _89.handlers, 'access', _90 => _90.onVersions, 'optionalCall', _91 => _91(welcome.versions)]);
561
+ }
562
+ if (conflict.reason === "resync-with-pending")
563
+ _optionalChain([this, 'access', _92 => _92.handlers, 'access', _93 => _93.onResync, 'optionalCall', _94 => _94(conflict.remote.version)]);
564
+ this.handlers.onDoc(structuredClone(conflict.remote.doc), conflict.remote.version);
565
+ this.setStatus("open");
566
+ this.publishReplica();
567
+ return true;
568
+ }
569
+ /** 用一批 steps 搬所有 peer 的 wire 选区(作者打字时彩光标贴字) */
570
+ mapPeerSelsThrough(steps) {
571
+ for (const [id, p] of this.peers) {
572
+ if (!p.sel)
573
+ continue;
574
+ let sel = wireToSel(p.sel);
575
+ for (const s of steps)
576
+ sel = _chunkBMPCVT3Bcjs.mapSelection.call(void 0, s, sel);
577
+ this.peers.set(id, { ...p, sel: selToWire(sel) });
578
+ }
579
+ }
580
+ /**
581
+ * 作者无历史 presence 时,从本批最后一次 insertText 推光标到插入末尾。
582
+ * 有 sel 时 mapPeerSelsThrough 已搬过,不再覆盖。
583
+ */
584
+ ensureAuthorCaret(siteId, steps) {
585
+ const p = this.peers.get(siteId);
586
+ if (!p || p.sel)
587
+ return;
588
+ for (let i = steps.length - 1; i >= 0; i--) {
589
+ const s = steps[i];
590
+ if (s.kind !== "insertText")
591
+ continue;
592
+ const offset = s.offset + s.text.length;
593
+ p.sel = {
594
+ anchor: { path: s.path.slice(), offset },
595
+ head: { path: s.path.slice(), offset }
596
+ };
597
+ this.peers.set(siteId, p);
598
+ return;
599
+ }
600
+ }
601
+ presence(sel) {
602
+ if (!this.joined)
603
+ return;
604
+ this.send({
605
+ type: "presence",
606
+ name: this.cfg.name,
607
+ color: _nullishCoalesce(this.cfg.color, () => ( collabColor(this.siteId))),
608
+ sel
609
+ });
610
+ }
611
+ close() {
612
+ this.closed = true;
613
+ this.joined = false;
614
+ this.setStatus("closed");
615
+ this.stopHeartbeat();
616
+ if (this.reconnectTimer) {
617
+ clearTimeout(this.reconnectTimer);
618
+ this.reconnectTimer = null;
619
+ }
620
+ try {
621
+ _optionalChain([this, 'access', _95 => _95.ws, 'optionalAccess', _96 => _96.close, 'call', _97 => _97()]);
622
+ } catch (e7) {
623
+ }
624
+ this.ws = null;
625
+ }
626
+ }, _class);
627
+ function connectCollab(cfg, handlers) {
628
+ return new CollabClient(cfg, handlers);
629
+ }
630
+
631
+
632
+
633
+
634
+
635
+
636
+
637
+
638
+
639
+
640
+
641
+
642
+ exports.COLLAB_COLORS = COLLAB_COLORS; exports.COLLAB_CARET_COLORS = COLLAB_CARET_COLORS; exports.collabColor = collabColor; exports.collabCaretColor = collabCaretColor; exports.solidify = solidify; exports.collabLuma = collabLuma; exports.collabFg = collabFg; exports.collabInitial = collabInitial; exports.CollabClient = CollabClient; exports.connectCollab = connectCollab;
@@ -1,7 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
2
 
3
-
4
- var _chunkZKQSQSSMcjs = require('./chunk-ZKQSQSSM.cjs');
3
+ var _chunkHXABDVLZcjs = require('./chunk-HXABDVLZ.cjs');
5
4
 
6
5
  // src/tree/map.ts
7
6
  function mapPosition(step, pos) {
@@ -89,38 +88,7 @@ function underPrefix(a, b) {
89
88
  return b.length > a.length && isPrefix(a, b);
90
89
  }
91
90
  function normalizePosition(doc, pos) {
92
- let cur = doc;
93
- for (let i = 0; i < pos.path.length - 1; i++) {
94
- if (!_chunkZKQSQSSMcjs.isElement.call(void 0, cur))
95
- break;
96
- const next = cur.children[pos.path[i]];
97
- if (!next)
98
- return pos;
99
- cur = next;
100
- }
101
- if (_chunkZKQSQSSMcjs.isText.call(void 0, cur))
102
- return pos;
103
- const at = _nullishCoalesce(pos.path[pos.path.length - 1], () => ( 0));
104
- const prefix = pos.path.slice(0, -1);
105
- return withPath(descendToText(cur, at, prefix), pos);
106
- }
107
- function descendToText(parent, at, prefix) {
108
- const kids = parent.children;
109
- if (kids.length === 0)
110
- return prefix.concat(0);
111
- const leftIdx = at > 0 ? at - 1 : 0;
112
- const toEnd = at > 0;
113
- let path = prefix.concat(leftIdx);
114
- let node = kids[Math.min(leftIdx, kids.length - 1)];
115
- while (_chunkZKQSQSSMcjs.isElement.call(void 0, node)) {
116
- const cs = node.children;
117
- if (cs.length === 0)
118
- return path.concat(0);
119
- const idx = toEnd ? cs.length - 1 : 0;
120
- path = path.concat(idx);
121
- node = cs[idx];
122
- }
123
- return path.concat(toEnd ? node.text.length : 0);
91
+ return withPath(_chunkHXABDVLZcjs.normalizeToLeaf.call(void 0, doc, pos.path), pos);
124
92
  }
125
93
 
126
94
  // src/tree/rebase.ts