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
@@ -0,0 +1,255 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;
2
+
3
+ var _chunkJS527FDVcjs = require('./chunk-JS527FDV.cjs');
4
+
5
+ // src/collab/colors.ts
6
+ var COLLAB_COLORS = [
7
+ "#3370ff",
8
+ "#f54a45",
9
+ "#ff8800",
10
+ "#00b578",
11
+ "#7b61ff",
12
+ "#e13edb",
13
+ "#14c0ff",
14
+ "#8f959e",
15
+ "#d83931",
16
+ "#2ea121"
17
+ ];
18
+ function collabColor(siteId, explicit) {
19
+ if (explicit)
20
+ return explicit;
21
+ const i = Math.abs((siteId || 1) - 1) % COLLAB_COLORS.length;
22
+ return COLLAB_COLORS[i];
23
+ }
24
+ function collabInitial(name, siteId) {
25
+ const n = (name || "").trim();
26
+ if (n) {
27
+ const ch = n[0];
28
+ return /[a-z]/i.test(ch) ? ch.toUpperCase() : ch;
29
+ }
30
+ return String(_nullishCoalesce(siteId, () => ( "?")));
31
+ }
32
+
33
+ // src/collab/client.ts
34
+ function toWsUrl(url, room) {
35
+ let u = url.replace(/\/+$/, "");
36
+ if (u.startsWith("https://"))
37
+ u = `wss://${u.slice(8)}`;
38
+ else if (u.startsWith("http://"))
39
+ u = `ws://${u.slice(7)}`;
40
+ return `${u}/room/${encodeURIComponent(room)}`;
41
+ }
42
+ function retryDelay(attempt) {
43
+ const base = Math.min(1e3 * 2 ** Math.min(attempt, 5), 3e4);
44
+ const jitter = base * 0.5 * Math.random();
45
+ return base * 0.75 + jitter;
46
+ }
47
+ var CollabClient = (_class = class {
48
+
49
+
50
+ __init() {this.ws = null}
51
+ __init2() {this.closed = false}
52
+ __init3() {this.retry = 0}
53
+ __init4() {this.reconnectTimer = null}
54
+ __init5() {this.siteId = 0}
55
+ __init6() {this.version = 0}
56
+ __init7() {this.clientSeq = 0}
57
+ __init8() {this.pending = []}
58
+ __init9() {this.peers = /* @__PURE__ */ new Map()}
59
+ __init10() {this.joined = false}
60
+ constructor(cfg, handlers) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);_class.prototype.__init7.call(this);_class.prototype.__init8.call(this);_class.prototype.__init9.call(this);_class.prototype.__init10.call(this);
61
+ this.cfg = cfg;
62
+ this.handlers = handlers;
63
+ this.connect();
64
+ }
65
+ get peersList() {
66
+ return [...this.peers.values()];
67
+ }
68
+ connect() {
69
+ if (this.closed)
70
+ return;
71
+ _optionalChain([this, 'access', _ => _.handlers, 'access', _2 => _2.onStatus, 'optionalCall', _3 => _3("connecting")]);
72
+ const url = toWsUrl(this.cfg.url, this.cfg.room);
73
+ let ws;
74
+ try {
75
+ ws = new WebSocket(url);
76
+ } catch (e) {
77
+ _optionalChain([this, 'access', _4 => _4.handlers, 'access', _5 => _5.onError, 'optionalCall', _6 => _6(e instanceof Error ? e.message : String(e))]);
78
+ this.scheduleReconnect();
79
+ return;
80
+ }
81
+ this.ws = ws;
82
+ ws.onopen = () => {
83
+ this.retry = 0;
84
+ _optionalChain([this, 'access', _7 => _7.handlers, 'access', _8 => _8.onStatus, 'optionalCall', _9 => _9("open")]);
85
+ this.send({
86
+ type: "join",
87
+ room: this.cfg.room,
88
+ name: this.cfg.name,
89
+ color: this.cfg.color,
90
+ seed: this.cfg.seed
91
+ });
92
+ };
93
+ ws.onclose = () => {
94
+ this.joined = false;
95
+ _optionalChain([this, 'access', _10 => _10.handlers, 'access', _11 => _11.onStatus, 'optionalCall', _12 => _12("closed")]);
96
+ if (!this.closed)
97
+ this.scheduleReconnect();
98
+ };
99
+ ws.onerror = () => {
100
+ try {
101
+ ws.close();
102
+ } catch (e2) {
103
+ }
104
+ };
105
+ ws.onmessage = (ev) => {
106
+ try {
107
+ const msg = JSON.parse(String(ev.data));
108
+ this.onServer(msg);
109
+ } catch (e3) {
110
+ }
111
+ };
112
+ }
113
+ scheduleReconnect() {
114
+ if (this.reconnectTimer || this.closed)
115
+ return;
116
+ const delay = retryDelay(this.retry++);
117
+ this.reconnectTimer = setTimeout(() => {
118
+ this.reconnectTimer = null;
119
+ this.connect();
120
+ }, delay);
121
+ }
122
+ send(msg) {
123
+ if (_optionalChain([this, 'access', _13 => _13.ws, 'optionalAccess', _14 => _14.readyState]) === WebSocket.OPEN)
124
+ this.ws.send(JSON.stringify(msg));
125
+ }
126
+ onServer(msg) {
127
+ switch (msg.type) {
128
+ case "welcome": {
129
+ this.siteId = msg.siteId;
130
+ this.version = msg.version;
131
+ this.pending = [];
132
+ this.joined = true;
133
+ if (!this.cfg.color)
134
+ this.cfg.color = collabColor(msg.siteId);
135
+ this.peers.clear();
136
+ for (const p of msg.peers) {
137
+ this.peers.set(p.siteId, {
138
+ ...p,
139
+ color: collabColor(p.siteId, p.color)
140
+ });
141
+ }
142
+ _optionalChain([this, 'access', _15 => _15.handlers, 'access', _16 => _16.onWelcome, 'optionalCall', _17 => _17({
143
+ siteId: msg.siteId,
144
+ version: msg.version,
145
+ name: this.cfg.name,
146
+ color: this.cfg.color
147
+ })]);
148
+ this.handlers.onDoc(msg.doc, msg.version);
149
+ _optionalChain([this, 'access', _18 => _18.handlers, 'access', _19 => _19.onPeers, 'optionalCall', _20 => _20(this.peersList)]);
150
+ break;
151
+ }
152
+ case "resync": {
153
+ this.version = msg.version;
154
+ this.pending = [];
155
+ this.handlers.onDoc(msg.doc, msg.version);
156
+ break;
157
+ }
158
+ case "op": {
159
+ if (!this.joined)
160
+ return;
161
+ if (msg.siteId === this.siteId) {
162
+ if (msg.clientSeq != null)
163
+ this.pending = this.pending.filter((p) => p.clientSeq > msg.clientSeq);
164
+ this.version = msg.version;
165
+ return;
166
+ }
167
+ const pendingAs = this.pending.map((p) => ({ siteId: this.siteId, steps: p.steps }));
168
+ const { remote, pending: nextP } = _chunkJS527FDVcjs.transformRemoteAgainstPending.call(void 0,
169
+ msg.steps,
170
+ msg.siteId,
171
+ pendingAs,
172
+ this.siteId
173
+ );
174
+ this.pending = this.pending.map((p, i) => ({
175
+ ...p,
176
+ steps: _nullishCoalesce(_optionalChain([nextP, 'access', _21 => _21[i], 'optionalAccess', _22 => _22.steps]), () => ( p.steps))
177
+ }));
178
+ this.version = msg.version;
179
+ if (remote.length)
180
+ this.handlers.onRemote(remote);
181
+ break;
182
+ }
183
+ case "presence": {
184
+ const cur = _nullishCoalesce(this.peers.get(msg.siteId), () => ( { siteId: msg.siteId }));
185
+ this.peers.set(msg.siteId, {
186
+ ...cur,
187
+ name: _nullishCoalesce(msg.name, () => ( cur.name)),
188
+ color: collabColor(msg.siteId, _nullishCoalesce(msg.color, () => ( cur.color))),
189
+ sel: msg.sel === void 0 ? cur.sel : msg.sel
190
+ });
191
+ _optionalChain([this, 'access', _23 => _23.handlers, 'access', _24 => _24.onPeers, 'optionalCall', _25 => _25(this.peersList)]);
192
+ break;
193
+ }
194
+ case "peer-join": {
195
+ this.peers.set(msg.siteId, {
196
+ siteId: msg.siteId,
197
+ name: msg.name,
198
+ color: collabColor(msg.siteId, msg.color)
199
+ });
200
+ _optionalChain([this, 'access', _26 => _26.handlers, 'access', _27 => _27.onPeers, 'optionalCall', _28 => _28(this.peersList)]);
201
+ break;
202
+ }
203
+ case "peer-leave": {
204
+ this.peers.delete(msg.siteId);
205
+ _optionalChain([this, 'access', _29 => _29.handlers, 'access', _30 => _30.onPeers, 'optionalCall', _31 => _31(this.peersList)]);
206
+ break;
207
+ }
208
+ case "error":
209
+ _optionalChain([this, 'access', _32 => _32.handlers, 'access', _33 => _33.onError, 'optionalCall', _34 => _34(msg.message)]);
210
+ break;
211
+ case "pong":
212
+ break;
213
+ }
214
+ }
215
+ /** 本地已 apply 的 steps 推上服务器 */
216
+ push(steps) {
217
+ if (!steps.length || !this.joined || this.siteId <= 0)
218
+ return;
219
+ const clientSeq = ++this.clientSeq;
220
+ const baseVersion = this.version;
221
+ this.pending.push({ clientSeq, baseVersion, steps });
222
+ this.send({ type: "op", baseVersion, clientSeq, steps });
223
+ }
224
+ presence(sel) {
225
+ if (!this.joined)
226
+ return;
227
+ this.send({
228
+ type: "presence",
229
+ name: this.cfg.name,
230
+ color: _nullishCoalesce(this.cfg.color, () => ( collabColor(this.siteId))),
231
+ sel
232
+ });
233
+ }
234
+ close() {
235
+ this.closed = true;
236
+ if (this.reconnectTimer)
237
+ clearTimeout(this.reconnectTimer);
238
+ try {
239
+ _optionalChain([this, 'access', _35 => _35.ws, 'optionalAccess', _36 => _36.close, 'call', _37 => _37()]);
240
+ } catch (e4) {
241
+ }
242
+ this.ws = null;
243
+ }
244
+ }, _class);
245
+ function connectCollab(cfg, handlers) {
246
+ return new CollabClient(cfg, handlers);
247
+ }
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+ exports.COLLAB_COLORS = COLLAB_COLORS; exports.collabColor = collabColor; exports.collabInitial = collabInitial; exports.CollabClient = CollabClient; exports.connectCollab = connectCollab;
@@ -1,12 +1,14 @@
1
1
  import {
2
2
  appendText,
3
3
  insertAfter,
4
- isText,
5
4
  mdToDoc,
6
5
  normalizeToLeaf,
7
6
  pathOfId,
8
7
  rewriteNode
9
- } from "./chunk-J3QWW6CN.js";
8
+ } from "./chunk-MFYF2A6U.js";
9
+ import {
10
+ isText
11
+ } from "./chunk-LA5SBCGQ.js";
10
12
 
11
13
  // src/view/selection.ts
12
14
  function rootEl(root) {
@@ -658,7 +660,7 @@ var MarkloomAi = class {
658
660
  this.txOpen = false;
659
661
  this.txBlockId = null;
660
662
  }
661
- /** 整笔撤回事务(= undo) */
663
+ /** 整笔撤回事务(= undo);务必不留 aiDraft 孤儿段 */
662
664
  discard(blockId) {
663
665
  if (this.streamingId === blockId) {
664
666
  this.abort?.abort();
@@ -670,16 +672,21 @@ var MarkloomAi = class {
670
672
  }
671
673
  if (this.txOpen && (this.txBlockId === blockId || !this.txBlockId)) {
672
674
  this.rollback();
675
+ this.purgeDrafts(blockId);
673
676
  return;
674
677
  }
678
+ this.purgeDrafts(blockId, true);
679
+ }
680
+ /**
681
+ * 清掉 aiFrom=blockId 的顶层 draft;shell=true 时把 ai 壳也换成空段。
682
+ * discard / 异常恢复共用。
683
+ */
684
+ purgeDrafts(blockId, shell = false) {
675
685
  const ed = this.getEditor();
676
686
  if (!ed)
677
687
  return;
678
- const found = this.findAi(ed, blockId);
679
- if (!found)
680
- return;
681
- const steps = [];
682
688
  const doc = ed.doc;
689
+ const steps = [];
683
690
  const idxs = draftIndices(doc, blockId);
684
691
  for (let i = idxs.length - 1; i >= 0; i--)
685
692
  steps.push({ kind: "spliceChildren", path: [], at: idxs[i], remove: 1, insert: [] });
@@ -690,19 +697,20 @@ var MarkloomAi = class {
690
697
  shift++;
691
698
  }
692
699
  aiIdx -= shift;
693
- if (aiIdx >= 0) {
700
+ if (shell && aiIdx >= 0) {
694
701
  steps.push({
695
702
  kind: "replaceNode",
696
703
  path: [aiIdx],
697
- node: { type: "paragraph", id: found.node.id, children: [{ text: "" }] }
698
- });
699
- }
700
- if (steps.length) {
701
- ed.dispatch(steps, {
702
- anchor: { path: [Math.max(0, aiIdx), 0, 0] },
703
- head: { path: [Math.max(0, aiIdx), 0, 0] }
704
+ node: { type: "paragraph", id: blockId, children: [{ text: "" }] }
704
705
  });
705
706
  }
707
+ if (!steps.length)
708
+ return;
709
+ const caretAt = Math.max(0, aiIdx >= 0 ? aiIdx : idxs[0] ?? 0);
710
+ ed.dispatch(steps, {
711
+ anchor: { path: [caretAt, 0, 0] },
712
+ head: { path: [caretAt, 0, 0] }
713
+ });
706
714
  }
707
715
  act(blockId, action) {
708
716
  if (action === "accept")
@@ -0,0 +1,16 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/tree/types.ts
2
+ function isText(n) {
3
+ return "text" in n;
4
+ }
5
+ function isElement(n) {
6
+ return "children" in n;
7
+ }
8
+ function isInlineAtom(n) {
9
+ return isElement(n) && (n.type === "mention" || n.type === "hard_break" || n.type === "math");
10
+ }
11
+
12
+
13
+
14
+
15
+
16
+ exports.isText = isText; exports.isElement = isElement; exports.isInlineAtom = isInlineAtom;
@@ -0,0 +1,294 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
+
3
+
4
+ var _chunkGQ2HHRGIcjs = require('./chunk-GQ2HHRGI.cjs');
5
+
6
+ // src/tree/map.ts
7
+ function mapPosition(step, pos) {
8
+ const p = pos.path;
9
+ switch (step.kind) {
10
+ case "insertText":
11
+ return mapText(step.path, step.offset, step.text.length, p, pos);
12
+ case "deleteText":
13
+ return mapText(step.path, step.offset, -step.length, p, pos);
14
+ case "spliceChildren":
15
+ return mapSplice(step.path, step.at, step.remove, step.insert.length, p, pos);
16
+ case "replaceNode":
17
+ return underPrefix(step.path, p) ? withPath(step.path.concat(0), pos) : pos;
18
+ }
19
+ }
20
+ function mapSelection(step, sel) {
21
+ return { anchor: mapPosition(step, sel.anchor), head: mapPosition(step, sel.head) };
22
+ }
23
+ function mapText(stepPath, at, delta, p, pos) {
24
+ if (p.length !== stepPath.length + 1 || !isPrefix(stepPath, p))
25
+ return pos;
26
+ const off = p[p.length - 1];
27
+ if (off < at)
28
+ return pos;
29
+ if (off === at) {
30
+ if (delta > 0 && (_nullishCoalesce(pos.assoc, () => ( 1))) === 1)
31
+ return withLast(p, off + delta, pos);
32
+ return pos;
33
+ }
34
+ if (delta < 0 && off < at - delta)
35
+ return withLast(p, at, pos);
36
+ return withLast(p, off + delta, pos);
37
+ }
38
+ function mapSplice(parentPath, at, remove, insertCount, p, pos) {
39
+ const d = parentPath.length;
40
+ if (!isPrefix(parentPath, p) || p.length <= d)
41
+ return pos;
42
+ const idx = p[d];
43
+ const delta = insertCount - remove;
44
+ if (idx < at)
45
+ return pos;
46
+ if (idx < at + remove)
47
+ return withPath(parentPath.concat(at), pos);
48
+ if (idx === at) {
49
+ if (insertCount > 0 && (_nullishCoalesce(pos.assoc, () => ( 1))) === 1)
50
+ return withSeg(p, d, idx + insertCount, pos);
51
+ return pos;
52
+ }
53
+ return withSeg(p, d, idx + delta, pos);
54
+ }
55
+ function withPath(path, src) {
56
+ return src.assoc === void 0 ? { path } : { path, assoc: src.assoc };
57
+ }
58
+ function withLast(path, last, src) {
59
+ return withPath(path.slice(0, -1).concat(last), src);
60
+ }
61
+ function withSeg(path, d, seg, src) {
62
+ const next = path.slice();
63
+ next[d] = seg;
64
+ return withPath(next, src);
65
+ }
66
+ function isPrefix(a, b) {
67
+ if (a.length > b.length)
68
+ return false;
69
+ for (let i = 0; i < a.length; i++) {
70
+ if (a[i] !== b[i])
71
+ return false;
72
+ }
73
+ return true;
74
+ }
75
+ function underPrefix(a, b) {
76
+ return b.length > a.length && isPrefix(a, b);
77
+ }
78
+ 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);
111
+ }
112
+
113
+ // src/tree/rebase.ts
114
+ function makeBias(thisSite, overSite) {
115
+ return thisSite > overSite ? 1 : -1;
116
+ }
117
+ function rebase(over, step, bias) {
118
+ switch (step.kind) {
119
+ case "insertText":
120
+ case "deleteText": {
121
+ if ((over.kind === "insertText" || over.kind === "deleteText") && samePath(over.path, step.path))
122
+ return textVsText(over, step, bias);
123
+ const want = step.path.length + 1;
124
+ const mlo = movePoint(over, step.path.concat(step.offset), bias);
125
+ if (mlo.length < want)
126
+ return null;
127
+ if (step.kind === "insertText")
128
+ return { ...step, path: mlo.slice(0, -1), offset: mlo[mlo.length - 1] };
129
+ const mhi = movePoint(over, step.path.concat(step.offset + step.length), bias);
130
+ if (mhi.length < want)
131
+ return null;
132
+ const a = mlo[mlo.length - 1];
133
+ const b = mhi[mhi.length - 1];
134
+ return { ...step, path: mlo.slice(0, -1), offset: a, length: Math.max(0, b - a) };
135
+ }
136
+ case "spliceChildren": {
137
+ if (over.kind === "spliceChildren" && samePath(over.path, step.path))
138
+ return spliceVsSplice(over, step, bias);
139
+ const want = step.path.length + 1;
140
+ const moved = movePoint(over, step.path.concat(step.at), bias);
141
+ if (moved.length < want)
142
+ return null;
143
+ return { ...step, path: moved.slice(0, -1), at: moved[moved.length - 1] };
144
+ }
145
+ case "replaceNode": {
146
+ if (removesTarget(over, step.path, bias))
147
+ return null;
148
+ return { ...step, path: movePoint(over, step.path, 1) };
149
+ }
150
+ }
151
+ }
152
+ function removesTarget(over, targetPath, bias) {
153
+ if (over.kind === "spliceChildren") {
154
+ const d = over.path.length;
155
+ if (targetPath.length <= d || !isPrefix2(over.path, targetPath))
156
+ return false;
157
+ const idx = targetPath[d];
158
+ return idx >= over.at && idx < over.at + over.remove;
159
+ }
160
+ if (over.kind === "replaceNode") {
161
+ if (isPrefix2(over.path, targetPath)) {
162
+ const sameNode = targetPath.length === over.path.length;
163
+ return sameNode ? bias === 1 : true;
164
+ }
165
+ return false;
166
+ }
167
+ return false;
168
+ }
169
+ function textVsText(over, step, bias) {
170
+ if (over.kind === "insertText") {
171
+ const o = over.offset;
172
+ const ins = over.text.length;
173
+ if (step.kind === "insertText") {
174
+ const x = step.offset;
175
+ const off = x < o ? x : x > o ? x + ins : bias === 1 ? x + ins : x;
176
+ return { ...step, offset: off };
177
+ }
178
+ const s2 = step.offset;
179
+ const e2 = step.offset + step.length;
180
+ if (o <= s2)
181
+ return { ...step, offset: s2 + ins, length: step.length };
182
+ if (o >= e2)
183
+ return { ...step, offset: s2, length: step.length };
184
+ return { ...step, offset: s2, length: step.length + ins };
185
+ }
186
+ const d = over.offset;
187
+ const oLen = over.length;
188
+ if (step.kind === "insertText") {
189
+ const x = step.offset;
190
+ if (x > d && x < d + oLen)
191
+ return null;
192
+ return { ...step, offset: x <= d ? x : x - oLen };
193
+ }
194
+ const s0 = step.offset;
195
+ const e0 = step.offset + step.length;
196
+ const s = s0 <= d ? s0 : s0 >= d + oLen ? s0 - oLen : d;
197
+ const e = e0 <= d ? e0 : e0 >= d + oLen ? e0 - oLen : d;
198
+ const len = Math.max(0, e - s);
199
+ if (len === 0)
200
+ return null;
201
+ return { ...step, offset: s, length: len };
202
+ }
203
+ function spliceVsSplice(over, step, bias) {
204
+ const oa = over.at;
205
+ const oe = over.at + over.remove;
206
+ const oN = over.insert.length;
207
+ const delta = oN - over.remove;
208
+ const sa = step.at;
209
+ const se = step.at + step.remove;
210
+ const out = [];
211
+ const leftRem = Math.max(0, Math.min(se, oa) - sa);
212
+ const rightRem = Math.max(0, se - Math.max(sa, oe));
213
+ if (rightRem > 0)
214
+ out.push({ kind: "spliceChildren", path: step.path, at: Math.max(sa, oe) + delta, remove: rightRem, insert: [] });
215
+ if (leftRem > 0)
216
+ out.push({ kind: "spliceChildren", path: step.path, at: sa, remove: leftRem, insert: [] });
217
+ if (step.insert.length > 0) {
218
+ const insAt = sa === oa ? bias === 1 ? oa + oN : oa : sa < oa ? sa : sa >= oe ? sa + delta : oa + oN;
219
+ out.push({ kind: "spliceChildren", path: step.path, at: insAt, remove: 0, insert: step.insert });
220
+ }
221
+ if (out.length === 0)
222
+ return null;
223
+ if (out.length === 1)
224
+ return out[0];
225
+ out.sort((x, y) => y.at - x.at || x.insert.length - y.insert.length);
226
+ return out;
227
+ }
228
+ function samePath(a, b) {
229
+ return a.length === b.length && a.every((x, i) => x === b[i]);
230
+ }
231
+ function isPrefix2(a, b) {
232
+ if (a.length > b.length)
233
+ return false;
234
+ for (let i = 0; i < a.length; i++) {
235
+ if (a[i] !== b[i])
236
+ return false;
237
+ }
238
+ return true;
239
+ }
240
+ function movePoint(over, path, bias) {
241
+ const pos = { path, assoc: bias };
242
+ return mapPosition(over, pos).path;
243
+ }
244
+
245
+ // src/collab/rebase-batch.ts
246
+ function rebaseOne(over, step, bias) {
247
+ const r = rebase(over, step, bias);
248
+ if (r == null)
249
+ return [];
250
+ return Array.isArray(r) ? r : [r];
251
+ }
252
+ function rebaseBatchOver(over, steps, bias) {
253
+ const out = [];
254
+ for (const s of steps)
255
+ out.push(...rebaseOne(over, s, bias));
256
+ return out;
257
+ }
258
+ function rebaseStepsThrough(steps, concurrent, thisSite) {
259
+ let cur = steps;
260
+ for (const over of concurrent) {
261
+ const bias = makeBias(thisSite, over.siteId);
262
+ for (const o of over.steps)
263
+ cur = rebaseBatchOver(o, cur, bias);
264
+ }
265
+ return cur;
266
+ }
267
+ function transformRemoteAgainstPending(remote, remoteSite, pending, localSite) {
268
+ let r = remote;
269
+ const nextPending = [];
270
+ for (const p of pending) {
271
+ const rBefore = r;
272
+ const biasR = makeBias(remoteSite, localSite);
273
+ for (const ps of p.steps)
274
+ r = rebaseBatchOver(ps, r, biasR);
275
+ const biasP = makeBias(localSite, remoteSite);
276
+ let pSteps = p.steps;
277
+ for (const rs of rBefore)
278
+ pSteps = rebaseBatchOver(rs, pSteps, biasP);
279
+ nextPending.push({ siteId: p.siteId, steps: pSteps });
280
+ }
281
+ return { remote: r, pending: nextPending };
282
+ }
283
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+
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;
@@ -0,0 +1,16 @@
1
+ // src/tree/types.ts
2
+ function isText(n) {
3
+ return "text" in n;
4
+ }
5
+ function isElement(n) {
6
+ return "children" in n;
7
+ }
8
+ function isInlineAtom(n) {
9
+ return isElement(n) && (n.type === "mention" || n.type === "hard_break" || n.type === "math");
10
+ }
11
+
12
+ export {
13
+ isText,
14
+ isElement,
15
+ isInlineAtom
16
+ };
@@ -2,11 +2,13 @@ import {
2
2
  applyAll,
3
3
  checkTable,
4
4
  invertAll,
5
+ validateAll
6
+ } from "./chunk-MFYF2A6U.js";
7
+ import {
5
8
  isElement,
6
9
  isInlineAtom,
7
- isText,
8
- validateAll
9
- } from "./chunk-J3QWW6CN.js";
10
+ isText
11
+ } from "./chunk-LA5SBCGQ.js";
10
12
 
11
13
  // src/tree/history.ts
12
14
  function init(doc, validate) {