markloom 0.5.0 → 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.
Files changed (67) hide show
  1. package/PACKAGES.md +26 -0
  2. package/README.md +20 -2
  3. package/collab.md +44 -7
  4. package/dist/ai/index.cjs +4 -4
  5. package/dist/ai/index.d.cts +5 -4
  6. package/dist/ai/index.d.ts +5 -4
  7. package/dist/ai/index.js +3 -3
  8. package/dist/ai/theme.css +66 -102
  9. package/dist/{chunk-YJAWPRJW.cjs → chunk-ACFYBYZJ.cjs} +2817 -903
  10. package/dist/chunk-CGYCJORO.js +759 -0
  11. package/dist/chunk-EADZKEU4.cjs +498 -0
  12. package/dist/chunk-EJSFZHGT.js +24 -0
  13. package/dist/{chunk-SLRD6FMP.cjs → chunk-I5VK4U7J.cjs} +122 -73
  14. package/dist/{chunk-WUHTCCAE.js → chunk-KYZJIOOL.js} +2403 -489
  15. package/dist/chunk-QEVME6ZU.cjs +759 -0
  16. package/dist/{chunk-MFYF2A6U.js → chunk-SNFF6X45.js} +51 -1
  17. package/dist/{chunk-JMOJU5EU.js → chunk-TB2OY3D4.js} +69 -20
  18. package/dist/{chunk-4TMHVNL6.cjs → chunk-TWD5T4YC.cjs} +155 -105
  19. package/dist/{chunk-JS527FDV.cjs → chunk-U3JZDFL4.cjs} +49 -9
  20. package/dist/{chunk-RLTHVQNQ.js → chunk-VSBIMMI7.js} +45 -5
  21. package/dist/chunk-YBAT343I.js +498 -0
  22. package/dist/chunk-ZKQSQSSM.cjs +24 -0
  23. package/dist/{client-BNams4wv.d.cts → client-CLYq17SW.d.ts} +70 -7
  24. package/dist/{client-A0ZQJnyR.d.ts → client-DAth8pPq.d.cts} +70 -7
  25. package/dist/collab/index.cjs +22 -4
  26. package/dist/collab/index.d.cts +24 -3
  27. package/dist/collab/index.d.ts +24 -3
  28. package/dist/collab/index.js +24 -6
  29. package/dist/{controller-BkY1VPzc.d.ts → controller-6AJ36G52.d.ts} +108 -8
  30. package/dist/{controller-CVIZI--A.d.cts → controller-BIOO_XKJ.d.cts} +108 -8
  31. package/dist/index.cjs +57 -7
  32. package/dist/index.d.cts +18 -5
  33. package/dist/index.d.ts +18 -5
  34. package/dist/index.js +54 -4
  35. package/dist/io/index.cjs +1413 -0
  36. package/dist/io/index.d.cts +33 -0
  37. package/dist/io/index.d.ts +33 -0
  38. package/dist/io/index.js +1413 -0
  39. package/dist/react/index.cjs +8 -8
  40. package/dist/react/index.d.cts +5 -4
  41. package/dist/react/index.d.ts +5 -4
  42. package/dist/react/index.js +7 -7
  43. package/dist/{rebase-batch-8rL_aXEE.d.ts → rebase-batch-DlerAcFN.d.ts} +11 -9
  44. package/dist/{rebase-batch-bYnBJpRO.d.cts → rebase-batch-PjENQ742.d.cts} +11 -9
  45. package/dist/suggest-Bk4zLzuv.d.ts +120 -0
  46. package/dist/suggest-BkGGiPKA.d.cts +120 -0
  47. package/dist/types-COUweEav.d.cts +30 -0
  48. package/dist/types-COUweEav.d.ts +30 -0
  49. package/dist/versions-9mG1epfB.d.cts +51 -0
  50. package/dist/versions-Bubyo4pt.d.ts +51 -0
  51. package/dist/view/index.cjs +56 -12
  52. package/dist/view/index.d.cts +203 -11
  53. package/dist/view/index.d.ts +203 -11
  54. package/dist/view/index.js +52 -8
  55. package/dist/view/theme.css +1840 -1526
  56. package/io.md +38 -0
  57. package/package.json +21 -5
  58. package/dist/chunk-6DCI7AL3.js +0 -208
  59. package/dist/chunk-AC5E42NH.cjs +0 -208
  60. package/dist/chunk-GQ2HHRGI.cjs +0 -16
  61. package/dist/chunk-LA5SBCGQ.js +0 -16
  62. package/dist/chunk-LEYAQNJA.js +0 -220
  63. package/dist/chunk-YXQDCW4Y.cjs +0 -220
  64. package/dist/comments-DlExS6f-.d.cts +0 -43
  65. package/dist/comments-PhnR7Gkn.d.ts +0 -43
  66. package/dist/step-BrvHIK7O.d.cts +0 -65
  67. package/dist/step-BrvHIK7O.d.ts +0 -65
@@ -5,15 +5,37 @@
5
5
 
6
6
 
7
7
 
8
- var _chunk4TMHVNL6cjs = require('./chunk-4TMHVNL6.cjs');
9
8
 
10
9
 
11
- var _chunkGQ2HHRGIcjs = require('./chunk-GQ2HHRGI.cjs');
10
+ var _chunkTWD5T4YCcjs = require('./chunk-TWD5T4YC.cjs');
11
+
12
+
13
+
14
+ var _chunkZKQSQSSMcjs = require('./chunk-ZKQSQSSM.cjs');
12
15
 
13
16
  // src/view/selection.ts
14
17
  function rootEl(root) {
15
18
  return _nullishCoalesce(root.querySelector(".markloom-doc"), () => ( root));
16
19
  }
20
+ function docExtent(doc) {
21
+ const n = doc.children.length;
22
+ if (n === 0)
23
+ return null;
24
+ const first = doc.children[0];
25
+ const last = doc.children[n - 1];
26
+ if (!_chunkZKQSQSSMcjs.isElement.call(void 0, first) || !_chunkZKQSQSSMcjs.isElement.call(void 0, last))
27
+ return null;
28
+ return {
29
+ anchor: { path: _chunkTWD5T4YCcjs.normalizeToLeaf.call(void 0, doc, [0, ..._chunkTWD5T4YCcjs.firstLeafPath.call(void 0, first), 0]) },
30
+ head: { path: _chunkTWD5T4YCcjs.normalizeToLeaf.call(void 0, doc, [n - 1, ..._chunkTWD5T4YCcjs.lastLeafEnd.call(void 0, last)]) }
31
+ };
32
+ }
33
+ function selectionCovers(sel, extent) {
34
+ return pathEq(sel.anchor.path, extent.anchor.path) && pathEq(sel.head.path, extent.head.path) || pathEq(sel.anchor.path, extent.head.path) && pathEq(sel.head.path, extent.anchor.path);
35
+ }
36
+ function pathEq(a, b) {
37
+ return a.length === b.length && a.every((v, i) => v === b[i]);
38
+ }
17
39
  function pointToDom(root, doc, pos) {
18
40
  const path = pos.path;
19
41
  if (path.length < 2)
@@ -70,7 +92,7 @@ function domToPoint(root, doc, container, domOffset) {
70
92
  }
71
93
  if (segs.length === 0)
72
94
  return null;
73
- return { path: _chunk4TMHVNL6cjs.normalizeToLeaf.call(void 0, doc, segs.concat(charOffset)) };
95
+ return { path: _chunkTWD5T4YCcjs.normalizeToLeaf.call(void 0, doc, segs.concat(charOffset)) };
74
96
  }
75
97
  function readSelection(root, doc) {
76
98
  const sel = root.ownerDocument.getSelection();
@@ -78,11 +100,25 @@ function readSelection(root, doc) {
78
100
  return null;
79
101
  const anchor = domToPoint(root, doc, sel.anchorNode, sel.anchorOffset);
80
102
  const head = domToPoint(root, doc, sel.focusNode, sel.focusOffset);
81
- if (!anchor || !head)
82
- return null;
83
- return { anchor, head };
103
+ if (anchor && head)
104
+ return { anchor, head };
105
+ if (rangeCoversSurface(sel, rootEl(root)))
106
+ return docExtent(doc);
107
+ return null;
84
108
  }
85
- function writeSelection(root, doc, selection) {
109
+ function rangeCoversSurface(sel, top) {
110
+ if (!sel.rangeCount)
111
+ return false;
112
+ try {
113
+ const r = sel.getRangeAt(0);
114
+ const tr = top.ownerDocument.createRange();
115
+ tr.selectNodeContents(top);
116
+ return r.compareBoundaryPoints(Range.START_TO_START, tr) <= 0 && r.compareBoundaryPoints(Range.END_TO_END, tr) >= 0;
117
+ } catch (e2) {
118
+ return false;
119
+ }
120
+ }
121
+ function writeSelection(root, doc, selection, opts) {
86
122
  const a = pointToDom(root, doc, selection.anchor);
87
123
  const hd = pointToDom(root, doc, selection.head);
88
124
  if (!a || !hd)
@@ -92,12 +128,17 @@ function writeSelection(root, doc, selection) {
92
128
  return;
93
129
  const host = editableHost(a.node, root);
94
130
  if (host && host !== root.ownerDocument.activeElement)
95
- host.focus({ preventScroll: true });
131
+ host.focus({ preventScroll: !_optionalChain([opts, 'optionalAccess', _3 => _3.scroll]) });
96
132
  const range = root.ownerDocument.createRange();
97
133
  range.setStart(a.node, a.offset);
98
134
  range.setEnd(hd.node, hd.offset);
99
135
  sel.removeAllRanges();
100
136
  sel.addRange(range);
137
+ if (_optionalChain([opts, 'optionalAccess', _4 => _4.scroll])) {
138
+ const n = a.node;
139
+ const el = n.nodeType === 3 ? n.parentElement : n instanceof HTMLElement ? n : null;
140
+ _optionalChain([el, 'optionalAccess', _5 => _5.scrollIntoView, 'optionalCall', _6 => _6({ block: "nearest", inline: "nearest" })]);
141
+ }
101
142
  }
102
143
  function editableHost(node, root) {
103
144
  let el = node.nodeType === 3 ? node.parentElement : node;
@@ -209,27 +250,27 @@ function locateInLeaf(leaf, charOff) {
209
250
  prev = { node: t, offset: s.length };
210
251
  off -= len;
211
252
  }
212
- return _nullishCoalesce(prev, () => ( { node: texts[texts.length - 1], offset: _nullishCoalesce(_optionalChain([texts, 'access', _3 => _3[texts.length - 1], 'access', _4 => _4.textContent, 'optionalAccess', _5 => _5.length]), () => ( 0)) }));
253
+ return _nullishCoalesce(prev, () => ( { node: texts[texts.length - 1], offset: _nullishCoalesce(_optionalChain([texts, 'access', _7 => _7[texts.length - 1], 'access', _8 => _8.textContent, 'optionalAccess', _9 => _9.length]), () => ( 0)) }));
213
254
  }
214
255
 
215
256
  // src/view/ai-write.ts
216
- var newId = () => _nullishCoalesce(_optionalChain([crypto, 'optionalAccess', _6 => _6.randomUUID, 'optionalCall', _7 => _7()]), () => ( `ai${Math.random().toString(36).slice(2)}`));
257
+ var newId = () => _nullishCoalesce(_optionalChain([crypto, 'optionalAccess', _10 => _10.randomUUID, 'optionalCall', _11 => _11()]), () => ( `ai${Math.random().toString(36).slice(2)}`));
217
258
  function focusBlockId(ed) {
218
259
  const sel = readSelection(ed.root, ed.doc);
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)));
260
+ const idx = _nullishCoalesce(_optionalChain([sel, 'optionalAccess', _12 => _12.head, 'access', _13 => _13.path, 'access', _14 => _14[0]]), () => ( Math.max(0, ed.doc.children.length - 1)));
220
261
  const b = ed.doc.children[idx];
221
- if (!b || _chunkGQ2HHRGIcjs.isText.call(void 0, b) || !b.id)
262
+ if (!b || _chunkZKQSQSSMcjs.isText.call(void 0, b) || !b.id)
222
263
  return null;
223
264
  return b.id;
224
265
  }
225
266
  function blockPlainText(doc, blockId) {
226
- const b = doc.children.find((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
267
+ const b = doc.children.find((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && c.id === blockId);
227
268
  if (!b)
228
269
  return "";
229
270
  return collectText(b);
230
271
  }
231
272
  function collectText(n) {
232
- if (_chunkGQ2HHRGIcjs.isText.call(void 0, n))
273
+ if (_chunkZKQSQSSMcjs.isText.call(void 0, n))
233
274
  return n.text;
234
275
  return n.children.map(collectText).join("");
235
276
  }
@@ -249,12 +290,12 @@ function emptyLike(block) {
249
290
  return { type: "paragraph", id: block.id, attrs: block.attrs ? { ...block.attrs } : void 0, children: [{ text: "" }] };
250
291
  }
251
292
  function streamTargetId(doc, blockId) {
252
- const b = doc.children.find((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
293
+ const b = doc.children.find((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && c.id === blockId);
253
294
  if (!b)
254
295
  return blockId;
255
296
  if (b.type === "quote" || b.type === "callout") {
256
- const inner = b.children.find((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.type === "paragraph");
257
- if (_optionalChain([inner, 'optionalAccess', _11 => _11.id]))
297
+ const inner = b.children.find((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && c.type === "paragraph");
298
+ if (_optionalChain([inner, 'optionalAccess', _15 => _15.id]))
258
299
  return inner.id;
259
300
  }
260
301
  return blockId;
@@ -275,16 +316,16 @@ function startAiWrite(ed, mode, blockId) {
275
316
  if (mode === "continue") {
276
317
  const id = newId();
277
318
  const empty = { type: "paragraph", id, children: [{ text: "" }] };
278
- ed.dispatch([_chunk4TMHVNL6cjs.insertAfter.call(void 0, doc, focus, [empty])]);
319
+ ed.dispatch([_chunkTWD5T4YCcjs.insertAfter.call(void 0, doc, focus, [empty])]);
279
320
  targetId = id;
280
321
  } else {
281
- const block = doc.children.find((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === focus);
322
+ const block = doc.children.find((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && c.id === focus);
282
323
  if (!block)
283
324
  throw new Error(`block not found: ${focus}`);
284
325
  if (block.type === "table" || block.type === "image" || block.type === "embed" || block.type === "raw")
285
326
  throw new Error(`cannot rewrite ${block.type}`);
286
327
  const cleared = emptyLike(block);
287
- ed.dispatch([_chunk4TMHVNL6cjs.rewriteNode.call(void 0, doc, focus, cleared)]);
328
+ ed.dispatch([_chunkTWD5T4YCcjs.rewriteNode.call(void 0, doc, focus, cleared)]);
288
329
  targetId = streamTargetId(ed.doc, focus);
289
330
  }
290
331
  opened = true;
@@ -293,7 +334,7 @@ function startAiWrite(ed, mode, blockId) {
293
334
  if (!alive || !text)
294
335
  return;
295
336
  setup();
296
- const step = _chunk4TMHVNL6cjs.appendText.call(void 0, ed.doc, targetId, text);
337
+ const step = _chunkTWD5T4YCcjs.appendText.call(void 0, ed.doc, targetId, text);
297
338
  ed.dispatchStream([step]);
298
339
  };
299
340
  const finish = () => {
@@ -318,11 +359,11 @@ function precedingPlainText(doc, blockIdx) {
318
359
  const parts = [];
319
360
  for (let i = 0; i < blockIdx && i < doc.children.length; i++) {
320
361
  const b = doc.children[i];
321
- if (_chunkGQ2HHRGIcjs.isText.call(void 0, b) || !b.id)
362
+ if (_chunkZKQSQSSMcjs.isText.call(void 0, b) || !b.id)
322
363
  continue;
323
364
  if (b.type === "image" || b.type === "table" || b.type === "embed" || b.type === "raw" || b.type === "ai")
324
365
  continue;
325
- if (_optionalChain([b, 'access', _12 => _12.attrs, 'optionalAccess', _13 => _13.aiDraft]))
366
+ if (_optionalChain([b, 'access', _16 => _16.attrs, 'optionalAccess', _17 => _17.aiDraft]))
326
367
  continue;
327
368
  const t = blockPlainText(doc, b.id);
328
369
  if (t.trim())
@@ -336,7 +377,7 @@ function aiInstructionFromQuery(query) {
336
377
  return "";
337
378
  return q.slice(2).trim();
338
379
  }
339
- var newAiId = () => _nullishCoalesce(_optionalChain([crypto, 'access', _14 => _14.randomUUID, 'optionalCall', _15 => _15()]), () => ( `ai${Math.random().toString(36).slice(2)}`));
380
+ var newAiId = () => _nullishCoalesce(_optionalChain([crypto, 'access', _18 => _18.randomUUID, 'optionalCall', _19 => _19()]), () => ( `ai${Math.random().toString(36).slice(2)}`));
340
381
 
341
382
  // src/ai/controller.ts
342
383
  var MarkloomAi = (_class = class {
@@ -355,7 +396,7 @@ var MarkloomAi = (_class = class {
355
396
  this.opts = opts;
356
397
  }
357
398
  destroy() {
358
- _optionalChain([this, 'access', _16 => _16.abort, 'optionalAccess', _17 => _17.abort, 'call', _18 => _18()]);
399
+ _optionalChain([this, 'access', _20 => _20.abort, 'optionalAccess', _21 => _21.abort, 'call', _22 => _22()]);
359
400
  this.clearRaf();
360
401
  this.streamingId = null;
361
402
  this.pendingMd = "";
@@ -368,14 +409,14 @@ var MarkloomAi = (_class = class {
368
409
  * · 空 → undo 整笔事务(等同丢弃)
369
410
  */
370
411
  stop() {
371
- _optionalChain([this, 'access', _19 => _19.abort, 'optionalAccess', _20 => _20.abort, 'call', _21 => _21()]);
412
+ _optionalChain([this, 'access', _23 => _23.abort, 'optionalAccess', _24 => _24.abort, 'call', _25 => _25()]);
372
413
  this.abort = null;
373
414
  this.clearRaf();
374
415
  const id = this.streamingId;
375
416
  const md = this.pendingMd;
376
417
  this.pendingMd = "";
377
418
  this.streamingId = null;
378
- _optionalChain([this, 'access', _22 => _22.opts, 'access', _23 => _23.onBusy, 'optionalCall', _24 => _24(false)]);
419
+ _optionalChain([this, 'access', _26 => _26.opts, 'access', _27 => _27.onBusy, 'optionalCall', _28 => _28(false)]);
379
420
  if (!id)
380
421
  return;
381
422
  const ed = this.getEditor();
@@ -407,11 +448,11 @@ var MarkloomAi = (_class = class {
407
448
  return;
408
449
  const bid = focusBlockId(ed);
409
450
  if (!bid) {
410
- _optionalChain([this, 'access', _25 => _25.opts, 'access', _26 => _26.onError, 'optionalCall', _27 => _27("\u8BF7\u5148\u628A\u5149\u6807\u653E\u5728\u4E00\u6BB5\u6587\u5B57\u91CC")]);
451
+ _optionalChain([this, 'access', _29 => _29.opts, 'access', _30 => _30.onError, 'optionalCall', _31 => _31("\u8BF7\u5148\u628A\u5149\u6807\u653E\u5728\u4E00\u6BB5\u6587\u5B57\u91CC")]);
411
452
  return;
412
453
  }
413
454
  const doc = ed.doc;
414
- const idx = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === bid);
455
+ const idx = doc.children.findIndex((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && c.id === bid);
415
456
  if (idx < 0)
416
457
  return;
417
458
  const cur = doc.children[idx];
@@ -419,7 +460,7 @@ var MarkloomAi = (_class = class {
419
460
  return;
420
461
  if (mode === "rewrite") {
421
462
  if (!blockPlainText(doc, bid).trim()) {
422
- _optionalChain([this, 'access', _28 => _28.opts, 'access', _29 => _29.onError, 'optionalCall', _30 => _30("\u5F53\u524D\u6BB5\u6CA1\u6709\u53EF\u6539\u5199\u7684\u6587\u5B57")]);
463
+ _optionalChain([this, 'access', _32 => _32.opts, 'access', _33 => _33.onError, 'optionalCall', _34 => _34("\u5F53\u524D\u6BB5\u6CA1\u6709\u53EF\u6539\u5199\u7684\u6587\u5B57")]);
423
464
  return;
424
465
  }
425
466
  const seed = blockPlainText(doc, bid);
@@ -492,10 +533,10 @@ var MarkloomAi = (_class = class {
492
533
  if (!found)
493
534
  return;
494
535
  const { path, node } = found;
495
- if (_optionalChain([node, 'access', _31 => _31.attrs, 'optionalAccess', _32 => _32.status]) === "loading")
536
+ if (_optionalChain([node, 'access', _35 => _35.attrs, 'optionalAccess', _36 => _36.status]) === "loading")
496
537
  return;
497
538
  if (this.txOpen && this.txBlockId === blockId) {
498
- _optionalChain([this, 'access', _33 => _33.abort, 'optionalAccess', _34 => _34.abort, 'call', _35 => _35()]);
539
+ _optionalChain([this, 'access', _37 => _37.abort, 'optionalAccess', _38 => _38.abort, 'call', _39 => _39()]);
499
540
  this.abort = null;
500
541
  this.clearRaf();
501
542
  this.streamingId = null;
@@ -506,9 +547,9 @@ var MarkloomAi = (_class = class {
506
547
  if (!live)
507
548
  return;
508
549
  const { path: aiPath, node: aiNode } = live;
509
- const mode = _optionalChain([aiNode, 'access', _36 => _36.attrs, 'optionalAccess', _37 => _37.mode]) === "rewrite" ? "rewrite" : "continue";
550
+ const mode = _optionalChain([aiNode, 'access', _40 => _40.attrs, 'optionalAccess', _41 => _41.mode]) === "rewrite" ? "rewrite" : "continue";
510
551
  const instruction = promptText(aiNode).trim();
511
- const seed = typeof _optionalChain([aiNode, 'access', _38 => _38.attrs, 'optionalAccess', _39 => _39.seed]) === "string" ? aiNode.attrs.seed : "";
552
+ const seed = typeof _optionalChain([aiNode, 'access', _42 => _42.attrs, 'optionalAccess', _43 => _43.seed]) === "string" ? aiNode.attrs.seed : "";
512
553
  const blockIdx = _nullishCoalesce(aiPath[0], () => ( 0));
513
554
  const before = precedingPlainText(ed.doc, blockIdx);
514
555
  const ctxBody = mode === "rewrite" ? seed || "\uFF08\u65E0\u539F\u6587\uFF09" : before.trim() || "\uFF08\u5C1A\u65E0\u4E0A\u6587\uFF09";
@@ -552,7 +593,7 @@ var MarkloomAi = (_class = class {
552
593
  this.streamingId = blockId;
553
594
  this.pendingMd = "";
554
595
  this.clearRaf();
555
- _optionalChain([this, 'access', _40 => _40.opts, 'access', _41 => _41.onBusy, 'optionalCall', _42 => _42(true)]);
596
+ _optionalChain([this, 'access', _44 => _44.opts, 'access', _45 => _45.onBusy, 'optionalCall', _46 => _46(true)]);
556
597
  const ac = new AbortController();
557
598
  this.abort = ac;
558
599
  const flush = () => {
@@ -586,9 +627,9 @@ var MarkloomAi = (_class = class {
586
627
  this.paintMd(ed, blockId, this.pendingMd, false);
587
628
  this.patchAi(ed, blockId, { status: "preview" });
588
629
  } catch (e) {
589
- if (_optionalChain([e, 'optionalAccess', _43 => _43.name]) !== "AbortError") {
590
- const msg = _optionalChain([e, 'optionalAccess', _44 => _44.message]) || String(e);
591
- _optionalChain([this, 'access', _45 => _45.opts, 'access', _46 => _46.onError, 'optionalCall', _47 => _47(msg)]);
630
+ if (_optionalChain([e, 'optionalAccess', _47 => _47.name]) !== "AbortError") {
631
+ const msg = _optionalChain([e, 'optionalAccess', _48 => _48.message]) || String(e);
632
+ _optionalChain([this, 'access', _49 => _49.opts, 'access', _50 => _50.onError, 'optionalCall', _51 => _51(msg)]);
592
633
  if (this.streamingId === blockId) {
593
634
  this.clearRaf();
594
635
  this.paintMd(ed, blockId, this.pendingMd, false);
@@ -599,7 +640,7 @@ var MarkloomAi = (_class = class {
599
640
  if (this.streamingId === blockId) {
600
641
  this.streamingId = null;
601
642
  this.abort = null;
602
- _optionalChain([this, 'access', _48 => _48.opts, 'access', _49 => _49.onBusy, 'optionalCall', _50 => _50(false)]);
643
+ _optionalChain([this, 'access', _52 => _52.opts, 'access', _53 => _53.onBusy, 'optionalCall', _54 => _54(false)]);
603
644
  }
604
645
  }
605
646
  }
@@ -614,13 +655,13 @@ var MarkloomAi = (_class = class {
614
655
  const found = this.findAi(ed, blockId);
615
656
  if (!found)
616
657
  return;
617
- if (_optionalChain([found, 'access', _51 => _51.node, 'access', _52 => _52.attrs, 'optionalAccess', _53 => _53.status]) !== "preview" && _optionalChain([found, 'access', _54 => _54.node, 'access', _55 => _55.attrs, 'optionalAccess', _56 => _56.status]) !== "error")
658
+ if (_optionalChain([found, 'access', _55 => _55.node, 'access', _56 => _56.attrs, 'optionalAccess', _57 => _57.status]) !== "preview" && _optionalChain([found, 'access', _58 => _58.node, 'access', _59 => _59.attrs, 'optionalAccess', _60 => _60.status]) !== "error")
618
659
  return;
619
660
  const doc = ed.doc;
620
661
  const steps = [];
621
662
  for (let i = 0; i < doc.children.length; i++) {
622
663
  const c = doc.children[i];
623
- if (_chunkGQ2HHRGIcjs.isText.call(void 0, c) || _optionalChain([c, 'access', _57 => _57.attrs, 'optionalAccess', _58 => _58.aiFrom]) !== blockId)
664
+ if (_chunkZKQSQSSMcjs.isText.call(void 0, c) || _optionalChain([c, 'access', _61 => _61.attrs, 'optionalAccess', _62 => _62.aiFrom]) !== blockId)
624
665
  continue;
625
666
  const attrs = { ..._nullishCoalesce(c.attrs, () => ( {})) };
626
667
  delete attrs.aiDraft;
@@ -636,12 +677,12 @@ var MarkloomAi = (_class = class {
636
677
  next.attrs = clean;
637
678
  steps.push({ kind: "replaceNode", path: [i], node: next });
638
679
  }
639
- const aiIdx0 = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
680
+ const aiIdx0 = doc.children.findIndex((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && c.id === blockId);
640
681
  if (aiIdx0 >= 0)
641
682
  steps.push({ kind: "spliceChildren", path: [], at: aiIdx0, remove: 1, insert: [] });
642
683
  if (!steps.length)
643
684
  return;
644
- const firstDraft = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _59 => _59.attrs, 'optionalAccess', _60 => _60.aiFrom]) === blockId);
685
+ const firstDraft = doc.children.findIndex((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _63 => _63.attrs, 'optionalAccess', _64 => _64.aiFrom]) === blockId);
645
686
  let caretAt = firstDraft >= 0 ? firstDraft : Math.max(0, aiIdx0);
646
687
  if (firstDraft > aiIdx0)
647
688
  caretAt = firstDraft - 1;
@@ -660,51 +701,57 @@ var MarkloomAi = (_class = class {
660
701
  this.txOpen = false;
661
702
  this.txBlockId = null;
662
703
  }
663
- /** 整笔撤回事务(= undo) */
704
+ /** 整笔撤回事务(= undo);务必不留 aiDraft 孤儿段 */
664
705
  discard(blockId) {
665
706
  if (this.streamingId === blockId) {
666
- _optionalChain([this, 'access', _61 => _61.abort, 'optionalAccess', _62 => _62.abort, 'call', _63 => _63()]);
707
+ _optionalChain([this, 'access', _65 => _65.abort, 'optionalAccess', _66 => _66.abort, 'call', _67 => _67()]);
667
708
  this.abort = null;
668
709
  this.clearRaf();
669
710
  this.streamingId = null;
670
711
  this.pendingMd = "";
671
- _optionalChain([this, 'access', _64 => _64.opts, 'access', _65 => _65.onBusy, 'optionalCall', _66 => _66(false)]);
712
+ _optionalChain([this, 'access', _68 => _68.opts, 'access', _69 => _69.onBusy, 'optionalCall', _70 => _70(false)]);
672
713
  }
673
714
  if (this.txOpen && (this.txBlockId === blockId || !this.txBlockId)) {
674
715
  this.rollback();
716
+ this.purgeDrafts(blockId);
675
717
  return;
676
718
  }
719
+ this.purgeDrafts(blockId, true);
720
+ }
721
+ /**
722
+ * 清掉 aiFrom=blockId 的顶层 draft;shell=true 时把 ai 壳也换成空段。
723
+ * discard / 异常恢复共用。
724
+ */
725
+ purgeDrafts(blockId, shell = false) {
677
726
  const ed = this.getEditor();
678
727
  if (!ed)
679
728
  return;
680
- const found = this.findAi(ed, blockId);
681
- if (!found)
682
- return;
683
- const steps = [];
684
729
  const doc = ed.doc;
730
+ const steps = [];
685
731
  const idxs = draftIndices(doc, blockId);
686
732
  for (let i = idxs.length - 1; i >= 0; i--)
687
733
  steps.push({ kind: "spliceChildren", path: [], at: idxs[i], remove: 1, insert: [] });
688
- let aiIdx = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
734
+ let aiIdx = doc.children.findIndex((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && c.id === blockId);
689
735
  let shift = 0;
690
736
  for (const i of idxs) {
691
737
  if (i < aiIdx)
692
738
  shift++;
693
739
  }
694
740
  aiIdx -= shift;
695
- if (aiIdx >= 0) {
741
+ if (shell && aiIdx >= 0) {
696
742
  steps.push({
697
743
  kind: "replaceNode",
698
744
  path: [aiIdx],
699
- node: { type: "paragraph", id: found.node.id, children: [{ text: "" }] }
700
- });
701
- }
702
- if (steps.length) {
703
- ed.dispatch(steps, {
704
- anchor: { path: [Math.max(0, aiIdx), 0, 0] },
705
- head: { path: [Math.max(0, aiIdx), 0, 0] }
745
+ node: { type: "paragraph", id: blockId, children: [{ text: "" }] }
706
746
  });
707
747
  }
748
+ if (!steps.length)
749
+ return;
750
+ const caretAt = Math.max(0, aiIdx >= 0 ? aiIdx : _nullishCoalesce(idxs[0], () => ( 0)));
751
+ ed.dispatch(steps, {
752
+ anchor: { path: [caretAt, 0, 0] },
753
+ head: { path: [caretAt, 0, 0] }
754
+ });
708
755
  }
709
756
  act(blockId, action) {
710
757
  if (action === "accept")
@@ -724,11 +771,11 @@ var MarkloomAi = (_class = class {
724
771
  this.txBlockId = null;
725
772
  }
726
773
  findAi(ed, blockId) {
727
- const path = _chunk4TMHVNL6cjs.pathOfId.call(void 0, ed.doc, blockId);
774
+ const path = _chunkTWD5T4YCcjs.pathOfId.call(void 0, ed.doc, blockId);
728
775
  if (!path)
729
776
  return null;
730
777
  const node = path.reduce((n, i) => n.children[i], ed.doc);
731
- if (!node || _chunkGQ2HHRGIcjs.isText.call(void 0, node) || node.type !== "ai")
778
+ if (!node || _chunkZKQSQSSMcjs.isText.call(void 0, node) || node.type !== "ai")
732
779
  return null;
733
780
  return { path, node };
734
781
  }
@@ -752,7 +799,7 @@ var MarkloomAi = (_class = class {
752
799
  paintMd(ed, blockId, md, streaming) {
753
800
  const doc = ed.doc;
754
801
  const idxs = draftIndices(doc, blockId);
755
- const aiIdx = doc.children.findIndex((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c) && c.id === blockId);
802
+ const aiIdx = doc.children.findIndex((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c) && c.id === blockId);
756
803
  const at = idxs.length ? idxs[0] : aiIdx >= 0 ? aiIdx + 1 : -1;
757
804
  if (at < 0)
758
805
  return;
@@ -782,9 +829,9 @@ var MarkloomAi = (_class = class {
782
829
  const src = streaming ? softCloseMd(raw) : raw;
783
830
  let blocks;
784
831
  try {
785
- const tops = _chunk4TMHVNL6cjs.mdToDoc.call(void 0, src).children.filter((c) => !_chunkGQ2HHRGIcjs.isText.call(void 0, c));
832
+ const tops = _chunkTWD5T4YCcjs.mdToDoc.call(void 0, src).children.filter((c) => !_chunkZKQSQSSMcjs.isText.call(void 0, c));
786
833
  blocks = tops.map((c, i) => stampDraft(c, blockId, `${blockId}_d${i}`, streaming && i === tops.length - 1));
787
- } catch (e2) {
834
+ } catch (e3) {
788
835
  blocks = [{
789
836
  type: "paragraph",
790
837
  id: `${blockId}_d0`,
@@ -871,7 +918,7 @@ function softCloseMd(md) {
871
918
  }
872
919
  function stampDraft(node, aiFrom, id, streamTail = false) {
873
920
  const kids = node.children.map((c, i) => {
874
- if (_chunkGQ2HHRGIcjs.isText.call(void 0, c))
921
+ if (_chunkZKQSQSSMcjs.isText.call(void 0, c))
875
922
  return c;
876
923
  return stampInner(c, `${id}_${i}`);
877
924
  });
@@ -882,7 +929,7 @@ function stampDraft(node, aiFrom, id, streamTail = false) {
882
929
  }
883
930
  function stampInner(node, id) {
884
931
  const kids = node.children.map((c, i) => {
885
- if (_chunkGQ2HHRGIcjs.isText.call(void 0, c))
932
+ if (_chunkZKQSQSSMcjs.isText.call(void 0, c))
886
933
  return c;
887
934
  return stampInner(c, `${id}_${i}`);
888
935
  });
@@ -897,7 +944,7 @@ function draftIndices(doc, aiId) {
897
944
  const out = [];
898
945
  for (let i = 0; i < doc.children.length; i++) {
899
946
  const c = doc.children[i];
900
- if (!_chunkGQ2HHRGIcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _67 => _67.attrs, 'optionalAccess', _68 => _68.aiFrom]) === aiId)
947
+ if (!_chunkZKQSQSSMcjs.isText.call(void 0, c) && _optionalChain([c, 'access', _71 => _71.attrs, 'optionalAccess', _72 => _72.aiFrom]) === aiId)
901
948
  out.push(i);
902
949
  }
903
950
  return out;
@@ -907,7 +954,7 @@ function promptText(ai) {
907
954
  return p ? collectText2(p) : "";
908
955
  }
909
956
  function collectText2(n) {
910
- if (_chunkGQ2HHRGIcjs.isText.call(void 0, n))
957
+ if (_chunkZKQSQSSMcjs.isText.call(void 0, n))
911
958
  return n.text;
912
959
  return n.children.map(collectText2).join("");
913
960
  }
@@ -954,7 +1001,7 @@ function completionsUrl(host) {
954
1001
  }
955
1002
  function resolveProvider(cfg) {
956
1003
  const p = typeof cfg.provider === "function" ? cfg.provider() : cfg.provider;
957
- if (!_optionalChain([p, 'optionalAccess', _69 => _69.host]) || !_optionalChain([p, 'optionalAccess', _70 => _70.apiKey]) || !_optionalChain([p, 'optionalAccess', _71 => _71.model]))
1004
+ if (!_optionalChain([p, 'optionalAccess', _73 => _73.host]) || !_optionalChain([p, 'optionalAccess', _74 => _74.apiKey]) || !_optionalChain([p, 'optionalAccess', _75 => _75.model]))
958
1005
  return null;
959
1006
  return p;
960
1007
  }
@@ -988,7 +1035,7 @@ async function readSse(res, onToken, mode) {
988
1035
  if (mode === "proxy" && j.content)
989
1036
  onToken(j.content);
990
1037
  if (mode === "openai") {
991
- const c = _optionalChain([j, 'access', _72 => _72.choices, 'optionalAccess', _73 => _73[0], 'optionalAccess', _74 => _74.delta, 'optionalAccess', _75 => _75.content]);
1038
+ const c = _optionalChain([j, 'access', _76 => _76.choices, 'optionalAccess', _77 => _77[0], 'optionalAccess', _78 => _78.delta, 'optionalAccess', _79 => _79.content]);
992
1039
  if (c)
993
1040
  onToken(c);
994
1041
  }
@@ -1006,7 +1053,7 @@ function createAiStream(cfg) {
1006
1053
  return cfg.stream;
1007
1054
  return async (input, onToken) => {
1008
1055
  const action = input.action;
1009
- const system = _optionalChain([(action === "rewrite" ? cfg.rewriteSystem : cfg.continueSystem), 'optionalAccess', _76 => _76.trim, 'call', _77 => _77()]) || defaultSystem(action);
1056
+ const system = _optionalChain([(action === "rewrite" ? cfg.rewriteSystem : cfg.continueSystem), 'optionalAccess', _80 => _80.trim, 'call', _81 => _81()]) || defaultSystem(action);
1010
1057
  const provider = resolveProvider(cfg);
1011
1058
  if (cfg.endpoint) {
1012
1059
  const res2 = await fetch(cfg.endpoint, {
@@ -1026,7 +1073,7 @@ function createAiStream(cfg) {
1026
1073
  try {
1027
1074
  const j = await res2.json();
1028
1075
  err = j.error || err;
1029
- } catch (e3) {
1076
+ } catch (e4) {
1030
1077
  err = await res2.text().catch(() => err);
1031
1078
  }
1032
1079
  throw new Error(err || `AI ${res2.status}`);
@@ -1075,4 +1122,6 @@ function createAiStream(cfg) {
1075
1122
 
1076
1123
 
1077
1124
 
1078
- exports.rootEl = rootEl; exports.pointToDom = pointToDom; exports.domToPoint = domToPoint; exports.readSelection = readSelection; exports.writeSelection = writeSelection; exports.elementChildren = elementChildren; exports.focusBlockId = focusBlockId; exports.blockPlainText = blockPlainText; exports.startAiWrite = startAiWrite; exports.precedingPlainText = precedingPlainText; exports.aiInstructionFromQuery = aiInstructionFromQuery; exports.MarkloomAi = MarkloomAi; exports.createAiStream = createAiStream;
1125
+
1126
+
1127
+ exports.rootEl = rootEl; exports.docExtent = docExtent; exports.selectionCovers = selectionCovers; exports.pointToDom = pointToDom; exports.domToPoint = domToPoint; exports.readSelection = readSelection; exports.writeSelection = writeSelection; exports.elementChildren = elementChildren; exports.focusBlockId = focusBlockId; exports.blockPlainText = blockPlainText; exports.startAiWrite = startAiWrite; exports.precedingPlainText = precedingPlainText; exports.aiInstructionFromQuery = aiInstructionFromQuery; exports.MarkloomAi = MarkloomAi; exports.createAiStream = createAiStream;