markloom 0.5.1 → 0.6.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/PACKAGES.md +26 -0
- package/README.md +20 -2
- package/collab.md +42 -7
- package/dist/ai/index.cjs +4 -4
- package/dist/ai/index.d.cts +5 -4
- package/dist/ai/index.d.ts +5 -4
- package/dist/ai/index.js +3 -3
- package/dist/ai/theme.css +66 -102
- package/dist/{chunk-VXM7Q6P5.cjs → chunk-ACFYBYZJ.cjs} +2604 -966
- package/dist/chunk-CGYCJORO.js +759 -0
- package/dist/chunk-EADZKEU4.cjs +498 -0
- package/dist/chunk-EJSFZHGT.js +24 -0
- package/dist/{chunk-YIFOGP2D.cjs → chunk-I5VK4U7J.cjs} +103 -60
- package/dist/{chunk-ZMTLSFLO.js → chunk-KYZJIOOL.js} +2138 -500
- package/dist/chunk-QEVME6ZU.cjs +759 -0
- package/dist/{chunk-MFYF2A6U.js → chunk-SNFF6X45.js} +51 -1
- package/dist/{chunk-FCH7LZFI.js → chunk-TB2OY3D4.js} +50 -7
- package/dist/{chunk-4TMHVNL6.cjs → chunk-TWD5T4YC.cjs} +155 -105
- package/dist/{chunk-JS527FDV.cjs → chunk-U3JZDFL4.cjs} +49 -9
- package/dist/{chunk-RLTHVQNQ.js → chunk-VSBIMMI7.js} +45 -5
- package/dist/chunk-YBAT343I.js +498 -0
- package/dist/chunk-ZKQSQSSM.cjs +24 -0
- package/dist/{client-ZRZj2nkq.d.cts → client-CLYq17SW.d.ts} +63 -7
- package/dist/{client-6KiH4kGK.d.ts → client-DAth8pPq.d.cts} +63 -7
- package/dist/collab/index.cjs +16 -4
- package/dist/collab/index.d.cts +19 -6
- package/dist/collab/index.d.ts +19 -6
- package/dist/collab/index.js +18 -6
- package/dist/{controller-BNuuUM3E.d.ts → controller-6AJ36G52.d.ts} +99 -7
- package/dist/{controller-DQITsFhy.d.cts → controller-BIOO_XKJ.d.cts} +99 -7
- package/dist/index.cjs +57 -7
- package/dist/index.d.cts +18 -5
- package/dist/index.d.ts +18 -5
- package/dist/index.js +54 -4
- package/dist/io/index.cjs +1413 -0
- package/dist/io/index.d.cts +33 -0
- package/dist/io/index.d.ts +33 -0
- package/dist/io/index.js +1413 -0
- package/dist/react/index.cjs +8 -8
- package/dist/react/index.d.cts +5 -4
- package/dist/react/index.d.ts +5 -4
- package/dist/react/index.js +7 -7
- package/dist/{rebase-batch-8rL_aXEE.d.ts → rebase-batch-DlerAcFN.d.ts} +11 -9
- package/dist/{rebase-batch-bYnBJpRO.d.cts → rebase-batch-PjENQ742.d.cts} +11 -9
- package/dist/suggest-Bk4zLzuv.d.ts +120 -0
- package/dist/suggest-BkGGiPKA.d.cts +120 -0
- package/dist/types-COUweEav.d.cts +30 -0
- package/dist/types-COUweEav.d.ts +30 -0
- package/dist/versions-9mG1epfB.d.cts +51 -0
- package/dist/versions-Bubyo4pt.d.ts +51 -0
- package/dist/view/index.cjs +56 -12
- package/dist/view/index.d.cts +203 -11
- package/dist/view/index.d.ts +203 -11
- package/dist/view/index.js +52 -8
- package/dist/view/theme.css +1829 -1587
- package/io.md +38 -0
- package/package.json +21 -5
- package/dist/chunk-4JSJAHTB.js +0 -255
- package/dist/chunk-ALZ3A5CS.cjs +0 -255
- package/dist/chunk-GQ2HHRGI.cjs +0 -16
- package/dist/chunk-LA5SBCGQ.js +0 -16
- package/dist/chunk-LEYAQNJA.js +0 -220
- package/dist/chunk-YXQDCW4Y.cjs +0 -220
- package/dist/comments-DlExS6f-.d.cts +0 -43
- package/dist/comments-PhnR7Gkn.d.ts +0 -43
- package/dist/step-BrvHIK7O.d.cts +0 -65
- package/dist/step-BrvHIK7O.d.ts +0 -65
|
@@ -1,7 +1,7 @@
|
|
|
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
3
|
|
|
4
|
-
var
|
|
4
|
+
var _chunkZKQSQSSMcjs = require('./chunk-ZKQSQSSM.cjs');
|
|
5
5
|
|
|
6
6
|
// src/tree/map.ts
|
|
7
7
|
function mapPosition(step, pos) {
|
|
@@ -20,6 +20,19 @@ function mapPosition(step, pos) {
|
|
|
20
20
|
function mapSelection(step, sel) {
|
|
21
21
|
return { anchor: mapPosition(step, sel.anchor), head: mapPosition(step, sel.head) };
|
|
22
22
|
}
|
|
23
|
+
function mapSelectionThrough(sel, steps) {
|
|
24
|
+
if (!sel)
|
|
25
|
+
return void 0;
|
|
26
|
+
let s = sel;
|
|
27
|
+
for (const st of steps)
|
|
28
|
+
s = mapSelection(st, s);
|
|
29
|
+
return s;
|
|
30
|
+
}
|
|
31
|
+
function resolveTxAfter(before, after, steps, gaps = []) {
|
|
32
|
+
if (after)
|
|
33
|
+
return _nullishCoalesce(mapSelectionThrough(after, gaps), () => ( after));
|
|
34
|
+
return mapSelectionThrough(before, [...steps, ...gaps]);
|
|
35
|
+
}
|
|
23
36
|
function mapText(stepPath, at, delta, p, pos) {
|
|
24
37
|
if (p.length !== stepPath.length + 1 || !isPrefix(stepPath, p))
|
|
25
38
|
return pos;
|
|
@@ -78,14 +91,14 @@ function underPrefix(a, b) {
|
|
|
78
91
|
function normalizePosition(doc, pos) {
|
|
79
92
|
let cur = doc;
|
|
80
93
|
for (let i = 0; i < pos.path.length - 1; i++) {
|
|
81
|
-
if (!
|
|
94
|
+
if (!_chunkZKQSQSSMcjs.isElement.call(void 0, cur))
|
|
82
95
|
break;
|
|
83
96
|
const next = cur.children[pos.path[i]];
|
|
84
97
|
if (!next)
|
|
85
98
|
return pos;
|
|
86
99
|
cur = next;
|
|
87
100
|
}
|
|
88
|
-
if (
|
|
101
|
+
if (_chunkZKQSQSSMcjs.isText.call(void 0, cur))
|
|
89
102
|
return pos;
|
|
90
103
|
const at = _nullishCoalesce(pos.path[pos.path.length - 1], () => ( 0));
|
|
91
104
|
const prefix = pos.path.slice(0, -1);
|
|
@@ -99,7 +112,7 @@ function descendToText(parent, at, prefix) {
|
|
|
99
112
|
const toEnd = at > 0;
|
|
100
113
|
let path = prefix.concat(leftIdx);
|
|
101
114
|
let node = kids[Math.min(leftIdx, kids.length - 1)];
|
|
102
|
-
while (
|
|
115
|
+
while (_chunkZKQSQSSMcjs.isElement.call(void 0, node)) {
|
|
103
116
|
const cs = node.children;
|
|
104
117
|
if (cs.length === 0)
|
|
105
118
|
return path.concat(0);
|
|
@@ -255,12 +268,36 @@ function rebaseBatchOver(over, steps, bias) {
|
|
|
255
268
|
out.push(...rebaseOne(over, s, bias));
|
|
256
269
|
return out;
|
|
257
270
|
}
|
|
271
|
+
function transformSeqThroughOne(seq, over, bias, biasOpp) {
|
|
272
|
+
if (seq.length === 0)
|
|
273
|
+
return [];
|
|
274
|
+
const out = [];
|
|
275
|
+
let oCur = [over];
|
|
276
|
+
for (const s of seq) {
|
|
277
|
+
let sMapped = [s];
|
|
278
|
+
for (const o of oCur)
|
|
279
|
+
sMapped = rebaseBatchOver(o, sMapped, bias);
|
|
280
|
+
out.push(...sMapped);
|
|
281
|
+
let nextO = [];
|
|
282
|
+
for (const o of oCur)
|
|
283
|
+
nextO.push(...rebaseOne(s, o, biasOpp));
|
|
284
|
+
oCur = nextO;
|
|
285
|
+
for (const sm of sMapped) {
|
|
286
|
+
nextO = [];
|
|
287
|
+
for (const o of oCur)
|
|
288
|
+
nextO.push(...rebaseOne(sm, o, biasOpp));
|
|
289
|
+
oCur = nextO;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
258
294
|
function rebaseStepsThrough(steps, concurrent, thisSite) {
|
|
259
295
|
let cur = steps;
|
|
260
296
|
for (const over of concurrent) {
|
|
261
297
|
const bias = makeBias(thisSite, over.siteId);
|
|
298
|
+
const biasOpp = makeBias(over.siteId, thisSite);
|
|
262
299
|
for (const o of over.steps)
|
|
263
|
-
cur =
|
|
300
|
+
cur = transformSeqThroughOne(cur, o, bias, biasOpp);
|
|
264
301
|
}
|
|
265
302
|
return cur;
|
|
266
303
|
}
|
|
@@ -270,12 +307,12 @@ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
|
|
|
270
307
|
for (const p of pending) {
|
|
271
308
|
const rBefore = r;
|
|
272
309
|
const biasR = makeBias(remoteSite, localSite);
|
|
310
|
+
const biasL = makeBias(localSite, remoteSite);
|
|
273
311
|
for (const ps of p.steps)
|
|
274
|
-
r =
|
|
275
|
-
const biasP = makeBias(localSite, remoteSite);
|
|
312
|
+
r = transformSeqThroughOne(r, ps, biasR, biasL);
|
|
276
313
|
let pSteps = p.steps;
|
|
277
314
|
for (const rs of rBefore)
|
|
278
|
-
pSteps =
|
|
315
|
+
pSteps = transformSeqThroughOne(pSteps, rs, biasL, biasR);
|
|
279
316
|
nextPending.push({ siteId: p.siteId, steps: pSteps });
|
|
280
317
|
}
|
|
281
318
|
return { remote: r, pending: nextPending };
|
|
@@ -291,4 +328,7 @@ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
|
|
|
291
328
|
|
|
292
329
|
|
|
293
330
|
|
|
294
|
-
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
exports.mapPosition = mapPosition; exports.mapSelection = mapSelection; exports.mapSelectionThrough = mapSelectionThrough; exports.resolveTxAfter = resolveTxAfter; exports.normalizePosition = normalizePosition; exports.makeBias = makeBias; exports.rebase = rebase; exports.rebaseOne = rebaseOne; exports.rebaseBatchOver = rebaseBatchOver; exports.transformSeqThroughOne = transformSeqThroughOne; exports.rebaseStepsThrough = rebaseStepsThrough; exports.transformRemoteAgainstPending = transformRemoteAgainstPending;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
isElement,
|
|
3
3
|
isText
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-EJSFZHGT.js";
|
|
5
5
|
|
|
6
6
|
// src/tree/map.ts
|
|
7
7
|
function mapPosition(step, pos) {
|
|
@@ -20,6 +20,19 @@ function mapPosition(step, pos) {
|
|
|
20
20
|
function mapSelection(step, sel) {
|
|
21
21
|
return { anchor: mapPosition(step, sel.anchor), head: mapPosition(step, sel.head) };
|
|
22
22
|
}
|
|
23
|
+
function mapSelectionThrough(sel, steps) {
|
|
24
|
+
if (!sel)
|
|
25
|
+
return void 0;
|
|
26
|
+
let s = sel;
|
|
27
|
+
for (const st of steps)
|
|
28
|
+
s = mapSelection(st, s);
|
|
29
|
+
return s;
|
|
30
|
+
}
|
|
31
|
+
function resolveTxAfter(before, after, steps, gaps = []) {
|
|
32
|
+
if (after)
|
|
33
|
+
return mapSelectionThrough(after, gaps) ?? after;
|
|
34
|
+
return mapSelectionThrough(before, [...steps, ...gaps]);
|
|
35
|
+
}
|
|
23
36
|
function mapText(stepPath, at, delta, p, pos) {
|
|
24
37
|
if (p.length !== stepPath.length + 1 || !isPrefix(stepPath, p))
|
|
25
38
|
return pos;
|
|
@@ -255,12 +268,36 @@ function rebaseBatchOver(over, steps, bias) {
|
|
|
255
268
|
out.push(...rebaseOne(over, s, bias));
|
|
256
269
|
return out;
|
|
257
270
|
}
|
|
271
|
+
function transformSeqThroughOne(seq, over, bias, biasOpp) {
|
|
272
|
+
if (seq.length === 0)
|
|
273
|
+
return [];
|
|
274
|
+
const out = [];
|
|
275
|
+
let oCur = [over];
|
|
276
|
+
for (const s of seq) {
|
|
277
|
+
let sMapped = [s];
|
|
278
|
+
for (const o of oCur)
|
|
279
|
+
sMapped = rebaseBatchOver(o, sMapped, bias);
|
|
280
|
+
out.push(...sMapped);
|
|
281
|
+
let nextO = [];
|
|
282
|
+
for (const o of oCur)
|
|
283
|
+
nextO.push(...rebaseOne(s, o, biasOpp));
|
|
284
|
+
oCur = nextO;
|
|
285
|
+
for (const sm of sMapped) {
|
|
286
|
+
nextO = [];
|
|
287
|
+
for (const o of oCur)
|
|
288
|
+
nextO.push(...rebaseOne(sm, o, biasOpp));
|
|
289
|
+
oCur = nextO;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
258
294
|
function rebaseStepsThrough(steps, concurrent, thisSite) {
|
|
259
295
|
let cur = steps;
|
|
260
296
|
for (const over of concurrent) {
|
|
261
297
|
const bias = makeBias(thisSite, over.siteId);
|
|
298
|
+
const biasOpp = makeBias(over.siteId, thisSite);
|
|
262
299
|
for (const o of over.steps)
|
|
263
|
-
cur =
|
|
300
|
+
cur = transformSeqThroughOne(cur, o, bias, biasOpp);
|
|
264
301
|
}
|
|
265
302
|
return cur;
|
|
266
303
|
}
|
|
@@ -270,12 +307,12 @@ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
|
|
|
270
307
|
for (const p of pending) {
|
|
271
308
|
const rBefore = r;
|
|
272
309
|
const biasR = makeBias(remoteSite, localSite);
|
|
310
|
+
const biasL = makeBias(localSite, remoteSite);
|
|
273
311
|
for (const ps of p.steps)
|
|
274
|
-
r =
|
|
275
|
-
const biasP = makeBias(localSite, remoteSite);
|
|
312
|
+
r = transformSeqThroughOne(r, ps, biasR, biasL);
|
|
276
313
|
let pSteps = p.steps;
|
|
277
314
|
for (const rs of rBefore)
|
|
278
|
-
pSteps =
|
|
315
|
+
pSteps = transformSeqThroughOne(pSteps, rs, biasL, biasR);
|
|
279
316
|
nextPending.push({ siteId: p.siteId, steps: pSteps });
|
|
280
317
|
}
|
|
281
318
|
return { remote: r, pending: nextPending };
|
|
@@ -284,11 +321,14 @@ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
|
|
|
284
321
|
export {
|
|
285
322
|
mapPosition,
|
|
286
323
|
mapSelection,
|
|
324
|
+
mapSelectionThrough,
|
|
325
|
+
resolveTxAfter,
|
|
287
326
|
normalizePosition,
|
|
288
327
|
makeBias,
|
|
289
328
|
rebase,
|
|
290
329
|
rebaseOne,
|
|
291
330
|
rebaseBatchOver,
|
|
331
|
+
transformSeqThroughOne,
|
|
292
332
|
rebaseStepsThrough,
|
|
293
333
|
transformRemoteAgainstPending
|
|
294
334
|
};
|
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import {
|
|
2
|
+
mapSelection,
|
|
3
|
+
transformRemoteAgainstPending
|
|
4
|
+
} from "./chunk-VSBIMMI7.js";
|
|
5
|
+
|
|
6
|
+
// src/collab/colors.ts
|
|
7
|
+
var COLLAB_COLORS = [
|
|
8
|
+
"#ffd6d6",
|
|
9
|
+
// 浅粉
|
|
10
|
+
"#ffe8b8",
|
|
11
|
+
// 浅杏
|
|
12
|
+
"#d4f0c8",
|
|
13
|
+
// 薄荷绿
|
|
14
|
+
"#c8e4ff",
|
|
15
|
+
// 天蓝
|
|
16
|
+
"#e0d4f5",
|
|
17
|
+
// 薰衣草
|
|
18
|
+
"#d0d0d0",
|
|
19
|
+
// 浅灰
|
|
20
|
+
"#6b6b6b",
|
|
21
|
+
// 深灰
|
|
22
|
+
"#e85d4c",
|
|
23
|
+
// 红
|
|
24
|
+
"#f5a623",
|
|
25
|
+
// 橙
|
|
26
|
+
"#f0c040",
|
|
27
|
+
// 黄
|
|
28
|
+
"#34b33a",
|
|
29
|
+
// 绿
|
|
30
|
+
"#3b6ff5",
|
|
31
|
+
// 蓝
|
|
32
|
+
"#7c5cff"
|
|
33
|
+
// 紫
|
|
34
|
+
];
|
|
35
|
+
var COLLAB_CARET_COLORS = [
|
|
36
|
+
"#f06a6a",
|
|
37
|
+
// 粉
|
|
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
|
+
];
|
|
63
|
+
function collabColor(siteId, explicit) {
|
|
64
|
+
if (explicit)
|
|
65
|
+
return explicit;
|
|
66
|
+
const i = Math.abs((siteId || 1) - 1) % COLLAB_COLORS.length;
|
|
67
|
+
return COLLAB_COLORS[i];
|
|
68
|
+
}
|
|
69
|
+
function collabCaretColor(siteId, explicit) {
|
|
70
|
+
if (explicit)
|
|
71
|
+
return solidify(explicit);
|
|
72
|
+
const i = Math.abs((siteId || 1) - 1) % COLLAB_CARET_COLORS.length;
|
|
73
|
+
return COLLAB_CARET_COLORS[i];
|
|
74
|
+
}
|
|
75
|
+
function solidify(hex) {
|
|
76
|
+
const rgb = parseHex(hex);
|
|
77
|
+
if (!rgb)
|
|
78
|
+
return hex;
|
|
79
|
+
const [r, g, b] = rgb;
|
|
80
|
+
const luma = 0.2126 * (r / 255) + 0.7152 * (g / 255) + 0.0722 * (b / 255);
|
|
81
|
+
if (luma <= 0.48)
|
|
82
|
+
return hex;
|
|
83
|
+
const target = 0.4;
|
|
84
|
+
const scale = target / Math.max(luma, 0.01);
|
|
85
|
+
const mid = (r + g + b) / 3;
|
|
86
|
+
const sat = 1.25;
|
|
87
|
+
const punch = (c) => {
|
|
88
|
+
const away = mid + (c - mid) * sat;
|
|
89
|
+
return Math.max(0, Math.min(255, Math.round(away * scale)));
|
|
90
|
+
};
|
|
91
|
+
return toHex(punch(r), punch(g), punch(b));
|
|
92
|
+
}
|
|
93
|
+
function parseHex(hex) {
|
|
94
|
+
const m = /^#?([0-9a-f]{6})$/i.exec(hex.trim());
|
|
95
|
+
if (!m)
|
|
96
|
+
return null;
|
|
97
|
+
const n = Number.parseInt(m[1], 16);
|
|
98
|
+
return [n >> 16 & 255, n >> 8 & 255, n & 255];
|
|
99
|
+
}
|
|
100
|
+
function toHex(r, g, b) {
|
|
101
|
+
return `#${[r, g, b].map((c) => c.toString(16).padStart(2, "0")).join("")}`;
|
|
102
|
+
}
|
|
103
|
+
function collabLuma(hex) {
|
|
104
|
+
const rgb = parseHex(hex);
|
|
105
|
+
if (!rgb)
|
|
106
|
+
return 0.5;
|
|
107
|
+
const [r, g, b] = rgb;
|
|
108
|
+
return 0.2126 * (r / 255) + 0.7152 * (g / 255) + 0.0722 * (b / 255);
|
|
109
|
+
}
|
|
110
|
+
function collabFg(bg) {
|
|
111
|
+
return collabLuma(bg) > 0.62 ? "rgba(0,0,0,0.78)" : "#fff";
|
|
112
|
+
}
|
|
113
|
+
function collabInitial(name, siteId) {
|
|
114
|
+
const n = (name || "").trim();
|
|
115
|
+
if (n) {
|
|
116
|
+
const ch = n[0];
|
|
117
|
+
return /[a-z]/i.test(ch) ? ch.toUpperCase() : ch;
|
|
118
|
+
}
|
|
119
|
+
return String(siteId ?? "?");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/collab/client.ts
|
|
123
|
+
function wireToSel(w) {
|
|
124
|
+
const toPos = (p) => ({
|
|
125
|
+
path: p.path.concat(p.offset)
|
|
126
|
+
});
|
|
127
|
+
return { anchor: toPos(w.anchor), head: toPos(w.head) };
|
|
128
|
+
}
|
|
129
|
+
function selToWire(sel) {
|
|
130
|
+
const split = (p) => {
|
|
131
|
+
const path = p.path;
|
|
132
|
+
if (path.length < 1)
|
|
133
|
+
return { path: [], offset: 0 };
|
|
134
|
+
return { path: path.slice(0, -1), offset: path[path.length - 1] };
|
|
135
|
+
};
|
|
136
|
+
return { anchor: split(sel.anchor), head: split(sel.head) };
|
|
137
|
+
}
|
|
138
|
+
function toWsUrl(url, room) {
|
|
139
|
+
let u = url.replace(/\/+$/, "");
|
|
140
|
+
if (u.startsWith("https://"))
|
|
141
|
+
u = `wss://${u.slice(8)}`;
|
|
142
|
+
else if (u.startsWith("http://"))
|
|
143
|
+
u = `ws://${u.slice(7)}`;
|
|
144
|
+
return `${u}/room/${encodeURIComponent(room)}`;
|
|
145
|
+
}
|
|
146
|
+
function retryDelay(attempt) {
|
|
147
|
+
const base = Math.min(1e3 * 2 ** Math.min(attempt, 5), 3e4);
|
|
148
|
+
const jitter = base * 0.5 * Math.random();
|
|
149
|
+
return base * 0.75 + jitter;
|
|
150
|
+
}
|
|
151
|
+
var CollabClient = class {
|
|
152
|
+
cfg;
|
|
153
|
+
handlers;
|
|
154
|
+
ws = null;
|
|
155
|
+
closed = false;
|
|
156
|
+
retry = 0;
|
|
157
|
+
reconnectTimer = null;
|
|
158
|
+
pingTimer = null;
|
|
159
|
+
lastPong = 0;
|
|
160
|
+
authFailed = false;
|
|
161
|
+
siteId = 0;
|
|
162
|
+
version = 0;
|
|
163
|
+
clientSeq = 0;
|
|
164
|
+
pending = [];
|
|
165
|
+
peers = /* @__PURE__ */ new Map();
|
|
166
|
+
joined = false;
|
|
167
|
+
constructor(cfg, handlers) {
|
|
168
|
+
this.cfg = cfg;
|
|
169
|
+
this.handlers = handlers;
|
|
170
|
+
this.connect();
|
|
171
|
+
}
|
|
172
|
+
get peersList() {
|
|
173
|
+
return [...this.peers.values()];
|
|
174
|
+
}
|
|
175
|
+
get status() {
|
|
176
|
+
if (this.authFailed)
|
|
177
|
+
return "auth-error";
|
|
178
|
+
if (this.closed)
|
|
179
|
+
return "closed";
|
|
180
|
+
if (!this.joined)
|
|
181
|
+
return this.ws?.readyState === WebSocket.OPEN ? "connecting" : "connecting";
|
|
182
|
+
return "open";
|
|
183
|
+
}
|
|
184
|
+
connect() {
|
|
185
|
+
if (this.closed || this.authFailed)
|
|
186
|
+
return;
|
|
187
|
+
this.handlers.onStatus?.("connecting");
|
|
188
|
+
const url = toWsUrl(this.cfg.url, this.cfg.room);
|
|
189
|
+
let ws;
|
|
190
|
+
try {
|
|
191
|
+
ws = new WebSocket(url);
|
|
192
|
+
} catch (e) {
|
|
193
|
+
this.handlers.onError?.(e instanceof Error ? e.message : String(e));
|
|
194
|
+
this.scheduleReconnect();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
this.ws = ws;
|
|
198
|
+
ws.onopen = () => {
|
|
199
|
+
this.retry = 0;
|
|
200
|
+
this.lastPong = Date.now();
|
|
201
|
+
this.startHeartbeat();
|
|
202
|
+
void this.sendJoin();
|
|
203
|
+
};
|
|
204
|
+
ws.onclose = () => {
|
|
205
|
+
this.joined = false;
|
|
206
|
+
this.stopHeartbeat();
|
|
207
|
+
if (this.authFailed) {
|
|
208
|
+
this.handlers.onStatus?.("auth-error");
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
this.handlers.onStatus?.("closed");
|
|
212
|
+
if (!this.closed)
|
|
213
|
+
this.scheduleReconnect();
|
|
214
|
+
};
|
|
215
|
+
ws.onerror = () => {
|
|
216
|
+
try {
|
|
217
|
+
ws.close();
|
|
218
|
+
} catch {
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
ws.onmessage = (ev) => {
|
|
222
|
+
try {
|
|
223
|
+
const msg = JSON.parse(String(ev.data));
|
|
224
|
+
this.onServer(msg);
|
|
225
|
+
} catch {
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
async sendJoin() {
|
|
230
|
+
let token;
|
|
231
|
+
try {
|
|
232
|
+
const t = this.cfg.token;
|
|
233
|
+
if (typeof t === "function")
|
|
234
|
+
token = await t();
|
|
235
|
+
else if (typeof t === "string")
|
|
236
|
+
token = t;
|
|
237
|
+
} catch (e) {
|
|
238
|
+
this.handlers.onError?.(e instanceof Error ? e.message : String(e));
|
|
239
|
+
this.authFailed = true;
|
|
240
|
+
this.handlers.onStatus?.("auth-error");
|
|
241
|
+
try {
|
|
242
|
+
this.ws?.close();
|
|
243
|
+
} catch {
|
|
244
|
+
}
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
this.send({
|
|
248
|
+
type: "join",
|
|
249
|
+
room: this.cfg.room,
|
|
250
|
+
name: this.cfg.name,
|
|
251
|
+
color: this.cfg.color,
|
|
252
|
+
seed: this.cfg.seed,
|
|
253
|
+
...token ? { token } : {}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
startHeartbeat() {
|
|
257
|
+
this.stopHeartbeat();
|
|
258
|
+
this.pingTimer = setInterval(() => {
|
|
259
|
+
if (this.ws?.readyState !== WebSocket.OPEN)
|
|
260
|
+
return;
|
|
261
|
+
if (this.lastPong && Date.now() - this.lastPong > 45e3) {
|
|
262
|
+
try {
|
|
263
|
+
this.ws.close();
|
|
264
|
+
} catch {
|
|
265
|
+
}
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
this.send({ type: "ping" });
|
|
269
|
+
}, 2e4);
|
|
270
|
+
}
|
|
271
|
+
stopHeartbeat() {
|
|
272
|
+
if (this.pingTimer) {
|
|
273
|
+
clearInterval(this.pingTimer);
|
|
274
|
+
this.pingTimer = null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
scheduleReconnect() {
|
|
278
|
+
if (this.reconnectTimer || this.closed || this.authFailed)
|
|
279
|
+
return;
|
|
280
|
+
const delay = retryDelay(this.retry++);
|
|
281
|
+
this.reconnectTimer = setTimeout(() => {
|
|
282
|
+
this.reconnectTimer = null;
|
|
283
|
+
this.connect();
|
|
284
|
+
}, delay);
|
|
285
|
+
}
|
|
286
|
+
send(msg) {
|
|
287
|
+
if (this.ws?.readyState === WebSocket.OPEN)
|
|
288
|
+
this.ws.send(JSON.stringify(msg));
|
|
289
|
+
}
|
|
290
|
+
onServer(msg) {
|
|
291
|
+
switch (msg.type) {
|
|
292
|
+
case "welcome": {
|
|
293
|
+
this.siteId = msg.siteId;
|
|
294
|
+
this.version = msg.version;
|
|
295
|
+
this.pending = [];
|
|
296
|
+
this.joined = true;
|
|
297
|
+
this.handlers.onStatus?.("open");
|
|
298
|
+
if (!this.cfg.color)
|
|
299
|
+
this.cfg.color = collabColor(msg.siteId);
|
|
300
|
+
this.peers.clear();
|
|
301
|
+
for (const p of msg.peers) {
|
|
302
|
+
this.peers.set(p.siteId, {
|
|
303
|
+
...p,
|
|
304
|
+
color: collabColor(p.siteId, p.color)
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
this.handlers.onWelcome?.({
|
|
308
|
+
siteId: msg.siteId,
|
|
309
|
+
version: msg.version,
|
|
310
|
+
name: this.cfg.name,
|
|
311
|
+
color: this.cfg.color
|
|
312
|
+
});
|
|
313
|
+
this.handlers.onDoc(msg.doc, msg.version);
|
|
314
|
+
this.handlers.onPeers?.(this.peersList);
|
|
315
|
+
if (msg.versions)
|
|
316
|
+
this.handlers.onVersions?.(msg.versions);
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
case "versions": {
|
|
320
|
+
this.handlers.onVersions?.(msg.versions);
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
case "resync": {
|
|
324
|
+
this.version = msg.version;
|
|
325
|
+
this.pending = [];
|
|
326
|
+
this.handlers.onStatus?.("resyncing");
|
|
327
|
+
this.handlers.onResync?.(msg.version);
|
|
328
|
+
this.handlers.onDoc(msg.doc, msg.version);
|
|
329
|
+
this.handlers.onStatus?.("open");
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
case "op": {
|
|
333
|
+
if (!this.joined)
|
|
334
|
+
return;
|
|
335
|
+
if (msg.siteId === this.siteId) {
|
|
336
|
+
if (msg.clientSeq != null)
|
|
337
|
+
this.pending = this.pending.filter((p) => p.clientSeq > msg.clientSeq);
|
|
338
|
+
this.version = msg.version;
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const pendingAs = this.pending.map((p) => ({ siteId: this.siteId, steps: p.steps }));
|
|
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
|
+
});
|
|
359
|
+
}
|
|
360
|
+
this.mapPeerSelsThrough(remote);
|
|
361
|
+
this.ensureAuthorCaret(msg.siteId, remote);
|
|
362
|
+
this.handlers.onRemote(remote, msg.siteId);
|
|
363
|
+
this.handlers.onPeers?.(this.peersList);
|
|
364
|
+
}
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
case "presence": {
|
|
368
|
+
const cur = this.peers.get(msg.siteId) ?? { siteId: msg.siteId };
|
|
369
|
+
this.peers.set(msg.siteId, {
|
|
370
|
+
...cur,
|
|
371
|
+
name: msg.name ?? cur.name,
|
|
372
|
+
color: collabColor(msg.siteId, msg.color ?? cur.color),
|
|
373
|
+
sel: msg.sel === void 0 ? cur.sel : msg.sel
|
|
374
|
+
});
|
|
375
|
+
this.handlers.onPeers?.(this.peersList);
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case "peer-join": {
|
|
379
|
+
this.peers.set(msg.siteId, {
|
|
380
|
+
siteId: msg.siteId,
|
|
381
|
+
name: msg.name,
|
|
382
|
+
color: collabColor(msg.siteId, msg.color)
|
|
383
|
+
});
|
|
384
|
+
this.handlers.onPeers?.(this.peersList);
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
case "peer-leave": {
|
|
388
|
+
this.peers.delete(msg.siteId);
|
|
389
|
+
this.handlers.onPeers?.(this.peersList);
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
case "error": {
|
|
393
|
+
this.handlers.onError?.(msg.message);
|
|
394
|
+
if (/auth|token|unauthorized/i.test(msg.message)) {
|
|
395
|
+
this.authFailed = true;
|
|
396
|
+
this.handlers.onStatus?.("auth-error");
|
|
397
|
+
try {
|
|
398
|
+
this.ws?.close();
|
|
399
|
+
} catch {
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
case "pong":
|
|
405
|
+
this.lastPong = Date.now();
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
/** 本地已 apply 的 steps 推上服务器 */
|
|
410
|
+
push(steps) {
|
|
411
|
+
if (!steps.length || !this.joined || this.siteId <= 0)
|
|
412
|
+
return;
|
|
413
|
+
const clientSeq = ++this.clientSeq;
|
|
414
|
+
const baseVersion = this.version;
|
|
415
|
+
this.pending.push({ clientSeq, baseVersion, steps });
|
|
416
|
+
this.send({ type: "op", baseVersion, clientSeq, steps });
|
|
417
|
+
}
|
|
418
|
+
/** 命名版本上行(侧信道,不进 OT) */
|
|
419
|
+
pushVersion(v) {
|
|
420
|
+
if (!this.joined || this.siteId <= 0)
|
|
421
|
+
return;
|
|
422
|
+
this.send({ type: "version-save", version: v });
|
|
423
|
+
}
|
|
424
|
+
removeVersion(id) {
|
|
425
|
+
if (!this.joined || this.siteId <= 0)
|
|
426
|
+
return;
|
|
427
|
+
this.send({ type: "version-remove", id });
|
|
428
|
+
}
|
|
429
|
+
/** 用一批 steps 搬所有 peer 的 wire 选区(作者打字时彩光标贴字) */
|
|
430
|
+
mapPeerSelsThrough(steps) {
|
|
431
|
+
for (const [id, p] of this.peers) {
|
|
432
|
+
if (!p.sel)
|
|
433
|
+
continue;
|
|
434
|
+
let sel = wireToSel(p.sel);
|
|
435
|
+
for (const s of steps)
|
|
436
|
+
sel = mapSelection(s, sel);
|
|
437
|
+
this.peers.set(id, { ...p, sel: selToWire(sel) });
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* 作者无历史 presence 时,从本批最后一次 insertText 推光标到插入末尾。
|
|
442
|
+
* 有 sel 时 mapPeerSelsThrough 已搬过,不再覆盖。
|
|
443
|
+
*/
|
|
444
|
+
ensureAuthorCaret(siteId, steps) {
|
|
445
|
+
const p = this.peers.get(siteId);
|
|
446
|
+
if (!p || p.sel)
|
|
447
|
+
return;
|
|
448
|
+
for (let i = steps.length - 1; i >= 0; i--) {
|
|
449
|
+
const s = steps[i];
|
|
450
|
+
if (s.kind !== "insertText")
|
|
451
|
+
continue;
|
|
452
|
+
const offset = s.offset + s.text.length;
|
|
453
|
+
p.sel = {
|
|
454
|
+
anchor: { path: s.path.slice(), offset },
|
|
455
|
+
head: { path: s.path.slice(), offset }
|
|
456
|
+
};
|
|
457
|
+
this.peers.set(siteId, p);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
presence(sel) {
|
|
462
|
+
if (!this.joined)
|
|
463
|
+
return;
|
|
464
|
+
this.send({
|
|
465
|
+
type: "presence",
|
|
466
|
+
name: this.cfg.name,
|
|
467
|
+
color: this.cfg.color ?? collabColor(this.siteId),
|
|
468
|
+
sel
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
close() {
|
|
472
|
+
this.closed = true;
|
|
473
|
+
this.stopHeartbeat();
|
|
474
|
+
if (this.reconnectTimer)
|
|
475
|
+
clearTimeout(this.reconnectTimer);
|
|
476
|
+
try {
|
|
477
|
+
this.ws?.close();
|
|
478
|
+
} catch {
|
|
479
|
+
}
|
|
480
|
+
this.ws = null;
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
function connectCollab(cfg, handlers) {
|
|
484
|
+
return new CollabClient(cfg, handlers);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export {
|
|
488
|
+
COLLAB_COLORS,
|
|
489
|
+
COLLAB_CARET_COLORS,
|
|
490
|
+
collabColor,
|
|
491
|
+
collabCaretColor,
|
|
492
|
+
solidify,
|
|
493
|
+
collabLuma,
|
|
494
|
+
collabFg,
|
|
495
|
+
collabInitial,
|
|
496
|
+
CollabClient,
|
|
497
|
+
connectCollab
|
|
498
|
+
};
|