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,297 @@
1
+ import {
2
+ isElement,
3
+ isInlineAtom,
4
+ isText
5
+ } from "./chunk-WSJUXPAG.js";
6
+
7
+ // src/tree/addr.ts
8
+ function resolve(doc, path) {
9
+ if (path.length === 0)
10
+ return { node: doc, offset: 0, parents: [], indexInParent: [] };
11
+ const parents = [];
12
+ const indexInParent = [];
13
+ let cur = doc;
14
+ for (let i = 0; i < path.length - 1; i++) {
15
+ if (!isElement(cur))
16
+ throw new Error(`path \u8D8A\u8FC7\u6587\u672C\u53F6: ${path.join(",")} \u5728\u7B2C ${i} \u6BB5`);
17
+ parents.push(cur);
18
+ indexInParent.push(path[i]);
19
+ const child = cur.children[path[i]];
20
+ if (!child)
21
+ throw new Error(`path \u8D8A\u754C: ${path.join(",")} \u5728\u7B2C ${i} \u6BB5\u65E0 child ${path[i]}`);
22
+ cur = child;
23
+ }
24
+ const last = path[path.length - 1];
25
+ return { node: cur, offset: last, parents, indexInParent };
26
+ }
27
+ function firstLeafPath(block) {
28
+ for (let i = 0; i < block.children.length; i++) {
29
+ const c = block.children[i];
30
+ if (isInlineAtom(c))
31
+ continue;
32
+ if (isText(c))
33
+ return [i];
34
+ return [i, ...firstLeafPath(c)];
35
+ }
36
+ return [0];
37
+ }
38
+ function absorbsAfter(n) {
39
+ if (isText(n))
40
+ return false;
41
+ if (n.type === "mention")
42
+ return false;
43
+ return n.children.length === 0 || !hasTextLeaf(n);
44
+ }
45
+ function gapToTextPath(parent, at, prefix = []) {
46
+ const kids = parent.children;
47
+ if (kids.length === 0)
48
+ return prefix.concat(0);
49
+ const clampAt = Math.max(0, Math.min(at, kids.length));
50
+ const tryEnd = (idx) => {
51
+ const n = kids[idx];
52
+ if (!n || isText(n))
53
+ return n && isText(n) ? prefix.concat(idx, n.text.length) : null;
54
+ if (n.children.length === 0 || !hasTextLeaf(n))
55
+ return null;
56
+ return prefix.concat(idx, ...lastLeafEnd(n));
57
+ };
58
+ const tryStart = (idx) => {
59
+ const n = kids[idx];
60
+ if (!n || isText(n))
61
+ return n && isText(n) ? prefix.concat(idx, 0) : null;
62
+ if (n.children.length === 0 || !hasTextLeaf(n))
63
+ return null;
64
+ return prefix.concat(idx, ...firstLeafPath(n), 0);
65
+ };
66
+ let i = clampAt;
67
+ while (i < kids.length && absorbsAfter(kids[i]))
68
+ i++;
69
+ if (i !== clampAt) {
70
+ const skipped = tryStart(i);
71
+ if (skipped)
72
+ return skipped;
73
+ } else if (clampAt > 0) {
74
+ const left = tryEnd(clampAt - 1);
75
+ if (left)
76
+ return left;
77
+ const right = tryStart(clampAt);
78
+ if (right)
79
+ return right;
80
+ } else {
81
+ const right = tryStart(0);
82
+ if (right)
83
+ return right;
84
+ }
85
+ for (let j = Math.min(i, kids.length) - 1; j >= 0; j--) {
86
+ const p = tryEnd(j);
87
+ if (p)
88
+ return p;
89
+ }
90
+ for (let j = i; j < kids.length; j++) {
91
+ const p = tryStart(j);
92
+ if (p)
93
+ return p;
94
+ }
95
+ return prefix.concat(0);
96
+ }
97
+ function hasTextLeaf(n) {
98
+ for (const c of n.children) {
99
+ if (isText(c))
100
+ return true;
101
+ if (isElement(c) && !isInlineAtom(c) && hasTextLeaf(c))
102
+ return true;
103
+ }
104
+ return false;
105
+ }
106
+ function normalizeToLeaf(doc, path) {
107
+ if (path.length === 0)
108
+ return gapToTextPath(doc, 0, []);
109
+ const prefix = [];
110
+ let cur = doc;
111
+ for (let i = 0; i < path.length; i++) {
112
+ if (isText(cur)) {
113
+ return prefix.concat(Math.max(0, path[i]));
114
+ }
115
+ if (i === path.length - 1) {
116
+ if (isElement(cur) && cur.children.length === 0 && prefix.length >= 1) {
117
+ const idx = prefix[prefix.length - 1];
118
+ const parentPath = prefix.slice(0, -1);
119
+ const parent = parentPath.length ? nodeAt(doc, parentPath) : doc;
120
+ return gapToTextPath(parent, absorbsAfter(cur) ? idx + 1 : idx, parentPath);
121
+ }
122
+ return gapToTextPath(cur, path[i], prefix);
123
+ }
124
+ const child = cur.children[path[i]];
125
+ if (!child) {
126
+ if (isElement(cur) && cur.children.length === 0 && prefix.length >= 1) {
127
+ const idx = prefix[prefix.length - 1];
128
+ const parentPath = prefix.slice(0, -1);
129
+ const parent = parentPath.length ? nodeAt(doc, parentPath) : doc;
130
+ return gapToTextPath(parent, absorbsAfter(cur) ? idx + 1 : idx, parentPath);
131
+ }
132
+ return gapToTextPath(cur, path[i] ?? 0, prefix);
133
+ }
134
+ prefix.push(path[i]);
135
+ cur = child;
136
+ }
137
+ if (isText(cur))
138
+ return prefix.concat(0);
139
+ if (isInlineAtom(cur) && prefix.length >= 1) {
140
+ const parent = nodeAt(doc, prefix.slice(0, -1));
141
+ const idx = prefix[prefix.length - 1];
142
+ return gapToTextPath(parent, absorbsAfter(cur) ? idx + 1 : idx, prefix.slice(0, -1));
143
+ }
144
+ if (isElement(cur) && cur.children.length === 0 && prefix.length >= 1) {
145
+ const parent = nodeAt(doc, prefix.slice(0, -1));
146
+ const idx = prefix[prefix.length - 1];
147
+ return gapToTextPath(parent, idx + 1, prefix.slice(0, -1));
148
+ }
149
+ if (isElement(cur))
150
+ return gapToTextPath(cur, 0, prefix);
151
+ return prefix.concat(0);
152
+ }
153
+ function lastLeafEnd(block) {
154
+ for (let i = block.children.length - 1; i >= 0; i--) {
155
+ const last = block.children[i];
156
+ if (isInlineAtom(last))
157
+ continue;
158
+ if (isText(last))
159
+ return [i, last.text.length];
160
+ return [i, ...lastLeafEnd(last)];
161
+ }
162
+ return [0, 0];
163
+ }
164
+ function nodeAt(doc, path) {
165
+ let cur = doc;
166
+ for (const seg of path) {
167
+ if (!isElement(cur))
168
+ throw new Error(`nodeAt \u8D8A\u8FC7\u6587\u672C\u53F6: ${path.join(",")}`);
169
+ const child = cur.children[seg];
170
+ if (!child)
171
+ throw new Error(`nodeAt \u8D8A\u754C: ${path.join(",")} \u65E0 child ${seg}`);
172
+ cur = child;
173
+ }
174
+ return cur;
175
+ }
176
+ function replaceAt(doc, path, next) {
177
+ if (path.length === 0) {
178
+ if (isText(next))
179
+ throw new Error("\u6839\u4E0D\u80FD\u662F\u6587\u672C\u53F6");
180
+ return next;
181
+ }
182
+ return update(doc, path, 0, () => next);
183
+ }
184
+ function spliceAt(doc, path, at, remove, insert) {
185
+ return update(doc, path, 0, (node) => {
186
+ if (!isElement(node))
187
+ throw new Error("spliceAt \u76EE\u6807\u4E0D\u662F\u5143\u7D20");
188
+ const children = node.children.slice();
189
+ children.splice(at, remove, ...insert);
190
+ return { ...node, children };
191
+ });
192
+ }
193
+ function update(node, path, depth, fn) {
194
+ if (depth === path.length) {
195
+ const r = fn(node);
196
+ if (isText(r))
197
+ throw new Error("update \u4E0D\u80FD\u628A\u5143\u7D20\u6362\u6210\u6587\u672C\u53F6");
198
+ return r;
199
+ }
200
+ if (!isElement(node))
201
+ throw new Error(`update \u8D8A\u8FC7\u6587\u672C\u53F6: ${path.join(",")}`);
202
+ const idx = path[depth];
203
+ const children = node.children.slice();
204
+ children[idx] = updateChild(children[idx], path, depth + 1, fn);
205
+ return { ...node, children };
206
+ }
207
+ function updateChild(node, path, depth, fn) {
208
+ if (depth === path.length)
209
+ return fn(node);
210
+ if (!isElement(node))
211
+ throw new Error(`update \u8D8A\u8FC7\u6587\u672C\u53F6: ${path.join(",")}`);
212
+ const idx = path[depth];
213
+ const children = node.children.slice();
214
+ children[idx] = updateChild(children[idx], path, depth + 1, fn);
215
+ return { ...node, children };
216
+ }
217
+
218
+ // src/tree/step.ts
219
+ function apply(doc, step) {
220
+ switch (step.kind) {
221
+ case "insertText": {
222
+ const leaf = nodeAt(doc, step.path);
223
+ if (!isText(leaf))
224
+ throw new Error("insertText \u76EE\u6807\u4E0D\u662F\u6587\u672C\u53F6");
225
+ const text = leaf.text.slice(0, step.offset) + step.text + leaf.text.slice(step.offset);
226
+ return replaceAt(doc, step.path, { ...leaf, text });
227
+ }
228
+ case "deleteText": {
229
+ const leaf = nodeAt(doc, step.path);
230
+ if (!isText(leaf))
231
+ throw new Error("deleteText \u76EE\u6807\u4E0D\u662F\u6587\u672C\u53F6");
232
+ const text = leaf.text.slice(0, step.offset) + leaf.text.slice(step.offset + step.length);
233
+ return replaceAt(doc, step.path, { ...leaf, text });
234
+ }
235
+ case "replaceNode":
236
+ return replaceAt(doc, step.path, step.node);
237
+ case "spliceChildren":
238
+ return spliceAt(doc, step.path, step.at, step.remove, step.insert);
239
+ }
240
+ }
241
+ function invert(doc, step) {
242
+ switch (step.kind) {
243
+ case "insertText":
244
+ return { kind: "deleteText", path: step.path, offset: step.offset, length: step.text.length };
245
+ case "deleteText": {
246
+ const leaf = nodeAt(doc, step.path);
247
+ const removed = leaf.text.slice(step.offset, step.offset + step.length);
248
+ const marks = leaf.marks;
249
+ return { kind: "insertText", path: step.path, offset: step.offset, text: removed, marks };
250
+ }
251
+ case "replaceNode":
252
+ return { kind: "replaceNode", path: step.path, node: nodeAt(doc, step.path) };
253
+ case "spliceChildren": {
254
+ const parent = nodeAt(doc, step.path);
255
+ const removed = parent.children.slice(step.at, step.at + step.remove);
256
+ return { kind: "spliceChildren", path: step.path, at: step.at, remove: step.insert.length, insert: removed };
257
+ }
258
+ }
259
+ }
260
+ function applyAll(doc, steps) {
261
+ return steps.reduce(apply, doc);
262
+ }
263
+ function moveBlock(doc, fromIdx, toGap) {
264
+ if (toGap === fromIdx || toGap === fromIdx + 1)
265
+ return [];
266
+ const block = doc.children[fromIdx];
267
+ const insertAt = toGap > fromIdx ? toGap - 1 : toGap;
268
+ return [
269
+ { kind: "spliceChildren", path: [], at: fromIdx, remove: 1, insert: [] },
270
+ { kind: "spliceChildren", path: [], at: insertAt, remove: 0, insert: [block] }
271
+ ];
272
+ }
273
+ function invertAll(doc, steps) {
274
+ const inverses = [];
275
+ let cur = doc;
276
+ for (const step of steps) {
277
+ inverses.push(invert(cur, step));
278
+ cur = apply(cur, step);
279
+ }
280
+ return inverses.reverse();
281
+ }
282
+
283
+ export {
284
+ resolve,
285
+ firstLeafPath,
286
+ gapToTextPath,
287
+ normalizeToLeaf,
288
+ lastLeafEnd,
289
+ nodeAt,
290
+ replaceAt,
291
+ spliceAt,
292
+ apply,
293
+ invert,
294
+ applyAll,
295
+ moveBlock,
296
+ invertAll
297
+ };
@@ -1,5 +1,5 @@
1
- import { P as Path, E as ElementNode } from './types-COUweEav.js';
2
- import { S as Step, a as DocVersion } from './versions-Bubyo4pt.js';
1
+ import { P as Path, E as ElementNode } from './types-BkHlmUFL.js';
2
+ import { S as Step, a as DocVersion } from './versions-DOkOyh2J.js';
3
3
 
4
4
  /** 光标/选区在协议里的轻量形态(path + offset,无 assoc) */
5
5
  type WireSel = {
@@ -21,6 +21,9 @@ type PeerInfo = {
21
21
  type ClientJoin = {
22
22
  type: 'join';
23
23
  room: string;
24
+ clientId: string;
25
+ /** 本地副本已确认到的服务端版本;新副本不传,接收完整快照。 */
26
+ version?: number;
24
27
  name?: string;
25
28
  color?: string;
26
29
  /** 空房时用客户端 doc 初始化 */
@@ -30,6 +33,7 @@ type ClientJoin = {
30
33
  };
31
34
  type ClientOp = {
32
35
  type: 'op';
36
+ clientId: string;
33
37
  baseVersion: number;
34
38
  clientSeq: number;
35
39
  steps: Step[];
@@ -57,7 +61,11 @@ type ServerWelcome = {
57
61
  type: 'welcome';
58
62
  siteId: number;
59
63
  version: number;
64
+ ackedSeq: number;
65
+ seeded: boolean;
60
66
  doc: ElementNode;
67
+ /** 已有本地副本只取 version 之后的完整增量,避免快照覆盖离线写。 */
68
+ ops: ServerOp[];
61
69
  peers: PeerInfo[];
62
70
  /** 房间内命名版本快照列表 */
63
71
  versions?: DocVersion[];
@@ -66,7 +74,8 @@ type ServerOp = {
66
74
  type: 'op';
67
75
  version: number;
68
76
  siteId: number;
69
- clientSeq?: number;
77
+ clientId: string;
78
+ clientSeq: number;
70
79
  steps: Step[];
71
80
  };
72
81
  type ServerPresence = {
@@ -91,8 +100,10 @@ type ServerResync = {
91
100
  version: number;
92
101
  doc: ElementNode;
93
102
  };
103
+ type ServerErrorCode = 'invalid-message' | 'unauthorized' | 'invalid-version' | 'not-joined' | 'identity-mismatch' | 'operation-rejected';
94
104
  type ServerError = {
95
105
  type: 'error';
106
+ code: ServerErrorCode;
96
107
  message: string;
97
108
  };
98
109
  type ServerPong = {
@@ -105,7 +116,24 @@ type ServerVersions = {
105
116
  };
106
117
  type ServerMsg = ServerWelcome | ServerOp | ServerPresence | ServerPeerJoin | ServerPeerLeave | ServerResync | ServerError | ServerPong | ServerVersions;
107
118
 
108
- type CollabStatus = 'connecting' | 'open' | 'closed' | 'resyncing' | 'auth-error';
119
+ type CollabStatus = 'connecting' | 'open' | 'closed' | 'resyncing' | 'conflict' | 'auth-error';
120
+ type CollabConflict = {
121
+ reason: 'remote-bootstrap-required' | 'local-diverged' | 'resync-with-pending';
122
+ remote: {
123
+ doc: ElementNode;
124
+ version: number;
125
+ };
126
+ };
127
+ type CollabReplica = {
128
+ clientId: string;
129
+ version: number;
130
+ clientSeq: number;
131
+ synced: boolean;
132
+ pending: {
133
+ clientSeq: number;
134
+ steps: Step[];
135
+ }[];
136
+ };
109
137
  type CollabConfig = {
110
138
  /** ws(s)://host 或 http(s)://host(自动改 scheme) */
111
139
  url: string;
@@ -114,6 +142,11 @@ type CollabConfig = {
114
142
  color?: string;
115
143
  /** 空房初始化文档;默认由 Editor 传入当前 doc */
116
144
  seed?: ElementNode;
145
+ /** 与本地文档一同持久化;恢复后可安全重放未确认操作。 */
146
+ replica?: CollabReplica;
147
+ onReplica?: (replica: CollabReplica) => void;
148
+ /** 显式把已有远端房间导入一个全新本地副本;默认永不以远端覆盖本地。 */
149
+ bootstrap?: 'remote';
117
150
  /**
118
151
  * 鉴权钩:静态 token 或每次 join 取一次(可 async)。
119
152
  * worker 设 COLLAB_TOKEN 时服务端校验;未设则忽略。
@@ -124,14 +157,15 @@ type CollabConfig = {
124
157
  * 不传则插在 editor root 内 sticky 顶(lab 默认)。
125
158
  */
126
159
  railParent?: () => HTMLElement | null;
127
- /** 连接状态(connecting/open/closed/resyncing/auth-error) */
160
+ /** 连接状态(connecting/open/closed/resyncing/conflict/auth-error) */
128
161
  onStatus?: (s: CollabStatus) => void;
129
162
  /** 服务端强制 resync(版本落后 / rebase 失败) */
130
163
  onResync?: (version: number) => void;
164
+ onConflict?: (conflict: CollabConflict) => void;
131
165
  onError?: (message: string) => void;
132
166
  };
133
167
  type CollabHandlers = {
134
- /** 权威快照(welcome / resync) */
168
+ /** 新副本快照 / 无 pending 的安全 resync */
135
169
  onDoc: (doc: ElementNode, version: number) => void;
136
170
  /** 已变换的远端 steps + 作者 siteId,应 applyRemote(不入本端 undo 栈) */
137
171
  onRemote: (steps: Step[], siteId: number) => void;
@@ -146,6 +180,7 @@ type CollabHandlers = {
146
180
  onStatus?: (s: CollabStatus) => void;
147
181
  /** 即将应用权威快照(resync 路径) */
148
182
  onResync?: (version: number) => void;
183
+ onConflict?: (conflict: CollabConflict) => void;
149
184
  /** 房间命名版本列表(welcome / 远端 save·remove) */
150
185
  onVersions?: (versions: DocVersion[]) => void;
151
186
  onError?: (message: string) => void;
@@ -164,7 +199,14 @@ declare class CollabClient {
164
199
  private pingTimer;
165
200
  private lastPong;
166
201
  private authFailed;
202
+ private currentStatus;
203
+ private notifiedStatus;
204
+ private synced;
205
+ private conflictValue;
206
+ private conflictAckedSeq;
207
+ private conflictWelcome;
167
208
  siteId: number;
209
+ readonly clientId: string;
168
210
  version: number;
169
211
  private clientSeq;
170
212
  private pending;
@@ -173,6 +215,11 @@ declare class CollabClient {
173
215
  constructor(cfg: CollabConfig, handlers: CollabHandlers);
174
216
  get peersList(): PeerInfo[];
175
217
  get status(): CollabStatus;
218
+ get conflict(): CollabConflict | null;
219
+ private setStatus;
220
+ get replica(): CollabReplica;
221
+ private publishReplica;
222
+ private enterConflict;
176
223
  private connect;
177
224
  private sendJoin;
178
225
  private startHeartbeat;
@@ -182,9 +229,12 @@ declare class CollabClient {
182
229
  private onServer;
183
230
  /** 本地已 apply 的 steps 推上服务器 */
184
231
  push(steps: Step[]): void;
232
+ private flushPending;
233
+ private applyServerOp;
185
234
  /** 命名版本上行(侧信道,不进 OT) */
186
235
  pushVersion(v: DocVersion): void;
187
236
  removeVersion(id: string): void;
237
+ acceptRemote(): boolean;
188
238
  /** 用一批 steps 搬所有 peer 的 wire 选区(作者打字时彩光标贴字) */
189
239
  private mapPeerSelsThrough;
190
240
  /**
@@ -197,4 +247,4 @@ declare class CollabClient {
197
247
  }
198
248
  declare function connectCollab(cfg: CollabConfig, handlers: CollabHandlers): CollabClient;
199
249
 
200
- export { type ClientJoin as C, type PeerInfo as P, type ServerMsg as S, type WireSel as W, type ClientMsg as a, type ClientOp as b, CollabClient as c, type CollabConfig as d, type CollabHandlers as e, type CollabStatus as f, type ServerOp as g, type ServerWelcome as h, connectCollab as i };
250
+ export { type ClientJoin as C, type PeerInfo as P, type ServerError as S, type WireSel as W, type ClientMsg as a, type ClientOp as b, CollabClient as c, type CollabConfig as d, type CollabConflict as e, type CollabHandlers as f, type CollabReplica as g, type CollabStatus as h, type ServerErrorCode as i, type ServerMsg as j, type ServerOp as k, type ServerWelcome as l, connectCollab as m };
@@ -1,5 +1,5 @@
1
- import { P as Path, E as ElementNode } from './types-COUweEav.cjs';
2
- import { S as Step, a as DocVersion } from './versions-9mG1epfB.cjs';
1
+ import { P as Path, E as ElementNode } from './types-BkHlmUFL.cjs';
2
+ import { S as Step, a as DocVersion } from './versions-DNFf8zVz.cjs';
3
3
 
4
4
  /** 光标/选区在协议里的轻量形态(path + offset,无 assoc) */
5
5
  type WireSel = {
@@ -21,6 +21,9 @@ type PeerInfo = {
21
21
  type ClientJoin = {
22
22
  type: 'join';
23
23
  room: string;
24
+ clientId: string;
25
+ /** 本地副本已确认到的服务端版本;新副本不传,接收完整快照。 */
26
+ version?: number;
24
27
  name?: string;
25
28
  color?: string;
26
29
  /** 空房时用客户端 doc 初始化 */
@@ -30,6 +33,7 @@ type ClientJoin = {
30
33
  };
31
34
  type ClientOp = {
32
35
  type: 'op';
36
+ clientId: string;
33
37
  baseVersion: number;
34
38
  clientSeq: number;
35
39
  steps: Step[];
@@ -57,7 +61,11 @@ type ServerWelcome = {
57
61
  type: 'welcome';
58
62
  siteId: number;
59
63
  version: number;
64
+ ackedSeq: number;
65
+ seeded: boolean;
60
66
  doc: ElementNode;
67
+ /** 已有本地副本只取 version 之后的完整增量,避免快照覆盖离线写。 */
68
+ ops: ServerOp[];
61
69
  peers: PeerInfo[];
62
70
  /** 房间内命名版本快照列表 */
63
71
  versions?: DocVersion[];
@@ -66,7 +74,8 @@ type ServerOp = {
66
74
  type: 'op';
67
75
  version: number;
68
76
  siteId: number;
69
- clientSeq?: number;
77
+ clientId: string;
78
+ clientSeq: number;
70
79
  steps: Step[];
71
80
  };
72
81
  type ServerPresence = {
@@ -91,8 +100,10 @@ type ServerResync = {
91
100
  version: number;
92
101
  doc: ElementNode;
93
102
  };
103
+ type ServerErrorCode = 'invalid-message' | 'unauthorized' | 'invalid-version' | 'not-joined' | 'identity-mismatch' | 'operation-rejected';
94
104
  type ServerError = {
95
105
  type: 'error';
106
+ code: ServerErrorCode;
96
107
  message: string;
97
108
  };
98
109
  type ServerPong = {
@@ -105,7 +116,24 @@ type ServerVersions = {
105
116
  };
106
117
  type ServerMsg = ServerWelcome | ServerOp | ServerPresence | ServerPeerJoin | ServerPeerLeave | ServerResync | ServerError | ServerPong | ServerVersions;
107
118
 
108
- type CollabStatus = 'connecting' | 'open' | 'closed' | 'resyncing' | 'auth-error';
119
+ type CollabStatus = 'connecting' | 'open' | 'closed' | 'resyncing' | 'conflict' | 'auth-error';
120
+ type CollabConflict = {
121
+ reason: 'remote-bootstrap-required' | 'local-diverged' | 'resync-with-pending';
122
+ remote: {
123
+ doc: ElementNode;
124
+ version: number;
125
+ };
126
+ };
127
+ type CollabReplica = {
128
+ clientId: string;
129
+ version: number;
130
+ clientSeq: number;
131
+ synced: boolean;
132
+ pending: {
133
+ clientSeq: number;
134
+ steps: Step[];
135
+ }[];
136
+ };
109
137
  type CollabConfig = {
110
138
  /** ws(s)://host 或 http(s)://host(自动改 scheme) */
111
139
  url: string;
@@ -114,6 +142,11 @@ type CollabConfig = {
114
142
  color?: string;
115
143
  /** 空房初始化文档;默认由 Editor 传入当前 doc */
116
144
  seed?: ElementNode;
145
+ /** 与本地文档一同持久化;恢复后可安全重放未确认操作。 */
146
+ replica?: CollabReplica;
147
+ onReplica?: (replica: CollabReplica) => void;
148
+ /** 显式把已有远端房间导入一个全新本地副本;默认永不以远端覆盖本地。 */
149
+ bootstrap?: 'remote';
117
150
  /**
118
151
  * 鉴权钩:静态 token 或每次 join 取一次(可 async)。
119
152
  * worker 设 COLLAB_TOKEN 时服务端校验;未设则忽略。
@@ -124,14 +157,15 @@ type CollabConfig = {
124
157
  * 不传则插在 editor root 内 sticky 顶(lab 默认)。
125
158
  */
126
159
  railParent?: () => HTMLElement | null;
127
- /** 连接状态(connecting/open/closed/resyncing/auth-error) */
160
+ /** 连接状态(connecting/open/closed/resyncing/conflict/auth-error) */
128
161
  onStatus?: (s: CollabStatus) => void;
129
162
  /** 服务端强制 resync(版本落后 / rebase 失败) */
130
163
  onResync?: (version: number) => void;
164
+ onConflict?: (conflict: CollabConflict) => void;
131
165
  onError?: (message: string) => void;
132
166
  };
133
167
  type CollabHandlers = {
134
- /** 权威快照(welcome / resync) */
168
+ /** 新副本快照 / 无 pending 的安全 resync */
135
169
  onDoc: (doc: ElementNode, version: number) => void;
136
170
  /** 已变换的远端 steps + 作者 siteId,应 applyRemote(不入本端 undo 栈) */
137
171
  onRemote: (steps: Step[], siteId: number) => void;
@@ -146,6 +180,7 @@ type CollabHandlers = {
146
180
  onStatus?: (s: CollabStatus) => void;
147
181
  /** 即将应用权威快照(resync 路径) */
148
182
  onResync?: (version: number) => void;
183
+ onConflict?: (conflict: CollabConflict) => void;
149
184
  /** 房间命名版本列表(welcome / 远端 save·remove) */
150
185
  onVersions?: (versions: DocVersion[]) => void;
151
186
  onError?: (message: string) => void;
@@ -164,7 +199,14 @@ declare class CollabClient {
164
199
  private pingTimer;
165
200
  private lastPong;
166
201
  private authFailed;
202
+ private currentStatus;
203
+ private notifiedStatus;
204
+ private synced;
205
+ private conflictValue;
206
+ private conflictAckedSeq;
207
+ private conflictWelcome;
167
208
  siteId: number;
209
+ readonly clientId: string;
168
210
  version: number;
169
211
  private clientSeq;
170
212
  private pending;
@@ -173,6 +215,11 @@ declare class CollabClient {
173
215
  constructor(cfg: CollabConfig, handlers: CollabHandlers);
174
216
  get peersList(): PeerInfo[];
175
217
  get status(): CollabStatus;
218
+ get conflict(): CollabConflict | null;
219
+ private setStatus;
220
+ get replica(): CollabReplica;
221
+ private publishReplica;
222
+ private enterConflict;
176
223
  private connect;
177
224
  private sendJoin;
178
225
  private startHeartbeat;
@@ -182,9 +229,12 @@ declare class CollabClient {
182
229
  private onServer;
183
230
  /** 本地已 apply 的 steps 推上服务器 */
184
231
  push(steps: Step[]): void;
232
+ private flushPending;
233
+ private applyServerOp;
185
234
  /** 命名版本上行(侧信道,不进 OT) */
186
235
  pushVersion(v: DocVersion): void;
187
236
  removeVersion(id: string): void;
237
+ acceptRemote(): boolean;
188
238
  /** 用一批 steps 搬所有 peer 的 wire 选区(作者打字时彩光标贴字) */
189
239
  private mapPeerSelsThrough;
190
240
  /**
@@ -197,4 +247,4 @@ declare class CollabClient {
197
247
  }
198
248
  declare function connectCollab(cfg: CollabConfig, handlers: CollabHandlers): CollabClient;
199
249
 
200
- export { type ClientJoin as C, type PeerInfo as P, type ServerMsg as S, type WireSel as W, type ClientMsg as a, type ClientOp as b, CollabClient as c, type CollabConfig as d, type CollabHandlers as e, type CollabStatus as f, type ServerOp as g, type ServerWelcome as h, connectCollab as i };
250
+ export { type ClientJoin as C, type PeerInfo as P, type ServerError as S, type WireSel as W, type ClientMsg as a, type ClientOp as b, CollabClient as c, type CollabConfig as d, type CollabConflict as e, type CollabHandlers as f, type CollabReplica as g, type CollabStatus as h, type ServerErrorCode as i, type ServerMsg as j, type ServerOp as k, type ServerWelcome as l, connectCollab as m };
@@ -9,15 +9,16 @@
9
9
 
10
10
 
11
11
 
12
- var _chunkEADZKEU4cjs = require('../chunk-EADZKEU4.cjs');
12
+ var _chunk75EJ4ARBcjs = require('../chunk-75EJ4ARB.cjs');
13
13
 
14
14
 
15
15
 
16
16
 
17
17
 
18
18
 
19
- var _chunkU3JZDFL4cjs = require('../chunk-U3JZDFL4.cjs');
20
- require('../chunk-ZKQSQSSM.cjs');
19
+ var _chunkBMPCVT3Bcjs = require('../chunk-BMPCVT3B.cjs');
20
+ require('../chunk-HXABDVLZ.cjs');
21
+ require('../chunk-PCSVBPOR.cjs');
21
22
 
22
23
 
23
24
 
@@ -34,4 +35,4 @@ require('../chunk-ZKQSQSSM.cjs');
34
35
 
35
36
 
36
37
 
37
- exports.COLLAB_CARET_COLORS = _chunkEADZKEU4cjs.COLLAB_CARET_COLORS; exports.COLLAB_COLORS = _chunkEADZKEU4cjs.COLLAB_COLORS; exports.CollabClient = _chunkEADZKEU4cjs.CollabClient; exports.collabCaretColor = _chunkEADZKEU4cjs.collabCaretColor; exports.collabColor = _chunkEADZKEU4cjs.collabColor; exports.collabFg = _chunkEADZKEU4cjs.collabFg; exports.collabInitial = _chunkEADZKEU4cjs.collabInitial; exports.collabLuma = _chunkEADZKEU4cjs.collabLuma; exports.connectCollab = _chunkEADZKEU4cjs.connectCollab; exports.rebaseBatchOver = _chunkU3JZDFL4cjs.rebaseBatchOver; exports.rebaseOne = _chunkU3JZDFL4cjs.rebaseOne; exports.rebaseStepsThrough = _chunkU3JZDFL4cjs.rebaseStepsThrough; exports.solidify = _chunkEADZKEU4cjs.solidify; exports.transformRemoteAgainstPending = _chunkU3JZDFL4cjs.transformRemoteAgainstPending; exports.transformSeqThroughOne = _chunkU3JZDFL4cjs.transformSeqThroughOne;
38
+ exports.COLLAB_CARET_COLORS = _chunk75EJ4ARBcjs.COLLAB_CARET_COLORS; exports.COLLAB_COLORS = _chunk75EJ4ARBcjs.COLLAB_COLORS; exports.CollabClient = _chunk75EJ4ARBcjs.CollabClient; exports.collabCaretColor = _chunk75EJ4ARBcjs.collabCaretColor; exports.collabColor = _chunk75EJ4ARBcjs.collabColor; exports.collabFg = _chunk75EJ4ARBcjs.collabFg; exports.collabInitial = _chunk75EJ4ARBcjs.collabInitial; exports.collabLuma = _chunk75EJ4ARBcjs.collabLuma; exports.connectCollab = _chunk75EJ4ARBcjs.connectCollab; exports.rebaseBatchOver = _chunkBMPCVT3Bcjs.rebaseBatchOver; exports.rebaseOne = _chunkBMPCVT3Bcjs.rebaseOne; exports.rebaseStepsThrough = _chunkBMPCVT3Bcjs.rebaseStepsThrough; exports.solidify = _chunk75EJ4ARBcjs.solidify; exports.transformRemoteAgainstPending = _chunkBMPCVT3Bcjs.transformRemoteAgainstPending; exports.transformSeqThroughOne = _chunkBMPCVT3Bcjs.transformSeqThroughOne;