prosemirror-changeset 2.2.0 → 2.3.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/CHANGELOG.md +12 -0
- package/LICENSE +1 -1
- package/README.md +35 -2
- package/dist/index.cjs +120 -252
- package/dist/index.d.cts +156 -0
- package/dist/index.d.ts +42 -4
- package/dist/index.js +29 -16
- package/package.json +5 -4
- package/src/README.md +3 -1
- package/src/change.ts +1 -1
- package/src/changeset.ts +20 -5
- package/src/diff.ts +43 -13
- package/dist/index.es.js +0 -605
- package/dist/index.es.js.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/index.es.js
DELETED
|
@@ -1,605 +0,0 @@
|
|
|
1
|
-
// Convert the given range of a fragment to tokens, where node open
|
|
2
|
-
// tokens are encoded as strings holding the node name, characters as
|
|
3
|
-
// their character code, and node close tokens as -1.
|
|
4
|
-
function tokens(frag, start, end, target) {
|
|
5
|
-
for (var i = 0, off = 0; i < frag.childCount; i++) {
|
|
6
|
-
var child = frag.child(i), endOff = off + child.nodeSize;
|
|
7
|
-
var from = Math.max(off, start), to = Math.min(endOff, end);
|
|
8
|
-
if (from < to) {
|
|
9
|
-
if (child.isText) {
|
|
10
|
-
for (var j = from; j < to; j++) { target.push(child.text.charCodeAt(j - off)); }
|
|
11
|
-
} else if (child.isLeaf) {
|
|
12
|
-
target.push(child.type.name);
|
|
13
|
-
} else {
|
|
14
|
-
if (from == off) { target.push(child.type.name); }
|
|
15
|
-
tokens(child.content, Math.max(off + 1, from) - off - 1, Math.min(endOff - 1, to) - off - 1, target);
|
|
16
|
-
if (to == endOff) { target.push(-1); }
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
off = endOff;
|
|
20
|
-
}
|
|
21
|
-
return target
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// The code below will refuse to compute a diff with more than 5000
|
|
25
|
-
// insertions or deletions, which takes about 300ms to reach on my
|
|
26
|
-
// machine. This is a safeguard against runaway computations.
|
|
27
|
-
var MAX_DIFF_SIZE = 5000;
|
|
28
|
-
|
|
29
|
-
// This obscure mess of constants computes the minimum length of an
|
|
30
|
-
// unchanged range (not at the start/end of the compared content). The
|
|
31
|
-
// idea is to make it higher in bigger replacements, so that you don't
|
|
32
|
-
// get a diff soup of coincidentally identical letters when replacing
|
|
33
|
-
// a paragraph.
|
|
34
|
-
function minUnchanged(sizeA, sizeB) {
|
|
35
|
-
return Math.min(15, Math.max(2, Math.floor(Math.max(sizeA, sizeB) / 10)))
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// : (Fragment, Fragment, Change) → [Change]
|
|
39
|
-
function computeDiff(fragA, fragB, range) {
|
|
40
|
-
var tokA = tokens(fragA, range.fromA, range.toA, []);
|
|
41
|
-
var tokB = tokens(fragB, range.fromB, range.toB, []);
|
|
42
|
-
|
|
43
|
-
// Scan from both sides to cheaply eliminate work
|
|
44
|
-
var start = 0, endA = tokA.length, endB = tokB.length;
|
|
45
|
-
while (start < tokA.length && start < tokB.length && tokA[start] === tokB[start]) { start++; }
|
|
46
|
-
if (start == tokA.length && start == tokB.length) { return [] }
|
|
47
|
-
while (endA > start && endB > start && tokA[endA - 1] === tokB[endB - 1]) { endA--, endB--; }
|
|
48
|
-
// If the result is simple _or_ too big to cheaply compute, return
|
|
49
|
-
// the remaining region as the diff
|
|
50
|
-
if (endA == start || endB == start || (endA == endB && endA == start + 1))
|
|
51
|
-
{ return [range.slice(start, endA, start, endB)] }
|
|
52
|
-
|
|
53
|
-
// This is an implementation of Myers' diff algorithm
|
|
54
|
-
// See https://neil.fraser.name/writing/diff/myers.pdf and
|
|
55
|
-
// https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/
|
|
56
|
-
|
|
57
|
-
var lenA = endA - start, lenB = endB - start;
|
|
58
|
-
var max = Math.min(MAX_DIFF_SIZE, lenA + lenB), off = max + 1;
|
|
59
|
-
var history = [];
|
|
60
|
-
var frontier = [];
|
|
61
|
-
for (var len = off * 2, i = 0; i < len; i++) { frontier[i] = -1; }
|
|
62
|
-
|
|
63
|
-
for (var size = 0; size <= max; size++) {
|
|
64
|
-
for (var diag = -size; diag <= size; diag += 2) {
|
|
65
|
-
var next = frontier[diag + 1 + max], prev = frontier[diag - 1 + max];
|
|
66
|
-
var x = next < prev ? prev : next + 1, y = x + diag;
|
|
67
|
-
while (x < lenA && y < lenB && tokA[start + x] === tokB[start + y]) { x++, y++; }
|
|
68
|
-
frontier[diag + max] = x;
|
|
69
|
-
// Found a match
|
|
70
|
-
if (x >= lenA && y >= lenB) {
|
|
71
|
-
// Trace back through the history to build up a set of changed ranges.
|
|
72
|
-
var diff = [], minSpan = minUnchanged(endA - start, endB - start);
|
|
73
|
-
// Used to add steps to a diff one at a time, back to front, merging
|
|
74
|
-
// ones that are less than minSpan tokens apart
|
|
75
|
-
var fromA = -1, toA = -1, fromB = -1, toB = -1;
|
|
76
|
-
var add = function (fA, tA, fB, tB) {
|
|
77
|
-
if (fromA > -1 && fromA < tA + minSpan) {
|
|
78
|
-
fromA = fA; fromB = fB;
|
|
79
|
-
} else {
|
|
80
|
-
if (fromA > -1)
|
|
81
|
-
{ diff.push(range.slice(fromA, toA, fromB, toB)); }
|
|
82
|
-
fromA = fA; toA = tA;
|
|
83
|
-
fromB = fB; toB = tB;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
for (var i$1 = size - 1; i$1 >= 0; i$1--) {
|
|
88
|
-
var next$1 = frontier[diag + 1 + max], prev$1 = frontier[diag - 1 + max];
|
|
89
|
-
if (next$1 < prev$1) { // Deletion
|
|
90
|
-
diag--;
|
|
91
|
-
x = prev$1 + start; y = x + diag;
|
|
92
|
-
add(x, x, y, y + 1);
|
|
93
|
-
} else { // Insertion
|
|
94
|
-
diag++;
|
|
95
|
-
x = next$1 + start; y = x + diag;
|
|
96
|
-
add(x, x + 1, y, y);
|
|
97
|
-
}
|
|
98
|
-
frontier = history[i$1 >> 1];
|
|
99
|
-
}
|
|
100
|
-
if (fromA > -1) { diff.push(range.slice(fromA, toA, fromB, toB)); }
|
|
101
|
-
return diff.reverse()
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
// Since only either odd or even diagonals are read from each
|
|
105
|
-
// frontier, we only copy them every other iteration.
|
|
106
|
-
if (size % 2 == 0) { history.push(frontier.slice()); }
|
|
107
|
-
}
|
|
108
|
-
// The loop exited, meaning the maximum amount of work was done.
|
|
109
|
-
// Just return a change spanning the entire range.
|
|
110
|
-
return [range.slice(start, endA, start, endB)]
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// ::- Stores metadata for a part of a change.
|
|
114
|
-
var Span = function Span(length, data) {
|
|
115
|
-
// :: number
|
|
116
|
-
this.length = length;
|
|
117
|
-
// :: any
|
|
118
|
-
this.data = data;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
Span.prototype.cut = function cut (length) {
|
|
122
|
-
return length == this.length ? this : new Span(length, this.data)
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
Span.slice = function slice (spans, from, to) {
|
|
126
|
-
if (from == to) { return Span.none }
|
|
127
|
-
if (from == 0 && to == Span.len(spans)) { return spans }
|
|
128
|
-
var result = [];
|
|
129
|
-
for (var i = 0, off = 0; off < to; i++) {
|
|
130
|
-
var span = spans[i], end = off + span.length;
|
|
131
|
-
var overlap = Math.min(to, end) - Math.max(from, off);
|
|
132
|
-
if (overlap > 0) { result.push(span.cut(overlap)); }
|
|
133
|
-
off = end;
|
|
134
|
-
}
|
|
135
|
-
return result
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
Span.join = function join (a, b, combine) {
|
|
139
|
-
if (a.length == 0) { return b }
|
|
140
|
-
if (b.length == 0) { return a }
|
|
141
|
-
var combined = combine(a[a.length - 1].data, b[0].data);
|
|
142
|
-
if (combined == null) { return a.concat(b) }
|
|
143
|
-
var result = a.slice(0, a.length - 1);
|
|
144
|
-
result.push(new Span(a[a.length - 1].length + b[0].length, combined));
|
|
145
|
-
for (var i = 1; i < b.length; i++) { result.push(b[i]); }
|
|
146
|
-
return result
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
Span.len = function len (spans) {
|
|
150
|
-
var len = 0;
|
|
151
|
-
for (var i = 0; i < spans.length; i++) { len += spans[i].length; }
|
|
152
|
-
return len
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
Span.none = [];
|
|
156
|
-
|
|
157
|
-
// ::- A replaced range with metadata associated with it.
|
|
158
|
-
var Change = function Change(fromA, toA, fromB, toB, deleted, inserted) {
|
|
159
|
-
// :: number The start of the range deleted/replaced in the old
|
|
160
|
-
// document.
|
|
161
|
-
this.fromA = fromA;
|
|
162
|
-
// :: number The end of the range in the old document.
|
|
163
|
-
this.toA = toA;
|
|
164
|
-
// :: number The start of the range inserted in the new document.
|
|
165
|
-
this.fromB = fromB;
|
|
166
|
-
// :: number The end of the range in the new document.
|
|
167
|
-
this.toB = toB;
|
|
168
|
-
// :: [Span] Data associated with the deleted content. The length
|
|
169
|
-
// of these spans adds up to `this.toA - this.fromA`.
|
|
170
|
-
this.deleted = deleted;
|
|
171
|
-
// :: [Span] Data associated with the inserted content. Length
|
|
172
|
-
// adds up to `this.toB - this.toA`.
|
|
173
|
-
this.inserted = inserted;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
var prototypeAccessors$1 = { lenA: { configurable: true },lenB: { configurable: true } };
|
|
177
|
-
|
|
178
|
-
prototypeAccessors$1.lenA.get = function () { return this.toA - this.fromA };
|
|
179
|
-
prototypeAccessors$1.lenB.get = function () { return this.toB - this.fromB };
|
|
180
|
-
|
|
181
|
-
Change.prototype.slice = function slice (startA, endA, startB, endB) {
|
|
182
|
-
if (startA == 0 && startB == 0 && endA == this.toA - this.fromA &&
|
|
183
|
-
endB == this.toB - this.fromB) { return this }
|
|
184
|
-
return new Change(this.fromA + startA, this.fromA + endA,
|
|
185
|
-
this.fromB + startB, this.fromB + endB,
|
|
186
|
-
Span.slice(this.deleted, startA, endA),
|
|
187
|
-
Span.slice(this.inserted, startB, endB))
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
// : ([Change], [Change], (any, any) → any) → [Change]
|
|
191
|
-
// This merges two changesets (the end document of x should be the
|
|
192
|
-
// start document of y) into a single one spanning the start of x to
|
|
193
|
-
// the end of y.
|
|
194
|
-
Change.merge = function merge (x, y, combine) {
|
|
195
|
-
if (x.length == 0) { return y }
|
|
196
|
-
if (y.length == 0) { return x }
|
|
197
|
-
|
|
198
|
-
var result = [];
|
|
199
|
-
// Iterate over both sets in parallel, using the middle coordinate
|
|
200
|
-
// system (B in x, A in y) to synchronize.
|
|
201
|
-
for (var iX = 0, iY = 0, curX = x[0], curY = y[0];;) {
|
|
202
|
-
if (!curX && !curY) {
|
|
203
|
-
return result
|
|
204
|
-
} else if (curX && (!curY || curX.toB < curY.fromA)) { // curX entirely in front of curY
|
|
205
|
-
var off = iY ? y[iY - 1].toB - y[iY - 1].toA : 0;
|
|
206
|
-
result.push(off == 0 ? curX :
|
|
207
|
-
new Change(curX.fromA, curX.toA, curX.fromB + off, curX.toB + off,
|
|
208
|
-
curX.deleted, curX.inserted));
|
|
209
|
-
curX = iX++ == x.length ? null : x[iX];
|
|
210
|
-
} else if (curY && (!curX || curY.toA < curX.fromB)) { // curY entirely in front of curX
|
|
211
|
-
var off$1 = iX ? x[iX - 1].toB - x[iX - 1].toA : 0;
|
|
212
|
-
result.push(off$1 == 0 ? curY :
|
|
213
|
-
new Change(curY.fromA - off$1, curY.toA - off$1, curY.fromB, curY.toB,
|
|
214
|
-
curY.deleted, curY.inserted));
|
|
215
|
-
curY = iY++ == y.length ? null : y[iY];
|
|
216
|
-
} else { // Touch, need to merge
|
|
217
|
-
// The rules for merging ranges are that deletions from the
|
|
218
|
-
// old set and insertions from the new are kept. Areas of the
|
|
219
|
-
// middle document covered by a but not by b are insertions
|
|
220
|
-
// from a that need to be added, and areas covered by b but
|
|
221
|
-
// not a are deletions from b that need to be added.
|
|
222
|
-
var pos = Math.min(curX.fromB, curY.fromA);
|
|
223
|
-
var fromA = Math.min(curX.fromA, curY.fromA - (iX ? x[iX - 1].toB - x[iX - 1].toA : 0)), toA = fromA;
|
|
224
|
-
var fromB = Math.min(curY.fromB, curX.fromB + (iY ? y[iY - 1].toB - y[iY - 1].toA : 0)), toB = fromB;
|
|
225
|
-
var deleted = Span.none, inserted = Span.none;
|
|
226
|
-
|
|
227
|
-
// Used to prevent appending ins/del range for the same Change twice
|
|
228
|
-
var enteredX = false, enteredY = false;
|
|
229
|
-
|
|
230
|
-
// Need to have an inner loop since any number of further
|
|
231
|
-
// ranges might be touching this group
|
|
232
|
-
for (;;) {
|
|
233
|
-
var nextX = !curX ? 2e8 : pos >= curX.fromB ? curX.toB : curX.fromB;
|
|
234
|
-
var nextY = !curY ? 2e8 : pos >= curY.fromA ? curY.toA : curY.fromA;
|
|
235
|
-
var next = Math.min(nextX, nextY);
|
|
236
|
-
var inX = curX && pos >= curX.fromB, inY = curY && pos >= curY.fromA;
|
|
237
|
-
if (!inX && !inY) { break }
|
|
238
|
-
if (inX && pos == curX.fromB && !enteredX) {
|
|
239
|
-
deleted = Span.join(deleted, curX.deleted, combine);
|
|
240
|
-
toA += curX.lenA;
|
|
241
|
-
enteredX = true;
|
|
242
|
-
}
|
|
243
|
-
if (inX && !inY) {
|
|
244
|
-
inserted = Span.join(inserted, Span.slice(curX.inserted, pos - curX.fromB, next - curX.fromB), combine);
|
|
245
|
-
toB += next - pos;
|
|
246
|
-
}
|
|
247
|
-
if (inY && pos == curY.fromA && !enteredY) {
|
|
248
|
-
inserted = Span.join(inserted, curY.inserted, combine);
|
|
249
|
-
toB += curY.lenB;
|
|
250
|
-
enteredY = true;
|
|
251
|
-
}
|
|
252
|
-
if (inY && !inX) {
|
|
253
|
-
deleted = Span.join(deleted, Span.slice(curY.deleted, pos - curY.fromA, next - curY.fromA), combine);
|
|
254
|
-
toA += next - pos;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
if (inX && next == curX.toB) {
|
|
258
|
-
curX = iX++ == x.length ? null : x[iX];
|
|
259
|
-
enteredX = false;
|
|
260
|
-
}
|
|
261
|
-
if (inY && next == curY.toA) {
|
|
262
|
-
curY = iY++ == y.length ? null : y[iY];
|
|
263
|
-
enteredY = false;
|
|
264
|
-
}
|
|
265
|
-
pos = next;
|
|
266
|
-
}
|
|
267
|
-
if (fromA < toA || fromB < toB)
|
|
268
|
-
{ result.push(new Change(fromA, toA, fromB, toB, deleted, inserted)); }
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
Object.defineProperties( Change.prototype, prototypeAccessors$1 );
|
|
274
|
-
|
|
275
|
-
var letter;
|
|
276
|
-
// If the runtime support unicode properties in regexps, that's a good
|
|
277
|
-
// source of info on whether something is a letter.
|
|
278
|
-
try { letter = new RegExp("[\\p{Alphabetic}_]", "u"); } catch(_) {}
|
|
279
|
-
|
|
280
|
-
// Otherwise, we see if the character changes when upper/lowercased,
|
|
281
|
-
// or if it is part of these common single-case scripts.
|
|
282
|
-
var nonASCIISingleCaseWordChar = /[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;
|
|
283
|
-
|
|
284
|
-
function isLetter(code) {
|
|
285
|
-
if (code < 128)
|
|
286
|
-
{ return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 79 && code <= 122 }
|
|
287
|
-
var ch = String.fromCharCode(code);
|
|
288
|
-
if (letter) { return letter.test(ch) }
|
|
289
|
-
return ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch)
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
// Convert a range of document into a string, so that we can easily
|
|
293
|
-
// access characters at a given position. Treat non-text tokens as
|
|
294
|
-
// spaces so that they aren't considered part of a word.
|
|
295
|
-
function getText(frag, start, end) {
|
|
296
|
-
var out = "";
|
|
297
|
-
function convert(frag, start, end) {
|
|
298
|
-
for (var i = 0, off = 0; i < frag.childCount; i++) {
|
|
299
|
-
var child = frag.child(i), endOff = off + child.nodeSize;
|
|
300
|
-
var from = Math.max(off, start), to = Math.min(endOff, end);
|
|
301
|
-
if (from < to) {
|
|
302
|
-
if (child.isText) {
|
|
303
|
-
out += child.text.slice(Math.max(0, start - off), Math.min(child.text.length, end - off));
|
|
304
|
-
} else if (child.isLeaf) {
|
|
305
|
-
out += " ";
|
|
306
|
-
} else {
|
|
307
|
-
if (from == off) { out += " "; }
|
|
308
|
-
convert(child.content, Math.max(0, from - off - 1), Math.min(child.content.size, end - off));
|
|
309
|
-
if (to == endOff) { out += " "; }
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
off = endOff;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
convert(frag, start, end);
|
|
316
|
-
return out
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// The distance changes have to be apart for us to not consider them
|
|
320
|
-
// candidates for merging.
|
|
321
|
-
var MAX_SIMPLIFY_DISTANCE = 30;
|
|
322
|
-
|
|
323
|
-
// :: ([Change], Node) → [Change]
|
|
324
|
-
// Simplifies a set of changes for presentation. This makes the
|
|
325
|
-
// assumption that having both insertions and deletions within a word
|
|
326
|
-
// is confusing, and, when such changes occur without a word boundary
|
|
327
|
-
// between them, they should be expanded to cover the entire set of
|
|
328
|
-
// words (in the new document) they touch. An exception is made for
|
|
329
|
-
// single-character replacements.
|
|
330
|
-
function simplifyChanges(changes, doc) {
|
|
331
|
-
var result = [];
|
|
332
|
-
for (var i = 0; i < changes.length; i++) {
|
|
333
|
-
var end = changes[i].toB, start = i;
|
|
334
|
-
while (i < changes.length - 1 && changes[i + 1].fromB <= end + MAX_SIMPLIFY_DISTANCE)
|
|
335
|
-
{ end = changes[++i].toB; }
|
|
336
|
-
simplifyAdjacentChanges(changes, start, i + 1, doc, result);
|
|
337
|
-
}
|
|
338
|
-
return result
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function simplifyAdjacentChanges(changes, from, to, doc, target) {
|
|
342
|
-
var start = Math.max(0, changes[from].fromB - MAX_SIMPLIFY_DISTANCE);
|
|
343
|
-
var end = Math.min(doc.content.size, changes[to - 1].toB + MAX_SIMPLIFY_DISTANCE);
|
|
344
|
-
var text = getText(doc.content, start, end);
|
|
345
|
-
|
|
346
|
-
for (var i = from; i < to; i++) {
|
|
347
|
-
var startI = i, last = changes[i], deleted = last.lenA, inserted = last.lenB;
|
|
348
|
-
while (i < to - 1) {
|
|
349
|
-
var next = changes[i + 1], boundary = false;
|
|
350
|
-
var prevLetter = last.toB == end ? false : isLetter(text.charCodeAt(last.toB - 1 - start));
|
|
351
|
-
for (var pos = last.toB; !boundary && pos < next.fromB; pos++) {
|
|
352
|
-
var nextLetter = pos == end ? false : isLetter(text.charCodeAt(pos - start));
|
|
353
|
-
if ((!prevLetter || !nextLetter) && pos != changes[startI].fromB) { boundary = true; }
|
|
354
|
-
prevLetter = nextLetter;
|
|
355
|
-
}
|
|
356
|
-
if (boundary) { break }
|
|
357
|
-
deleted += next.lenA; inserted += next.lenB;
|
|
358
|
-
last = next;
|
|
359
|
-
i++;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
if (inserted > 0 && deleted > 0 && !(inserted == 1 && deleted == 1)) {
|
|
363
|
-
var from$1 = changes[startI].fromB, to$1 = changes[i].toB;
|
|
364
|
-
if (from$1 < end && isLetter(text.charCodeAt(from$1 - start)))
|
|
365
|
-
{ while (from$1 > start && isLetter(text.charCodeAt(from$1 - 1 - start))) { from$1--; } }
|
|
366
|
-
if (to$1 > start && isLetter(text.charCodeAt(to$1 - 1 - start)))
|
|
367
|
-
{ while (to$1 < end && isLetter(text.charCodeAt(to$1 - start))) { to$1++; } }
|
|
368
|
-
var joined = fillChange(changes.slice(startI, i + 1), from$1, to$1);
|
|
369
|
-
var last$1 = target.length ? target[target.length - 1] : null;
|
|
370
|
-
if (last$1 && last$1.toA == joined.fromA)
|
|
371
|
-
{ target[target.length - 1] = new Change(last$1.fromA, joined.toA, last$1.fromB, joined.toB,
|
|
372
|
-
last$1.deleted.concat(joined.deleted), last$1.inserted.concat(joined.inserted)); }
|
|
373
|
-
else
|
|
374
|
-
{ target.push(joined); }
|
|
375
|
-
} else {
|
|
376
|
-
for (var j = startI; j <= i; j++) { target.push(changes[j]); }
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
return changes
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
function combine(a, b) { return a === b ? a : null }
|
|
383
|
-
|
|
384
|
-
function fillChange(changes, fromB, toB) {
|
|
385
|
-
var fromA = changes[0].fromA - (changes[0].fromB - fromB);
|
|
386
|
-
var last = changes[changes.length - 1];
|
|
387
|
-
var toA = last.toA + (toB - last.toB);
|
|
388
|
-
var deleted = Span.none, inserted = Span.none;
|
|
389
|
-
var delData = (changes[0].deleted.length ? changes[0].deleted : changes[0].inserted)[0].data;
|
|
390
|
-
var insData = (changes[0].inserted.length ? changes[0].inserted : changes[0].deleted)[0].data;
|
|
391
|
-
for (var posA = fromA, posB = fromB, i = 0;; i++) {
|
|
392
|
-
var next = i == changes.length ? null : changes[i];
|
|
393
|
-
var endA = next ? next.fromA : toA, endB = next ? next.fromB : toB;
|
|
394
|
-
if (endA > posA) { deleted = Span.join(deleted, [new Span(endA - posA, delData)], combine); }
|
|
395
|
-
if (endB > posB) { inserted = Span.join(inserted, [new Span(endB - posB, insData)], combine); }
|
|
396
|
-
if (!next) { break }
|
|
397
|
-
deleted = Span.join(deleted, next.deleted, combine);
|
|
398
|
-
inserted = Span.join(inserted, next.inserted, combine);
|
|
399
|
-
if (deleted.length) { delData = deleted[deleted.length - 1].data; }
|
|
400
|
-
if (inserted.length) { insData = inserted[inserted.length - 1].data; }
|
|
401
|
-
posA = next.toA; posB = next.toB;
|
|
402
|
-
}
|
|
403
|
-
return new Change(fromA, toA, fromB, toB, deleted, inserted)
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// ::- A change set tracks the changes to a document from a given
|
|
407
|
-
// point in the past. It condenses a number of step maps down to a
|
|
408
|
-
// flat sequence of replacements, and simplifies replacments that
|
|
409
|
-
// partially undo themselves by comparing their content.
|
|
410
|
-
var ChangeSet = function ChangeSet(config, changes) {
|
|
411
|
-
this.config = config;
|
|
412
|
-
// :: [Change] Replaced regions.
|
|
413
|
-
this.changes = changes;
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
var prototypeAccessors = { startDoc: { configurable: true } };
|
|
417
|
-
|
|
418
|
-
// :: (Node, [StepMap], union<[any], any>) → ChangeSet
|
|
419
|
-
// Computes a new changeset by adding the given step maps and
|
|
420
|
-
// metadata (either as an array, per-map, or as a single value to be
|
|
421
|
-
// associated with all maps) to the current set. Will not mutate the
|
|
422
|
-
// old set.
|
|
423
|
-
//
|
|
424
|
-
// Note that due to simplification that happens after each add,
|
|
425
|
-
// incrementally adding steps might create a different final set
|
|
426
|
-
// than adding all those changes at once, since different document
|
|
427
|
-
// tokens might be matched during simplification depending on the
|
|
428
|
-
// boundaries of the current changed ranges.
|
|
429
|
-
ChangeSet.prototype.addSteps = function addSteps (newDoc, maps, data) {
|
|
430
|
-
var this$1$1 = this;
|
|
431
|
-
|
|
432
|
-
// This works by inspecting the position maps for the changes,
|
|
433
|
-
// which indicate what parts of the document were replaced by new
|
|
434
|
-
// content, and the size of that new content. It uses these to
|
|
435
|
-
// build up Change objects.
|
|
436
|
-
//
|
|
437
|
-
// These change objects are put in sets and merged together using
|
|
438
|
-
// Change.merge, giving us the changes created by the new steps.
|
|
439
|
-
// Those changes can then be merged with the existing set of
|
|
440
|
-
// changes.
|
|
441
|
-
//
|
|
442
|
-
// For each change that was touched by the new steps, we recompute
|
|
443
|
-
// a diff to try to minimize the change by dropping matching
|
|
444
|
-
// pieces of the old and new document from the change.
|
|
445
|
-
|
|
446
|
-
var stepChanges = [];
|
|
447
|
-
// Add spans for new steps.
|
|
448
|
-
var loop = function ( i ) {
|
|
449
|
-
var d = Array.isArray(data) ? data[i] : data;
|
|
450
|
-
var off = 0;
|
|
451
|
-
maps[i].forEach(function (fromA, toA, fromB, toB) {
|
|
452
|
-
|
|
453
|
-
stepChanges.push(new Change(fromA + off, toA + off, fromB, toB,
|
|
454
|
-
fromA == toA ? Span.none : [new Span(toA - fromA, d)],
|
|
455
|
-
fromB == toB ? Span.none : [new Span(toB - fromB, d)]));
|
|
456
|
-
|
|
457
|
-
off = (toB - fromB) - (toA - fromA);
|
|
458
|
-
});
|
|
459
|
-
};
|
|
460
|
-
|
|
461
|
-
for (var i = 0; i < maps.length; i++) loop( i );
|
|
462
|
-
if (stepChanges.length == 0) { return this }
|
|
463
|
-
|
|
464
|
-
var newChanges = mergeAll(stepChanges, this.config.combine);
|
|
465
|
-
var changes = Change.merge(this.changes, newChanges, this.config.combine);
|
|
466
|
-
|
|
467
|
-
// Minimize changes when possible
|
|
468
|
-
var loop$1 = function ( i$2 ) {
|
|
469
|
-
var change = changes[i$2];
|
|
470
|
-
if (change.fromA == change.toA || change.fromB == change.toB ||
|
|
471
|
-
// Only look at changes that touch newly added changed ranges
|
|
472
|
-
!newChanges.some(function (r) { return r.toB > change.fromB && r.fromB < change.toB; })) { return }
|
|
473
|
-
var diff = computeDiff(this$1$1.config.doc.content, newDoc.content, change);
|
|
474
|
-
|
|
475
|
-
// Fast path: If they are completely different, don't do anything
|
|
476
|
-
if (diff.length == 1 && diff[0].fromB == 0 && diff[0].toB == change.toB - change.fromB)
|
|
477
|
-
{ return }
|
|
478
|
-
|
|
479
|
-
if (diff.length == 1) {
|
|
480
|
-
changes[i$2] = diff[0];
|
|
481
|
-
} else {
|
|
482
|
-
changes.splice.apply(changes, [ i$2, 1 ].concat( diff ));
|
|
483
|
-
i$2 += diff.length - 1;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
i$1 = i$2;
|
|
487
|
-
};
|
|
488
|
-
|
|
489
|
-
for (var i$1 = 0; i$1 < changes.length; i$1++) loop$1( i$1 );
|
|
490
|
-
|
|
491
|
-
return new ChangeSet(this.config, changes)
|
|
492
|
-
};
|
|
493
|
-
|
|
494
|
-
// :: Node
|
|
495
|
-
// The starting document of the change set.
|
|
496
|
-
prototypeAccessors.startDoc.get = function () { return this.config.doc };
|
|
497
|
-
|
|
498
|
-
// :: (f: (range: Change) → any) → ChangeSet
|
|
499
|
-
// Map the span's data values in the given set through a function
|
|
500
|
-
// and construct a new set with the resulting data.
|
|
501
|
-
ChangeSet.prototype.map = function map (f) {
|
|
502
|
-
return new ChangeSet(this.config, this.changes.map(function (change) {
|
|
503
|
-
var data = f(change);
|
|
504
|
-
return data === change.data ? change :
|
|
505
|
-
new Change(change.fromA, change.toA, change.fromB, change.toB, data)
|
|
506
|
-
}))
|
|
507
|
-
};
|
|
508
|
-
|
|
509
|
-
// :: (ChangeSet, ?[StepMap]) → ?{from: number, to: number}
|
|
510
|
-
// Compare two changesets and return the range in which they are
|
|
511
|
-
// changed, if any. If the document changed between the maps, pass
|
|
512
|
-
// the maps for the steps that changed it as second argument, and
|
|
513
|
-
// make sure the method is called on the old set and passed the new
|
|
514
|
-
// set. The returned positions will be in new document coordinates.
|
|
515
|
-
ChangeSet.prototype.changedRange = function changedRange (b, maps) {
|
|
516
|
-
if (b == this) { return null }
|
|
517
|
-
var touched = maps && touchedRange(maps);
|
|
518
|
-
var moved = touched ? (touched.toB - touched.fromB) - (touched.toA - touched.fromA) : 0;
|
|
519
|
-
function map(p) {
|
|
520
|
-
return !touched || p <= touched.fromA ? p : p + moved
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
var from = touched ? touched.fromB : 2e8, to = touched ? touched.toB : -2e8;
|
|
524
|
-
function add(start, end) {
|
|
525
|
-
if ( end === void 0 ) end = start;
|
|
526
|
-
|
|
527
|
-
from = Math.min(start, from); to = Math.max(end, to);
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
var rA = this.changes, rB = b.changes;
|
|
531
|
-
for (var iA = 0, iB = 0; iA < rA.length && iB < rB.length;) {
|
|
532
|
-
var rangeA = rA[iA], rangeB = rB[iB];
|
|
533
|
-
if (rangeA && rangeB && sameRanges(rangeA, rangeB, map)) { iA++; iB++; }
|
|
534
|
-
else if (rangeB && (!rangeA || map(rangeA.fromB) >= rangeB.fromB)) { add(rangeB.fromB, rangeB.toB); iB++; }
|
|
535
|
-
else { add(map(rangeA.fromB), map(rangeA.toB)); iA++; }
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
return from <= to ? {from: from, to: to} : null
|
|
539
|
-
};
|
|
540
|
-
|
|
541
|
-
// :: (Node, ?(a: any, b: any) → any) → ChangeSet
|
|
542
|
-
// Create a changeset with the given base object and configuration.
|
|
543
|
-
// The `combine` function is used to compare and combine metadata—it
|
|
544
|
-
// should return null when metadata isn't compatible, and a combined
|
|
545
|
-
// version for a merged range when it is.
|
|
546
|
-
ChangeSet.create = function create (doc, combine) {
|
|
547
|
-
if ( combine === void 0 ) combine = function (a, b) { return a === b ? a : null; };
|
|
548
|
-
|
|
549
|
-
return new ChangeSet({combine: combine, doc: doc}, [], [])
|
|
550
|
-
};
|
|
551
|
-
|
|
552
|
-
Object.defineProperties( ChangeSet.prototype, prototypeAccessors );
|
|
553
|
-
|
|
554
|
-
// Exported for testing
|
|
555
|
-
ChangeSet.computeDiff = computeDiff;
|
|
556
|
-
|
|
557
|
-
// : ([[Change]], (any, any) → any, number, number) → [Change]
|
|
558
|
-
// Divide-and-conquer approach to merging a series of ranges.
|
|
559
|
-
function mergeAll(ranges, combine, start, end) {
|
|
560
|
-
if ( start === void 0 ) start = 0;
|
|
561
|
-
if ( end === void 0 ) end = ranges.length;
|
|
562
|
-
|
|
563
|
-
if (end == start + 1) { return [ranges[start]] }
|
|
564
|
-
var mid = (start + end) >> 1;
|
|
565
|
-
return Change.merge(mergeAll(ranges, combine, start, mid),
|
|
566
|
-
mergeAll(ranges, combine, mid, end), combine)
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
function endRange(maps) {
|
|
570
|
-
var from = 2e8, to = -2e8;
|
|
571
|
-
for (var i = 0; i < maps.length; i++) {
|
|
572
|
-
var map = maps[i];
|
|
573
|
-
if (from != 2e8) {
|
|
574
|
-
from = map.map(from, -1);
|
|
575
|
-
to = map.map(to, 1);
|
|
576
|
-
}
|
|
577
|
-
map.forEach(function (_s, _e, start, end) {
|
|
578
|
-
from = Math.min(from, start);
|
|
579
|
-
to = Math.max(to, end);
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
return from == 2e8 ? null : {from: from, to: to}
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
function touchedRange(maps) {
|
|
586
|
-
var b = endRange(maps);
|
|
587
|
-
if (!b) { return null }
|
|
588
|
-
var a = endRange(maps.map(function (m) { return m.invert(); }).reverse());
|
|
589
|
-
return {fromA: a.from, toA: a.to, fromB: b.from, toB: b.to}
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
function sameRanges(a, b, map) {
|
|
593
|
-
return map(a.fromB) == b.fromB && map(a.toB) == b.toB &&
|
|
594
|
-
sameSpans(a.deleted, b.deleted) && sameSpans(a.inserted, b.inserted)
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
function sameSpans(a, b) {
|
|
598
|
-
if (a.length != b.length) { return false }
|
|
599
|
-
for (var i = 0; i < a.length; i++)
|
|
600
|
-
{ if (a[i].length != b[i].length || a[i].data !== b[i].data) { return false } }
|
|
601
|
-
return true
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
export { Change, ChangeSet, Span, simplifyChanges };
|
|
605
|
-
//# sourceMappingURL=index.es.js.map
|