From f7a19cab866735c8485a25bad4d7abc817d066dd Mon Sep 17 00:00:00 2001 From: Aswin S Date: Tue, 23 Jan 2024 09:50:32 +0530 Subject: [PATCH] fix: common suffix length calculation --- src/libs/ComposerUtils/index.ts | 6 ++++-- .../ComposerWithSuggestions/ComposerWithSuggestions.js | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libs/ComposerUtils/index.ts b/src/libs/ComposerUtils/index.ts index 54af287a67b7..c0e01e3e751b 100644 --- a/src/libs/ComposerUtils/index.ts +++ b/src/libs/ComposerUtils/index.ts @@ -38,11 +38,13 @@ function canSkipTriggerHotkeys(isSmallScreenWidth: boolean, isKeyboardShown: boo * Finds the length of common suffix between two texts * @param str1 - first string to compare * @param str2 - next string to compare + * @param cursorPosition - position of cursor * @returns number - Length of the common suffix */ -function findCommonSuffixLength(str1: string, str2: string) { +function findCommonSuffixLength(str1: string, str2: string, cursorPosition: number) { let commonSuffixLength = 0; - const minLength = Math.min(str1.length, str2.length); + const minLength = Math.min(str1.length - cursorPosition, str2.length); + for (let i = 1; i <= minLength; i++) { if (str1.charAt(str1.length - i) === str2.charAt(str2.length - i)) { commonSuffixLength++; diff --git a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.js b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.js index 0706fa4fc8e2..90738c66579c 100644 --- a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.js +++ b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.js @@ -213,7 +213,7 @@ function ComposerWithSuggestions({ if (currentIndex < newText.length) { startIndex = currentIndex; - const commonSuffixLength = ComposerUtils.findCommonSuffixLength(prevText, newText); + const commonSuffixLength = ComposerUtils.findCommonSuffixLength(prevText, newText, selection.end); // if text is getting pasted over find length of common suffix and subtract it from new text length if (commonSuffixLength > 0 || selection.end - selection.start > 0) { endIndex = newText.length - commonSuffixLength; @@ -228,7 +228,7 @@ function ComposerWithSuggestions({ diff: newText.substring(startIndex, endIndex), }; }, - [selection.end, selection.start], + [selection.start, selection.end], ); /**