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.
- package/README.md +29 -16
- package/ai.md +160 -0
- package/collab.md +68 -0
- package/dist/ai/index.cjs +4 -3
- package/dist/ai/index.d.cts +4 -2
- package/dist/ai/index.d.ts +4 -2
- package/dist/ai/index.js +3 -2
- package/dist/{chunk-VVFIQLVR.cjs → chunk-4TMHVNL6.cjs} +71 -79
- package/dist/chunk-6DCI7AL3.js +208 -0
- package/dist/chunk-AC5E42NH.cjs +208 -0
- package/dist/chunk-GQ2HHRGI.cjs +16 -0
- package/dist/{chunk-QXJHFQLJ.js → chunk-JMOJU5EU.js} +4 -2
- package/dist/chunk-JS527FDV.cjs +294 -0
- package/dist/chunk-LA5SBCGQ.js +16 -0
- package/dist/{chunk-ITVAR2UM.js → chunk-LEYAQNJA.js} +5 -3
- package/dist/{chunk-J3QWW6CN.js → chunk-MFYF2A6U.js} +5 -13
- package/dist/chunk-RLTHVQNQ.js +294 -0
- package/dist/{chunk-LNSKDJMF.cjs → chunk-SLRD6FMP.cjs} +27 -25
- package/dist/{chunk-VMUJM52A.js → chunk-WUHTCCAE.js} +65 -6
- package/dist/{chunk-VSTOMEU2.cjs → chunk-YJAWPRJW.cjs} +400 -341
- package/dist/{chunk-HA6XHJUD.cjs → chunk-YXQDCW4Y.cjs} +22 -20
- package/dist/client-A0ZQJnyR.d.ts +137 -0
- package/dist/client-BNams4wv.d.cts +137 -0
- package/dist/collab/index.cjs +19 -0
- package/dist/collab/index.d.cts +3 -0
- package/dist/collab/index.d.ts +3 -0
- package/dist/collab/index.js +19 -0
- package/dist/comments-DlExS6f-.d.cts +43 -0
- package/dist/comments-PhnR7Gkn.d.ts +43 -0
- package/dist/{controller-B_wG2Wh-.d.ts → controller-BkY1VPzc.d.ts} +17 -1
- package/dist/{controller-Cd1Dprrk.d.cts → controller-CVIZI--A.d.cts} +17 -1
- package/dist/index.cjs +21 -243
- package/dist/index.d.cts +5 -7
- package/dist/index.d.ts +5 -7
- package/dist/index.js +22 -244
- package/dist/react/index.cjs +8 -5
- package/dist/react/index.d.cts +4 -2
- package/dist/react/index.d.ts +4 -2
- package/dist/react/index.js +7 -4
- package/dist/rebase-batch-8rL_aXEE.d.ts +38 -0
- package/dist/rebase-batch-bYnBJpRO.d.cts +38 -0
- package/dist/step-BrvHIK7O.d.cts +65 -0
- package/dist/step-BrvHIK7O.d.ts +65 -0
- package/dist/view/index.cjs +11 -8
- package/dist/view/index.d.cts +5 -3
- package/dist/view/index.d.ts +5 -3
- package/dist/view/index.js +7 -4
- package/package.json +10 -3
- package/dist/comments-USjT1wYN.d.cts +0 -105
- package/dist/comments-USjT1wYN.d.ts +0 -105
|
@@ -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
|
-
|
|
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) {
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
isElement,
|
|
3
|
+
isInlineAtom,
|
|
4
|
+
isText
|
|
5
|
+
} from "./chunk-LA5SBCGQ.js";
|
|
11
6
|
|
|
12
7
|
// src/tree/addr.ts
|
|
13
8
|
function resolve(doc, path) {
|
|
@@ -2240,9 +2235,6 @@ function deepEqual(a, b) {
|
|
|
2240
2235
|
}
|
|
2241
2236
|
|
|
2242
2237
|
export {
|
|
2243
|
-
isText,
|
|
2244
|
-
isElement,
|
|
2245
|
-
isInlineAtom,
|
|
2246
2238
|
resolve,
|
|
2247
2239
|
firstLeafPath,
|
|
2248
2240
|
normalizeToLeaf,
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isElement,
|
|
3
|
+
isText
|
|
4
|
+
} from "./chunk-LA5SBCGQ.js";
|
|
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 && (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 && (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 (!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);
|
|
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
|
+
export {
|
|
285
|
+
mapPosition,
|
|
286
|
+
mapSelection,
|
|
287
|
+
normalizePosition,
|
|
288
|
+
makeBias,
|
|
289
|
+
rebase,
|
|
290
|
+
rebaseOne,
|
|
291
|
+
rebaseBatchOver,
|
|
292
|
+
rebaseStepsThrough,
|
|
293
|
+
transformRemoteAgainstPending
|
|
294
|
+
};
|