markloom 0.4.0 → 0.5.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 (50) hide show
  1. package/README.md +29 -16
  2. package/ai.md +160 -0
  3. package/collab.md +68 -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-VVFIQLVR.cjs → chunk-4TMHVNL6.cjs} +71 -79
  9. package/dist/chunk-6DCI7AL3.js +208 -0
  10. package/dist/chunk-AC5E42NH.cjs +208 -0
  11. package/dist/chunk-GQ2HHRGI.cjs +16 -0
  12. package/dist/{chunk-QXJHFQLJ.js → chunk-JMOJU5EU.js} +4 -2
  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-LNSKDJMF.cjs → chunk-SLRD6FMP.cjs} +27 -25
  19. package/dist/{chunk-VMUJM52A.js → chunk-WUHTCCAE.js} +65 -6
  20. package/dist/{chunk-VSTOMEU2.cjs → chunk-YJAWPRJW.cjs} +400 -341
  21. package/dist/{chunk-HA6XHJUD.cjs → chunk-YXQDCW4Y.cjs} +22 -20
  22. package/dist/client-A0ZQJnyR.d.ts +137 -0
  23. package/dist/client-BNams4wv.d.cts +137 -0
  24. package/dist/collab/index.cjs +19 -0
  25. package/dist/collab/index.d.cts +3 -0
  26. package/dist/collab/index.d.ts +3 -0
  27. package/dist/collab/index.js +19 -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-BkY1VPzc.d.ts} +17 -1
  31. package/dist/{controller-Cd1Dprrk.d.cts → controller-CVIZI--A.d.cts} +17 -1
  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/package.json +10 -3
  49. package/dist/comments-USjT1wYN.d.cts +0 -105
  50. package/dist/comments-USjT1wYN.d.ts +0 -105
@@ -5,8 +5,10 @@
5
5
 
6
6
 
7
7
 
8
+ var _chunk4TMHVNL6cjs = require('./chunk-4TMHVNL6.cjs');
8
9
 
9
- var _chunkVVFIQLVRcjs = require('./chunk-VVFIQLVR.cjs');
10
+
11
+ var _chunkGQ2HHRGIcjs = require('./chunk-GQ2HHRGI.cjs');
10
12
 
11
13
  // src/view/selection.ts
12
14
  function rootEl(root) {
@@ -68,7 +70,7 @@ function domToPoint(root, doc, container, domOffset) {
68
70
  }
69
71
  if (segs.length === 0)
70
72
  return null;
71
- return { path: _chunkVVFIQLVRcjs.normalizeToLeaf.call(void 0, doc, segs.concat(charOffset)) };
73
+ return { path: _chunk4TMHVNL6cjs.normalizeToLeaf.call(void 0, doc, segs.concat(charOffset)) };
72
74
  }
73
75
  function readSelection(root, doc) {
74
76
  const sel = root.ownerDocument.getSelection();
@@ -216,18 +218,18 @@ function focusBlockId(ed) {
216
218
  const sel = readSelection(ed.root, ed.doc);
217
219
  const idx = _nullishCoalesce(_optionalChain([sel, 'optionalAccess', _8 => _8.head, 'access', _9 => _9.path, 'access', _10 => _10[0]]), () => ( Math.max(0, ed.doc.children.length - 1)));
218
220
  const b = ed.doc.children[idx];
219
- if (!b || _chunkVVFIQLVRcjs.isText.call(void 0, b) || !b.id)
221
+ if (!b || _chunkGQ2HHRGIcjs.isText.call(void 0, b) || !b.id)
220
222
  return null;
221
223
  return b.id;
222
224
  }
223
225
  function blockPlainText(doc, blockId) {
224
- const b = doc.children.find((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && c.id === blockId);
226
+ const b = doc.children.find((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
225
227
  if (!b)
226
228
  return "";
227
229
  return collectText(b);
228
230
  }
229
231
  function collectText(n) {
230
- if (_chunkVVFIQLVRcjs.isText.call(void 0, n))
232
+ if (_chunkGQ2HHRGIcjs.isText.call(void 0, n))
231
233
  return n.text;
232
234
  return n.children.map(collectText).join("");
233
235
  }
@@ -247,11 +249,11 @@ function emptyLike(block) {
247
249
  return { type: "paragraph", id: block.id, attrs: block.attrs ? { ...block.attrs } : void 0, children: [{ text: "" }] };
248
250
  }
249
251
  function streamTargetId(doc, blockId) {
250
- const b = doc.children.find((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && c.id === blockId);
252
+ const b = doc.children.find((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
251
253
  if (!b)
252
254
  return blockId;
253
255
  if (b.type === "quote" || b.type === "callout") {
254
- const inner = b.children.find((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && c.type === "paragraph");
256
+ const inner = b.children.find((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.type === "paragraph");
255
257
  if (_optionalChain([inner, 'optionalAccess', _11 => _11.id]))
256
258
  return inner.id;
257
259
  }
@@ -273,16 +275,16 @@ function startAiWrite(ed, mode, blockId) {
273
275
  if (mode === "continue") {
274
276
  const id = newId();
275
277
  const empty = { type: "paragraph", id, children: [{ text: "" }] };
276
- ed.dispatch([_chunkVVFIQLVRcjs.insertAfter.call(void 0, doc, focus, [empty])]);
278
+ ed.dispatch([_chunk4TMHVNL6cjs.insertAfter.call(void 0, doc, focus, [empty])]);
277
279
  targetId = id;
278
280
  } else {
279
- const block = doc.children.find((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && c.id === focus);
281
+ const block = doc.children.find((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === focus);
280
282
  if (!block)
281
283
  throw new Error(`block not found: ${focus}`);
282
284
  if (block.type === "table" || block.type === "image" || block.type === "embed" || block.type === "raw")
283
285
  throw new Error(`cannot rewrite ${block.type}`);
284
286
  const cleared = emptyLike(block);
285
- ed.dispatch([_chunkVVFIQLVRcjs.rewriteNode.call(void 0, doc, focus, cleared)]);
287
+ ed.dispatch([_chunk4TMHVNL6cjs.rewriteNode.call(void 0, doc, focus, cleared)]);
286
288
  targetId = streamTargetId(ed.doc, focus);
287
289
  }
288
290
  opened = true;
@@ -291,7 +293,7 @@ function startAiWrite(ed, mode, blockId) {
291
293
  if (!alive || !text)
292
294
  return;
293
295
  setup();
294
- const step = _chunkVVFIQLVRcjs.appendText.call(void 0, ed.doc, targetId, text);
296
+ const step = _chunk4TMHVNL6cjs.appendText.call(void 0, ed.doc, targetId, text);
295
297
  ed.dispatchStream([step]);
296
298
  };
297
299
  const finish = () => {
@@ -316,7 +318,7 @@ function precedingPlainText(doc, blockIdx) {
316
318
  const parts = [];
317
319
  for (let i = 0; i < blockIdx && i < doc.children.length; i++) {
318
320
  const b = doc.children[i];
319
- if (_chunkVVFIQLVRcjs.isText.call(void 0, b) || !b.id)
321
+ if (_chunkGQ2HHRGIcjs.isText.call(void 0, b) || !b.id)
320
322
  continue;
321
323
  if (b.type === "image" || b.type === "table" || b.type === "embed" || b.type === "raw" || b.type === "ai")
322
324
  continue;
@@ -409,7 +411,7 @@ var MarkloomAi = (_class = class {
409
411
  return;
410
412
  }
411
413
  const doc = ed.doc;
412
- const idx = doc.children.findIndex((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && c.id === bid);
414
+ const idx = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === bid);
413
415
  if (idx < 0)
414
416
  return;
415
417
  const cur = doc.children[idx];
@@ -618,7 +620,7 @@ var MarkloomAi = (_class = class {
618
620
  const steps = [];
619
621
  for (let i = 0; i < doc.children.length; i++) {
620
622
  const c = doc.children[i];
621
- if (_chunkVVFIQLVRcjs.isText.call(void 0, c) || _optionalChain([c, 'access', _57 => _57.attrs, 'optionalAccess', _58 => _58.aiFrom]) !== blockId)
623
+ if (_chunkGQ2HHRGIcjs.isText.call(void 0, c) || _optionalChain([c, 'access', _57 => _57.attrs, 'optionalAccess', _58 => _58.aiFrom]) !== blockId)
622
624
  continue;
623
625
  const attrs = { ..._nullishCoalesce(c.attrs, () => ( {})) };
624
626
  delete attrs.aiDraft;
@@ -634,12 +636,12 @@ var MarkloomAi = (_class = class {
634
636
  next.attrs = clean;
635
637
  steps.push({ kind: "replaceNode", path: [i], node: next });
636
638
  }
637
- const aiIdx0 = doc.children.findIndex((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && c.id === blockId);
639
+ const aiIdx0 = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
638
640
  if (aiIdx0 >= 0)
639
641
  steps.push({ kind: "spliceChildren", path: [], at: aiIdx0, remove: 1, insert: [] });
640
642
  if (!steps.length)
641
643
  return;
642
- const firstDraft = doc.children.findIndex((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _59 => _59.attrs, 'optionalAccess', _60 => _60.aiFrom]) === blockId);
644
+ const firstDraft = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _59 => _59.attrs, 'optionalAccess', _60 => _60.aiFrom]) === blockId);
643
645
  let caretAt = firstDraft >= 0 ? firstDraft : Math.max(0, aiIdx0);
644
646
  if (firstDraft > aiIdx0)
645
647
  caretAt = firstDraft - 1;
@@ -683,7 +685,7 @@ var MarkloomAi = (_class = class {
683
685
  const idxs = draftIndices(doc, blockId);
684
686
  for (let i = idxs.length - 1; i >= 0; i--)
685
687
  steps.push({ kind: "spliceChildren", path: [], at: idxs[i], remove: 1, insert: [] });
686
- let aiIdx = doc.children.findIndex((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && c.id === blockId);
688
+ let aiIdx = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
687
689
  let shift = 0;
688
690
  for (const i of idxs) {
689
691
  if (i < aiIdx)
@@ -722,11 +724,11 @@ var MarkloomAi = (_class = class {
722
724
  this.txBlockId = null;
723
725
  }
724
726
  findAi(ed, blockId) {
725
- const path = _chunkVVFIQLVRcjs.pathOfId.call(void 0, ed.doc, blockId);
727
+ const path = _chunk4TMHVNL6cjs.pathOfId.call(void 0, ed.doc, blockId);
726
728
  if (!path)
727
729
  return null;
728
730
  const node = path.reduce((n, i) => n.children[i], ed.doc);
729
- if (!node || _chunkVVFIQLVRcjs.isText.call(void 0, node) || node.type !== "ai")
731
+ if (!node || _chunkGQ2HHRGIcjs.isText.call(void 0, node) || node.type !== "ai")
730
732
  return null;
731
733
  return { path, node };
732
734
  }
@@ -750,7 +752,7 @@ var MarkloomAi = (_class = class {
750
752
  paintMd(ed, blockId, md, streaming) {
751
753
  const doc = ed.doc;
752
754
  const idxs = draftIndices(doc, blockId);
753
- const aiIdx = doc.children.findIndex((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c) && c.id === blockId);
755
+ const aiIdx = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
754
756
  const at = idxs.length ? idxs[0] : aiIdx >= 0 ? aiIdx + 1 : -1;
755
757
  if (at < 0)
756
758
  return;
@@ -780,7 +782,7 @@ var MarkloomAi = (_class = class {
780
782
  const src = streaming ? softCloseMd(raw) : raw;
781
783
  let blocks;
782
784
  try {
783
- const tops = _chunkVVFIQLVRcjs.mdToDoc.call(void 0, src).children.filter((c) => !_chunkVVFIQLVRcjs.isText.call(void 0, c));
785
+ const tops = _chunk4TMHVNL6cjs.mdToDoc.call(void 0, src).children.filter((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c));
784
786
  blocks = tops.map((c, i) => stampDraft(c, blockId, `${blockId}_d${i}`, streaming && i === tops.length - 1));
785
787
  } catch (e2) {
786
788
  blocks = [{
@@ -869,7 +871,7 @@ function softCloseMd(md) {
869
871
  }
870
872
  function stampDraft(node, aiFrom, id, streamTail = false) {
871
873
  const kids = node.children.map((c, i) => {
872
- if (_chunkVVFIQLVRcjs.isText.call(void 0, c))
874
+ if (_chunkGQ2HHRGIcjs.isText.call(void 0, c))
873
875
  return c;
874
876
  return stampInner(c, `${id}_${i}`);
875
877
  });
@@ -880,7 +882,7 @@ function stampDraft(node, aiFrom, id, streamTail = false) {
880
882
  }
881
883
  function stampInner(node, id) {
882
884
  const kids = node.children.map((c, i) => {
883
- if (_chunkVVFIQLVRcjs.isText.call(void 0, c))
885
+ if (_chunkGQ2HHRGIcjs.isText.call(void 0, c))
884
886
  return c;
885
887
  return stampInner(c, `${id}_${i}`);
886
888
  });
@@ -895,7 +897,7 @@ function draftIndices(doc, aiId) {
895
897
  const out = [];
896
898
  for (let i = 0; i < doc.children.length; i++) {
897
899
  const c = doc.children[i];
898
- if (!_chunkVVFIQLVRcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _67 => _67.attrs, 'optionalAccess', _68 => _68.aiFrom]) === aiId)
900
+ if (!_chunkGQ2HHRGIcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _67 => _67.attrs, 'optionalAccess', _68 => _68.aiFrom]) === aiId)
899
901
  out.push(i);
900
902
  }
901
903
  return out;
@@ -905,7 +907,7 @@ function promptText(ai) {
905
907
  return p ? collectText2(p) : "";
906
908
  }
907
909
  function collectText2(n) {
908
- if (_chunkVVFIQLVRcjs.isText.call(void 0, n))
910
+ if (_chunkGQ2HHRGIcjs.isText.call(void 0, n))
909
911
  return n.text;
910
912
  return n.children.map(collectText2).join("");
911
913
  }
@@ -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-JMOJU5EU.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,15 @@ import {
56
53
  setFreeze,
57
54
  splitCell,
58
55
  tableColWidths
59
- } from "./chunk-J3QWW6CN.js";
56
+ } from "./chunk-MFYF2A6U.js";
57
+ import {
58
+ connectCollab
59
+ } from "./chunk-6DCI7AL3.js";
60
+ import {
61
+ isElement,
62
+ isInlineAtom,
63
+ isText
64
+ } from "./chunk-LA5SBCGQ.js";
60
65
 
61
66
  // src/view/render.ts
62
67
  import { h as h2 } from "preact";
@@ -7397,6 +7402,9 @@ var Editor = class {
7397
7402
  decorations;
7398
7403
  /** AI 一等公民(opts.ai):斜杠 / 格式栏 / 卡片全内置 */
7399
7404
  aiCtl = null;
7405
+ /** 协作会话;applyingRemote 期间本地 steps 不上行 */
7406
+ collabCtl = null;
7407
+ applyingRemote = false;
7400
7408
  constructor(root, doc, opts = {}) {
7401
7409
  this.root = root;
7402
7410
  const dev = typeof process !== "undefined" && process.env?.NODE_ENV !== "production";
@@ -7461,6 +7469,19 @@ var Editor = class {
7461
7469
  onBusy: cfg.onBusy
7462
7470
  });
7463
7471
  }
7472
+ if (opts.collab) {
7473
+ this.collabCtl = connectCollab(
7474
+ { ...opts.collab, seed: opts.collab.seed ?? doc },
7475
+ {
7476
+ onDoc: (d) => this.replaceDoc(d),
7477
+ onRemote: (steps) => this.applyRemote(steps),
7478
+ onError: (msg) => {
7479
+ if (typeof console !== "undefined")
7480
+ console.warn("[markloom collab]", msg);
7481
+ }
7482
+ }
7483
+ );
7484
+ }
7464
7485
  const host = {
7465
7486
  root,
7466
7487
  doc: () => this.history.doc,
@@ -7526,6 +7547,8 @@ var Editor = class {
7526
7547
  this.commentPanel?.destroy();
7527
7548
  this.tocPanel?.destroy();
7528
7549
  this.aiCtl?.destroy();
7550
+ this.collabCtl?.close();
7551
+ this.collabCtl = null;
7529
7552
  this.aiCtl = null;
7530
7553
  for (const d of this.decorations)
7531
7554
  d.destroy();
@@ -7535,6 +7558,10 @@ var Editor = class {
7535
7558
  get ai() {
7536
7559
  return this.aiCtl;
7537
7560
  }
7561
+ /** 协作客户端(opts.collab 启用时) */
7562
+ get collab() {
7563
+ return this.collabCtl;
7564
+ }
7538
7565
  openAi(mode) {
7539
7566
  this.aiCtl?.openAtFocus(mode);
7540
7567
  }
@@ -7650,6 +7677,8 @@ var Editor = class {
7650
7677
  this.history = commit(this.history, batch);
7651
7678
  this.draw(selectionAfter);
7652
7679
  this.opts.onChange?.(this.history.doc);
7680
+ if (!this.applyingRemote)
7681
+ this.collabCtl?.push(batch);
7653
7682
  }
7654
7683
  // AI 流式 / 事务内续写:并入 past 最后一条,一次 undo 回退整笔事务。
7655
7684
  dispatchStream(steps, selectionAfter) {
@@ -7660,24 +7689,54 @@ var Editor = class {
7660
7689
  this.history = amend(this.history, batch);
7661
7690
  this.draw(selectionAfter);
7662
7691
  this.opts.onChange?.(this.history.doc);
7692
+ if (!this.applyingRemote)
7693
+ this.collabCtl?.push(batch);
7694
+ }
7695
+ /** 远端协作 op:写入 model 但不上行 */
7696
+ applyRemote(steps, selectionAfter) {
7697
+ if (steps.length === 0)
7698
+ return;
7699
+ this.applyingRemote = true;
7700
+ try {
7701
+ const after = applyAll(this.history.doc, steps);
7702
+ const batch = steps.concat(gapSteps(after));
7703
+ this.history = commit(this.history, batch);
7704
+ this.draw(selectionAfter);
7705
+ this.opts.onChange?.(this.history.doc);
7706
+ } finally {
7707
+ this.applyingRemote = false;
7708
+ }
7709
+ }
7710
+ /** 权威快照替换(welcome/resync);清空本地 undo 栈 */
7711
+ replaceDoc(doc) {
7712
+ const dev = typeof process !== "undefined" && process.env?.NODE_ENV !== "production";
7713
+ this.history = init(ensureGaps(doc), dev ? (d) => firstViolation(invariants(basicSchema, d)) : void 0);
7714
+ this.draw();
7715
+ this.opts.onChange?.(this.history.doc);
7663
7716
  }
7664
7717
  /** 回退一笔 history(AI 事务 discard = undo 整笔) */
7665
7718
  undo() {
7719
+ const last = this.history.past[this.history.past.length - 1];
7666
7720
  const next = undo(this.history);
7667
7721
  if (next === this.history)
7668
7722
  return false;
7669
7723
  this.history = next;
7670
7724
  this.draw();
7671
7725
  this.opts.onChange?.(this.history.doc);
7726
+ if (!this.applyingRemote && last)
7727
+ this.collabCtl?.push(last.inverse);
7672
7728
  return true;
7673
7729
  }
7674
7730
  redo() {
7731
+ const fut = this.history.future[0];
7675
7732
  const next = redo(this.history);
7676
7733
  if (next === this.history)
7677
7734
  return false;
7678
7735
  this.history = next;
7679
7736
  this.draw();
7680
7737
  this.opts.onChange?.(this.history.doc);
7738
+ if (!this.applyingRemote && fut)
7739
+ this.collabCtl?.push(fut.steps);
7681
7740
  return true;
7682
7741
  }
7683
7742
  /** 续写:当前块后插空段并流式灌入 */