markloom 0.4.0 → 0.5.1

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 (51) hide show
  1. package/README.md +29 -16
  2. package/ai.md +160 -0
  3. package/collab.md +70 -0
  4. package/dist/ai/index.cjs +4 -3
  5. package/dist/ai/index.d.cts +4 -2
  6. package/dist/ai/index.d.ts +4 -2
  7. package/dist/ai/index.js +3 -2
  8. package/dist/chunk-4JSJAHTB.js +255 -0
  9. package/dist/{chunk-VVFIQLVR.cjs → chunk-4TMHVNL6.cjs} +71 -79
  10. package/dist/chunk-ALZ3A5CS.cjs +255 -0
  11. package/dist/{chunk-QXJHFQLJ.js → chunk-FCH7LZFI.js} +23 -15
  12. package/dist/chunk-GQ2HHRGI.cjs +16 -0
  13. package/dist/chunk-JS527FDV.cjs +294 -0
  14. package/dist/chunk-LA5SBCGQ.js +16 -0
  15. package/dist/{chunk-ITVAR2UM.js → chunk-LEYAQNJA.js} +5 -3
  16. package/dist/{chunk-J3QWW6CN.js → chunk-MFYF2A6U.js} +5 -13
  17. package/dist/chunk-RLTHVQNQ.js +294 -0
  18. package/dist/{chunk-VSTOMEU2.cjs → chunk-VXM7Q6P5.cjs} +813 -478
  19. package/dist/{chunk-LNSKDJMF.cjs → chunk-YIFOGP2D.cjs} +46 -38
  20. package/dist/{chunk-HA6XHJUD.cjs → chunk-YXQDCW4Y.cjs} +22 -20
  21. package/dist/{chunk-VMUJM52A.js → chunk-ZMTLSFLO.js} +350 -15
  22. package/dist/client-6KiH4kGK.d.ts +144 -0
  23. package/dist/client-ZRZj2nkq.d.cts +144 -0
  24. package/dist/collab/index.cjs +25 -0
  25. package/dist/collab/index.d.cts +11 -0
  26. package/dist/collab/index.d.ts +11 -0
  27. package/dist/collab/index.js +25 -0
  28. package/dist/comments-DlExS6f-.d.cts +43 -0
  29. package/dist/comments-PhnR7Gkn.d.ts +43 -0
  30. package/dist/{controller-B_wG2Wh-.d.ts → controller-BNuuUM3E.d.ts} +26 -2
  31. package/dist/{controller-Cd1Dprrk.d.cts → controller-DQITsFhy.d.cts} +26 -2
  32. package/dist/index.cjs +21 -243
  33. package/dist/index.d.cts +5 -7
  34. package/dist/index.d.ts +5 -7
  35. package/dist/index.js +22 -244
  36. package/dist/react/index.cjs +8 -5
  37. package/dist/react/index.d.cts +4 -2
  38. package/dist/react/index.d.ts +4 -2
  39. package/dist/react/index.js +7 -4
  40. package/dist/rebase-batch-8rL_aXEE.d.ts +38 -0
  41. package/dist/rebase-batch-bYnBJpRO.d.cts +38 -0
  42. package/dist/step-BrvHIK7O.d.cts +65 -0
  43. package/dist/step-BrvHIK7O.d.ts +65 -0
  44. package/dist/view/index.cjs +11 -8
  45. package/dist/view/index.d.cts +5 -3
  46. package/dist/view/index.d.ts +5 -3
  47. package/dist/view/index.js +7 -4
  48. package/dist/view/theme.css +72 -0
  49. package/package.json +10 -3
  50. package/dist/comments-USjT1wYN.d.cts +0 -105
  51. package/dist/comments-USjT1wYN.d.ts +0 -105
@@ -8,7 +8,7 @@ import {
8
8
  redo,
9
9
  stripCommentMarks,
10
10
  undo
11
- } from "./chunk-ITVAR2UM.js";
11
+ } from "./chunk-LEYAQNJA.js";
12
12
  import {
13
13
  MarkloomAi,
14
14
  createAiStream,
@@ -18,7 +18,7 @@ import {
18
18
  rootEl,
19
19
  startAiWrite,
20
20
  writeSelection
21
- } from "./chunk-QXJHFQLJ.js";
21
+ } from "./chunk-FCH7LZFI.js";
22
22
  import {
23
23
  applyAll,
24
24
  arrowCell,
@@ -35,10 +35,7 @@ import {
35
35
  htmlToBlocks,
36
36
  insertCol,
37
37
  insertRow,
38
- isElement,
39
- isInlineAtom,
40
38
  isRichHtmlBlocks,
41
- isText,
42
39
  lastLeafEnd,
43
40
  makeTable,
44
41
  mdToDoc,
@@ -56,7 +53,17 @@ import {
56
53
  setFreeze,
57
54
  splitCell,
58
55
  tableColWidths
59
- } from "./chunk-J3QWW6CN.js";
56
+ } from "./chunk-MFYF2A6U.js";
57
+ import {
58
+ collabColor,
59
+ collabInitial,
60
+ connectCollab
61
+ } from "./chunk-4JSJAHTB.js";
62
+ import {
63
+ isElement,
64
+ isInlineAtom,
65
+ isText
66
+ } from "./chunk-LA5SBCGQ.js";
60
67
 
61
68
  // src/view/render.ts
62
69
  import { h as h2 } from "preact";
@@ -1223,11 +1230,32 @@ function mapInput(doc, intent, data, sel, newId4, rules = [], pendingMarks) {
1223
1230
  return { steps: [{ kind: "deleteText", path: leafPath, offset: from - 1, length: 1 }], selection: caret(leafPath.concat(from - 1)) };
1224
1231
  {
1225
1232
  const aiParent = parentPath.length ? nodeAt(doc, parentPath) : null;
1226
- if (aiParent && aiParent.type === "ai" && isEmptyBlock(block) && leafIdx === 0) {
1227
- const node = { type: "paragraph", id: aiParent.id, children: [{ text: "" }] };
1233
+ if (aiParent && aiParent.type === "ai" && aiParent.id && isEmptyBlock(block) && leafIdx === 0) {
1234
+ const aiId = aiParent.id;
1235
+ const steps = [];
1236
+ const draftAt = [];
1237
+ for (let i = 0; i < doc.children.length; i++) {
1238
+ const c = doc.children[i];
1239
+ if (!isText(c) && c.attrs?.aiFrom === aiId)
1240
+ draftAt.push(i);
1241
+ }
1242
+ for (let i = draftAt.length - 1; i >= 0; i--)
1243
+ steps.push({ kind: "spliceChildren", path: [], at: draftAt[i], remove: 1, insert: [] });
1244
+ let aiIdx = parentPath[0] ?? 0;
1245
+ let shift3 = 0;
1246
+ for (const i of draftAt) {
1247
+ if (i < aiIdx)
1248
+ shift3++;
1249
+ }
1250
+ aiIdx -= shift3;
1251
+ steps.push({
1252
+ kind: "replaceNode",
1253
+ path: [aiIdx],
1254
+ node: { type: "paragraph", id: aiId, children: [{ text: "" }] }
1255
+ });
1228
1256
  return {
1229
- steps: [{ kind: "replaceNode", path: parentPath, node }],
1230
- selection: caret([...parentPath, 0, 0])
1257
+ steps,
1258
+ selection: caret([Math.max(0, aiIdx), 0, 0])
1231
1259
  };
1232
1260
  }
1233
1261
  }
@@ -6317,6 +6345,201 @@ function isToday(y, m, day) {
6317
6345
  // src/view/editor.ts
6318
6346
  import { render as render11 } from "preact";
6319
6347
 
6348
+ // src/view/collab-presence.ts
6349
+ function wireOf(sel) {
6350
+ const split2 = (p) => {
6351
+ const path = p.path;
6352
+ if (path.length < 1)
6353
+ return { path: [], offset: 0 };
6354
+ return { path: path.slice(0, -1), offset: path[path.length - 1] };
6355
+ };
6356
+ return { anchor: split2(sel.anchor), head: split2(sel.head) };
6357
+ }
6358
+ function posOf(w) {
6359
+ return { path: w.path.concat(w.offset) };
6360
+ }
6361
+ var CollabPresence = class {
6362
+ host;
6363
+ rail;
6364
+ avatars;
6365
+ carets;
6366
+ peers = [];
6367
+ self = null;
6368
+ presenceTimer = null;
6369
+ lastWire = "";
6370
+ onSel = () => this.schedulePresence();
6371
+ onScroll = () => this.paintCarets();
6372
+ onResize = () => this.paintCarets();
6373
+ destroyed = false;
6374
+ constructor(host) {
6375
+ this.host = host;
6376
+ const root = host.root;
6377
+ if (getComputedStyle(root).position === "static")
6378
+ root.style.position = "relative";
6379
+ this.rail = document.createElement("div");
6380
+ this.rail.className = "markloom-collab-rail";
6381
+ this.rail.setAttribute("contenteditable", "false");
6382
+ this.avatars = document.createElement("div");
6383
+ this.avatars.className = "markloom-collab-avatars";
6384
+ this.rail.appendChild(this.avatars);
6385
+ this.carets = document.createElement("div");
6386
+ this.carets.className = "markloom-collab-carets";
6387
+ this.carets.setAttribute("contenteditable", "false");
6388
+ root.appendChild(this.rail);
6389
+ root.appendChild(this.carets);
6390
+ document.addEventListener("selectionchange", this.onSel);
6391
+ window.addEventListener("scroll", this.onScroll, true);
6392
+ window.addEventListener("resize", this.onResize);
6393
+ }
6394
+ /** welcome 后设本人;peers 更新时刷新 */
6395
+ setSelf(siteId, name, color) {
6396
+ this.self = { siteId, name, color: collabColor(siteId, color) };
6397
+ this.paintAvatars();
6398
+ }
6399
+ setPeers(peers) {
6400
+ this.peers = peers.map((p) => ({
6401
+ ...p,
6402
+ color: collabColor(p.siteId, p.color)
6403
+ }));
6404
+ this.paintAvatars();
6405
+ this.paintCarets();
6406
+ }
6407
+ /** model 重绘后光标 DOM 位可能变 */
6408
+ onRedraw() {
6409
+ this.paintCarets();
6410
+ }
6411
+ destroy() {
6412
+ this.destroyed = true;
6413
+ document.removeEventListener("selectionchange", this.onSel);
6414
+ window.removeEventListener("scroll", this.onScroll, true);
6415
+ window.removeEventListener("resize", this.onResize);
6416
+ if (this.presenceTimer)
6417
+ clearTimeout(this.presenceTimer);
6418
+ this.rail.remove();
6419
+ this.carets.remove();
6420
+ }
6421
+ schedulePresence() {
6422
+ if (this.presenceTimer)
6423
+ clearTimeout(this.presenceTimer);
6424
+ this.presenceTimer = setTimeout(() => {
6425
+ this.presenceTimer = null;
6426
+ this.sendPresence();
6427
+ }, 80);
6428
+ }
6429
+ sendPresence() {
6430
+ const c = this.host.collab();
6431
+ if (!c || this.destroyed)
6432
+ return;
6433
+ const sel = readSelection(this.host.root, this.host.doc());
6434
+ if (!sel) {
6435
+ c.presence(null);
6436
+ return;
6437
+ }
6438
+ const w = wireOf(sel);
6439
+ const key = JSON.stringify(w);
6440
+ if (key === this.lastWire)
6441
+ return;
6442
+ this.lastWire = key;
6443
+ c.presence(w);
6444
+ }
6445
+ paintAvatars() {
6446
+ const list = [];
6447
+ if (this.self) {
6448
+ list.push({
6449
+ siteId: this.self.siteId,
6450
+ name: this.self.name,
6451
+ color: this.self.color
6452
+ });
6453
+ }
6454
+ for (const p of this.peers) {
6455
+ if (this.self && p.siteId === this.self.siteId)
6456
+ continue;
6457
+ list.push(p);
6458
+ }
6459
+ list.sort((a, b) => {
6460
+ if (this.self) {
6461
+ if (a.siteId === this.self.siteId)
6462
+ return 1;
6463
+ if (b.siteId === this.self.siteId)
6464
+ return -1;
6465
+ }
6466
+ return a.siteId - b.siteId;
6467
+ });
6468
+ this.avatars.replaceChildren();
6469
+ if (list.length === 0) {
6470
+ this.rail.hidden = true;
6471
+ return;
6472
+ }
6473
+ this.rail.hidden = false;
6474
+ for (const p of list) {
6475
+ const el = document.createElement("div");
6476
+ el.className = "markloom-collab-avatar";
6477
+ if (this.self && p.siteId === this.self.siteId)
6478
+ el.classList.add("is-self");
6479
+ el.style.setProperty("--collab-c", p.color || collabColor(p.siteId));
6480
+ el.textContent = collabInitial(p.name, p.siteId);
6481
+ el.title = p.name || `\u7528\u6237 ${p.siteId}`;
6482
+ this.avatars.appendChild(el);
6483
+ }
6484
+ }
6485
+ paintCarets() {
6486
+ this.carets.replaceChildren();
6487
+ const root = this.host.root;
6488
+ const doc = this.host.doc();
6489
+ const rootRect = root.getBoundingClientRect();
6490
+ const scrollX = root.scrollLeft;
6491
+ const scrollY = root.scrollTop;
6492
+ for (const p of this.peers) {
6493
+ if (this.self && p.siteId === this.self.siteId)
6494
+ continue;
6495
+ if (!p.sel?.head)
6496
+ continue;
6497
+ const pos = posOf(p.sel.head);
6498
+ const dom = pointToDom(root, doc, pos);
6499
+ if (!dom)
6500
+ continue;
6501
+ const range = document.createRange();
6502
+ try {
6503
+ const n = dom.node;
6504
+ const max2 = n.nodeType === 3 ? n.textContent?.length ?? 0 : 0;
6505
+ const off = Math.max(0, Math.min(dom.offset, max2));
6506
+ range.setStart(n, off);
6507
+ range.setEnd(n, off);
6508
+ } catch {
6509
+ continue;
6510
+ }
6511
+ const rects = range.getClientRects();
6512
+ const rect = rects[0] ?? range.getBoundingClientRect();
6513
+ if (!rect || rect.width === 0 && rect.height === 0 && rect.top === 0) {
6514
+ const leaf = dom.node.parentElement;
6515
+ if (!leaf)
6516
+ continue;
6517
+ const r = leaf.getBoundingClientRect();
6518
+ this.addCaret(p, r, rootRect, scrollX, scrollY);
6519
+ continue;
6520
+ }
6521
+ this.addCaret(p, rect, rootRect, scrollX, scrollY);
6522
+ }
6523
+ }
6524
+ addCaret(p, rect, rootRect, scrollX, scrollY) {
6525
+ const color = p.color || collabColor(p.siteId);
6526
+ const el = document.createElement("div");
6527
+ el.className = "markloom-collab-caret";
6528
+ el.style.setProperty("--collab-c", color);
6529
+ const left = rect.left - rootRect.left + scrollX;
6530
+ const top = rect.top - rootRect.top + scrollY;
6531
+ const h13 = Math.max(rect.height || 16, 14);
6532
+ el.style.left = `${left}px`;
6533
+ el.style.top = `${top}px`;
6534
+ el.style.height = `${h13}px`;
6535
+ const lab = document.createElement("div");
6536
+ lab.className = "markloom-collab-caret-lab";
6537
+ lab.textContent = p.name || `\u7528\u6237${p.siteId}`;
6538
+ el.appendChild(lab);
6539
+ this.carets.appendChild(el);
6540
+ }
6541
+ };
6542
+
6320
6543
  // src/view/image-controls.ts
6321
6544
  import { h as h11, render as render9 } from "preact";
6322
6545
  var DRAG_THRESHOLD = 4;
@@ -7397,6 +7620,10 @@ var Editor = class {
7397
7620
  decorations;
7398
7621
  /** AI 一等公民(opts.ai):斜杠 / 格式栏 / 卡片全内置 */
7399
7622
  aiCtl = null;
7623
+ /** 协作会话;applyingRemote 期间本地 steps 不上行 */
7624
+ collabCtl = null;
7625
+ collabUi = null;
7626
+ applyingRemote = false;
7400
7627
  constructor(root, doc, opts = {}) {
7401
7628
  this.root = root;
7402
7629
  const dev = typeof process !== "undefined" && process.env?.NODE_ENV !== "production";
@@ -7461,6 +7688,26 @@ var Editor = class {
7461
7688
  onBusy: cfg.onBusy
7462
7689
  });
7463
7690
  }
7691
+ if (opts.collab) {
7692
+ this.collabUi = new CollabPresence({
7693
+ root,
7694
+ doc: () => this.history.doc,
7695
+ collab: () => this.collabCtl
7696
+ });
7697
+ this.collabCtl = connectCollab(
7698
+ { ...opts.collab, seed: opts.collab.seed ?? doc },
7699
+ {
7700
+ onDoc: (d) => this.replaceDoc(d),
7701
+ onRemote: (steps) => this.applyRemote(steps),
7702
+ onWelcome: (info) => this.collabUi?.setSelf(info.siteId, info.name, info.color),
7703
+ onPeers: (peers) => this.collabUi?.setPeers(peers),
7704
+ onError: (msg) => {
7705
+ if (typeof console !== "undefined")
7706
+ console.warn("[markloom collab]", msg);
7707
+ }
7708
+ }
7709
+ );
7710
+ }
7464
7711
  const host = {
7465
7712
  root,
7466
7713
  doc: () => this.history.doc,
@@ -7526,6 +7773,10 @@ var Editor = class {
7526
7773
  this.commentPanel?.destroy();
7527
7774
  this.tocPanel?.destroy();
7528
7775
  this.aiCtl?.destroy();
7776
+ this.collabUi?.destroy();
7777
+ this.collabUi = null;
7778
+ this.collabCtl?.close();
7779
+ this.collabCtl = null;
7529
7780
  this.aiCtl = null;
7530
7781
  for (const d of this.decorations)
7531
7782
  d.destroy();
@@ -7535,6 +7786,10 @@ var Editor = class {
7535
7786
  get ai() {
7536
7787
  return this.aiCtl;
7537
7788
  }
7789
+ /** 协作客户端(opts.collab 启用时) */
7790
+ get collab() {
7791
+ return this.collabCtl;
7792
+ }
7538
7793
  openAi(mode) {
7539
7794
  this.aiCtl?.openAtFocus(mode);
7540
7795
  }
@@ -7588,6 +7843,7 @@ var Editor = class {
7588
7843
  writeSelection(this.root, this.history.doc, restore);
7589
7844
  this.root.dispatchEvent(new Event("markloom-redraw", { bubbles: true }));
7590
7845
  this.syncPlaceholder();
7846
+ this.collabUi?.onRedraw();
7591
7847
  }
7592
7848
  // 空块提示(不进 model):空 paragraph → opts.placeholder(slash 引导);
7593
7849
  // 空 heading → 类型名 H1…H6(飞书/Notion 式)。空文档常显;光标落空块时也显。
@@ -7650,6 +7906,8 @@ var Editor = class {
7650
7906
  this.history = commit(this.history, batch);
7651
7907
  this.draw(selectionAfter);
7652
7908
  this.opts.onChange?.(this.history.doc);
7909
+ if (!this.applyingRemote)
7910
+ this.collabCtl?.push(batch);
7653
7911
  }
7654
7912
  // AI 流式 / 事务内续写:并入 past 最后一条,一次 undo 回退整笔事务。
7655
7913
  dispatchStream(steps, selectionAfter) {
@@ -7660,24 +7918,54 @@ var Editor = class {
7660
7918
  this.history = amend(this.history, batch);
7661
7919
  this.draw(selectionAfter);
7662
7920
  this.opts.onChange?.(this.history.doc);
7921
+ if (!this.applyingRemote)
7922
+ this.collabCtl?.push(batch);
7923
+ }
7924
+ /** 远端协作 op:写入 model 但不上行 */
7925
+ applyRemote(steps, selectionAfter) {
7926
+ if (steps.length === 0)
7927
+ return;
7928
+ this.applyingRemote = true;
7929
+ try {
7930
+ const after = applyAll(this.history.doc, steps);
7931
+ const batch = steps.concat(gapSteps(after));
7932
+ this.history = commit(this.history, batch);
7933
+ this.draw(selectionAfter);
7934
+ this.opts.onChange?.(this.history.doc);
7935
+ } finally {
7936
+ this.applyingRemote = false;
7937
+ }
7938
+ }
7939
+ /** 权威快照替换(welcome/resync);清空本地 undo 栈 */
7940
+ replaceDoc(doc) {
7941
+ const dev = typeof process !== "undefined" && process.env?.NODE_ENV !== "production";
7942
+ this.history = init(ensureGaps(doc), dev ? (d) => firstViolation(invariants(basicSchema, d)) : void 0);
7943
+ this.draw();
7944
+ this.opts.onChange?.(this.history.doc);
7663
7945
  }
7664
7946
  /** 回退一笔 history(AI 事务 discard = undo 整笔) */
7665
7947
  undo() {
7948
+ const last = this.history.past[this.history.past.length - 1];
7666
7949
  const next = undo(this.history);
7667
7950
  if (next === this.history)
7668
7951
  return false;
7669
7952
  this.history = next;
7670
7953
  this.draw();
7671
7954
  this.opts.onChange?.(this.history.doc);
7955
+ if (!this.applyingRemote && last)
7956
+ this.collabCtl?.push(last.inverse);
7672
7957
  return true;
7673
7958
  }
7674
7959
  redo() {
7960
+ const fut = this.history.future[0];
7675
7961
  const next = redo(this.history);
7676
7962
  if (next === this.history)
7677
7963
  return false;
7678
7964
  this.history = next;
7679
7965
  this.draw();
7680
7966
  this.opts.onChange?.(this.history.doc);
7967
+ if (!this.applyingRemote && fut)
7968
+ this.collabCtl?.push(fut.steps);
7681
7969
  return true;
7682
7970
  }
7683
7971
  /** 续写:当前块后插空段并流式灌入 */
@@ -7893,15 +8181,23 @@ var Editor = class {
7893
8181
  return top;
7894
8182
  }
7895
8183
  // AI 输入守卫:
7896
- // · 顶层 draft 段(attrs.aiDraft)只读
7897
- // · ai 块内:Enter 生成/确认;loading 锁输入
8184
+ // · 顶层 draft(attrs.aiDraft)且对应 ai 壳仍在 → 只读(确认前淡化)
8185
+ // · 孤儿 draft(壳已丢)→ 当场解锁可编辑,避免「虚区域无法输入」
8186
+ // · ai 块内:Enter 生成/确认;loading 锁输入;空指令块首退格 → discard
7898
8187
  aiBlockGuard(intent, sel, e) {
7899
8188
  if (!this.aiCtl)
7900
8189
  return false;
7901
8190
  const top = this.history.doc.children[sel.head.path[0]];
7902
- if (top && !isText(top) && top.type === "paragraph" && top.attrs?.aiDraft) {
7903
- e.preventDefault();
7904
- return true;
8191
+ if (top && !isText(top) && top.attrs?.aiDraft) {
8192
+ const from = typeof top.attrs.aiFrom === "string" ? top.attrs.aiFrom : "";
8193
+ const live = !!from && this.history.doc.children.some(
8194
+ (c) => !isText(c) && c.type === "ai" && c.id === from
8195
+ );
8196
+ if (live) {
8197
+ e.preventDefault();
8198
+ return true;
8199
+ }
8200
+ this.promoteOrphanAiDrafts();
7905
8201
  }
7906
8202
  const ai = this.aiBlockAt(sel);
7907
8203
  if (!ai || !ai.id)
@@ -7911,6 +8207,16 @@ var Editor = class {
7911
8207
  e.preventDefault();
7912
8208
  return true;
7913
8209
  }
8210
+ if (intent === "deleteBackward") {
8211
+ const prompt = ai.children[0];
8212
+ const empty = !prompt || isText(prompt) || !isText(prompt) && prompt.type === "paragraph" && prompt.children.every((c) => isText(c) && c.text === "");
8213
+ const atPromptStart = sel.head.path.length >= 4 && sel.head.path[1] === 0 && sel.head.path[sel.head.path.length - 1] === 0;
8214
+ if (empty && atPromptStart) {
8215
+ e.preventDefault();
8216
+ this.aiCtl.discard(ai.id);
8217
+ return true;
8218
+ }
8219
+ }
7914
8220
  if (intent === "insertParagraph") {
7915
8221
  e.preventDefault();
7916
8222
  if (status === "preview")
@@ -7921,6 +8227,35 @@ var Editor = class {
7921
8227
  }
7922
8228
  return false;
7923
8229
  }
8230
+ /** 壳已不在的 aiDraft 顶层块 → 剥事务标记,恢复可编辑 */
8231
+ promoteOrphanAiDrafts() {
8232
+ const doc = this.history.doc;
8233
+ const liveAi = /* @__PURE__ */ new Set();
8234
+ for (const c of doc.children) {
8235
+ if (!isText(c) && c.type === "ai" && c.id)
8236
+ liveAi.add(c.id);
8237
+ }
8238
+ const steps = [];
8239
+ for (let i = 0; i < doc.children.length; i++) {
8240
+ const c = doc.children[i];
8241
+ if (isText(c) || !c.attrs?.aiDraft)
8242
+ continue;
8243
+ const from = typeof c.attrs.aiFrom === "string" ? c.attrs.aiFrom : "";
8244
+ if (from && liveAi.has(from))
8245
+ continue;
8246
+ const attrs = { ...c.attrs };
8247
+ delete attrs.aiDraft;
8248
+ delete attrs.aiFrom;
8249
+ delete attrs.aiStream;
8250
+ const clean = Object.keys(attrs).length ? attrs : void 0;
8251
+ const next = { type: c.type, id: c.id, children: c.children };
8252
+ if (clean)
8253
+ next.attrs = clean;
8254
+ steps.push({ kind: "replaceNode", path: [i], node: next });
8255
+ }
8256
+ if (steps.length)
8257
+ this.dispatch(steps);
8258
+ }
7924
8259
  // 塌缩 caret 落在**非空** code 块的边界时,给出它在首/尾。否则返 null(不在 code / 非塌缩 / 空块 → 放行给 mapInput)。
7925
8260
  // 「code 内首尾删会与相邻块并块、破坏『单文本叶』不变量」是 codeBlockGuard 与 codeBoundaryDelete 共守的同一判定,
7926
8261
  // 抽成单一真相:collapsed/off/len/边界只算一处,两处消费,口径不漂。空块(len===0)无内容可护,放行删空块。
@@ -0,0 +1,144 @@
1
+ import { P as Path, E as ElementNode, S as Step } from './step-BrvHIK7O.js';
2
+
3
+ /** 光标/选区在协议里的轻量形态(path + offset,无 assoc) */
4
+ type WireSel = {
5
+ anchor: {
6
+ path: Path;
7
+ offset: number;
8
+ };
9
+ head: {
10
+ path: Path;
11
+ offset: number;
12
+ };
13
+ };
14
+ type PeerInfo = {
15
+ siteId: number;
16
+ name?: string;
17
+ color?: string;
18
+ sel?: WireSel | null;
19
+ };
20
+ type ClientJoin = {
21
+ type: 'join';
22
+ room: string;
23
+ name?: string;
24
+ color?: string;
25
+ /** 空房时用客户端 doc 初始化 */
26
+ seed?: ElementNode;
27
+ };
28
+ type ClientOp = {
29
+ type: 'op';
30
+ baseVersion: number;
31
+ clientSeq: number;
32
+ steps: Step[];
33
+ };
34
+ type ClientPresence = {
35
+ type: 'presence';
36
+ name?: string;
37
+ color?: string;
38
+ sel?: WireSel | null;
39
+ };
40
+ type ClientPing = {
41
+ type: 'ping';
42
+ };
43
+ type ClientMsg = ClientJoin | ClientOp | ClientPresence | ClientPing;
44
+ type ServerWelcome = {
45
+ type: 'welcome';
46
+ siteId: number;
47
+ version: number;
48
+ doc: ElementNode;
49
+ peers: PeerInfo[];
50
+ };
51
+ type ServerOp = {
52
+ type: 'op';
53
+ version: number;
54
+ siteId: number;
55
+ clientSeq?: number;
56
+ steps: Step[];
57
+ };
58
+ type ServerPresence = {
59
+ type: 'presence';
60
+ siteId: number;
61
+ name?: string;
62
+ color?: string;
63
+ sel?: WireSel | null;
64
+ };
65
+ type ServerPeerJoin = {
66
+ type: 'peer-join';
67
+ siteId: number;
68
+ name?: string;
69
+ color?: string;
70
+ };
71
+ type ServerPeerLeave = {
72
+ type: 'peer-leave';
73
+ siteId: number;
74
+ };
75
+ type ServerResync = {
76
+ type: 'resync';
77
+ version: number;
78
+ doc: ElementNode;
79
+ };
80
+ type ServerError = {
81
+ type: 'error';
82
+ message: string;
83
+ };
84
+ type ServerPong = {
85
+ type: 'pong';
86
+ };
87
+ type ServerMsg = ServerWelcome | ServerOp | ServerPresence | ServerPeerJoin | ServerPeerLeave | ServerResync | ServerError | ServerPong;
88
+
89
+ type CollabConfig = {
90
+ /** ws(s)://host 或 http(s)://host(自动改 scheme) */
91
+ url: string;
92
+ room: string;
93
+ name?: string;
94
+ color?: string;
95
+ /** 空房初始化文档;默认由 Editor 传入当前 doc */
96
+ seed?: ElementNode;
97
+ };
98
+ type CollabHandlers = {
99
+ /** 权威快照(welcome / resync) */
100
+ onDoc: (doc: ElementNode, version: number) => void;
101
+ /** 已变换的远端 steps,应 applyRemote */
102
+ onRemote: (steps: Step[]) => void;
103
+ /** 进房成功(分配 siteId,可画本人头像) */
104
+ onWelcome?: (info: {
105
+ siteId: number;
106
+ version: number;
107
+ name?: string;
108
+ color?: string;
109
+ }) => void;
110
+ onPeers?: (peers: PeerInfo[]) => void;
111
+ onStatus?: (s: 'connecting' | 'open' | 'closed') => void;
112
+ onError?: (message: string) => void;
113
+ };
114
+ /**
115
+ * 协作会话:WS + pending OT。
116
+ * Editor 调 push(localSteps);远端经 onRemote / onDoc。
117
+ */
118
+ declare class CollabClient {
119
+ private cfg;
120
+ private handlers;
121
+ private ws;
122
+ private closed;
123
+ private retry;
124
+ private reconnectTimer;
125
+ siteId: number;
126
+ version: number;
127
+ private clientSeq;
128
+ private pending;
129
+ private peers;
130
+ private joined;
131
+ constructor(cfg: CollabConfig, handlers: CollabHandlers);
132
+ get peersList(): PeerInfo[];
133
+ private connect;
134
+ private scheduleReconnect;
135
+ private send;
136
+ private onServer;
137
+ /** 本地已 apply 的 steps 推上服务器 */
138
+ push(steps: Step[]): void;
139
+ presence(sel: WireSel | null): void;
140
+ close(): void;
141
+ }
142
+ declare function connectCollab(cfg: CollabConfig, handlers: CollabHandlers): CollabClient;
143
+
144
+ 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 ServerOp as f, type ServerWelcome as g, connectCollab as h };