@lvce-editor/editor-worker 19.67.6 → 19.67.7
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/dist/editorWorkerMain.js +342 -1
- package/package.json +1 -1
package/dist/editorWorkerMain.js
CHANGED
|
@@ -6316,6 +6316,343 @@ const addCursorBelow = editor => {
|
|
|
6316
6316
|
};
|
|
6317
6317
|
};
|
|
6318
6318
|
|
|
6319
|
+
class Diff {
|
|
6320
|
+
diff(oldStr, newStr,
|
|
6321
|
+
// Type below is not accurate/complete - see above for full possibilities - but it compiles
|
|
6322
|
+
options = {}) {
|
|
6323
|
+
let callback;
|
|
6324
|
+
if (typeof options === 'function') {
|
|
6325
|
+
callback = options;
|
|
6326
|
+
options = {};
|
|
6327
|
+
} else if ('callback' in options) {
|
|
6328
|
+
callback = options.callback;
|
|
6329
|
+
}
|
|
6330
|
+
// Allow subclasses to massage the input prior to running
|
|
6331
|
+
const oldString = this.castInput(oldStr, options);
|
|
6332
|
+
const newString = this.castInput(newStr, options);
|
|
6333
|
+
const oldTokens = this.removeEmpty(this.tokenize(oldString, options));
|
|
6334
|
+
const newTokens = this.removeEmpty(this.tokenize(newString, options));
|
|
6335
|
+
return this.diffWithOptionsObj(oldTokens, newTokens, options, callback);
|
|
6336
|
+
}
|
|
6337
|
+
diffWithOptionsObj(oldTokens, newTokens, options, callback) {
|
|
6338
|
+
var _a;
|
|
6339
|
+
const done = value => {
|
|
6340
|
+
value = this.postProcess(value, options);
|
|
6341
|
+
if (callback) {
|
|
6342
|
+
setTimeout(function () {
|
|
6343
|
+
callback(value);
|
|
6344
|
+
}, 0);
|
|
6345
|
+
return undefined;
|
|
6346
|
+
} else {
|
|
6347
|
+
return value;
|
|
6348
|
+
}
|
|
6349
|
+
};
|
|
6350
|
+
const newLen = newTokens.length,
|
|
6351
|
+
oldLen = oldTokens.length;
|
|
6352
|
+
let editLength = 1;
|
|
6353
|
+
let maxEditLength = newLen + oldLen;
|
|
6354
|
+
if (options.maxEditLength != null) {
|
|
6355
|
+
maxEditLength = Math.min(maxEditLength, options.maxEditLength);
|
|
6356
|
+
}
|
|
6357
|
+
const maxExecutionTime = (_a = options.timeout) !== null && _a !== void 0 ? _a : Infinity;
|
|
6358
|
+
const abortAfterTimestamp = Date.now() + maxExecutionTime;
|
|
6359
|
+
const bestPath = [{
|
|
6360
|
+
oldPos: -1,
|
|
6361
|
+
lastComponent: undefined
|
|
6362
|
+
}];
|
|
6363
|
+
// Seed editLength = 0, i.e. the content starts with the same values
|
|
6364
|
+
let newPos = this.extractCommon(bestPath[0], newTokens, oldTokens, 0, options);
|
|
6365
|
+
if (bestPath[0].oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
|
|
6366
|
+
// Identity per the equality and tokenizer
|
|
6367
|
+
return done(this.buildValues(bestPath[0].lastComponent, newTokens, oldTokens));
|
|
6368
|
+
}
|
|
6369
|
+
// Once we hit the right edge of the edit graph on some diagonal k, we can
|
|
6370
|
+
// definitely reach the end of the edit graph in no more than k edits, so
|
|
6371
|
+
// there's no point in considering any moves to diagonal k+1 any more (from
|
|
6372
|
+
// which we're guaranteed to need at least k+1 more edits).
|
|
6373
|
+
// Similarly, once we've reached the bottom of the edit graph, there's no
|
|
6374
|
+
// point considering moves to lower diagonals.
|
|
6375
|
+
// We record this fact by setting minDiagonalToConsider and
|
|
6376
|
+
// maxDiagonalToConsider to some finite value once we've hit the edge of
|
|
6377
|
+
// the edit graph.
|
|
6378
|
+
// This optimization is not faithful to the original algorithm presented in
|
|
6379
|
+
// Myers's paper, which instead pointlessly extends D-paths off the end of
|
|
6380
|
+
// the edit graph - see page 7 of Myers's paper which notes this point
|
|
6381
|
+
// explicitly and illustrates it with a diagram. This has major performance
|
|
6382
|
+
// implications for some common scenarios. For instance, to compute a diff
|
|
6383
|
+
// where the new text simply appends d characters on the end of the
|
|
6384
|
+
// original text of length n, the true Myers algorithm will take O(n+d^2)
|
|
6385
|
+
// time while this optimization needs only O(n+d) time.
|
|
6386
|
+
let minDiagonalToConsider = -Infinity,
|
|
6387
|
+
maxDiagonalToConsider = Infinity;
|
|
6388
|
+
// Main worker method. checks all permutations of a given edit length for acceptance.
|
|
6389
|
+
const execEditLength = () => {
|
|
6390
|
+
for (let diagonalPath = Math.max(minDiagonalToConsider, -editLength); diagonalPath <= Math.min(maxDiagonalToConsider, editLength); diagonalPath += 2) {
|
|
6391
|
+
let basePath;
|
|
6392
|
+
const removePath = bestPath[diagonalPath - 1],
|
|
6393
|
+
addPath = bestPath[diagonalPath + 1];
|
|
6394
|
+
if (removePath) {
|
|
6395
|
+
// No one else is going to attempt to use this value, clear it
|
|
6396
|
+
// @ts-expect-error - perf optimisation. This type-violating value will never be read.
|
|
6397
|
+
bestPath[diagonalPath - 1] = undefined;
|
|
6398
|
+
}
|
|
6399
|
+
let canAdd = false;
|
|
6400
|
+
if (addPath) {
|
|
6401
|
+
// what newPos will be after we do an insertion:
|
|
6402
|
+
const addPathNewPos = addPath.oldPos - diagonalPath;
|
|
6403
|
+
canAdd = addPath && 0 <= addPathNewPos && addPathNewPos < newLen;
|
|
6404
|
+
}
|
|
6405
|
+
const canRemove = removePath && removePath.oldPos + 1 < oldLen;
|
|
6406
|
+
if (!canAdd && !canRemove) {
|
|
6407
|
+
// If this path is a terminal then prune
|
|
6408
|
+
// @ts-expect-error - perf optimisation. This type-violating value will never be read.
|
|
6409
|
+
bestPath[diagonalPath] = undefined;
|
|
6410
|
+
continue;
|
|
6411
|
+
}
|
|
6412
|
+
// Select the diagonal that we want to branch from. We select the prior
|
|
6413
|
+
// path whose position in the old string is the farthest from the origin
|
|
6414
|
+
// and does not pass the bounds of the diff graph
|
|
6415
|
+
if (!canRemove || canAdd && removePath.oldPos < addPath.oldPos) {
|
|
6416
|
+
basePath = this.addToPath(addPath, true, false, 0, options);
|
|
6417
|
+
} else {
|
|
6418
|
+
basePath = this.addToPath(removePath, false, true, 1, options);
|
|
6419
|
+
}
|
|
6420
|
+
newPos = this.extractCommon(basePath, newTokens, oldTokens, diagonalPath, options);
|
|
6421
|
+
if (basePath.oldPos + 1 >= oldLen && newPos + 1 >= newLen) {
|
|
6422
|
+
// If we have hit the end of both strings, then we are done
|
|
6423
|
+
return done(this.buildValues(basePath.lastComponent, newTokens, oldTokens)) || true;
|
|
6424
|
+
} else {
|
|
6425
|
+
bestPath[diagonalPath] = basePath;
|
|
6426
|
+
if (basePath.oldPos + 1 >= oldLen) {
|
|
6427
|
+
maxDiagonalToConsider = Math.min(maxDiagonalToConsider, diagonalPath - 1);
|
|
6428
|
+
}
|
|
6429
|
+
if (newPos + 1 >= newLen) {
|
|
6430
|
+
minDiagonalToConsider = Math.max(minDiagonalToConsider, diagonalPath + 1);
|
|
6431
|
+
}
|
|
6432
|
+
}
|
|
6433
|
+
}
|
|
6434
|
+
editLength++;
|
|
6435
|
+
};
|
|
6436
|
+
// Performs the length of edit iteration. Is a bit fugly as this has to support the
|
|
6437
|
+
// sync and async mode which is never fun. Loops over execEditLength until a value
|
|
6438
|
+
// is produced, or until the edit length exceeds options.maxEditLength (if given),
|
|
6439
|
+
// in which case it will return undefined.
|
|
6440
|
+
if (callback) {
|
|
6441
|
+
(function exec() {
|
|
6442
|
+
setTimeout(function () {
|
|
6443
|
+
if (editLength > maxEditLength || Date.now() > abortAfterTimestamp) {
|
|
6444
|
+
return callback(undefined);
|
|
6445
|
+
}
|
|
6446
|
+
if (!execEditLength()) {
|
|
6447
|
+
exec();
|
|
6448
|
+
}
|
|
6449
|
+
}, 0);
|
|
6450
|
+
})();
|
|
6451
|
+
} else {
|
|
6452
|
+
while (editLength <= maxEditLength && Date.now() <= abortAfterTimestamp) {
|
|
6453
|
+
const ret = execEditLength();
|
|
6454
|
+
if (ret) {
|
|
6455
|
+
return ret;
|
|
6456
|
+
}
|
|
6457
|
+
}
|
|
6458
|
+
}
|
|
6459
|
+
}
|
|
6460
|
+
addToPath(path, added, removed, oldPosInc, options) {
|
|
6461
|
+
const last = path.lastComponent;
|
|
6462
|
+
if (last && !options.oneChangePerToken && last.added === added && last.removed === removed) {
|
|
6463
|
+
return {
|
|
6464
|
+
oldPos: path.oldPos + oldPosInc,
|
|
6465
|
+
lastComponent: {
|
|
6466
|
+
count: last.count + 1,
|
|
6467
|
+
added: added,
|
|
6468
|
+
removed: removed,
|
|
6469
|
+
previousComponent: last.previousComponent
|
|
6470
|
+
}
|
|
6471
|
+
};
|
|
6472
|
+
} else {
|
|
6473
|
+
return {
|
|
6474
|
+
oldPos: path.oldPos + oldPosInc,
|
|
6475
|
+
lastComponent: {
|
|
6476
|
+
count: 1,
|
|
6477
|
+
added: added,
|
|
6478
|
+
removed: removed,
|
|
6479
|
+
previousComponent: last
|
|
6480
|
+
}
|
|
6481
|
+
};
|
|
6482
|
+
}
|
|
6483
|
+
}
|
|
6484
|
+
extractCommon(basePath, newTokens, oldTokens, diagonalPath, options) {
|
|
6485
|
+
const newLen = newTokens.length,
|
|
6486
|
+
oldLen = oldTokens.length;
|
|
6487
|
+
let oldPos = basePath.oldPos,
|
|
6488
|
+
newPos = oldPos - diagonalPath,
|
|
6489
|
+
commonCount = 0;
|
|
6490
|
+
while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(oldTokens[oldPos + 1], newTokens[newPos + 1], options)) {
|
|
6491
|
+
newPos++;
|
|
6492
|
+
oldPos++;
|
|
6493
|
+
commonCount++;
|
|
6494
|
+
if (options.oneChangePerToken) {
|
|
6495
|
+
basePath.lastComponent = {
|
|
6496
|
+
count: 1,
|
|
6497
|
+
previousComponent: basePath.lastComponent,
|
|
6498
|
+
added: false,
|
|
6499
|
+
removed: false
|
|
6500
|
+
};
|
|
6501
|
+
}
|
|
6502
|
+
}
|
|
6503
|
+
if (commonCount && !options.oneChangePerToken) {
|
|
6504
|
+
basePath.lastComponent = {
|
|
6505
|
+
count: commonCount,
|
|
6506
|
+
previousComponent: basePath.lastComponent,
|
|
6507
|
+
added: false,
|
|
6508
|
+
removed: false
|
|
6509
|
+
};
|
|
6510
|
+
}
|
|
6511
|
+
basePath.oldPos = oldPos;
|
|
6512
|
+
return newPos;
|
|
6513
|
+
}
|
|
6514
|
+
equals(left, right, options) {
|
|
6515
|
+
if (options.comparator) {
|
|
6516
|
+
return options.comparator(left, right);
|
|
6517
|
+
} else {
|
|
6518
|
+
return left === right || !!options.ignoreCase && left.toLowerCase() === right.toLowerCase();
|
|
6519
|
+
}
|
|
6520
|
+
}
|
|
6521
|
+
removeEmpty(array) {
|
|
6522
|
+
const ret = [];
|
|
6523
|
+
for (let i = 0; i < array.length; i++) {
|
|
6524
|
+
if (array[i]) {
|
|
6525
|
+
ret.push(array[i]);
|
|
6526
|
+
}
|
|
6527
|
+
}
|
|
6528
|
+
return ret;
|
|
6529
|
+
}
|
|
6530
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6531
|
+
castInput(value, options) {
|
|
6532
|
+
return value;
|
|
6533
|
+
}
|
|
6534
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6535
|
+
tokenize(value, options) {
|
|
6536
|
+
return Array.from(value);
|
|
6537
|
+
}
|
|
6538
|
+
join(chars) {
|
|
6539
|
+
// Assumes ValueT is string, which is the case for most subclasses.
|
|
6540
|
+
// When it's false, e.g. in diffArrays, this method needs to be overridden (e.g. with a no-op)
|
|
6541
|
+
// Yes, the casts are verbose and ugly, because this pattern - of having the base class SORT OF
|
|
6542
|
+
// assume tokens and values are strings, but not completely - is weird and janky.
|
|
6543
|
+
return chars.join('');
|
|
6544
|
+
}
|
|
6545
|
+
postProcess(changeObjects,
|
|
6546
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6547
|
+
options) {
|
|
6548
|
+
return changeObjects;
|
|
6549
|
+
}
|
|
6550
|
+
get useLongestToken() {
|
|
6551
|
+
return false;
|
|
6552
|
+
}
|
|
6553
|
+
buildValues(lastComponent, newTokens, oldTokens) {
|
|
6554
|
+
// First we convert our linked list of components in reverse order to an
|
|
6555
|
+
// array in the right order:
|
|
6556
|
+
const components = [];
|
|
6557
|
+
let nextComponent;
|
|
6558
|
+
while (lastComponent) {
|
|
6559
|
+
components.push(lastComponent);
|
|
6560
|
+
nextComponent = lastComponent.previousComponent;
|
|
6561
|
+
delete lastComponent.previousComponent;
|
|
6562
|
+
lastComponent = nextComponent;
|
|
6563
|
+
}
|
|
6564
|
+
components.reverse();
|
|
6565
|
+
const componentLen = components.length;
|
|
6566
|
+
let componentPos = 0,
|
|
6567
|
+
newPos = 0,
|
|
6568
|
+
oldPos = 0;
|
|
6569
|
+
for (; componentPos < componentLen; componentPos++) {
|
|
6570
|
+
const component = components[componentPos];
|
|
6571
|
+
if (!component.removed) {
|
|
6572
|
+
if (!component.added && this.useLongestToken) {
|
|
6573
|
+
let value = newTokens.slice(newPos, newPos + component.count);
|
|
6574
|
+
value = value.map(function (value, i) {
|
|
6575
|
+
const oldValue = oldTokens[oldPos + i];
|
|
6576
|
+
return oldValue.length > value.length ? oldValue : value;
|
|
6577
|
+
});
|
|
6578
|
+
component.value = this.join(value);
|
|
6579
|
+
} else {
|
|
6580
|
+
component.value = this.join(newTokens.slice(newPos, newPos + component.count));
|
|
6581
|
+
}
|
|
6582
|
+
newPos += component.count;
|
|
6583
|
+
// Common case
|
|
6584
|
+
if (!component.added) {
|
|
6585
|
+
oldPos += component.count;
|
|
6586
|
+
}
|
|
6587
|
+
} else {
|
|
6588
|
+
component.value = this.join(oldTokens.slice(oldPos, oldPos + component.count));
|
|
6589
|
+
oldPos += component.count;
|
|
6590
|
+
}
|
|
6591
|
+
}
|
|
6592
|
+
return components;
|
|
6593
|
+
}
|
|
6594
|
+
}
|
|
6595
|
+
|
|
6596
|
+
class CharacterDiff extends Diff {}
|
|
6597
|
+
const characterDiff = new CharacterDiff();
|
|
6598
|
+
function diffChars(oldStr, newStr, options) {
|
|
6599
|
+
return characterDiff.diff(oldStr, newStr, options);
|
|
6600
|
+
}
|
|
6601
|
+
|
|
6602
|
+
const mapOffset = (offset, changes) => {
|
|
6603
|
+
let oldOffset = 0;
|
|
6604
|
+
let newOffset = 0;
|
|
6605
|
+
for (const change of changes) {
|
|
6606
|
+
const {
|
|
6607
|
+
length
|
|
6608
|
+
} = change.value;
|
|
6609
|
+
if (change.added) {
|
|
6610
|
+
newOffset += length;
|
|
6611
|
+
} else if (change.removed) {
|
|
6612
|
+
if (offset < oldOffset + length) {
|
|
6613
|
+
return newOffset;
|
|
6614
|
+
}
|
|
6615
|
+
oldOffset += length;
|
|
6616
|
+
} else {
|
|
6617
|
+
if (offset < oldOffset + length) {
|
|
6618
|
+
return newOffset + offset - oldOffset;
|
|
6619
|
+
}
|
|
6620
|
+
oldOffset += length;
|
|
6621
|
+
newOffset += length;
|
|
6622
|
+
}
|
|
6623
|
+
}
|
|
6624
|
+
return newOffset;
|
|
6625
|
+
};
|
|
6626
|
+
const getFormattingSelections = (lines, newLines, selections) => {
|
|
6627
|
+
const text = lines.join('\n');
|
|
6628
|
+
const newText = newLines.join('\n');
|
|
6629
|
+
if (text === newText) {
|
|
6630
|
+
return selections;
|
|
6631
|
+
}
|
|
6632
|
+
// Bound expensive diffs of unrelated documents; preserve valid coordinates if the limit is reached.
|
|
6633
|
+
const changes = diffChars(text, newText, {
|
|
6634
|
+
maxEditLength: 1000
|
|
6635
|
+
});
|
|
6636
|
+
const result = new Uint32Array(selections.length);
|
|
6637
|
+
for (let i = 0; i < selections.length; i += 2) {
|
|
6638
|
+
if (!changes) {
|
|
6639
|
+
const row = Math.min(selections[i], newLines.length - 1);
|
|
6640
|
+
result[i] = row;
|
|
6641
|
+
result[i + 1] = Math.min(selections[i + 1], newLines[row].length);
|
|
6642
|
+
continue;
|
|
6643
|
+
}
|
|
6644
|
+
const offset = offsetAt({
|
|
6645
|
+
lines
|
|
6646
|
+
}, selections[i], selections[i + 1]);
|
|
6647
|
+
const position = positionAt({
|
|
6648
|
+
lines: newLines
|
|
6649
|
+
}, mapOffset(offset, changes));
|
|
6650
|
+
result[i] = position.rowIndex;
|
|
6651
|
+
result[i + 1] = position.columnIndex;
|
|
6652
|
+
}
|
|
6653
|
+
return result;
|
|
6654
|
+
};
|
|
6655
|
+
|
|
6319
6656
|
const warn = (...args) => {
|
|
6320
6657
|
console.warn(...args);
|
|
6321
6658
|
};
|
|
@@ -6332,7 +6669,11 @@ const applyDocumentEdits$1 = (editor, edits) => {
|
|
|
6332
6669
|
return editor;
|
|
6333
6670
|
}
|
|
6334
6671
|
const documentEdits = getDocumentEdits(editor, edits);
|
|
6335
|
-
|
|
6672
|
+
const newLines = applyEdits({
|
|
6673
|
+
...editor
|
|
6674
|
+
}, documentEdits);
|
|
6675
|
+
const selections = getFormattingSelections(editor.lines, newLines, editor.selections);
|
|
6676
|
+
return scheduleDocumentAndCursorsSelections(editor, documentEdits, selections);
|
|
6336
6677
|
};
|
|
6337
6678
|
|
|
6338
6679
|
const applyEdit = async (editor, changes, selectionChanges) => {
|