From 31096c5b44b33717f668dc7a350fd2c994ec4596 Mon Sep 17 00:00:00 2001 From: Ali Toshmatov Date: Thu, 13 Jul 2023 22:10:14 +0500 Subject: [PATCH 1/2] Added getCommonEndingLength and implemented it --- src/libs/ComposerUtils/index.js | 16 +++++++++++++++- src/pages/home/report/ReportActionCompose.js | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/libs/ComposerUtils/index.js b/src/libs/ComposerUtils/index.js index 2866b8408746..d8feee80dec7 100644 --- a/src/libs/ComposerUtils/index.js +++ b/src/libs/ComposerUtils/index.js @@ -25,4 +25,18 @@ function canSkipTriggerHotkeys(isSmallScreenWidth, isKeyboardShown) { return (isSmallScreenWidth && DeviceCapabilities.canUseTouchScreen()) || isKeyboardShown; } -export {getNumberOfLines, updateNumberOfLines, insertText, canSkipTriggerHotkeys}; +/** + * Find length of common ending of two strings + * @param {String} str1 + * @param {String} str2 + * @returns {Number} + */ +function getCommonEndingLength(str1, str2) { + let i = 0; + while (str1[str1.length - 1 - i] === str2[str2.length - 1 - i]) { + i++; + } + return i; +} + +export {getNumberOfLines, updateNumberOfLines, insertText, canSkipTriggerHotkeys, getCommonEndingLength}; diff --git a/src/pages/home/report/ReportActionCompose.js b/src/pages/home/report/ReportActionCompose.js index 03f86205a809..108fa99621ae 100644 --- a/src/pages/home/report/ReportActionCompose.js +++ b/src/pages/home/report/ReportActionCompose.js @@ -797,13 +797,13 @@ class ReportActionCompose extends React.Component { this.debouncedUpdateFrequentlyUsedEmojis(); } - this.setState((prevState) => { + this.setState(() => { const newState = { isCommentEmpty: !!newComment.match(/^(\s)*$/), value: newComment, }; if (comment !== newComment) { - const remainder = prevState.value.slice(prevState.selection.end).length; + const remainder = ComposerUtils.getCommonEndingLength(comment, newComment); newState.selection = { start: newComment.length - remainder, end: newComment.length - remainder, From e1580b59d8af009df1d9330b89511a56e223f12f Mon Sep 17 00:00:00 2001 From: Ali Toshmatov Date: Mon, 17 Jul 2023 12:12:55 +0500 Subject: [PATCH 2/2] Updated function name --- src/libs/ComposerUtils/index.js | 15 +++++++++------ src/pages/home/report/ReportActionCompose.js | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/libs/ComposerUtils/index.js b/src/libs/ComposerUtils/index.js index d8feee80dec7..dfe6cf446809 100644 --- a/src/libs/ComposerUtils/index.js +++ b/src/libs/ComposerUtils/index.js @@ -26,12 +26,15 @@ function canSkipTriggerHotkeys(isSmallScreenWidth, isKeyboardShown) { } /** - * Find length of common ending of two strings - * @param {String} str1 - * @param {String} str2 - * @returns {Number} + * Returns the length of the common suffix between two input strings. + * The common suffix is the number of characters shared by both strings + * at the end (suffix) until a mismatch is encountered. + * + * @param {string} str1 + * @param {string} str2 + * @returns {number} The length of the common suffix between the strings. */ -function getCommonEndingLength(str1, str2) { +function getCommonSuffixLength(str1, str2) { let i = 0; while (str1[str1.length - 1 - i] === str2[str2.length - 1 - i]) { i++; @@ -39,4 +42,4 @@ function getCommonEndingLength(str1, str2) { return i; } -export {getNumberOfLines, updateNumberOfLines, insertText, canSkipTriggerHotkeys, getCommonEndingLength}; +export {getNumberOfLines, updateNumberOfLines, insertText, canSkipTriggerHotkeys, getCommonSuffixLength}; diff --git a/src/pages/home/report/ReportActionCompose.js b/src/pages/home/report/ReportActionCompose.js index 108fa99621ae..4b8fa50cd54d 100644 --- a/src/pages/home/report/ReportActionCompose.js +++ b/src/pages/home/report/ReportActionCompose.js @@ -803,7 +803,7 @@ class ReportActionCompose extends React.Component { value: newComment, }; if (comment !== newComment) { - const remainder = ComposerUtils.getCommonEndingLength(comment, newComment); + const remainder = ComposerUtils.getCommonSuffixLength(comment, newComment); newState.selection = { start: newComment.length - remainder, end: newComment.length - remainder,