@portabletext/editor 1.17.0 → 1.18.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 +228 -261
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-selection-collapsed.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
- package/lib/_chunks-es/selector.is-selection-collapsed.js.map +1 -1
- package/lib/behaviors/index.cjs +40 -38
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +23 -23
- package/lib/behaviors/index.d.ts +23 -23
- package/lib/behaviors/index.js +40 -38
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +301 -351
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +123 -753
- package/lib/index.d.ts +123 -753
- package/lib/index.js +301 -351
- package/lib/index.js.map +1 -1
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +30 -30
- package/lib/selectors/index.d.ts +30 -30
- package/lib/selectors/index.js.map +1 -1
- package/package.json +10 -10
- package/src/behaviors/behavior.code-editor.ts +2 -2
- package/src/behaviors/behavior.core.ts +2 -2
- package/src/behaviors/behavior.emoji-picker.ts +52 -41
- package/src/behaviors/behavior.links.ts +2 -2
- package/src/behaviors/behavior.markdown.ts +2 -2
- package/src/behaviors/behavior.types.ts +10 -10
- package/src/editor/PortableTextEditor.tsx +2 -0
- package/src/editor/__tests__/self-solving.test.tsx +14 -0
- package/src/editor/components/Synchronizer.tsx +6 -1
- package/src/editor/create-editor.ts +14 -3
- package/src/editor/define-schema.ts +4 -4
- package/src/editor/editor-event-listener.tsx +1 -1
- package/src/editor/editor-machine.ts +42 -52
- package/src/editor/editor-provider.tsx +3 -3
- package/src/editor/editor-selector.ts +31 -14
- package/src/editor/editor-snapshot.ts +2 -2
- package/src/editor/get-value.ts +12 -5
- package/src/editor/hooks/usePortableTextEditor.ts +1 -0
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +1 -0
- package/src/selectors/selector.get-active-list-item.ts +1 -1
- package/src/selectors/selector.get-active-style.ts +1 -1
- package/src/selectors/selector.get-selected-spans.ts +1 -1
- package/src/selectors/selector.get-selection-text.ts +1 -1
- package/src/selectors/selector.get-text-before.ts +1 -1
- package/src/selectors/selector.is-active-annotation.ts +1 -1
- package/src/selectors/selector.is-active-decorator.ts +1 -1
- package/src/selectors/selector.is-active-list-item.ts +1 -1
- package/src/selectors/selector.is-active-style.ts +1 -1
- package/src/selectors/selector.is-selection-collapsed.ts +1 -1
- package/src/selectors/selector.is-selection-expanded.ts +1 -1
- package/src/selectors/selectors.ts +13 -13
- package/src/types/editor.ts +2 -2
package/lib/index.js
CHANGED
|
@@ -1256,14 +1256,6 @@ function withoutPatching(editor, fn) {
|
|
|
1256
1256
|
function isPatching(editor) {
|
|
1257
1257
|
return PATCHING.get(editor);
|
|
1258
1258
|
}
|
|
1259
|
-
function isHighSurrogate(char) {
|
|
1260
|
-
const charCode = char.charCodeAt(0);
|
|
1261
|
-
return charCode >= 55296 && charCode <= 56319;
|
|
1262
|
-
}
|
|
1263
|
-
function isLowSurrogate(char) {
|
|
1264
|
-
const charCode = char.charCodeAt(0);
|
|
1265
|
-
return charCode >= 56320 && charCode <= 57343;
|
|
1266
|
-
}
|
|
1267
1259
|
function cloneDiff(diff2) {
|
|
1268
1260
|
const [type, patch] = diff2;
|
|
1269
1261
|
return [type, patch];
|
|
@@ -1271,123 +1263,41 @@ function cloneDiff(diff2) {
|
|
|
1271
1263
|
function getCommonOverlap(textA, textB) {
|
|
1272
1264
|
let text1 = textA, text2 = textB;
|
|
1273
1265
|
const text1Length = text1.length, text2Length = text2.length;
|
|
1274
|
-
if (text1Length === 0 || text2Length === 0)
|
|
1275
|
-
return 0;
|
|
1266
|
+
if (text1Length === 0 || text2Length === 0) return 0;
|
|
1276
1267
|
text1Length > text2Length ? text1 = text1.substring(text1Length - text2Length) : text1Length < text2Length && (text2 = text2.substring(0, text1Length));
|
|
1277
1268
|
const textLength = Math.min(text1Length, text2Length);
|
|
1278
|
-
if (text1 === text2)
|
|
1279
|
-
return textLength;
|
|
1269
|
+
if (text1 === text2) return textLength;
|
|
1280
1270
|
let best = 0, length = 1;
|
|
1281
1271
|
for (let found = 0; found !== -1; ) {
|
|
1282
1272
|
const pattern = text1.substring(textLength - length);
|
|
1283
|
-
if (found = text2.indexOf(pattern), found === -1)
|
|
1284
|
-
return best;
|
|
1273
|
+
if (found = text2.indexOf(pattern), found === -1) return best;
|
|
1285
1274
|
length += found, (found === 0 || text1.substring(textLength - length) === text2.substring(0, length)) && (best = length, length++);
|
|
1286
1275
|
}
|
|
1287
1276
|
return best;
|
|
1288
1277
|
}
|
|
1289
1278
|
function getCommonPrefix(text1, text2) {
|
|
1290
|
-
if (!text1 || !text2 || text1[0] !== text2[0])
|
|
1291
|
-
return 0;
|
|
1279
|
+
if (!text1 || !text2 || text1[0] !== text2[0]) return 0;
|
|
1292
1280
|
let pointerMin = 0, pointerMax = Math.min(text1.length, text2.length), pointerMid = pointerMax, pointerStart = 0;
|
|
1293
|
-
for (; pointerMin < pointerMid; )
|
|
1294
|
-
text1.substring(pointerStart, pointerMid) === text2.substring(pointerStart, pointerMid) ? (pointerMin = pointerMid, pointerStart = pointerMin) : pointerMax = pointerMid, pointerMid = Math.floor((pointerMax - pointerMin) / 2 + pointerMin);
|
|
1281
|
+
for (; pointerMin < pointerMid; ) text1.substring(pointerStart, pointerMid) === text2.substring(pointerStart, pointerMid) ? (pointerMin = pointerMid, pointerStart = pointerMin) : pointerMax = pointerMid, pointerMid = Math.floor((pointerMax - pointerMin) / 2 + pointerMin);
|
|
1295
1282
|
return pointerMid;
|
|
1296
1283
|
}
|
|
1297
1284
|
function getCommonSuffix(text1, text2) {
|
|
1298
|
-
if (!text1 || !text2 || text1[text1.length - 1] !== text2[text2.length - 1])
|
|
1299
|
-
return 0;
|
|
1285
|
+
if (!text1 || !text2 || text1[text1.length - 1] !== text2[text2.length - 1]) return 0;
|
|
1300
1286
|
let pointerMin = 0, pointerMax = Math.min(text1.length, text2.length), pointerMid = pointerMax, pointerEnd = 0;
|
|
1301
|
-
for (; pointerMin < pointerMid; )
|
|
1302
|
-
text1.substring(text1.length - pointerMid, text1.length - pointerEnd) === text2.substring(text2.length - pointerMid, text2.length - pointerEnd) ? (pointerMin = pointerMid, pointerEnd = pointerMin) : pointerMax = pointerMid, pointerMid = Math.floor((pointerMax - pointerMin) / 2 + pointerMin);
|
|
1287
|
+
for (; pointerMin < pointerMid; ) text1.substring(text1.length - pointerMid, text1.length - pointerEnd) === text2.substring(text2.length - pointerMid, text2.length - pointerEnd) ? (pointerMin = pointerMid, pointerEnd = pointerMin) : pointerMax = pointerMid, pointerMid = Math.floor((pointerMax - pointerMin) / 2 + pointerMin);
|
|
1303
1288
|
return pointerMid;
|
|
1304
1289
|
}
|
|
1305
|
-
function
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
let equalitiesLength = 0, lastEquality = null, pointer = 0, lengthInsertions1 = 0, lengthDeletions1 = 0, lengthInsertions2 = 0, lengthDeletions2 = 0;
|
|
1309
|
-
for (; pointer < diffs.length; )
|
|
1310
|
-
diffs[pointer][0] === DIFF_EQUAL ? (equalities[equalitiesLength++] = pointer, lengthInsertions1 = lengthInsertions2, lengthDeletions1 = lengthDeletions2, lengthInsertions2 = 0, lengthDeletions2 = 0, lastEquality = diffs[pointer][1]) : (diffs[pointer][0] === DIFF_INSERT ? lengthInsertions2 += diffs[pointer][1].length : lengthDeletions2 += diffs[pointer][1].length, lastEquality && lastEquality.length <= Math.max(lengthInsertions1, lengthDeletions1) && lastEquality.length <= Math.max(lengthInsertions2, lengthDeletions2) && (diffs.splice(equalities[equalitiesLength - 1], 0, [DIFF_DELETE, lastEquality]), diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT, equalitiesLength--, equalitiesLength--, pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1, lengthInsertions1 = 0, lengthDeletions1 = 0, lengthInsertions2 = 0, lengthDeletions2 = 0, lastEquality = null, hasChanges = !0)), pointer++;
|
|
1311
|
-
for (hasChanges && (diffs = cleanupMerge(diffs)), diffs = cleanupSemanticLossless(diffs), pointer = 1; pointer < diffs.length; ) {
|
|
1312
|
-
if (diffs[pointer - 1][0] === DIFF_DELETE && diffs[pointer][0] === DIFF_INSERT) {
|
|
1313
|
-
const deletion = diffs[pointer - 1][1], insertion = diffs[pointer][1], overlapLength1 = getCommonOverlap(deletion, insertion), overlapLength2 = getCommonOverlap(insertion, deletion);
|
|
1314
|
-
overlapLength1 >= overlapLength2 ? (overlapLength1 >= deletion.length / 2 || overlapLength1 >= insertion.length / 2) && (diffs.splice(pointer, 0, [DIFF_EQUAL, insertion.substring(0, overlapLength1)]), diffs[pointer - 1][1] = deletion.substring(0, deletion.length - overlapLength1), diffs[pointer + 1][1] = insertion.substring(overlapLength1), pointer++) : (overlapLength2 >= deletion.length / 2 || overlapLength2 >= insertion.length / 2) && (diffs.splice(pointer, 0, [DIFF_EQUAL, deletion.substring(0, overlapLength2)]), diffs[pointer - 1][0] = DIFF_INSERT, diffs[pointer - 1][1] = insertion.substring(0, insertion.length - overlapLength2), diffs[pointer + 1][0] = DIFF_DELETE, diffs[pointer + 1][1] = deletion.substring(overlapLength2), pointer++), pointer++;
|
|
1315
|
-
}
|
|
1316
|
-
pointer++;
|
|
1317
|
-
}
|
|
1318
|
-
return diffs;
|
|
1319
|
-
}
|
|
1320
|
-
const nonAlphaNumericRegex = /[^a-zA-Z0-9]/, whitespaceRegex = /\s/, linebreakRegex = /[\r\n]/, blanklineEndRegex = /\n\r?\n$/, blanklineStartRegex = /^\r?\n\r?\n/;
|
|
1321
|
-
function cleanupSemanticLossless(rawDiffs) {
|
|
1322
|
-
const diffs = rawDiffs.map((diff2) => cloneDiff(diff2));
|
|
1323
|
-
function diffCleanupSemanticScore(one, two) {
|
|
1324
|
-
if (!one || !two)
|
|
1325
|
-
return 6;
|
|
1326
|
-
const char1 = one.charAt(one.length - 1), char2 = two.charAt(0), nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex), nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex), whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex), whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex), lineBreak1 = whitespace1 && char1.match(linebreakRegex), lineBreak2 = whitespace2 && char2.match(linebreakRegex), blankLine1 = lineBreak1 && one.match(blanklineEndRegex), blankLine2 = lineBreak2 && two.match(blanklineStartRegex);
|
|
1327
|
-
return blankLine1 || blankLine2 ? 5 : lineBreak1 || lineBreak2 ? 4 : nonAlphaNumeric1 && !whitespace1 && whitespace2 ? 3 : whitespace1 || whitespace2 ? 2 : nonAlphaNumeric1 || nonAlphaNumeric2 ? 1 : 0;
|
|
1328
|
-
}
|
|
1329
|
-
let pointer = 1;
|
|
1330
|
-
for (; pointer < diffs.length - 1; ) {
|
|
1331
|
-
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
|
1332
|
-
let equality1 = diffs[pointer - 1][1], edit = diffs[pointer][1], equality2 = diffs[pointer + 1][1];
|
|
1333
|
-
const commonOffset = getCommonSuffix(equality1, edit);
|
|
1334
|
-
if (commonOffset) {
|
|
1335
|
-
const commonString = edit.substring(edit.length - commonOffset);
|
|
1336
|
-
equality1 = equality1.substring(0, equality1.length - commonOffset), edit = commonString + edit.substring(0, edit.length - commonOffset), equality2 = commonString + equality2;
|
|
1337
|
-
}
|
|
1338
|
-
let bestEquality1 = equality1, bestEdit = edit, bestEquality2 = equality2, bestScore = diffCleanupSemanticScore(equality1, edit) + diffCleanupSemanticScore(edit, equality2);
|
|
1339
|
-
for (; edit.charAt(0) === equality2.charAt(0); ) {
|
|
1340
|
-
equality1 += edit.charAt(0), edit = edit.substring(1) + equality2.charAt(0), equality2 = equality2.substring(1);
|
|
1341
|
-
const score = diffCleanupSemanticScore(equality1, edit) + diffCleanupSemanticScore(edit, equality2);
|
|
1342
|
-
score >= bestScore && (bestScore = score, bestEquality1 = equality1, bestEdit = edit, bestEquality2 = equality2);
|
|
1343
|
-
}
|
|
1344
|
-
diffs[pointer - 1][1] !== bestEquality1 && (bestEquality1 ? diffs[pointer - 1][1] = bestEquality1 : (diffs.splice(pointer - 1, 1), pointer--), diffs[pointer][1] = bestEdit, bestEquality2 ? diffs[pointer + 1][1] = bestEquality2 : (diffs.splice(pointer + 1, 1), pointer--));
|
|
1345
|
-
}
|
|
1346
|
-
pointer++;
|
|
1347
|
-
}
|
|
1348
|
-
return diffs;
|
|
1349
|
-
}
|
|
1350
|
-
function cleanupMerge(rawDiffs) {
|
|
1351
|
-
let diffs = rawDiffs.map((diff2) => cloneDiff(diff2));
|
|
1352
|
-
diffs.push([DIFF_EQUAL, ""]);
|
|
1353
|
-
let pointer = 0, countDelete = 0, countInsert = 0, textDelete = "", textInsert = "", commonlength;
|
|
1354
|
-
for (; pointer < diffs.length; )
|
|
1355
|
-
switch (diffs[pointer][0]) {
|
|
1356
|
-
case DIFF_INSERT:
|
|
1357
|
-
countInsert++, textInsert += diffs[pointer][1], pointer++;
|
|
1358
|
-
break;
|
|
1359
|
-
case DIFF_DELETE:
|
|
1360
|
-
countDelete++, textDelete += diffs[pointer][1], pointer++;
|
|
1361
|
-
break;
|
|
1362
|
-
case DIFF_EQUAL:
|
|
1363
|
-
countDelete + countInsert > 1 ? (countDelete !== 0 && countInsert !== 0 && (commonlength = getCommonPrefix(textInsert, textDelete), commonlength !== 0 && (pointer - countDelete - countInsert > 0 && diffs[pointer - countDelete - countInsert - 1][0] === DIFF_EQUAL ? diffs[pointer - countDelete - countInsert - 1][1] += textInsert.substring(0, commonlength) : (diffs.splice(0, 0, [DIFF_EQUAL, textInsert.substring(0, commonlength)]), pointer++), textInsert = textInsert.substring(commonlength), textDelete = textDelete.substring(commonlength)), commonlength = getCommonSuffix(textInsert, textDelete), commonlength !== 0 && (diffs[pointer][1] = textInsert.substring(textInsert.length - commonlength) + diffs[pointer][1], textInsert = textInsert.substring(0, textInsert.length - commonlength), textDelete = textDelete.substring(0, textDelete.length - commonlength))), pointer -= countDelete + countInsert, diffs.splice(pointer, countDelete + countInsert), textDelete.length && (diffs.splice(pointer, 0, [DIFF_DELETE, textDelete]), pointer++), textInsert.length && (diffs.splice(pointer, 0, [DIFF_INSERT, textInsert]), pointer++), pointer++) : pointer !== 0 && diffs[pointer - 1][0] === DIFF_EQUAL ? (diffs[pointer - 1][1] += diffs[pointer][1], diffs.splice(pointer, 1)) : pointer++, countInsert = 0, countDelete = 0, textDelete = "", textInsert = "";
|
|
1364
|
-
break;
|
|
1365
|
-
default:
|
|
1366
|
-
throw new Error("Unknown diff operation");
|
|
1367
|
-
}
|
|
1368
|
-
diffs[diffs.length - 1][1] === "" && diffs.pop();
|
|
1369
|
-
let hasChanges = !1;
|
|
1370
|
-
for (pointer = 1; pointer < diffs.length - 1; )
|
|
1371
|
-
diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL && (diffs[pointer][1].substring(diffs[pointer][1].length - diffs[pointer - 1][1].length) === diffs[pointer - 1][1] ? (diffs[pointer][1] = diffs[pointer - 1][1] + diffs[pointer][1].substring(0, diffs[pointer][1].length - diffs[pointer - 1][1].length), diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1], diffs.splice(pointer - 1, 1), hasChanges = !0) : diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) === diffs[pointer + 1][1] && (diffs[pointer - 1][1] += diffs[pointer + 1][1], diffs[pointer][1] = diffs[pointer][1].substring(diffs[pointer + 1][1].length) + diffs[pointer + 1][1], diffs.splice(pointer + 1, 1), hasChanges = !0)), pointer++;
|
|
1372
|
-
return hasChanges && (diffs = cleanupMerge(diffs)), diffs;
|
|
1373
|
-
}
|
|
1374
|
-
function trueCount() {
|
|
1375
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++)
|
|
1376
|
-
args[_key] = arguments[_key];
|
|
1377
|
-
return args.reduce((n, bool) => n + (bool ? 1 : 0), 0);
|
|
1290
|
+
function isHighSurrogate(char) {
|
|
1291
|
+
const charCode = char.charCodeAt(0);
|
|
1292
|
+
return charCode >= 55296 && charCode <= 56319;
|
|
1378
1293
|
}
|
|
1379
|
-
function
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
let equalitiesLength = 0, lastEquality = null, pointer = 0, preIns = !1, preDel = !1, postIns = !1, postDel = !1;
|
|
1383
|
-
for (; pointer < diffs.length; )
|
|
1384
|
-
diffs[pointer][0] === DIFF_EQUAL ? (diffs[pointer][1].length < editCost && (postIns || postDel) ? (equalities[equalitiesLength++] = pointer, preIns = postIns, preDel = postDel, lastEquality = diffs[pointer][1]) : (equalitiesLength = 0, lastEquality = null), postIns = !1, postDel = !1) : (diffs[pointer][0] === DIFF_DELETE ? postDel = !0 : postIns = !0, lastEquality && (preIns && preDel && postIns && postDel || lastEquality.length < editCost / 2 && trueCount(preIns, preDel, postIns, postDel) === 3) && (diffs.splice(equalities[equalitiesLength - 1], 0, [DIFF_DELETE, lastEquality]), diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT, equalitiesLength--, lastEquality = null, preIns && preDel ? (postIns = !0, postDel = !0, equalitiesLength = 0) : (equalitiesLength--, pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1, postIns = !1, postDel = !1), hasChanges = !0)), pointer++;
|
|
1385
|
-
return hasChanges && (diffs = cleanupMerge(diffs)), diffs;
|
|
1294
|
+
function isLowSurrogate(char) {
|
|
1295
|
+
const charCode = char.charCodeAt(0);
|
|
1296
|
+
return charCode >= 56320 && charCode <= 57343;
|
|
1386
1297
|
}
|
|
1387
1298
|
function bisect(text1, text2, deadline) {
|
|
1388
1299
|
const text1Length = text1.length, text2Length = text2.length, maxD = Math.ceil((text1Length + text2Length) / 2), vOffset = maxD, vLength = 2 * maxD, v1 = new Array(vLength), v2 = new Array(vLength);
|
|
1389
|
-
for (let x = 0; x < vLength; x++)
|
|
1390
|
-
v1[x] = -1, v2[x] = -1;
|
|
1300
|
+
for (let x = 0; x < vLength; x++) v1[x] = -1, v2[x] = -1;
|
|
1391
1301
|
v1[vOffset + 1] = 0, v2[vOffset + 1] = 0;
|
|
1392
1302
|
const delta = text1Length - text2Length, front = delta % 2 !== 0;
|
|
1393
1303
|
let k1start = 0, k1end = 0, k2start = 0, k2end = 0;
|
|
@@ -1397,18 +1307,14 @@ function bisect(text1, text2, deadline) {
|
|
|
1397
1307
|
let x1;
|
|
1398
1308
|
k1 === -d || k1 !== d && v1[k1Offset - 1] < v1[k1Offset + 1] ? x1 = v1[k1Offset + 1] : x1 = v1[k1Offset - 1] + 1;
|
|
1399
1309
|
let y1 = x1 - k1;
|
|
1400
|
-
for (; x1 < text1Length && y1 < text2Length && text1.charAt(x1) === text2.charAt(y1); )
|
|
1401
|
-
|
|
1402
|
-
if (
|
|
1403
|
-
k1end += 2;
|
|
1404
|
-
else if (y1 > text2Length)
|
|
1405
|
-
k1start += 2;
|
|
1310
|
+
for (; x1 < text1Length && y1 < text2Length && text1.charAt(x1) === text2.charAt(y1); ) x1++, y1++;
|
|
1311
|
+
if (v1[k1Offset] = x1, x1 > text1Length) k1end += 2;
|
|
1312
|
+
else if (y1 > text2Length) k1start += 2;
|
|
1406
1313
|
else if (front) {
|
|
1407
1314
|
const k2Offset = vOffset + delta - k1;
|
|
1408
1315
|
if (k2Offset >= 0 && k2Offset < vLength && v2[k2Offset] !== -1) {
|
|
1409
1316
|
const x2 = text1Length - v2[k2Offset];
|
|
1410
|
-
if (x1 >= x2)
|
|
1411
|
-
return bisectSplit(text1, text2, x1, y1, deadline);
|
|
1317
|
+
if (x1 >= x2) return bisectSplit(text1, text2, x1, y1, deadline);
|
|
1412
1318
|
}
|
|
1413
1319
|
}
|
|
1414
1320
|
}
|
|
@@ -1417,18 +1323,14 @@ function bisect(text1, text2, deadline) {
|
|
|
1417
1323
|
let x2;
|
|
1418
1324
|
k2 === -d || k2 !== d && v2[k2Offset - 1] < v2[k2Offset + 1] ? x2 = v2[k2Offset + 1] : x2 = v2[k2Offset - 1] + 1;
|
|
1419
1325
|
let y2 = x2 - k2;
|
|
1420
|
-
for (; x2 < text1Length && y2 < text2Length && text1.charAt(text1Length - x2 - 1) === text2.charAt(text2Length - y2 - 1); )
|
|
1421
|
-
|
|
1422
|
-
if (
|
|
1423
|
-
k2end += 2;
|
|
1424
|
-
else if (y2 > text2Length)
|
|
1425
|
-
k2start += 2;
|
|
1326
|
+
for (; x2 < text1Length && y2 < text2Length && text1.charAt(text1Length - x2 - 1) === text2.charAt(text2Length - y2 - 1); ) x2++, y2++;
|
|
1327
|
+
if (v2[k2Offset] = x2, x2 > text1Length) k2end += 2;
|
|
1328
|
+
else if (y2 > text2Length) k2start += 2;
|
|
1426
1329
|
else if (!front) {
|
|
1427
1330
|
const k1Offset = vOffset + delta - k2;
|
|
1428
1331
|
if (k1Offset >= 0 && k1Offset < vLength && v1[k1Offset] !== -1) {
|
|
1429
1332
|
const x1 = v1[k1Offset], y1 = vOffset + x1 - k1Offset;
|
|
1430
|
-
if (x2 = text1Length - x2, x1 >= x2)
|
|
1431
|
-
return bisectSplit(text1, text2, x1, y1, deadline);
|
|
1333
|
+
if (x2 = text1Length - x2, x1 >= x2) return bisectSplit(text1, text2, x1, y1, deadline);
|
|
1432
1334
|
}
|
|
1433
1335
|
}
|
|
1434
1336
|
}
|
|
@@ -1445,23 +1347,18 @@ function bisectSplit(text1, text2, x, y, deadline) {
|
|
|
1445
1347
|
});
|
|
1446
1348
|
return diffs.concat(diffsb);
|
|
1447
1349
|
}
|
|
1448
|
-
function findHalfMatch(text1, text2) {
|
|
1449
|
-
if (
|
|
1450
|
-
return null;
|
|
1350
|
+
function findHalfMatch(text1, text2, timeout = 1) {
|
|
1351
|
+
if (timeout <= 0) return null;
|
|
1451
1352
|
const longText = text1.length > text2.length ? text1 : text2, shortText = text1.length > text2.length ? text2 : text1;
|
|
1452
|
-
if (longText.length < 4 || shortText.length * 2 < longText.length)
|
|
1453
|
-
return null;
|
|
1353
|
+
if (longText.length < 4 || shortText.length * 2 < longText.length) return null;
|
|
1454
1354
|
const halfMatch1 = halfMatchI(longText, shortText, Math.ceil(longText.length / 4)), halfMatch2 = halfMatchI(longText, shortText, Math.ceil(longText.length / 2));
|
|
1455
1355
|
let halfMatch;
|
|
1456
|
-
if (halfMatch1 && halfMatch2)
|
|
1457
|
-
halfMatch = halfMatch1[4].length > halfMatch2[4].length ? halfMatch1 : halfMatch2;
|
|
1356
|
+
if (halfMatch1 && halfMatch2) halfMatch = halfMatch1[4].length > halfMatch2[4].length ? halfMatch1 : halfMatch2;
|
|
1458
1357
|
else {
|
|
1459
|
-
if (!halfMatch1 && !halfMatch2)
|
|
1460
|
-
return null;
|
|
1358
|
+
if (!halfMatch1 && !halfMatch2) return null;
|
|
1461
1359
|
halfMatch2 ? halfMatch1 || (halfMatch = halfMatch2) : halfMatch = halfMatch1;
|
|
1462
1360
|
}
|
|
1463
|
-
if (!halfMatch)
|
|
1464
|
-
throw new Error("Unable to find a half match.");
|
|
1361
|
+
if (!halfMatch) throw new Error("Unable to find a half match.");
|
|
1465
1362
|
let text1A, text1B, text2A, text2B;
|
|
1466
1363
|
text1.length > text2.length ? (text1A = halfMatch[0], text1B = halfMatch[1], text2A = halfMatch[2], text2B = halfMatch[3]) : (text2A = halfMatch[0], text2B = halfMatch[1], text1A = halfMatch[2], text1B = halfMatch[3]);
|
|
1467
1364
|
const midCommon = halfMatch[4];
|
|
@@ -1479,8 +1376,7 @@ function halfMatchI(longText, shortText, i) {
|
|
|
1479
1376
|
function charsToLines(diffs, lineArray) {
|
|
1480
1377
|
for (let x = 0; x < diffs.length; x++) {
|
|
1481
1378
|
const chars = diffs[x][1], text = [];
|
|
1482
|
-
for (let y = 0; y < chars.length; y++)
|
|
1483
|
-
text[y] = lineArray[chars.charCodeAt(y)];
|
|
1379
|
+
for (let y = 0; y < chars.length; y++) text[y] = lineArray[chars.charCodeAt(y)];
|
|
1484
1380
|
diffs[x][1] = text.join("");
|
|
1485
1381
|
}
|
|
1486
1382
|
}
|
|
@@ -1533,8 +1429,7 @@ function doLineModeDiff(textA, textB, opts) {
|
|
|
1533
1429
|
checkLines: !1,
|
|
1534
1430
|
deadline: opts.deadline
|
|
1535
1431
|
});
|
|
1536
|
-
for (let j = aa.length - 1; j >= 0; j--)
|
|
1537
|
-
diffs.splice(pointer, 0, aa[j]);
|
|
1432
|
+
for (let j = aa.length - 1; j >= 0; j--) diffs.splice(pointer, 0, aa[j]);
|
|
1538
1433
|
pointer += aa.length;
|
|
1539
1434
|
}
|
|
1540
1435
|
countInsert = 0, countDelete = 0, textDelete = "", textInsert = "";
|
|
@@ -1548,15 +1443,11 @@ function doLineModeDiff(textA, textB, opts) {
|
|
|
1548
1443
|
}
|
|
1549
1444
|
function computeDiff(text1, text2, opts) {
|
|
1550
1445
|
let diffs;
|
|
1551
|
-
if (!text1)
|
|
1552
|
-
|
|
1553
|
-
if (!text2)
|
|
1554
|
-
return [[DIFF_DELETE, text1]];
|
|
1446
|
+
if (!text1) return [[DIFF_INSERT, text2]];
|
|
1447
|
+
if (!text2) return [[DIFF_DELETE, text1]];
|
|
1555
1448
|
const longtext = text1.length > text2.length ? text1 : text2, shorttext = text1.length > text2.length ? text2 : text1, i = longtext.indexOf(shorttext);
|
|
1556
|
-
if (i !== -1)
|
|
1557
|
-
|
|
1558
|
-
if (shorttext.length === 1)
|
|
1559
|
-
return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
|
|
1449
|
+
if (i !== -1) return diffs = [[DIFF_INSERT, longtext.substring(0, i)], [DIFF_EQUAL, shorttext], [DIFF_INSERT, longtext.substring(i + shorttext.length)]], text1.length > text2.length && (diffs[0][0] = DIFF_DELETE, diffs[2][0] = DIFF_DELETE), diffs;
|
|
1450
|
+
if (shorttext.length === 1) return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
|
|
1560
1451
|
const halfMatch = findHalfMatch(text1, text2);
|
|
1561
1452
|
if (halfMatch) {
|
|
1562
1453
|
const text1A = halfMatch[0], text1B = halfMatch[1], text2A = halfMatch[2], text2B = halfMatch[3], midCommon = halfMatch[4], diffsA = doDiff(text1A, text2A, opts), diffsB = doDiff(text1B, text2B, opts);
|
|
@@ -1564,17 +1455,25 @@ function computeDiff(text1, text2, opts) {
|
|
|
1564
1455
|
}
|
|
1565
1456
|
return opts.checkLines && text1.length > 100 && text2.length > 100 ? doLineModeDiff(text1, text2, opts) : bisect(text1, text2, opts.deadline);
|
|
1566
1457
|
}
|
|
1458
|
+
var __defProp$2 = Object.defineProperty, __getOwnPropSymbols$2 = Object.getOwnPropertySymbols, __hasOwnProp$2 = Object.prototype.hasOwnProperty, __propIsEnum$2 = Object.prototype.propertyIsEnumerable, __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, {
|
|
1459
|
+
enumerable: !0,
|
|
1460
|
+
configurable: !0,
|
|
1461
|
+
writable: !0,
|
|
1462
|
+
value
|
|
1463
|
+
}) : obj[key] = value, __spreadValues$2 = (a, b) => {
|
|
1464
|
+
for (var prop in b || (b = {})) __hasOwnProp$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
1465
|
+
if (__getOwnPropSymbols$2) for (var prop of __getOwnPropSymbols$2(b)) __propIsEnum$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
1466
|
+
return a;
|
|
1467
|
+
};
|
|
1567
1468
|
const DIFF_DELETE = -1, DIFF_INSERT = 1, DIFF_EQUAL = 0;
|
|
1568
1469
|
function diff(textA, textB, opts) {
|
|
1569
|
-
if (textA === null || textB === null)
|
|
1570
|
-
throw new Error("Null input. (diff)");
|
|
1470
|
+
if (textA === null || textB === null) throw new Error("Null input. (diff)");
|
|
1571
1471
|
const diffs = doDiff(textA, textB, createInternalOpts(opts || {}));
|
|
1572
1472
|
return adjustDiffForSurrogatePairs(diffs), diffs;
|
|
1573
1473
|
}
|
|
1574
1474
|
function doDiff(textA, textB, options) {
|
|
1575
1475
|
let text1 = textA, text2 = textB;
|
|
1576
|
-
if (text1 === text2)
|
|
1577
|
-
return text1 ? [[DIFF_EQUAL, text1]] : [];
|
|
1476
|
+
if (text1 === text2) return text1 ? [[DIFF_EQUAL, text1]] : [];
|
|
1578
1477
|
let commonlength = getCommonPrefix(text1, text2);
|
|
1579
1478
|
const commonprefix = text1.substring(0, commonlength);
|
|
1580
1479
|
text1 = text1.substring(commonlength), text2 = text2.substring(commonlength), commonlength = getCommonSuffix(text1, text2);
|
|
@@ -1588,11 +1487,10 @@ function createDeadLine(timeout) {
|
|
|
1588
1487
|
return typeof timeout < "u" && (t = timeout <= 0 ? Number.MAX_VALUE : timeout), Date.now() + t * 1e3;
|
|
1589
1488
|
}
|
|
1590
1489
|
function createInternalOpts(opts) {
|
|
1591
|
-
return {
|
|
1490
|
+
return __spreadValues$2({
|
|
1592
1491
|
checkLines: !0,
|
|
1593
|
-
deadline: createDeadLine(opts.timeout || 1)
|
|
1594
|
-
|
|
1595
|
-
};
|
|
1492
|
+
deadline: createDeadLine(opts.timeout || 1)
|
|
1493
|
+
}, opts);
|
|
1596
1494
|
}
|
|
1597
1495
|
function combineChar(data, char, dir) {
|
|
1598
1496
|
return dir === 1 ? data + char : char + data;
|
|
@@ -1640,9 +1538,93 @@ function adjustDiffForSurrogatePairs(diffs) {
|
|
|
1640
1538
|
const firstChar = diffText[0], lastChar = diffText[diffText.length - 1];
|
|
1641
1539
|
isHighSurrogate(lastChar) && diffType === DIFF_EQUAL && deisolateChar(diffs, i, 1), isLowSurrogate(firstChar) && diffType === DIFF_EQUAL && deisolateChar(diffs, i, -1);
|
|
1642
1540
|
}
|
|
1643
|
-
for (let i = 0; i < diffs.length; i++)
|
|
1644
|
-
|
|
1541
|
+
for (let i = 0; i < diffs.length; i++) diffs[i][1].length === 0 && diffs.splice(i, 1);
|
|
1542
|
+
}
|
|
1543
|
+
function cleanupSemantic(rawDiffs) {
|
|
1544
|
+
let diffs = rawDiffs.map((diff2) => cloneDiff(diff2)), hasChanges = !1;
|
|
1545
|
+
const equalities = [];
|
|
1546
|
+
let equalitiesLength = 0, lastEquality = null, pointer = 0, lengthInsertions1 = 0, lengthDeletions1 = 0, lengthInsertions2 = 0, lengthDeletions2 = 0;
|
|
1547
|
+
for (; pointer < diffs.length; ) diffs[pointer][0] === DIFF_EQUAL ? (equalities[equalitiesLength++] = pointer, lengthInsertions1 = lengthInsertions2, lengthDeletions1 = lengthDeletions2, lengthInsertions2 = 0, lengthDeletions2 = 0, lastEquality = diffs[pointer][1]) : (diffs[pointer][0] === DIFF_INSERT ? lengthInsertions2 += diffs[pointer][1].length : lengthDeletions2 += diffs[pointer][1].length, lastEquality && lastEquality.length <= Math.max(lengthInsertions1, lengthDeletions1) && lastEquality.length <= Math.max(lengthInsertions2, lengthDeletions2) && (diffs.splice(equalities[equalitiesLength - 1], 0, [DIFF_DELETE, lastEquality]), diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT, equalitiesLength--, equalitiesLength--, pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1, lengthInsertions1 = 0, lengthDeletions1 = 0, lengthInsertions2 = 0, lengthDeletions2 = 0, lastEquality = null, hasChanges = !0)), pointer++;
|
|
1548
|
+
for (hasChanges && (diffs = cleanupMerge(diffs)), diffs = cleanupSemanticLossless(diffs), pointer = 1; pointer < diffs.length; ) {
|
|
1549
|
+
if (diffs[pointer - 1][0] === DIFF_DELETE && diffs[pointer][0] === DIFF_INSERT) {
|
|
1550
|
+
const deletion = diffs[pointer - 1][1], insertion = diffs[pointer][1], overlapLength1 = getCommonOverlap(deletion, insertion), overlapLength2 = getCommonOverlap(insertion, deletion);
|
|
1551
|
+
overlapLength1 >= overlapLength2 ? (overlapLength1 >= deletion.length / 2 || overlapLength1 >= insertion.length / 2) && (diffs.splice(pointer, 0, [DIFF_EQUAL, insertion.substring(0, overlapLength1)]), diffs[pointer - 1][1] = deletion.substring(0, deletion.length - overlapLength1), diffs[pointer + 1][1] = insertion.substring(overlapLength1), pointer++) : (overlapLength2 >= deletion.length / 2 || overlapLength2 >= insertion.length / 2) && (diffs.splice(pointer, 0, [DIFF_EQUAL, deletion.substring(0, overlapLength2)]), diffs[pointer - 1][0] = DIFF_INSERT, diffs[pointer - 1][1] = insertion.substring(0, insertion.length - overlapLength2), diffs[pointer + 1][0] = DIFF_DELETE, diffs[pointer + 1][1] = deletion.substring(overlapLength2), pointer++), pointer++;
|
|
1552
|
+
}
|
|
1553
|
+
pointer++;
|
|
1554
|
+
}
|
|
1555
|
+
return diffs;
|
|
1556
|
+
}
|
|
1557
|
+
const nonAlphaNumericRegex = /[^a-zA-Z0-9]/, whitespaceRegex = /\s/, linebreakRegex = /[\r\n]/, blanklineEndRegex = /\n\r?\n$/, blanklineStartRegex = /^\r?\n\r?\n/;
|
|
1558
|
+
function cleanupSemanticLossless(rawDiffs) {
|
|
1559
|
+
const diffs = rawDiffs.map((diff2) => cloneDiff(diff2));
|
|
1560
|
+
function diffCleanupSemanticScore(one, two) {
|
|
1561
|
+
if (!one || !two) return 6;
|
|
1562
|
+
const char1 = one.charAt(one.length - 1), char2 = two.charAt(0), nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex), nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex), whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex), whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex), lineBreak1 = whitespace1 && char1.match(linebreakRegex), lineBreak2 = whitespace2 && char2.match(linebreakRegex), blankLine1 = lineBreak1 && one.match(blanklineEndRegex), blankLine2 = lineBreak2 && two.match(blanklineStartRegex);
|
|
1563
|
+
return blankLine1 || blankLine2 ? 5 : lineBreak1 || lineBreak2 ? 4 : nonAlphaNumeric1 && !whitespace1 && whitespace2 ? 3 : whitespace1 || whitespace2 ? 2 : nonAlphaNumeric1 || nonAlphaNumeric2 ? 1 : 0;
|
|
1564
|
+
}
|
|
1565
|
+
let pointer = 1;
|
|
1566
|
+
for (; pointer < diffs.length - 1; ) {
|
|
1567
|
+
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
|
1568
|
+
let equality1 = diffs[pointer - 1][1], edit = diffs[pointer][1], equality2 = diffs[pointer + 1][1];
|
|
1569
|
+
const commonOffset = getCommonSuffix(equality1, edit);
|
|
1570
|
+
if (commonOffset) {
|
|
1571
|
+
const commonString = edit.substring(edit.length - commonOffset);
|
|
1572
|
+
equality1 = equality1.substring(0, equality1.length - commonOffset), edit = commonString + edit.substring(0, edit.length - commonOffset), equality2 = commonString + equality2;
|
|
1573
|
+
}
|
|
1574
|
+
let bestEquality1 = equality1, bestEdit = edit, bestEquality2 = equality2, bestScore = diffCleanupSemanticScore(equality1, edit) + diffCleanupSemanticScore(edit, equality2);
|
|
1575
|
+
for (; edit.charAt(0) === equality2.charAt(0); ) {
|
|
1576
|
+
equality1 += edit.charAt(0), edit = edit.substring(1) + equality2.charAt(0), equality2 = equality2.substring(1);
|
|
1577
|
+
const score = diffCleanupSemanticScore(equality1, edit) + diffCleanupSemanticScore(edit, equality2);
|
|
1578
|
+
score >= bestScore && (bestScore = score, bestEquality1 = equality1, bestEdit = edit, bestEquality2 = equality2);
|
|
1579
|
+
}
|
|
1580
|
+
diffs[pointer - 1][1] !== bestEquality1 && (bestEquality1 ? diffs[pointer - 1][1] = bestEquality1 : (diffs.splice(pointer - 1, 1), pointer--), diffs[pointer][1] = bestEdit, bestEquality2 ? diffs[pointer + 1][1] = bestEquality2 : (diffs.splice(pointer + 1, 1), pointer--));
|
|
1581
|
+
}
|
|
1582
|
+
pointer++;
|
|
1583
|
+
}
|
|
1584
|
+
return diffs;
|
|
1585
|
+
}
|
|
1586
|
+
function cleanupMerge(rawDiffs) {
|
|
1587
|
+
let diffs = rawDiffs.map((diff2) => cloneDiff(diff2));
|
|
1588
|
+
diffs.push([DIFF_EQUAL, ""]);
|
|
1589
|
+
let pointer = 0, countDelete = 0, countInsert = 0, textDelete = "", textInsert = "", commonlength;
|
|
1590
|
+
for (; pointer < diffs.length; ) switch (diffs[pointer][0]) {
|
|
1591
|
+
case DIFF_INSERT:
|
|
1592
|
+
countInsert++, textInsert += diffs[pointer][1], pointer++;
|
|
1593
|
+
break;
|
|
1594
|
+
case DIFF_DELETE:
|
|
1595
|
+
countDelete++, textDelete += diffs[pointer][1], pointer++;
|
|
1596
|
+
break;
|
|
1597
|
+
case DIFF_EQUAL:
|
|
1598
|
+
countDelete + countInsert > 1 ? (countDelete !== 0 && countInsert !== 0 && (commonlength = getCommonPrefix(textInsert, textDelete), commonlength !== 0 && (pointer - countDelete - countInsert > 0 && diffs[pointer - countDelete - countInsert - 1][0] === DIFF_EQUAL ? diffs[pointer - countDelete - countInsert - 1][1] += textInsert.substring(0, commonlength) : (diffs.splice(0, 0, [DIFF_EQUAL, textInsert.substring(0, commonlength)]), pointer++), textInsert = textInsert.substring(commonlength), textDelete = textDelete.substring(commonlength)), commonlength = getCommonSuffix(textInsert, textDelete), commonlength !== 0 && (diffs[pointer][1] = textInsert.substring(textInsert.length - commonlength) + diffs[pointer][1], textInsert = textInsert.substring(0, textInsert.length - commonlength), textDelete = textDelete.substring(0, textDelete.length - commonlength))), pointer -= countDelete + countInsert, diffs.splice(pointer, countDelete + countInsert), textDelete.length && (diffs.splice(pointer, 0, [DIFF_DELETE, textDelete]), pointer++), textInsert.length && (diffs.splice(pointer, 0, [DIFF_INSERT, textInsert]), pointer++), pointer++) : pointer !== 0 && diffs[pointer - 1][0] === DIFF_EQUAL ? (diffs[pointer - 1][1] += diffs[pointer][1], diffs.splice(pointer, 1)) : pointer++, countInsert = 0, countDelete = 0, textDelete = "", textInsert = "";
|
|
1599
|
+
break;
|
|
1600
|
+
default:
|
|
1601
|
+
throw new Error("Unknown diff operation");
|
|
1602
|
+
}
|
|
1603
|
+
diffs[diffs.length - 1][1] === "" && diffs.pop();
|
|
1604
|
+
let hasChanges = !1;
|
|
1605
|
+
for (pointer = 1; pointer < diffs.length - 1; ) diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL && (diffs[pointer][1].substring(diffs[pointer][1].length - diffs[pointer - 1][1].length) === diffs[pointer - 1][1] ? (diffs[pointer][1] = diffs[pointer - 1][1] + diffs[pointer][1].substring(0, diffs[pointer][1].length - diffs[pointer - 1][1].length), diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1], diffs.splice(pointer - 1, 1), hasChanges = !0) : diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) === diffs[pointer + 1][1] && (diffs[pointer - 1][1] += diffs[pointer + 1][1], diffs[pointer][1] = diffs[pointer][1].substring(diffs[pointer + 1][1].length) + diffs[pointer + 1][1], diffs.splice(pointer + 1, 1), hasChanges = !0)), pointer++;
|
|
1606
|
+
return hasChanges && (diffs = cleanupMerge(diffs)), diffs;
|
|
1607
|
+
}
|
|
1608
|
+
function trueCount(...args) {
|
|
1609
|
+
return args.reduce((n, bool) => n + (bool ? 1 : 0), 0);
|
|
1610
|
+
}
|
|
1611
|
+
function cleanupEfficiency(rawDiffs, editCost = 4) {
|
|
1612
|
+
let diffs = rawDiffs.map((diff2) => cloneDiff(diff2)), hasChanges = !1;
|
|
1613
|
+
const equalities = [];
|
|
1614
|
+
let equalitiesLength = 0, lastEquality = null, pointer = 0, preIns = !1, preDel = !1, postIns = !1, postDel = !1;
|
|
1615
|
+
for (; pointer < diffs.length; ) diffs[pointer][0] === DIFF_EQUAL ? (diffs[pointer][1].length < editCost && (postIns || postDel) ? (equalities[equalitiesLength++] = pointer, preIns = postIns, preDel = postDel, lastEquality = diffs[pointer][1]) : (equalitiesLength = 0, lastEquality = null), postIns = !1, postDel = !1) : (diffs[pointer][0] === DIFF_DELETE ? postDel = !0 : postIns = !0, lastEquality && (preIns && preDel && postIns && postDel || lastEquality.length < editCost / 2 && trueCount(preIns, preDel, postIns, postDel) === 3) && (diffs.splice(equalities[equalitiesLength - 1], 0, [DIFF_DELETE, lastEquality]), diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT, equalitiesLength--, lastEquality = null, preIns && preDel ? (postIns = !0, postDel = !0, equalitiesLength = 0) : (equalitiesLength--, pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1, postIns = !1, postDel = !1), hasChanges = !0)), pointer++;
|
|
1616
|
+
return hasChanges && (diffs = cleanupMerge(diffs)), diffs;
|
|
1645
1617
|
}
|
|
1618
|
+
var __defProp$1 = Object.defineProperty, __getOwnPropSymbols$1 = Object.getOwnPropertySymbols, __hasOwnProp$1 = Object.prototype.hasOwnProperty, __propIsEnum$1 = Object.prototype.propertyIsEnumerable, __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, {
|
|
1619
|
+
enumerable: !0,
|
|
1620
|
+
configurable: !0,
|
|
1621
|
+
writable: !0,
|
|
1622
|
+
value
|
|
1623
|
+
}) : obj[key] = value, __spreadValues$1 = (a, b) => {
|
|
1624
|
+
for (var prop in b || (b = {})) __hasOwnProp$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
1625
|
+
if (__getOwnPropSymbols$1) for (var prop of __getOwnPropSymbols$1(b)) __propIsEnum$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
1626
|
+
return a;
|
|
1627
|
+
};
|
|
1646
1628
|
const DEFAULT_OPTIONS = {
|
|
1647
1629
|
/**
|
|
1648
1630
|
* At what point is no match declared (0.0 = perfection, 1.0 = very loose).
|
|
@@ -1656,16 +1638,11 @@ const DEFAULT_OPTIONS = {
|
|
|
1656
1638
|
distance: 1e3
|
|
1657
1639
|
};
|
|
1658
1640
|
function applyDefaults(options) {
|
|
1659
|
-
return {
|
|
1660
|
-
...DEFAULT_OPTIONS,
|
|
1661
|
-
...options
|
|
1662
|
-
};
|
|
1641
|
+
return __spreadValues$1(__spreadValues$1({}, DEFAULT_OPTIONS), options);
|
|
1663
1642
|
}
|
|
1664
1643
|
const MAX_BITS$1 = 32;
|
|
1665
|
-
function bitap(text, pattern, loc) {
|
|
1666
|
-
|
|
1667
|
-
if (pattern.length > MAX_BITS$1)
|
|
1668
|
-
throw new Error("Pattern too long for this browser.");
|
|
1644
|
+
function bitap(text, pattern, loc, opts = {}) {
|
|
1645
|
+
if (pattern.length > MAX_BITS$1) throw new Error("Pattern too long for this browser.");
|
|
1669
1646
|
const options = applyDefaults(opts), s = getAlphabetFromPattern(pattern);
|
|
1670
1647
|
function getBitapScore(e, x) {
|
|
1671
1648
|
const accuracy = e / pattern.length, proximity = Math.abs(loc - x);
|
|
@@ -1677,8 +1654,7 @@ function bitap(text, pattern, loc) {
|
|
|
1677
1654
|
bestLoc = -1;
|
|
1678
1655
|
let binMin, binMid, binMax = pattern.length + text.length, lastRd = [];
|
|
1679
1656
|
for (let d = 0; d < pattern.length; d++) {
|
|
1680
|
-
for (binMin = 0, binMid = binMax; binMin < binMid; )
|
|
1681
|
-
getBitapScore(d, loc + binMid) <= scoreThreshold ? binMin = binMid : binMax = binMid, binMid = Math.floor((binMax - binMin) / 2 + binMin);
|
|
1657
|
+
for (binMin = 0, binMid = binMax; binMin < binMid; ) getBitapScore(d, loc + binMid) <= scoreThreshold ? binMin = binMid : binMax = binMid, binMid = Math.floor((binMax - binMin) / 2 + binMin);
|
|
1682
1658
|
binMax = binMid;
|
|
1683
1659
|
let start = Math.max(1, loc - binMid + 1);
|
|
1684
1660
|
const finish = Math.min(loc + binMid, text.length) + pattern.length, rd = new Array(finish + 2);
|
|
@@ -1687,106 +1663,40 @@ function bitap(text, pattern, loc) {
|
|
|
1687
1663
|
const charMatch = s[text.charAt(j - 1)];
|
|
1688
1664
|
if (d === 0 ? rd[j] = (rd[j + 1] << 1 | 1) & charMatch : rd[j] = (rd[j + 1] << 1 | 1) & charMatch | ((lastRd[j + 1] | lastRd[j]) << 1 | 1) | lastRd[j + 1], rd[j] & matchmask) {
|
|
1689
1665
|
const score = getBitapScore(d, j - 1);
|
|
1690
|
-
if (score <= scoreThreshold)
|
|
1691
|
-
|
|
1692
|
-
start = Math.max(1, 2 * loc - bestLoc);
|
|
1693
|
-
else
|
|
1694
|
-
break;
|
|
1666
|
+
if (score <= scoreThreshold) if (scoreThreshold = score, bestLoc = j - 1, bestLoc > loc) start = Math.max(1, 2 * loc - bestLoc);
|
|
1667
|
+
else break;
|
|
1695
1668
|
}
|
|
1696
1669
|
}
|
|
1697
|
-
if (getBitapScore(d + 1, loc) > scoreThreshold)
|
|
1698
|
-
break;
|
|
1670
|
+
if (getBitapScore(d + 1, loc) > scoreThreshold) break;
|
|
1699
1671
|
lastRd = rd;
|
|
1700
1672
|
}
|
|
1701
1673
|
return bestLoc;
|
|
1702
1674
|
}
|
|
1703
1675
|
function getAlphabetFromPattern(pattern) {
|
|
1704
1676
|
const s = {};
|
|
1705
|
-
for (let i = 0; i < pattern.length; i++)
|
|
1706
|
-
|
|
1707
|
-
for (let i = 0; i < pattern.length; i++)
|
|
1708
|
-
s[pattern.charAt(i)] |= 1 << pattern.length - i - 1;
|
|
1677
|
+
for (let i = 0; i < pattern.length; i++) s[pattern.charAt(i)] = 0;
|
|
1678
|
+
for (let i = 0; i < pattern.length; i++) s[pattern.charAt(i)] |= 1 << pattern.length - i - 1;
|
|
1709
1679
|
return s;
|
|
1710
1680
|
}
|
|
1711
1681
|
function match(text, pattern, searchLocation) {
|
|
1712
|
-
if (text === null || pattern === null || searchLocation === null)
|
|
1713
|
-
throw new Error("Null input. (match())");
|
|
1682
|
+
if (text === null || pattern === null || searchLocation === null) throw new Error("Null input. (match())");
|
|
1714
1683
|
const loc = Math.max(0, Math.min(searchLocation, text.length));
|
|
1715
|
-
if (text === pattern)
|
|
1716
|
-
return 0;
|
|
1684
|
+
if (text === pattern) return 0;
|
|
1717
1685
|
if (text.length) {
|
|
1718
|
-
if (text.substring(loc, loc + pattern.length) === pattern)
|
|
1719
|
-
return loc;
|
|
1686
|
+
if (text.substring(loc, loc + pattern.length) === pattern) return loc;
|
|
1720
1687
|
} else return -1;
|
|
1721
1688
|
return bitap(text, pattern, loc);
|
|
1722
1689
|
}
|
|
1723
|
-
function createPatchObject(start1, start2) {
|
|
1724
|
-
return {
|
|
1725
|
-
diffs: [],
|
|
1726
|
-
start1,
|
|
1727
|
-
start2,
|
|
1728
|
-
utf8Start1: start1,
|
|
1729
|
-
utf8Start2: start2,
|
|
1730
|
-
length1: 0,
|
|
1731
|
-
length2: 0,
|
|
1732
|
-
utf8Length1: 0,
|
|
1733
|
-
utf8Length2: 0
|
|
1734
|
-
};
|
|
1735
|
-
}
|
|
1736
1690
|
function diffText1(diffs) {
|
|
1737
1691
|
const text = [];
|
|
1738
|
-
for (let x = 0; x < diffs.length; x++)
|
|
1739
|
-
diffs[x][0] !== DIFF_INSERT && (text[x] = diffs[x][1]);
|
|
1692
|
+
for (let x = 0; x < diffs.length; x++) diffs[x][0] !== DIFF_INSERT && (text[x] = diffs[x][1]);
|
|
1740
1693
|
return text.join("");
|
|
1741
1694
|
}
|
|
1742
1695
|
function diffText2(diffs) {
|
|
1743
1696
|
const text = [];
|
|
1744
|
-
for (let x = 0; x < diffs.length; x++)
|
|
1745
|
-
diffs[x][0] !== DIFF_DELETE && (text[x] = diffs[x][1]);
|
|
1697
|
+
for (let x = 0; x < diffs.length; x++) diffs[x][0] !== DIFF_DELETE && (text[x] = diffs[x][1]);
|
|
1746
1698
|
return text.join("");
|
|
1747
1699
|
}
|
|
1748
|
-
function countUtf8Bytes(str) {
|
|
1749
|
-
let bytes = 0;
|
|
1750
|
-
for (let i = 0; i < str.length; i++) {
|
|
1751
|
-
const codePoint = str.codePointAt(i);
|
|
1752
|
-
if (typeof codePoint > "u")
|
|
1753
|
-
throw new Error("Failed to get codepoint");
|
|
1754
|
-
bytes += utf8len(codePoint);
|
|
1755
|
-
}
|
|
1756
|
-
return bytes;
|
|
1757
|
-
}
|
|
1758
|
-
function adjustIndiciesToUcs2(patches, base) {
|
|
1759
|
-
let options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}, byteOffset = 0, idx = 0;
|
|
1760
|
-
function advanceTo(target) {
|
|
1761
|
-
for (; byteOffset < target; ) {
|
|
1762
|
-
const codePoint = base.codePointAt(idx);
|
|
1763
|
-
if (typeof codePoint > "u")
|
|
1764
|
-
return idx;
|
|
1765
|
-
byteOffset += utf8len(codePoint), codePoint > 65535 ? idx += 2 : idx += 1;
|
|
1766
|
-
}
|
|
1767
|
-
if (!options.allowExceedingIndices && byteOffset !== target)
|
|
1768
|
-
throw new Error("Failed to determine byte offset");
|
|
1769
|
-
return idx;
|
|
1770
|
-
}
|
|
1771
|
-
const adjusted = [];
|
|
1772
|
-
for (const patch of patches)
|
|
1773
|
-
adjusted.push({
|
|
1774
|
-
diffs: patch.diffs.map((diff2) => cloneDiff(diff2)),
|
|
1775
|
-
start1: advanceTo(patch.start1),
|
|
1776
|
-
start2: advanceTo(patch.start2),
|
|
1777
|
-
utf8Start1: patch.utf8Start1,
|
|
1778
|
-
utf8Start2: patch.utf8Start2,
|
|
1779
|
-
length1: patch.length1,
|
|
1780
|
-
length2: patch.length2,
|
|
1781
|
-
utf8Length1: patch.utf8Length1,
|
|
1782
|
-
utf8Length2: patch.utf8Length2
|
|
1783
|
-
});
|
|
1784
|
-
return adjusted;
|
|
1785
|
-
}
|
|
1786
|
-
function utf8len(codePoint) {
|
|
1787
|
-
return codePoint <= 127 ? 1 : codePoint <= 2047 ? 2 : codePoint <= 65535 ? 3 : 4;
|
|
1788
|
-
}
|
|
1789
|
-
const MAX_BITS = 32, DEFAULT_MARGIN = 4;
|
|
1790
1700
|
function levenshtein(diffs) {
|
|
1791
1701
|
let leven = 0, insertions = 0, deletions = 0;
|
|
1792
1702
|
for (let x = 0; x < diffs.length; x++) {
|
|
@@ -1809,38 +1719,82 @@ function levenshtein(diffs) {
|
|
|
1809
1719
|
}
|
|
1810
1720
|
function xIndex(diffs, loc) {
|
|
1811
1721
|
let chars1 = 0, chars2 = 0, lastChars1 = 0, lastChars2 = 0, x;
|
|
1812
|
-
for (x = 0; x < diffs.length && (diffs[x][0] !== DIFF_INSERT && (chars1 += diffs[x][1].length), diffs[x][0] !== DIFF_DELETE && (chars2 += diffs[x][1].length), !(chars1 > loc)); x++)
|
|
1813
|
-
lastChars1 = chars1, lastChars2 = chars2;
|
|
1722
|
+
for (x = 0; x < diffs.length && (diffs[x][0] !== DIFF_INSERT && (chars1 += diffs[x][1].length), diffs[x][0] !== DIFF_DELETE && (chars2 += diffs[x][1].length), !(chars1 > loc)); x++) lastChars1 = chars1, lastChars2 = chars2;
|
|
1814
1723
|
return diffs.length !== x && diffs[x][0] === DIFF_DELETE ? lastChars2 : lastChars2 + (loc - lastChars1);
|
|
1815
1724
|
}
|
|
1816
|
-
function
|
|
1817
|
-
|
|
1725
|
+
function countUtf8Bytes(str) {
|
|
1726
|
+
let bytes = 0;
|
|
1727
|
+
for (let i = 0; i < str.length; i++) {
|
|
1728
|
+
const codePoint = str.codePointAt(i);
|
|
1729
|
+
if (typeof codePoint > "u") throw new Error("Failed to get codepoint");
|
|
1730
|
+
bytes += utf8len(codePoint);
|
|
1731
|
+
}
|
|
1732
|
+
return bytes;
|
|
1733
|
+
}
|
|
1734
|
+
function adjustIndiciesToUcs2(patches, base, options = {}) {
|
|
1735
|
+
let byteOffset = 0, idx = 0;
|
|
1736
|
+
function advanceTo(target) {
|
|
1737
|
+
for (; byteOffset < target; ) {
|
|
1738
|
+
const codePoint = base.codePointAt(idx);
|
|
1739
|
+
if (typeof codePoint > "u") return idx;
|
|
1740
|
+
byteOffset += utf8len(codePoint), codePoint > 65535 ? idx += 2 : idx += 1;
|
|
1741
|
+
}
|
|
1742
|
+
if (!options.allowExceedingIndices && byteOffset !== target) throw new Error("Failed to determine byte offset");
|
|
1743
|
+
return idx;
|
|
1744
|
+
}
|
|
1745
|
+
const adjusted = [];
|
|
1746
|
+
for (const patch of patches) adjusted.push({
|
|
1747
|
+
diffs: patch.diffs.map((diff2) => cloneDiff(diff2)),
|
|
1748
|
+
start1: advanceTo(patch.start1),
|
|
1749
|
+
start2: advanceTo(patch.start2),
|
|
1750
|
+
utf8Start1: patch.utf8Start1,
|
|
1751
|
+
utf8Start2: patch.utf8Start2,
|
|
1752
|
+
length1: patch.length1,
|
|
1753
|
+
length2: patch.length2,
|
|
1754
|
+
utf8Length1: patch.utf8Length1,
|
|
1755
|
+
utf8Length2: patch.utf8Length2
|
|
1756
|
+
});
|
|
1757
|
+
return adjusted;
|
|
1758
|
+
}
|
|
1759
|
+
function utf8len(codePoint) {
|
|
1760
|
+
return codePoint <= 127 ? 1 : codePoint <= 2047 ? 2 : codePoint <= 65535 ? 3 : 4;
|
|
1761
|
+
}
|
|
1762
|
+
const MAX_BITS = 32, DEFAULT_MARGIN = 4;
|
|
1763
|
+
function addPadding(patches, margin = DEFAULT_MARGIN) {
|
|
1764
|
+
const paddingLength = margin;
|
|
1818
1765
|
let nullPadding = "";
|
|
1819
|
-
for (let x = 1; x <= paddingLength; x++)
|
|
1820
|
-
|
|
1821
|
-
for (const p of patches)
|
|
1822
|
-
p.start1 += paddingLength, p.start2 += paddingLength, p.utf8Start1 += paddingLength, p.utf8Start2 += paddingLength;
|
|
1766
|
+
for (let x = 1; x <= paddingLength; x++) nullPadding += String.fromCharCode(x);
|
|
1767
|
+
for (const p of patches) p.start1 += paddingLength, p.start2 += paddingLength, p.utf8Start1 += paddingLength, p.utf8Start2 += paddingLength;
|
|
1823
1768
|
let patch = patches[0], diffs = patch.diffs;
|
|
1824
|
-
if (diffs.length === 0 || diffs[0][0] !== DIFF_EQUAL)
|
|
1825
|
-
diffs.unshift([DIFF_EQUAL, nullPadding]), patch.start1 -= paddingLength, patch.start2 -= paddingLength, patch.utf8Start1 -= paddingLength, patch.utf8Start2 -= paddingLength, patch.length1 += paddingLength, patch.length2 += paddingLength, patch.utf8Length1 += paddingLength, patch.utf8Length2 += paddingLength;
|
|
1769
|
+
if (diffs.length === 0 || diffs[0][0] !== DIFF_EQUAL) diffs.unshift([DIFF_EQUAL, nullPadding]), patch.start1 -= paddingLength, patch.start2 -= paddingLength, patch.utf8Start1 -= paddingLength, patch.utf8Start2 -= paddingLength, patch.length1 += paddingLength, patch.length2 += paddingLength, patch.utf8Length1 += paddingLength, patch.utf8Length2 += paddingLength;
|
|
1826
1770
|
else if (paddingLength > diffs[0][1].length) {
|
|
1827
1771
|
const firstDiffLength = diffs[0][1].length, extraLength = paddingLength - firstDiffLength;
|
|
1828
1772
|
diffs[0][1] = nullPadding.substring(firstDiffLength) + diffs[0][1], patch.start1 -= extraLength, patch.start2 -= extraLength, patch.utf8Start1 -= extraLength, patch.utf8Start2 -= extraLength, patch.length1 += extraLength, patch.length2 += extraLength, patch.utf8Length1 += extraLength, patch.utf8Length2 += extraLength;
|
|
1829
1773
|
}
|
|
1830
|
-
if (patch = patches[patches.length - 1], diffs = patch.diffs, diffs.length === 0 || diffs[diffs.length - 1][0] !== DIFF_EQUAL)
|
|
1831
|
-
diffs.push([DIFF_EQUAL, nullPadding]), patch.length1 += paddingLength, patch.length2 += paddingLength, patch.utf8Length1 += paddingLength, patch.utf8Length2 += paddingLength;
|
|
1774
|
+
if (patch = patches[patches.length - 1], diffs = patch.diffs, diffs.length === 0 || diffs[diffs.length - 1][0] !== DIFF_EQUAL) diffs.push([DIFF_EQUAL, nullPadding]), patch.length1 += paddingLength, patch.length2 += paddingLength, patch.utf8Length1 += paddingLength, patch.utf8Length2 += paddingLength;
|
|
1832
1775
|
else if (paddingLength > diffs[diffs.length - 1][1].length) {
|
|
1833
1776
|
const extraLength = paddingLength - diffs[diffs.length - 1][1].length;
|
|
1834
1777
|
diffs[diffs.length - 1][1] += nullPadding.substring(0, extraLength), patch.length1 += extraLength, patch.length2 += extraLength, patch.utf8Length1 += extraLength, patch.utf8Length2 += extraLength;
|
|
1835
1778
|
}
|
|
1836
1779
|
return nullPadding;
|
|
1837
1780
|
}
|
|
1838
|
-
function
|
|
1839
|
-
|
|
1781
|
+
function createPatchObject(start1, start2) {
|
|
1782
|
+
return {
|
|
1783
|
+
diffs: [],
|
|
1784
|
+
start1,
|
|
1785
|
+
start2,
|
|
1786
|
+
utf8Start1: start1,
|
|
1787
|
+
utf8Start2: start2,
|
|
1788
|
+
length1: 0,
|
|
1789
|
+
length2: 0,
|
|
1790
|
+
utf8Length1: 0,
|
|
1791
|
+
utf8Length2: 0
|
|
1792
|
+
};
|
|
1793
|
+
}
|
|
1794
|
+
function splitMax(patches, margin = DEFAULT_MARGIN) {
|
|
1840
1795
|
const patchSize = MAX_BITS;
|
|
1841
1796
|
for (let x = 0; x < patches.length; x++) {
|
|
1842
|
-
if (patches[x].length1 <= patchSize)
|
|
1843
|
-
continue;
|
|
1797
|
+
if (patches[x].length1 <= patchSize) continue;
|
|
1844
1798
|
const bigpatch = patches[x];
|
|
1845
1799
|
patches.splice(x--, 1);
|
|
1846
1800
|
let start1 = bigpatch.start1, start2 = bigpatch.start2, preContext = "";
|
|
@@ -1866,13 +1820,10 @@ function splitMax(patches) {
|
|
|
1866
1820
|
}
|
|
1867
1821
|
}
|
|
1868
1822
|
}
|
|
1869
|
-
function apply(patches, originalText) {
|
|
1870
|
-
|
|
1871
|
-
if (typeof patches == "string")
|
|
1872
|
-
throw new Error("Patches must be an array - pass the patch to `parsePatch()` first");
|
|
1823
|
+
function apply(patches, originalText, opts = {}) {
|
|
1824
|
+
if (typeof patches == "string") throw new Error("Patches must be an array - pass the patch to `parsePatch()` first");
|
|
1873
1825
|
let text = originalText;
|
|
1874
|
-
if (patches.length === 0)
|
|
1875
|
-
return [text, []];
|
|
1826
|
+
if (patches.length === 0) return [text, []];
|
|
1876
1827
|
const parsed = adjustIndiciesToUcs2(patches, text, {
|
|
1877
1828
|
allowExceedingIndices: opts.allowExceedingIndices
|
|
1878
1829
|
}), margin = opts.margin || DEFAULT_MARGIN, deleteThreshold = opts.deleteThreshold || 0.4, nullPadding = addPadding(parsed, margin);
|
|
@@ -1882,19 +1833,16 @@ function apply(patches, originalText) {
|
|
|
1882
1833
|
for (let x = 0; x < parsed.length; x++) {
|
|
1883
1834
|
const expectedLoc = parsed[x].start2 + delta, text1 = diffText1(parsed[x].diffs);
|
|
1884
1835
|
let startLoc, endLoc = -1;
|
|
1885
|
-
if (text1.length > MAX_BITS ? (startLoc = match(text, text1.substring(0, MAX_BITS), expectedLoc), startLoc !== -1 && (endLoc = match(text, text1.substring(text1.length - MAX_BITS), expectedLoc + text1.length - MAX_BITS), (endLoc === -1 || startLoc >= endLoc) && (startLoc = -1))) : startLoc = match(text, text1, expectedLoc), startLoc === -1)
|
|
1886
|
-
results[x] = !1, delta -= parsed[x].length2 - parsed[x].length1;
|
|
1836
|
+
if (text1.length > MAX_BITS ? (startLoc = match(text, text1.substring(0, MAX_BITS), expectedLoc), startLoc !== -1 && (endLoc = match(text, text1.substring(text1.length - MAX_BITS), expectedLoc + text1.length - MAX_BITS), (endLoc === -1 || startLoc >= endLoc) && (startLoc = -1))) : startLoc = match(text, text1, expectedLoc), startLoc === -1) results[x] = !1, delta -= parsed[x].length2 - parsed[x].length1;
|
|
1887
1837
|
else {
|
|
1888
1838
|
results[x] = !0, delta = startLoc - expectedLoc;
|
|
1889
1839
|
let text2;
|
|
1890
|
-
if (endLoc === -1 ? text2 = text.substring(startLoc, startLoc + text1.length) : text2 = text.substring(startLoc, endLoc + MAX_BITS), text1 === text2)
|
|
1891
|
-
text = text.substring(0, startLoc) + diffText2(parsed[x].diffs) + text.substring(startLoc + text1.length);
|
|
1840
|
+
if (endLoc === -1 ? text2 = text.substring(startLoc, startLoc + text1.length) : text2 = text.substring(startLoc, endLoc + MAX_BITS), text1 === text2) text = text.substring(0, startLoc) + diffText2(parsed[x].diffs) + text.substring(startLoc + text1.length);
|
|
1892
1841
|
else {
|
|
1893
1842
|
let diffs = diff(text1, text2, {
|
|
1894
1843
|
checkLines: !1
|
|
1895
1844
|
});
|
|
1896
|
-
if (text1.length > MAX_BITS && levenshtein(diffs) / text1.length > deleteThreshold)
|
|
1897
|
-
results[x] = !1;
|
|
1845
|
+
if (text1.length > MAX_BITS && levenshtein(diffs) / text1.length > deleteThreshold) results[x] = !1;
|
|
1898
1846
|
else {
|
|
1899
1847
|
diffs = cleanupSemanticLossless(diffs);
|
|
1900
1848
|
let index1 = 0, index2 = 0;
|
|
@@ -1910,20 +1858,17 @@ function apply(patches, originalText) {
|
|
|
1910
1858
|
}
|
|
1911
1859
|
const patchHeader = /^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$/;
|
|
1912
1860
|
function parse(textline) {
|
|
1913
|
-
if (!textline)
|
|
1914
|
-
return [];
|
|
1861
|
+
if (!textline) return [];
|
|
1915
1862
|
const patches = [], lines = textline.split(`
|
|
1916
1863
|
`);
|
|
1917
1864
|
let textPointer = 0;
|
|
1918
1865
|
for (; textPointer < lines.length; ) {
|
|
1919
1866
|
const m = lines[textPointer].match(patchHeader);
|
|
1920
|
-
if (!m)
|
|
1921
|
-
throw new Error("Invalid patch string: ".concat(lines[textPointer]));
|
|
1867
|
+
if (!m) throw new Error(`Invalid patch string: ${lines[textPointer]}`);
|
|
1922
1868
|
const patch = createPatchObject(toInt(m[1]), toInt(m[3]));
|
|
1923
1869
|
for (patches.push(patch), m[2] === "" ? (patch.start1--, patch.utf8Start1--, patch.length1 = 1, patch.utf8Length1 = 1) : m[2] === "0" ? (patch.length1 = 0, patch.utf8Length1 = 0) : (patch.start1--, patch.utf8Start1--, patch.utf8Length1 = toInt(m[2]), patch.length1 = patch.utf8Length1), m[4] === "" ? (patch.start2--, patch.utf8Start2--, patch.length2 = 1, patch.utf8Length2 = 1) : m[4] === "0" ? (patch.length2 = 0, patch.utf8Length2 = 0) : (patch.start2--, patch.utf8Start2--, patch.utf8Length2 = toInt(m[4]), patch.length2 = patch.utf8Length2), textPointer++; textPointer < lines.length; ) {
|
|
1924
1870
|
const currentLine = lines[textPointer], sign = currentLine.charAt(0);
|
|
1925
|
-
if (sign === "@")
|
|
1926
|
-
break;
|
|
1871
|
+
if (sign === "@") break;
|
|
1927
1872
|
if (sign === "") {
|
|
1928
1873
|
textPointer++;
|
|
1929
1874
|
continue;
|
|
@@ -1932,17 +1877,13 @@ function parse(textline) {
|
|
|
1932
1877
|
try {
|
|
1933
1878
|
line = decodeURI(currentLine.slice(1));
|
|
1934
1879
|
} catch {
|
|
1935
|
-
throw new Error(
|
|
1880
|
+
throw new Error(`Illegal escape in parse: ${currentLine}`);
|
|
1936
1881
|
}
|
|
1937
1882
|
const utf8Diff = countUtf8Bytes(line) - line.length;
|
|
1938
|
-
if (sign === "-")
|
|
1939
|
-
|
|
1940
|
-
else if (sign === "
|
|
1941
|
-
|
|
1942
|
-
else if (sign === " ")
|
|
1943
|
-
patch.diffs.push([DIFF_EQUAL, line]), patch.length1 -= utf8Diff, patch.length2 -= utf8Diff;
|
|
1944
|
-
else
|
|
1945
|
-
throw new Error('Invalid patch mode "'.concat(sign, '" in: ').concat(line));
|
|
1883
|
+
if (sign === "-") patch.diffs.push([DIFF_DELETE, line]), patch.length1 -= utf8Diff;
|
|
1884
|
+
else if (sign === "+") patch.diffs.push([DIFF_INSERT, line]), patch.length2 -= utf8Diff;
|
|
1885
|
+
else if (sign === " ") patch.diffs.push([DIFF_EQUAL, line]), patch.length1 -= utf8Diff, patch.length2 -= utf8Diff;
|
|
1886
|
+
else throw new Error(`Invalid patch mode "${sign}" in: ${line}`);
|
|
1946
1887
|
textPointer++;
|
|
1947
1888
|
}
|
|
1948
1889
|
}
|
|
@@ -2685,7 +2626,12 @@ function Synchronizer(props) {
|
|
|
2685
2626
|
type: "has pending patches"
|
|
2686
2627
|
}), event.type === "mutation" && (syncActorRef.send({
|
|
2687
2628
|
type: "mutation"
|
|
2688
|
-
}), editorActor.send(
|
|
2629
|
+
}), editorActor.send({
|
|
2630
|
+
type: "mutation",
|
|
2631
|
+
patches: event.patches,
|
|
2632
|
+
snapshot: event.snapshot,
|
|
2633
|
+
value: event.snapshot
|
|
2634
|
+
}));
|
|
2689
2635
|
});
|
|
2690
2636
|
return () => {
|
|
2691
2637
|
subscription.unsubscribe();
|
|
@@ -5829,11 +5775,12 @@ const editorMachine = setup({
|
|
|
5829
5775
|
if (eventBehaviors.length === 0) {
|
|
5830
5776
|
if (!defaultAction)
|
|
5831
5777
|
return;
|
|
5832
|
-
|
|
5833
|
-
|
|
5834
|
-
|
|
5835
|
-
|
|
5836
|
-
|
|
5778
|
+
Editor.withoutNormalizing(event.editor, () => {
|
|
5779
|
+
performAction({
|
|
5780
|
+
context,
|
|
5781
|
+
action: defaultAction
|
|
5782
|
+
});
|
|
5783
|
+
}), event.editor.onChange();
|
|
5837
5784
|
return;
|
|
5838
5785
|
}
|
|
5839
5786
|
const value = fromSlateValue(event.editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(event.editor)), selection = toPortableTextRange(value, event.editor.selection, context.schema), editorContext = {
|
|
@@ -5859,10 +5806,20 @@ const editorMachine = setup({
|
|
|
5859
5806
|
event: event.behaviorEvent
|
|
5860
5807
|
}, shouldRun));
|
|
5861
5808
|
for (const actionIntends of actionIntendSets)
|
|
5862
|
-
behaviorOverwritten = behaviorOverwritten || actionIntends.length > 0 && actionIntends.some((actionIntend) => actionIntend.type !== "effect"),
|
|
5863
|
-
|
|
5864
|
-
|
|
5865
|
-
|
|
5809
|
+
behaviorOverwritten = behaviorOverwritten || actionIntends.length > 0 && actionIntends.some((actionIntend) => actionIntend.type !== "effect"), Editor.withoutNormalizing(event.editor, () => {
|
|
5810
|
+
for (const actionIntend of actionIntends) {
|
|
5811
|
+
const action = {
|
|
5812
|
+
...actionIntend,
|
|
5813
|
+
editor: event.editor
|
|
5814
|
+
};
|
|
5815
|
+
performAction({
|
|
5816
|
+
context,
|
|
5817
|
+
action
|
|
5818
|
+
});
|
|
5819
|
+
}
|
|
5820
|
+
}), event.editor.onChange(), actionIntends.some((actionIntend) => actionIntend.type === "reselect") && enqueue.raise({
|
|
5821
|
+
type: "selection",
|
|
5822
|
+
selection: toPortableTextRange(event.editor.children, event.editor.selection, context.schema)
|
|
5866
5823
|
});
|
|
5867
5824
|
if (behaviorOverwritten) {
|
|
5868
5825
|
event.nativeEvent?.preventDefault();
|
|
@@ -5872,11 +5829,12 @@ const editorMachine = setup({
|
|
|
5872
5829
|
if (!behaviorOverwritten) {
|
|
5873
5830
|
if (!defaultAction)
|
|
5874
5831
|
return;
|
|
5875
|
-
|
|
5876
|
-
|
|
5877
|
-
|
|
5878
|
-
|
|
5879
|
-
|
|
5832
|
+
Editor.withoutNormalizing(event.editor, () => {
|
|
5833
|
+
performAction({
|
|
5834
|
+
context,
|
|
5835
|
+
action: defaultAction
|
|
5836
|
+
});
|
|
5837
|
+
}), event.editor.onChange();
|
|
5880
5838
|
}
|
|
5881
5839
|
})
|
|
5882
5840
|
}
|
|
@@ -5968,34 +5926,6 @@ const editorMachine = setup({
|
|
|
5968
5926
|
event
|
|
5969
5927
|
}) => event.maxBlocks
|
|
5970
5928
|
})
|
|
5971
|
-
},
|
|
5972
|
-
"behavior action intends": {
|
|
5973
|
-
actions: [({
|
|
5974
|
-
context,
|
|
5975
|
-
event
|
|
5976
|
-
}) => {
|
|
5977
|
-
Editor.withoutNormalizing(event.editor, () => {
|
|
5978
|
-
for (const actionIntend of event.actionIntends) {
|
|
5979
|
-
const action = {
|
|
5980
|
-
...actionIntend,
|
|
5981
|
-
editor: event.editor
|
|
5982
|
-
};
|
|
5983
|
-
performAction({
|
|
5984
|
-
context,
|
|
5985
|
-
action
|
|
5986
|
-
});
|
|
5987
|
-
}
|
|
5988
|
-
}), event.editor.onChange();
|
|
5989
|
-
}, enqueueActions(({
|
|
5990
|
-
context,
|
|
5991
|
-
event,
|
|
5992
|
-
enqueue
|
|
5993
|
-
}) => {
|
|
5994
|
-
event.actionIntends.some((actionIntend) => actionIntend.type === "reselect") && enqueue.raise({
|
|
5995
|
-
type: "selection",
|
|
5996
|
-
selection: toPortableTextRange(event.editor.children, event.editor.selection, context.schema)
|
|
5997
|
-
});
|
|
5998
|
-
})]
|
|
5999
5929
|
}
|
|
6000
5930
|
},
|
|
6001
5931
|
type: "parallel",
|
|
@@ -6156,7 +6086,48 @@ const editorMachine = setup({
|
|
|
6156
6086
|
}
|
|
6157
6087
|
}
|
|
6158
6088
|
}
|
|
6159
|
-
})
|
|
6089
|
+
});
|
|
6090
|
+
function getValue({
|
|
6091
|
+
editorActorSnapshot,
|
|
6092
|
+
slateEditorInstance
|
|
6093
|
+
}) {
|
|
6094
|
+
return fromSlateValue(slateEditorInstance.children, editorActorSnapshot.context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(slateEditorInstance));
|
|
6095
|
+
}
|
|
6096
|
+
function defaultCompare(a, b) {
|
|
6097
|
+
return a === b;
|
|
6098
|
+
}
|
|
6099
|
+
function useEditorSelector(editor, selector, t0) {
|
|
6100
|
+
const $ = c(3), compare = t0 === void 0 ? defaultCompare : t0;
|
|
6101
|
+
let t1;
|
|
6102
|
+
return $[0] !== editor._internal.slateEditor.instance || $[1] !== selector ? (t1 = (editorActorSnapshot) => {
|
|
6103
|
+
const snapshot = getEditorSnapshot({
|
|
6104
|
+
editorActorSnapshot,
|
|
6105
|
+
slateEditorInstance: editor._internal.slateEditor.instance
|
|
6106
|
+
});
|
|
6107
|
+
return selector(snapshot);
|
|
6108
|
+
}, $[0] = editor._internal.slateEditor.instance, $[1] = selector, $[2] = t1) : t1 = $[2], useSelector(editor._internal.editorActor, t1, compare);
|
|
6109
|
+
}
|
|
6110
|
+
function getEditorSnapshot({
|
|
6111
|
+
editorActorSnapshot,
|
|
6112
|
+
slateEditorInstance
|
|
6113
|
+
}) {
|
|
6114
|
+
return {
|
|
6115
|
+
context: {
|
|
6116
|
+
activeDecorators: getActiveDecorators({
|
|
6117
|
+
schema: editorActorSnapshot.context.schema,
|
|
6118
|
+
slateEditorInstance
|
|
6119
|
+
}),
|
|
6120
|
+
keyGenerator: editorActorSnapshot.context.keyGenerator,
|
|
6121
|
+
schema: editorActorSnapshot.context.schema,
|
|
6122
|
+
selection: editorActorSnapshot.context.selection,
|
|
6123
|
+
value: getValue({
|
|
6124
|
+
editorActorSnapshot,
|
|
6125
|
+
slateEditorInstance
|
|
6126
|
+
})
|
|
6127
|
+
}
|
|
6128
|
+
};
|
|
6129
|
+
}
|
|
6130
|
+
const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
|
|
6160
6131
|
let table;
|
|
6161
6132
|
return () => {
|
|
6162
6133
|
if (table)
|
|
@@ -6208,6 +6179,10 @@ function createEditorFromActor(editorActor) {
|
|
|
6208
6179
|
editorActor
|
|
6209
6180
|
}), editable = createEditableAPI(slateEditor.instance, editorActor);
|
|
6210
6181
|
return {
|
|
6182
|
+
getSnapshot: () => getEditorSnapshot({
|
|
6183
|
+
editorActorSnapshot: editorActor.getSnapshot(),
|
|
6184
|
+
slateEditorInstance: slateEditor.instance
|
|
6185
|
+
}),
|
|
6211
6186
|
send: (event) => {
|
|
6212
6187
|
editorActor.send(event);
|
|
6213
6188
|
},
|
|
@@ -7204,31 +7179,6 @@ function EditorEventListener(props) {
|
|
|
7204
7179
|
};
|
|
7205
7180
|
}, t1 = [editor, on], $[0] = editor, $[1] = on, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), useEffect(t0, t1), null;
|
|
7206
7181
|
}
|
|
7207
|
-
function getValue(editor) {
|
|
7208
|
-
return fromSlateValue(editor._internal.slateEditor.instance.children, editor._internal.editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor._internal.slateEditor.instance));
|
|
7209
|
-
}
|
|
7210
|
-
function defaultCompare(a, b) {
|
|
7211
|
-
return a === b;
|
|
7212
|
-
}
|
|
7213
|
-
function useEditorSelector(editor, selector, t0) {
|
|
7214
|
-
const $ = c(3), compare = t0 === void 0 ? defaultCompare : t0;
|
|
7215
|
-
let t1;
|
|
7216
|
-
return $[0] !== editor || $[1] !== selector ? (t1 = (snapshot) => {
|
|
7217
|
-
const context = {
|
|
7218
|
-
activeDecorators: getActiveDecorators({
|
|
7219
|
-
schema: snapshot.context.schema,
|
|
7220
|
-
slateEditorInstance: editor._internal.slateEditor.instance
|
|
7221
|
-
}),
|
|
7222
|
-
keyGenerator: snapshot.context.keyGenerator,
|
|
7223
|
-
schema: snapshot.context.schema,
|
|
7224
|
-
selection: snapshot.context.selection,
|
|
7225
|
-
value: getValue(editor)
|
|
7226
|
-
};
|
|
7227
|
-
return selector({
|
|
7228
|
-
context
|
|
7229
|
-
});
|
|
7230
|
-
}, $[0] = editor, $[1] = selector, $[2] = t1) : t1 = $[2], useSelector(editor._internal.editorActor, t1, compare);
|
|
7231
|
-
}
|
|
7232
7182
|
export {
|
|
7233
7183
|
EditorEventListener,
|
|
7234
7184
|
EditorProvider,
|