2019-05-06 19:21:28 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 17:16:32 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-06 19:21:28 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
export interface IDiff {
|
|
|
|
removed?: string;
|
|
|
|
added?: string;
|
|
|
|
at?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
function firstDiff(a: string, b: string) {
|
2019-05-06 19:21:28 +03: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 11:45:45 +03:00
|
|
|
function diffStringsAtEnd(oldStr: string, newStr: string): IDiff {
|
2019-05-06 19:21:28 +03:00
|
|
|
const len = Math.min(oldStr.length, newStr.length);
|
|
|
|
const startInCommon = oldStr.substr(0, len) === newStr.substr(0, len);
|
|
|
|
if (startInCommon && oldStr.length > newStr.length) {
|
|
|
|
return {removed: oldStr.substr(len), at: len};
|
|
|
|
} else if (startInCommon && oldStr.length < newStr.length) {
|
|
|
|
return {added: newStr.substr(len), at: len};
|
|
|
|
} else {
|
|
|
|
const commonStartLen = firstDiff(oldStr, newStr);
|
|
|
|
return {
|
|
|
|
removed: oldStr.substr(commonStartLen),
|
|
|
|
added: newStr.substr(commonStartLen),
|
|
|
|
at: commonStartLen,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 19:09:31 +03:00
|
|
|
// assumes only characters have been deleted at one location in the string, and none added
|
2020-07-15 11:45:45 +03:00
|
|
|
export function diffDeletion(oldStr: string, newStr: string): IDiff {
|
2019-05-06 19:21:28 +03:00
|
|
|
if (oldStr === newStr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const firstDiffIdx = firstDiff(oldStr, newStr);
|
2019-07-18 19:09:31 +03:00
|
|
|
const amount = oldStr.length - newStr.length;
|
|
|
|
return {at: firstDiffIdx, removed: oldStr.substr(firstDiffIdx, amount)};
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:26:18 +03: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 13:34:30 +03: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 18:26:18 +03:00
|
|
|
*/
|
2020-07-15 11:45:45 +03:00
|
|
|
export function diffAtCaret(oldValue: string, newValue: string, caretPosition: number): IDiff {
|
2019-05-06 19:21:28 +03:00
|
|
|
const diffLen = newValue.length - oldValue.length;
|
|
|
|
const caretPositionBeforeInput = caretPosition - diffLen;
|
|
|
|
const oldValueBeforeCaret = oldValue.substr(0, caretPositionBeforeInput);
|
|
|
|
const newValueBeforeCaret = newValue.substr(0, caretPosition);
|
|
|
|
return diffStringsAtEnd(oldValueBeforeCaret, newValueBeforeCaret);
|
|
|
|
}
|