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.
- package/collab.md +25 -13
- package/dist/ai/index.cjs +5 -4
- package/dist/ai/index.d.cts +5 -5
- package/dist/ai/index.d.ts +5 -5
- package/dist/ai/index.js +4 -3
- package/dist/{chunk-KYZJIOOL.js → chunk-4Q7PAR2T.js} +3671 -904
- package/dist/chunk-75EJ4ARB.cjs +642 -0
- package/dist/{chunk-U3JZDFL4.cjs → chunk-BMPCVT3B.cjs} +2 -34
- package/dist/{chunk-VSBIMMI7.js → chunk-EFYT2223.js} +3 -35
- package/dist/chunk-HXABDVLZ.cjs +297 -0
- package/dist/{chunk-I5VK4U7J.cjs → chunk-I2LDEVLA.cjs} +434 -80
- package/dist/{chunk-YBAT343I.js → chunk-MQLRZQZL.js} +263 -119
- package/dist/chunk-OVHASRID.cjs +4279 -0
- package/dist/chunk-PCSVBPOR.cjs +40 -0
- package/dist/{chunk-TB2OY3D4.js → chunk-PSAXWBNM.js} +413 -59
- package/dist/{chunk-QEVME6ZU.cjs → chunk-PZRWTLLB.cjs} +260 -161
- package/dist/chunk-QO7JEZDB.js +4279 -0
- package/dist/{chunk-EJSFZHGT.js → chunk-WSJUXPAG.js} +18 -2
- package/dist/{chunk-ACFYBYZJ.cjs → chunk-Y4IXQFWL.cjs} +4212 -1445
- package/dist/{chunk-CGYCJORO.js → chunk-YLVSMDIO.js} +220 -121
- package/dist/chunk-ZL6WT6OY.js +297 -0
- package/dist/{client-CLYq17SW.d.ts → client-BvvyHjhh.d.ts} +57 -7
- package/dist/{client-DAth8pPq.d.cts → client-Ciy7Fa_k.d.cts} +57 -7
- package/dist/collab/index.cjs +5 -4
- package/dist/collab/index.d.cts +13 -15
- package/dist/collab/index.d.ts +13 -15
- package/dist/collab/index.js +4 -3
- package/dist/{controller-BIOO_XKJ.d.cts → controller-CHznEhAR.d.cts} +186 -27
- package/dist/{controller-6AJ36G52.d.ts → controller-DLqI8_jm.d.ts} +186 -27
- package/dist/index.cjs +69 -7
- package/dist/index.d.cts +127 -8
- package/dist/index.d.ts +127 -8
- package/dist/index.js +80 -18
- package/dist/io/index.cjs +58 -59
- package/dist/io/index.d.cts +1 -1
- package/dist/io/index.d.ts +1 -1
- package/dist/io/index.js +15 -16
- package/dist/react/index.cjs +9 -8
- package/dist/react/index.d.cts +5 -5
- package/dist/react/index.d.ts +5 -5
- package/dist/react/index.js +8 -7
- package/dist/{rebase-batch-DlerAcFN.d.ts → rebase-batch-CSnv8FKp.d.ts} +1 -1
- package/dist/{rebase-batch-PjENQ742.d.cts → rebase-batch-DO1ygkx9.d.cts} +1 -1
- package/dist/{types-COUweEav.d.cts → types-BkHlmUFL.d.cts} +5 -1
- package/dist/{types-COUweEav.d.ts → types-BkHlmUFL.d.ts} +5 -1
- package/dist/{versions-9mG1epfB.d.cts → versions-DNFf8zVz.d.cts} +1 -1
- package/dist/{versions-Bubyo4pt.d.ts → versions-DOkOyh2J.d.ts} +1 -1
- package/dist/view/index.cjs +215 -17
- package/dist/view/index.d.cts +82 -9
- package/dist/view/index.d.ts +82 -9
- package/dist/view/index.js +212 -14
- package/dist/view/theme.css +349 -75
- package/dist/{suggest-Bk4zLzuv.d.ts → word-count-BPw9G2Bg.d.ts} +15 -5
- package/dist/{suggest-BkGGiPKA.d.cts → word-count-DNLn1h_t.d.cts} +15 -5
- package/package.json +11 -6
- package/dist/chunk-EADZKEU4.cjs +0 -498
- package/dist/chunk-SNFF6X45.js +0 -2347
- package/dist/chunk-TWD5T4YC.cjs +0 -2347
- package/dist/chunk-ZKQSQSSM.cjs +0 -24
|
@@ -1,95 +1,92 @@
|
|
|
1
1
|
import {
|
|
2
2
|
mapSelection,
|
|
3
3
|
transformRemoteAgainstPending
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-EFYT2223.js";
|
|
5
|
+
import {
|
|
6
|
+
applyAll
|
|
7
|
+
} from "./chunk-ZL6WT6OY.js";
|
|
5
8
|
|
|
6
9
|
// src/collab/colors.ts
|
|
7
|
-
var
|
|
8
|
-
"#
|
|
9
|
-
// 浅粉
|
|
10
|
-
"#ffe8b8",
|
|
11
|
-
// 浅杏
|
|
12
|
-
"#d4f0c8",
|
|
13
|
-
// 薄荷绿
|
|
14
|
-
"#c8e4ff",
|
|
15
|
-
// 天蓝
|
|
16
|
-
"#e0d4f5",
|
|
17
|
-
// 薰衣草
|
|
18
|
-
"#d0d0d0",
|
|
19
|
-
// 浅灰
|
|
20
|
-
"#6b6b6b",
|
|
21
|
-
// 深灰
|
|
22
|
-
"#e85d4c",
|
|
10
|
+
var COLLAB_PALETTE = [
|
|
11
|
+
{ soft: "#FFE2E0", solid: "#E5484D" },
|
|
23
12
|
// 红
|
|
24
|
-
"#
|
|
13
|
+
{ soft: "#FFE8D1", solid: "#F76B15" },
|
|
25
14
|
// 橙
|
|
26
|
-
"#
|
|
27
|
-
//
|
|
28
|
-
"#
|
|
15
|
+
{ soft: "#FFF1B8", solid: "#D97706" },
|
|
16
|
+
// 琥珀
|
|
17
|
+
{ soft: "#D9F5E5", solid: "#30A46C" },
|
|
29
18
|
// 绿
|
|
30
|
-
"#
|
|
19
|
+
{ soft: "#D4F4F4", solid: "#12A594" },
|
|
20
|
+
// 青
|
|
21
|
+
{ soft: "#D6EBFF", solid: "#3B82F6" },
|
|
31
22
|
// 蓝
|
|
32
|
-
"#
|
|
23
|
+
{ soft: "#E8E1FF", solid: "#7C5CFC" },
|
|
33
24
|
// 紫
|
|
34
|
-
|
|
35
|
-
var COLLAB_CARET_COLORS = [
|
|
36
|
-
"#f06a6a",
|
|
25
|
+
{ soft: "#FCE1F0", solid: "#E14BAF" }
|
|
37
26
|
// 粉
|
|
38
|
-
"#e09a20",
|
|
39
|
-
// 杏
|
|
40
|
-
"#3db84a",
|
|
41
|
-
// 绿
|
|
42
|
-
"#3d8cf0",
|
|
43
|
-
// 天蓝
|
|
44
|
-
"#8b6ef0",
|
|
45
|
-
// 薰衣草
|
|
46
|
-
"#8a8a8a",
|
|
47
|
-
// 灰
|
|
48
|
-
"#4a4a4a",
|
|
49
|
-
// 深灰
|
|
50
|
-
"#e03d30",
|
|
51
|
-
// 红
|
|
52
|
-
"#f09000",
|
|
53
|
-
// 橙
|
|
54
|
-
"#e0a800",
|
|
55
|
-
// 黄
|
|
56
|
-
"#1f9e28",
|
|
57
|
-
// 绿
|
|
58
|
-
"#2f5ff0",
|
|
59
|
-
// 蓝
|
|
60
|
-
"#6a48f0"
|
|
61
|
-
// 紫
|
|
62
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
|
+
}
|
|
63
33
|
function collabColor(siteId, explicit) {
|
|
64
|
-
|
|
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)
|
|
65
41
|
return explicit;
|
|
66
|
-
|
|
67
|
-
|
|
42
|
+
if (collabLuma(explicit) < 0.55)
|
|
43
|
+
return soften(explicit);
|
|
44
|
+
return base;
|
|
68
45
|
}
|
|
69
46
|
function collabCaretColor(siteId, explicit) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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;
|
|
74
56
|
}
|
|
75
57
|
function solidify(hex) {
|
|
76
58
|
const rgb = parseHex(hex);
|
|
77
59
|
if (!rgb)
|
|
78
60
|
return hex;
|
|
79
61
|
const [r, g, b] = rgb;
|
|
80
|
-
const luma =
|
|
62
|
+
const luma = collabLuma(hex);
|
|
81
63
|
if (luma <= 0.48)
|
|
82
64
|
return hex;
|
|
83
|
-
const target = 0.
|
|
65
|
+
const target = 0.42;
|
|
84
66
|
const scale = target / Math.max(luma, 0.01);
|
|
85
67
|
const mid = (r + g + b) / 3;
|
|
86
|
-
const sat = 1.
|
|
68
|
+
const sat = 1.35;
|
|
87
69
|
const punch = (c) => {
|
|
88
70
|
const away = mid + (c - mid) * sat;
|
|
89
71
|
return Math.max(0, Math.min(255, Math.round(away * scale)));
|
|
90
72
|
};
|
|
91
73
|
return toHex(punch(r), punch(g), punch(b));
|
|
92
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
|
+
}
|
|
93
90
|
function parseHex(hex) {
|
|
94
91
|
const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
|
|
95
92
|
if (!m)
|
|
@@ -158,33 +155,78 @@ var CollabClient = class {
|
|
|
158
155
|
pingTimer = null;
|
|
159
156
|
lastPong = 0;
|
|
160
157
|
authFailed = false;
|
|
158
|
+
currentStatus = "connecting";
|
|
159
|
+
notifiedStatus = null;
|
|
160
|
+
synced;
|
|
161
|
+
conflictValue = null;
|
|
162
|
+
conflictAckedSeq = 0;
|
|
163
|
+
conflictWelcome = null;
|
|
161
164
|
siteId = 0;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
+
clientId;
|
|
166
|
+
version;
|
|
167
|
+
clientSeq;
|
|
168
|
+
pending;
|
|
165
169
|
peers = /* @__PURE__ */ new Map();
|
|
166
170
|
joined = false;
|
|
167
171
|
constructor(cfg, handlers) {
|
|
168
172
|
this.cfg = cfg;
|
|
169
173
|
this.handlers = handlers;
|
|
174
|
+
this.clientId = cfg.replica?.clientId ?? crypto.randomUUID();
|
|
175
|
+
this.version = cfg.replica?.version ?? 0;
|
|
176
|
+
this.clientSeq = cfg.replica?.clientSeq ?? 0;
|
|
177
|
+
this.synced = cfg.replica?.synced ?? false;
|
|
178
|
+
this.pending = (cfg.replica?.pending ?? []).map((item) => ({
|
|
179
|
+
clientSeq: item.clientSeq,
|
|
180
|
+
baseVersion: this.version,
|
|
181
|
+
steps: structuredClone(item.steps),
|
|
182
|
+
sent: false
|
|
183
|
+
}));
|
|
184
|
+
this.publishReplica();
|
|
170
185
|
this.connect();
|
|
171
186
|
}
|
|
172
187
|
get peersList() {
|
|
173
188
|
return [...this.peers.values()];
|
|
174
189
|
}
|
|
175
190
|
get status() {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
+
this.handlers.onStatus?.(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
|
+
this.cfg.onReplica?.(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
|
+
this.handlers.onConflict?.(structuredClone(conflict));
|
|
183
225
|
}
|
|
184
226
|
connect() {
|
|
185
227
|
if (this.closed || this.authFailed)
|
|
186
228
|
return;
|
|
187
|
-
this.
|
|
229
|
+
this.setStatus("connecting");
|
|
188
230
|
const url = toWsUrl(this.cfg.url, this.cfg.room);
|
|
189
231
|
let ws;
|
|
190
232
|
try {
|
|
@@ -203,12 +245,15 @@ var CollabClient = class {
|
|
|
203
245
|
};
|
|
204
246
|
ws.onclose = () => {
|
|
205
247
|
this.joined = false;
|
|
248
|
+
for (const item of this.pending)
|
|
249
|
+
item.sent = false;
|
|
250
|
+
this.publishReplica();
|
|
206
251
|
this.stopHeartbeat();
|
|
207
252
|
if (this.authFailed) {
|
|
208
|
-
this.
|
|
253
|
+
this.setStatus("auth-error");
|
|
209
254
|
return;
|
|
210
255
|
}
|
|
211
|
-
this.
|
|
256
|
+
this.setStatus("closed");
|
|
212
257
|
if (!this.closed)
|
|
213
258
|
this.scheduleReconnect();
|
|
214
259
|
};
|
|
@@ -237,7 +282,7 @@ var CollabClient = class {
|
|
|
237
282
|
} catch (e) {
|
|
238
283
|
this.handlers.onError?.(e instanceof Error ? e.message : String(e));
|
|
239
284
|
this.authFailed = true;
|
|
240
|
-
this.
|
|
285
|
+
this.setStatus("auth-error");
|
|
241
286
|
try {
|
|
242
287
|
this.ws?.close();
|
|
243
288
|
} catch {
|
|
@@ -247,6 +292,8 @@ var CollabClient = class {
|
|
|
247
292
|
this.send({
|
|
248
293
|
type: "join",
|
|
249
294
|
room: this.cfg.room,
|
|
295
|
+
clientId: this.clientId,
|
|
296
|
+
...this.synced ? { version: this.version } : {},
|
|
250
297
|
name: this.cfg.name,
|
|
251
298
|
color: this.cfg.color,
|
|
252
299
|
seed: this.cfg.seed,
|
|
@@ -291,10 +338,14 @@ var CollabClient = class {
|
|
|
291
338
|
switch (msg.type) {
|
|
292
339
|
case "welcome": {
|
|
293
340
|
this.siteId = msg.siteId;
|
|
294
|
-
this.
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
341
|
+
if (!this.synced && !msg.seeded && (this.pending.length || this.cfg.bootstrap !== "remote")) {
|
|
342
|
+
this.handlers.onError?.(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
|
+
}
|
|
298
349
|
if (!this.cfg.color)
|
|
299
350
|
this.cfg.color = collabColor(msg.siteId);
|
|
300
351
|
this.peers.clear();
|
|
@@ -304,16 +355,30 @@ var CollabClient = class {
|
|
|
304
355
|
color: collabColor(p.siteId, p.color)
|
|
305
356
|
});
|
|
306
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");
|
|
307
371
|
this.handlers.onWelcome?.({
|
|
308
372
|
siteId: msg.siteId,
|
|
309
373
|
version: msg.version,
|
|
310
374
|
name: this.cfg.name,
|
|
311
375
|
color: this.cfg.color
|
|
312
376
|
});
|
|
313
|
-
this.handlers.onDoc(msg.doc, msg.version);
|
|
314
377
|
this.handlers.onPeers?.(this.peersList);
|
|
315
378
|
if (msg.versions)
|
|
316
379
|
this.handlers.onVersions?.(msg.versions);
|
|
380
|
+
this.publishReplica();
|
|
381
|
+
this.flushPending();
|
|
317
382
|
break;
|
|
318
383
|
}
|
|
319
384
|
case "versions": {
|
|
@@ -321,47 +386,38 @@ var CollabClient = class {
|
|
|
321
386
|
break;
|
|
322
387
|
}
|
|
323
388
|
case "resync": {
|
|
389
|
+
if (this.pending.length) {
|
|
390
|
+
this.handlers.onError?.("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
|
+
}
|
|
324
397
|
this.version = msg.version;
|
|
325
|
-
this.
|
|
326
|
-
this.handlers.onStatus?.("resyncing");
|
|
398
|
+
this.setStatus("resyncing");
|
|
327
399
|
this.handlers.onResync?.(msg.version);
|
|
328
400
|
this.handlers.onDoc(msg.doc, msg.version);
|
|
329
|
-
this.
|
|
401
|
+
this.setStatus("open");
|
|
402
|
+
this.publishReplica();
|
|
330
403
|
break;
|
|
331
404
|
}
|
|
332
405
|
case "op": {
|
|
333
|
-
if (!this.joined)
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const { remote, pending: nextP } = transformRemoteAgainstPending(
|
|
343
|
-
msg.steps,
|
|
344
|
-
msg.siteId,
|
|
345
|
-
pendingAs,
|
|
346
|
-
this.siteId
|
|
347
|
-
);
|
|
348
|
-
this.pending = this.pending.map((p, i) => ({
|
|
349
|
-
...p,
|
|
350
|
-
steps: nextP[i]?.steps ?? p.steps
|
|
351
|
-
}));
|
|
352
|
-
this.version = msg.version;
|
|
353
|
-
if (remote.length) {
|
|
354
|
-
if (!this.peers.has(msg.siteId)) {
|
|
355
|
-
this.peers.set(msg.siteId, {
|
|
356
|
-
siteId: msg.siteId,
|
|
357
|
-
color: collabColor(msg.siteId)
|
|
358
|
-
});
|
|
406
|
+
if (!this.joined) {
|
|
407
|
+
if (this.conflictValue && msg.version > this.conflictValue.remote.version) {
|
|
408
|
+
try {
|
|
409
|
+
this.conflictValue.remote.doc = applyAll(this.conflictValue.remote.doc, msg.steps);
|
|
410
|
+
this.conflictValue.remote.version = msg.version;
|
|
411
|
+
this.handlers.onConflict?.(structuredClone(this.conflictValue));
|
|
412
|
+
} catch (error) {
|
|
413
|
+
this.handlers.onError?.(error instanceof Error ? error.message : String(error));
|
|
414
|
+
}
|
|
359
415
|
}
|
|
360
|
-
|
|
361
|
-
this.ensureAuthorCaret(msg.siteId, remote);
|
|
362
|
-
this.handlers.onRemote(remote, msg.siteId);
|
|
363
|
-
this.handlers.onPeers?.(this.peersList);
|
|
416
|
+
return;
|
|
364
417
|
}
|
|
418
|
+
this.applyServerOp(msg);
|
|
419
|
+
this.publishReplica();
|
|
420
|
+
this.flushPending();
|
|
365
421
|
break;
|
|
366
422
|
}
|
|
367
423
|
case "presence": {
|
|
@@ -391,9 +447,9 @@ var CollabClient = class {
|
|
|
391
447
|
}
|
|
392
448
|
case "error": {
|
|
393
449
|
this.handlers.onError?.(msg.message);
|
|
394
|
-
if (
|
|
450
|
+
if (msg.code === "unauthorized") {
|
|
395
451
|
this.authFailed = true;
|
|
396
|
-
this.
|
|
452
|
+
this.setStatus("auth-error");
|
|
397
453
|
try {
|
|
398
454
|
this.ws?.close();
|
|
399
455
|
} catch {
|
|
@@ -408,12 +464,57 @@ var CollabClient = class {
|
|
|
408
464
|
}
|
|
409
465
|
/** 本地已 apply 的 steps 推上服务器 */
|
|
410
466
|
push(steps) {
|
|
411
|
-
if (!steps.length
|
|
467
|
+
if (!steps.length)
|
|
412
468
|
return;
|
|
413
469
|
const clientSeq = ++this.clientSeq;
|
|
414
|
-
|
|
415
|
-
this.
|
|
416
|
-
this.
|
|
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 = transformRemoteAgainstPending(
|
|
496
|
+
msg.steps,
|
|
497
|
+
msg.siteId,
|
|
498
|
+
pendingAs,
|
|
499
|
+
this.siteId
|
|
500
|
+
);
|
|
501
|
+
this.pending = this.pending.map((item, i) => ({
|
|
502
|
+
...item,
|
|
503
|
+
steps: transformed.pending[i]?.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
|
+
this.handlers.onPeers?.(this.peersList);
|
|
417
518
|
}
|
|
418
519
|
/** 命名版本上行(侧信道,不进 OT) */
|
|
419
520
|
pushVersion(v) {
|
|
@@ -426,6 +527,45 @@ var CollabClient = class {
|
|
|
426
527
|
return;
|
|
427
528
|
this.send({ type: "version-remove", id });
|
|
428
529
|
}
|
|
530
|
+
acceptRemote() {
|
|
531
|
+
const conflict = this.conflictValue;
|
|
532
|
+
if (!conflict || this.currentStatus !== "conflict" || this.ws?.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
|
+
this.handlers.onWelcome?.({
|
|
553
|
+
siteId: welcome.siteId,
|
|
554
|
+
version: conflict.remote.version,
|
|
555
|
+
name: this.cfg.name,
|
|
556
|
+
color: this.cfg.color
|
|
557
|
+
});
|
|
558
|
+
this.handlers.onPeers?.(this.peersList);
|
|
559
|
+
if (welcome.versions)
|
|
560
|
+
this.handlers.onVersions?.(welcome.versions);
|
|
561
|
+
}
|
|
562
|
+
if (conflict.reason === "resync-with-pending")
|
|
563
|
+
this.handlers.onResync?.(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
|
+
}
|
|
429
569
|
/** 用一批 steps 搬所有 peer 的 wire 选区(作者打字时彩光标贴字) */
|
|
430
570
|
mapPeerSelsThrough(steps) {
|
|
431
571
|
for (const [id, p] of this.peers) {
|
|
@@ -470,9 +610,13 @@ var CollabClient = class {
|
|
|
470
610
|
}
|
|
471
611
|
close() {
|
|
472
612
|
this.closed = true;
|
|
613
|
+
this.joined = false;
|
|
614
|
+
this.setStatus("closed");
|
|
473
615
|
this.stopHeartbeat();
|
|
474
|
-
if (this.reconnectTimer)
|
|
616
|
+
if (this.reconnectTimer) {
|
|
475
617
|
clearTimeout(this.reconnectTimer);
|
|
618
|
+
this.reconnectTimer = null;
|
|
619
|
+
}
|
|
476
620
|
try {
|
|
477
621
|
this.ws?.close();
|
|
478
622
|
} catch {
|