markloom 0.5.1 → 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 (73) hide show
  1. package/PACKAGES.md +26 -0
  2. package/README.md +20 -2
  3. package/collab.md +60 -13
  4. package/dist/ai/index.cjs +5 -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 +4 -3
  8. package/dist/ai/theme.css +66 -102
  9. package/dist/{chunk-ZMTLSFLO.js → chunk-4Q7PAR2T.js} +5746 -1341
  10. package/dist/chunk-75EJ4ARB.cjs +642 -0
  11. package/dist/{chunk-JS527FDV.cjs → chunk-BMPCVT3B.cjs} +47 -39
  12. package/dist/{chunk-RLTHVQNQ.js → chunk-EFYT2223.js} +47 -39
  13. package/dist/chunk-HXABDVLZ.cjs +297 -0
  14. package/dist/{chunk-YIFOGP2D.cjs → chunk-I2LDEVLA.cjs} +499 -102
  15. package/dist/chunk-MQLRZQZL.js +642 -0
  16. package/dist/chunk-OVHASRID.cjs +4279 -0
  17. package/dist/chunk-PCSVBPOR.cjs +40 -0
  18. package/dist/{chunk-FCH7LZFI.js → chunk-PSAXWBNM.js} +450 -53
  19. package/dist/chunk-PZRWTLLB.cjs +858 -0
  20. package/dist/chunk-QO7JEZDB.js +4279 -0
  21. package/dist/chunk-WSJUXPAG.js +40 -0
  22. package/dist/{chunk-VXM7Q6P5.cjs → chunk-Y4IXQFWL.cjs} +6026 -1621
  23. package/dist/chunk-YLVSMDIO.js +858 -0
  24. package/dist/chunk-ZL6WT6OY.js +297 -0
  25. package/dist/client-BvvyHjhh.d.ts +250 -0
  26. package/dist/client-Ciy7Fa_k.d.cts +250 -0
  27. package/dist/collab/index.cjs +17 -4
  28. package/dist/collab/index.d.cts +18 -7
  29. package/dist/collab/index.d.ts +18 -7
  30. package/dist/collab/index.js +19 -6
  31. package/dist/{controller-DQITsFhy.d.cts → controller-CHznEhAR.d.cts} +277 -26
  32. package/dist/{controller-BNuuUM3E.d.ts → controller-DLqI8_jm.d.ts} +277 -26
  33. package/dist/index.cjs +119 -7
  34. package/dist/index.d.cts +138 -6
  35. package/dist/index.d.ts +138 -6
  36. package/dist/index.js +130 -18
  37. package/dist/io/index.cjs +1412 -0
  38. package/dist/io/index.d.cts +33 -0
  39. package/dist/io/index.d.ts +33 -0
  40. package/dist/io/index.js +1412 -0
  41. package/dist/react/index.cjs +9 -8
  42. package/dist/react/index.d.cts +5 -4
  43. package/dist/react/index.d.ts +5 -4
  44. package/dist/react/index.js +8 -7
  45. package/dist/{rebase-batch-8rL_aXEE.d.ts → rebase-batch-CSnv8FKp.d.ts} +11 -9
  46. package/dist/{rebase-batch-bYnBJpRO.d.cts → rebase-batch-DO1ygkx9.d.cts} +11 -9
  47. package/dist/types-BkHlmUFL.d.cts +34 -0
  48. package/dist/types-BkHlmUFL.d.ts +34 -0
  49. package/dist/versions-DNFf8zVz.d.cts +51 -0
  50. package/dist/versions-DOkOyh2J.d.ts +51 -0
  51. package/dist/view/index.cjs +259 -17
  52. package/dist/view/index.d.cts +277 -12
  53. package/dist/view/index.d.ts +277 -12
  54. package/dist/view/index.js +256 -14
  55. package/dist/view/theme.css +2093 -1577
  56. package/dist/word-count-BPw9G2Bg.d.ts +130 -0
  57. package/dist/word-count-DNLn1h_t.d.cts +130 -0
  58. package/io.md +38 -0
  59. package/package.json +25 -4
  60. package/dist/chunk-4JSJAHTB.js +0 -255
  61. package/dist/chunk-4TMHVNL6.cjs +0 -2297
  62. package/dist/chunk-ALZ3A5CS.cjs +0 -255
  63. package/dist/chunk-GQ2HHRGI.cjs +0 -16
  64. package/dist/chunk-LA5SBCGQ.js +0 -16
  65. package/dist/chunk-LEYAQNJA.js +0 -220
  66. package/dist/chunk-MFYF2A6U.js +0 -2297
  67. package/dist/chunk-YXQDCW4Y.cjs +0 -220
  68. package/dist/client-6KiH4kGK.d.ts +0 -144
  69. package/dist/client-ZRZj2nkq.d.cts +0 -144
  70. package/dist/comments-DlExS6f-.d.cts +0 -43
  71. package/dist/comments-PhnR7Gkn.d.ts +0 -43
  72. package/dist/step-BrvHIK7O.d.cts +0 -65
  73. package/dist/step-BrvHIK7O.d.ts +0 -65
@@ -1,7 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
2
 
3
-
4
- var _chunkGQ2HHRGIcjs = require('./chunk-GQ2HHRGI.cjs');
3
+ var _chunkHXABDVLZcjs = require('./chunk-HXABDVLZ.cjs');
5
4
 
6
5
  // src/tree/map.ts
7
6
  function mapPosition(step, pos) {
@@ -20,6 +19,19 @@ function mapPosition(step, pos) {
20
19
  function mapSelection(step, sel) {
21
20
  return { anchor: mapPosition(step, sel.anchor), head: mapPosition(step, sel.head) };
22
21
  }
22
+ function mapSelectionThrough(sel, steps) {
23
+ if (!sel)
24
+ return void 0;
25
+ let s = sel;
26
+ for (const st of steps)
27
+ s = mapSelection(st, s);
28
+ return s;
29
+ }
30
+ function resolveTxAfter(before, after, steps, gaps = []) {
31
+ if (after)
32
+ return _nullishCoalesce(mapSelectionThrough(after, gaps), () => ( after));
33
+ return mapSelectionThrough(before, [...steps, ...gaps]);
34
+ }
23
35
  function mapText(stepPath, at, delta, p, pos) {
24
36
  if (p.length !== stepPath.length + 1 || !isPrefix(stepPath, p))
25
37
  return pos;
@@ -76,38 +88,7 @@ function underPrefix(a, b) {
76
88
  return b.length > a.length && isPrefix(a, b);
77
89
  }
78
90
  function normalizePosition(doc, pos) {
79
- let cur = doc;
80
- for (let i = 0; i < pos.path.length - 1; i++) {
81
- if (!_chunkGQ2HHRGIcjs.isElement.call(void 0, cur))
82
- break;
83
- const next = cur.children[pos.path[i]];
84
- if (!next)
85
- return pos;
86
- cur = next;
87
- }
88
- if (_chunkGQ2HHRGIcjs.isText.call(void 0, cur))
89
- return pos;
90
- const at = _nullishCoalesce(pos.path[pos.path.length - 1], () => ( 0));
91
- const prefix = pos.path.slice(0, -1);
92
- return withPath(descendToText(cur, at, prefix), pos);
93
- }
94
- function descendToText(parent, at, prefix) {
95
- const kids = parent.children;
96
- if (kids.length === 0)
97
- return prefix.concat(0);
98
- const leftIdx = at > 0 ? at - 1 : 0;
99
- const toEnd = at > 0;
100
- let path = prefix.concat(leftIdx);
101
- let node = kids[Math.min(leftIdx, kids.length - 1)];
102
- while (_chunkGQ2HHRGIcjs.isElement.call(void 0, node)) {
103
- const cs = node.children;
104
- if (cs.length === 0)
105
- return path.concat(0);
106
- const idx = toEnd ? cs.length - 1 : 0;
107
- path = path.concat(idx);
108
- node = cs[idx];
109
- }
110
- return path.concat(toEnd ? node.text.length : 0);
91
+ return withPath(_chunkHXABDVLZcjs.normalizeToLeaf.call(void 0, doc, pos.path), pos);
111
92
  }
112
93
 
113
94
  // src/tree/rebase.ts
@@ -255,12 +236,36 @@ function rebaseBatchOver(over, steps, bias) {
255
236
  out.push(...rebaseOne(over, s, bias));
256
237
  return out;
257
238
  }
239
+ function transformSeqThroughOne(seq, over, bias, biasOpp) {
240
+ if (seq.length === 0)
241
+ return [];
242
+ const out = [];
243
+ let oCur = [over];
244
+ for (const s of seq) {
245
+ let sMapped = [s];
246
+ for (const o of oCur)
247
+ sMapped = rebaseBatchOver(o, sMapped, bias);
248
+ out.push(...sMapped);
249
+ let nextO = [];
250
+ for (const o of oCur)
251
+ nextO.push(...rebaseOne(s, o, biasOpp));
252
+ oCur = nextO;
253
+ for (const sm of sMapped) {
254
+ nextO = [];
255
+ for (const o of oCur)
256
+ nextO.push(...rebaseOne(sm, o, biasOpp));
257
+ oCur = nextO;
258
+ }
259
+ }
260
+ return out;
261
+ }
258
262
  function rebaseStepsThrough(steps, concurrent, thisSite) {
259
263
  let cur = steps;
260
264
  for (const over of concurrent) {
261
265
  const bias = makeBias(thisSite, over.siteId);
266
+ const biasOpp = makeBias(over.siteId, thisSite);
262
267
  for (const o of over.steps)
263
- cur = rebaseBatchOver(o, cur, bias);
268
+ cur = transformSeqThroughOne(cur, o, bias, biasOpp);
264
269
  }
265
270
  return cur;
266
271
  }
@@ -270,12 +275,12 @@ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
270
275
  for (const p of pending) {
271
276
  const rBefore = r;
272
277
  const biasR = makeBias(remoteSite, localSite);
278
+ const biasL = makeBias(localSite, remoteSite);
273
279
  for (const ps of p.steps)
274
- r = rebaseBatchOver(ps, r, biasR);
275
- const biasP = makeBias(localSite, remoteSite);
280
+ r = transformSeqThroughOne(r, ps, biasR, biasL);
276
281
  let pSteps = p.steps;
277
282
  for (const rs of rBefore)
278
- pSteps = rebaseBatchOver(rs, pSteps, biasP);
283
+ pSteps = transformSeqThroughOne(pSteps, rs, biasL, biasR);
279
284
  nextPending.push({ siteId: p.siteId, steps: pSteps });
280
285
  }
281
286
  return { remote: r, pending: nextPending };
@@ -291,4 +296,7 @@ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
291
296
 
292
297
 
293
298
 
294
- exports.mapPosition = mapPosition; exports.mapSelection = mapSelection; exports.normalizePosition = normalizePosition; exports.makeBias = makeBias; exports.rebase = rebase; exports.rebaseOne = rebaseOne; exports.rebaseBatchOver = rebaseBatchOver; exports.rebaseStepsThrough = rebaseStepsThrough; exports.transformRemoteAgainstPending = transformRemoteAgainstPending;
299
+
300
+
301
+
302
+ exports.mapPosition = mapPosition; exports.mapSelection = mapSelection; exports.mapSelectionThrough = mapSelectionThrough; exports.resolveTxAfter = resolveTxAfter; exports.normalizePosition = normalizePosition; exports.makeBias = makeBias; exports.rebase = rebase; exports.rebaseOne = rebaseOne; exports.rebaseBatchOver = rebaseBatchOver; exports.transformSeqThroughOne = transformSeqThroughOne; exports.rebaseStepsThrough = rebaseStepsThrough; exports.transformRemoteAgainstPending = transformRemoteAgainstPending;
@@ -1,7 +1,6 @@
1
1
  import {
2
- isElement,
3
- isText
4
- } from "./chunk-LA5SBCGQ.js";
2
+ normalizeToLeaf
3
+ } from "./chunk-ZL6WT6OY.js";
5
4
 
6
5
  // src/tree/map.ts
7
6
  function mapPosition(step, pos) {
@@ -20,6 +19,19 @@ function mapPosition(step, pos) {
20
19
  function mapSelection(step, sel) {
21
20
  return { anchor: mapPosition(step, sel.anchor), head: mapPosition(step, sel.head) };
22
21
  }
22
+ function mapSelectionThrough(sel, steps) {
23
+ if (!sel)
24
+ return void 0;
25
+ let s = sel;
26
+ for (const st of steps)
27
+ s = mapSelection(st, s);
28
+ return s;
29
+ }
30
+ function resolveTxAfter(before, after, steps, gaps = []) {
31
+ if (after)
32
+ return mapSelectionThrough(after, gaps) ?? after;
33
+ return mapSelectionThrough(before, [...steps, ...gaps]);
34
+ }
23
35
  function mapText(stepPath, at, delta, p, pos) {
24
36
  if (p.length !== stepPath.length + 1 || !isPrefix(stepPath, p))
25
37
  return pos;
@@ -76,38 +88,7 @@ function underPrefix(a, b) {
76
88
  return b.length > a.length && isPrefix(a, b);
77
89
  }
78
90
  function normalizePosition(doc, pos) {
79
- let cur = doc;
80
- for (let i = 0; i < pos.path.length - 1; i++) {
81
- if (!isElement(cur))
82
- break;
83
- const next = cur.children[pos.path[i]];
84
- if (!next)
85
- return pos;
86
- cur = next;
87
- }
88
- if (isText(cur))
89
- return pos;
90
- const at = pos.path[pos.path.length - 1] ?? 0;
91
- const prefix = pos.path.slice(0, -1);
92
- return withPath(descendToText(cur, at, prefix), pos);
93
- }
94
- function descendToText(parent, at, prefix) {
95
- const kids = parent.children;
96
- if (kids.length === 0)
97
- return prefix.concat(0);
98
- const leftIdx = at > 0 ? at - 1 : 0;
99
- const toEnd = at > 0;
100
- let path = prefix.concat(leftIdx);
101
- let node = kids[Math.min(leftIdx, kids.length - 1)];
102
- while (isElement(node)) {
103
- const cs = node.children;
104
- if (cs.length === 0)
105
- return path.concat(0);
106
- const idx = toEnd ? cs.length - 1 : 0;
107
- path = path.concat(idx);
108
- node = cs[idx];
109
- }
110
- return path.concat(toEnd ? node.text.length : 0);
91
+ return withPath(normalizeToLeaf(doc, pos.path), pos);
111
92
  }
112
93
 
113
94
  // src/tree/rebase.ts
@@ -255,12 +236,36 @@ function rebaseBatchOver(over, steps, bias) {
255
236
  out.push(...rebaseOne(over, s, bias));
256
237
  return out;
257
238
  }
239
+ function transformSeqThroughOne(seq, over, bias, biasOpp) {
240
+ if (seq.length === 0)
241
+ return [];
242
+ const out = [];
243
+ let oCur = [over];
244
+ for (const s of seq) {
245
+ let sMapped = [s];
246
+ for (const o of oCur)
247
+ sMapped = rebaseBatchOver(o, sMapped, bias);
248
+ out.push(...sMapped);
249
+ let nextO = [];
250
+ for (const o of oCur)
251
+ nextO.push(...rebaseOne(s, o, biasOpp));
252
+ oCur = nextO;
253
+ for (const sm of sMapped) {
254
+ nextO = [];
255
+ for (const o of oCur)
256
+ nextO.push(...rebaseOne(sm, o, biasOpp));
257
+ oCur = nextO;
258
+ }
259
+ }
260
+ return out;
261
+ }
258
262
  function rebaseStepsThrough(steps, concurrent, thisSite) {
259
263
  let cur = steps;
260
264
  for (const over of concurrent) {
261
265
  const bias = makeBias(thisSite, over.siteId);
266
+ const biasOpp = makeBias(over.siteId, thisSite);
262
267
  for (const o of over.steps)
263
- cur = rebaseBatchOver(o, cur, bias);
268
+ cur = transformSeqThroughOne(cur, o, bias, biasOpp);
264
269
  }
265
270
  return cur;
266
271
  }
@@ -270,12 +275,12 @@ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
270
275
  for (const p of pending) {
271
276
  const rBefore = r;
272
277
  const biasR = makeBias(remoteSite, localSite);
278
+ const biasL = makeBias(localSite, remoteSite);
273
279
  for (const ps of p.steps)
274
- r = rebaseBatchOver(ps, r, biasR);
275
- const biasP = makeBias(localSite, remoteSite);
280
+ r = transformSeqThroughOne(r, ps, biasR, biasL);
276
281
  let pSteps = p.steps;
277
282
  for (const rs of rBefore)
278
- pSteps = rebaseBatchOver(rs, pSteps, biasP);
283
+ pSteps = transformSeqThroughOne(pSteps, rs, biasL, biasR);
279
284
  nextPending.push({ siteId: p.siteId, steps: pSteps });
280
285
  }
281
286
  return { remote: r, pending: nextPending };
@@ -284,11 +289,14 @@ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
284
289
  export {
285
290
  mapPosition,
286
291
  mapSelection,
292
+ mapSelectionThrough,
293
+ resolveTxAfter,
287
294
  normalizePosition,
288
295
  makeBias,
289
296
  rebase,
290
297
  rebaseOne,
291
298
  rebaseBatchOver,
299
+ transformSeqThroughOne,
292
300
  rebaseStepsThrough,
293
301
  transformRemoteAgainstPending
294
302
  };
@@ -0,0 +1,297 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
+
3
+
4
+
5
+ var _chunkPCSVBPORcjs = require('./chunk-PCSVBPOR.cjs');
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 (!_chunkPCSVBPORcjs.isElement.call(void 0, 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 (_chunkPCSVBPORcjs.isInlineAtom.call(void 0, c))
31
+ continue;
32
+ if (_chunkPCSVBPORcjs.isText.call(void 0, c))
33
+ return [i];
34
+ return [i, ...firstLeafPath(c)];
35
+ }
36
+ return [0];
37
+ }
38
+ function absorbsAfter(n) {
39
+ if (_chunkPCSVBPORcjs.isText.call(void 0, 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 || _chunkPCSVBPORcjs.isText.call(void 0, n))
53
+ return n && _chunkPCSVBPORcjs.isText.call(void 0, 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 || _chunkPCSVBPORcjs.isText.call(void 0, n))
61
+ return n && _chunkPCSVBPORcjs.isText.call(void 0, 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 (_chunkPCSVBPORcjs.isText.call(void 0, c))
100
+ return true;
101
+ if (_chunkPCSVBPORcjs.isElement.call(void 0, c) && !_chunkPCSVBPORcjs.isInlineAtom.call(void 0, 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 (_chunkPCSVBPORcjs.isText.call(void 0, cur)) {
113
+ return prefix.concat(Math.max(0, path[i]));
114
+ }
115
+ if (i === path.length - 1) {
116
+ if (_chunkPCSVBPORcjs.isElement.call(void 0, 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 (_chunkPCSVBPORcjs.isElement.call(void 0, 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, _nullishCoalesce(path[i], () => ( 0)), prefix);
133
+ }
134
+ prefix.push(path[i]);
135
+ cur = child;
136
+ }
137
+ if (_chunkPCSVBPORcjs.isText.call(void 0, cur))
138
+ return prefix.concat(0);
139
+ if (_chunkPCSVBPORcjs.isInlineAtom.call(void 0, 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 (_chunkPCSVBPORcjs.isElement.call(void 0, 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 (_chunkPCSVBPORcjs.isElement.call(void 0, 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 (_chunkPCSVBPORcjs.isInlineAtom.call(void 0, last))
157
+ continue;
158
+ if (_chunkPCSVBPORcjs.isText.call(void 0, 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 (!_chunkPCSVBPORcjs.isElement.call(void 0, 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 (_chunkPCSVBPORcjs.isText.call(void 0, 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 (!_chunkPCSVBPORcjs.isElement.call(void 0, 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 (_chunkPCSVBPORcjs.isText.call(void 0, r))
197
+ throw new Error("update \u4E0D\u80FD\u628A\u5143\u7D20\u6362\u6210\u6587\u672C\u53F6");
198
+ return r;
199
+ }
200
+ if (!_chunkPCSVBPORcjs.isElement.call(void 0, 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 (!_chunkPCSVBPORcjs.isElement.call(void 0, 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 (!_chunkPCSVBPORcjs.isText.call(void 0, 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 (!_chunkPCSVBPORcjs.isText.call(void 0, 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
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+ exports.resolve = resolve; exports.firstLeafPath = firstLeafPath; exports.gapToTextPath = gapToTextPath; exports.normalizeToLeaf = normalizeToLeaf; exports.lastLeafEnd = lastLeafEnd; exports.nodeAt = nodeAt; exports.replaceAt = replaceAt; exports.spliceAt = spliceAt; exports.apply = apply; exports.invert = invert; exports.applyAll = applyAll; exports.moveBlock = moveBlock; exports.invertAll = invertAll;