2019-05-06 18:21:28 +02:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2019-2024 New Vector Ltd.
|
2019-05-22 16:16:32 +02:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-06 18:21:28 +02:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-05-06 18:21:28 +02:00
|
|
|
*/
|
|
|
|
|
|
2020-07-15 09:45:45 +01:00
|
|
|
export interface IDiff {
|
|
|
|
|
removed?: string;
|
|
|
|
|
added?: string;
|
|
|
|
|
at?: number;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-12 13:26:34 +01:00
|
|
|
function firstDiff(a: string, b: string): number {
|
2019-05-06 18:21:28 +02:00
|
|
|
const compareLen = Math.min(a.length, b.length);
|
|
|
|
|
for (let i = 0; i < compareLen; ++i) {
|
|
|
|
|
if (a[i] !== b[i]) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return compareLen;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 09:45:45 +01:00
|
|
|
function diffStringsAtEnd(oldStr: string, newStr: string): IDiff {
|
2019-05-06 18:21:28 +02:00
|
|
|
const len = Math.min(oldStr.length, newStr.length);
|
2022-04-14 09:52:42 +02:00
|
|
|
const startInCommon = oldStr.slice(0, len) === newStr.slice(0, len);
|
2019-05-06 18:21:28 +02:00
|
|
|
if (startInCommon && oldStr.length > newStr.length) {
|
2022-04-14 09:52:42 +02:00
|
|
|
return { removed: oldStr.slice(len), at: len };
|
2019-05-06 18:21:28 +02:00
|
|
|
} else if (startInCommon && oldStr.length < newStr.length) {
|
2022-04-14 09:52:42 +02:00
|
|
|
return { added: newStr.slice(len), at: len };
|
2019-05-06 18:21:28 +02:00
|
|
|
} else {
|
|
|
|
|
const commonStartLen = firstDiff(oldStr, newStr);
|
|
|
|
|
return {
|
2022-04-14 09:52:42 +02:00
|
|
|
removed: oldStr.slice(commonStartLen),
|
|
|
|
|
added: newStr.slice(commonStartLen),
|
2019-05-06 18:21:28 +02:00
|
|
|
at: commonStartLen,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 18:09:31 +02:00
|
|
|
// assumes only characters have been deleted at one location in the string, and none added
|
2020-07-15 09:45:45 +01:00
|
|
|
export function diffDeletion(oldStr: string, newStr: string): IDiff {
|
2019-05-06 18:21:28 +02:00
|
|
|
if (oldStr === newStr) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
const firstDiffIdx = firstDiff(oldStr, newStr);
|
2019-07-18 18:09:31 +02:00
|
|
|
const amount = oldStr.length - newStr.length;
|
2022-04-14 09:52:42 +02:00
|
|
|
return { at: firstDiffIdx, removed: oldStr.slice(firstDiffIdx, firstDiffIdx + amount) };
|
2019-05-06 18:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-24 17:26:18 +02:00
|
|
|
/**
|
|
|
|
|
* Calculates which string was added and removed around the caret position
|
|
|
|
|
* @param {String} oldValue the previous value
|
|
|
|
|
* @param {String} newValue the new value
|
|
|
|
|
* @param {Number} caretPosition the position of the caret after `newValue` was applied.
|
2019-07-30 12:34:30 +02:00
|
|
|
* @return {object} an object with `at` as the offset where characters were removed and/or added,
|
|
|
|
|
* `added` with the added string (if any), and
|
|
|
|
|
* `removed` with the removed string (if any)
|
2019-07-24 17:26:18 +02:00
|
|
|
*/
|
2020-07-15 09:45:45 +01:00
|
|
|
export function diffAtCaret(oldValue: string, newValue: string, caretPosition: number): IDiff {
|
2019-05-06 18:21:28 +02:00
|
|
|
const diffLen = newValue.length - oldValue.length;
|
|
|
|
|
const caretPositionBeforeInput = caretPosition - diffLen;
|
2022-04-14 09:52:42 +02:00
|
|
|
const oldValueBeforeCaret = oldValue.substring(0, caretPositionBeforeInput);
|
|
|
|
|
const newValueBeforeCaret = newValue.substring(0, caretPosition);
|
2019-05-06 18:21:28 +02:00
|
|
|
return diffStringsAtEnd(oldValueBeforeCaret, newValueBeforeCaret);
|
|
|
|
|
}
|