-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: append whitespace after inserting emoji #30955
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
639136a
fix: do not append whitespace to emoji if whitespace is already present
aswin-s 944093c
Merge remote-tracking branch 'upstream/main' into fix/emoji-with-whitβ¦
aswin-s 6062753
fix: whitespace after emoji with skin tone
aswin-s 4d6412e
fix: whitespace after short codes right before a word
aswin-s 56b3d78
Merge remote-tracking branch 'upstream/main' into fix/emoji-with-whitβ¦
aswin-s 276cae2
fix: insert whitespace after emoji
aswin-s d726960
fix: prettier issue
aswin-s 8053572
fix: prevent duplicate character insertion on refocus
aswin-s f856ab8
Merge branch 'main' into fix/emoji-with-whitespace
aswin-s 658038c
fix: refactor utility methods
aswin-s 6c9d374
Merge remote-tracking branch 'upstream/main' into fix/emoji-with-whitβ¦
aswin-s f768fb6
Merge remote-tracking branch 'upstream/main' into fix/emoji-with-whitβ¦
aswin-s 2cc4858
Merge remote-tracking branch 'upstream/main' into fix/emoji-with-whitβ¦
aswin-s f7a19ca
fix: common suffix length calculation
aswin-s 8355315
refactor: remove redundant changes
aswin-s 84f0851
fix: remove params & returns from jsdoc
aswin-s f0ebda9
fix: remove redundant param shouldAddTrailSpace
aswin-s 27f5395
fix: remove empty line
aswin-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,7 @@ function ComposerWithSuggestions({ | |
return draft; | ||
}); | ||
const commentRef = useRef(value); | ||
const lastTextRef = useRef(value); | ||
|
||
const {isSmallScreenWidth} = useWindowDimensions(); | ||
const maxComposerLines = isSmallScreenWidth ? CONST.COMPOSER.MAX_LINES_SMALL_SCREEN : CONST.COMPOSER.MAX_LINES; | ||
|
@@ -189,6 +190,47 @@ function ComposerWithSuggestions({ | |
[], | ||
); | ||
|
||
/** | ||
* Find the newly added characters between the previous text and the new text based on the selection. | ||
* | ||
* @param {string} prevText - The previous text. | ||
* @param {string} newText - The new text. | ||
* @returns {object} An object containing information about the newly added characters. | ||
* @property {number} startIndex - The start index of the newly added characters in the new text. | ||
* @property {number} endIndex - The end index of the newly added characters in the new text. | ||
* @property {string} diff - The newly added characters. | ||
*/ | ||
const findNewlyAddedChars = useCallback( | ||
(prevText, newText) => { | ||
let startIndex = -1; | ||
let endIndex = -1; | ||
let currentIndex = 0; | ||
|
||
// Find the first character mismatch with newText | ||
while (currentIndex < newText.length && prevText.charAt(currentIndex) === newText.charAt(currentIndex) && selection.start > currentIndex) { | ||
currentIndex++; | ||
} | ||
|
||
if (currentIndex < newText.length) { | ||
startIndex = currentIndex; | ||
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; | ||
} else { | ||
endIndex = currentIndex + newText.length; | ||
} | ||
} | ||
|
||
return { | ||
startIndex, | ||
endIndex, | ||
diff: newText.substring(startIndex, endIndex), | ||
}; | ||
}, | ||
[selection.start, selection.end], | ||
); | ||
|
||
/** | ||
* Update the value of the comment in Onyx | ||
* | ||
|
@@ -198,7 +240,13 @@ function ComposerWithSuggestions({ | |
const updateComment = useCallback( | ||
(commentValue, shouldDebounceSaveComment) => { | ||
raiseIsScrollLikelyLayoutTriggered(); | ||
const {text: newComment, emojis, cursorPosition} = EmojiUtils.replaceAndExtractEmojis(commentValue, preferredSkinTone, preferredLocale); | ||
const {startIndex, endIndex, diff} = findNewlyAddedChars(lastTextRef.current, commentValue); | ||
const isEmojiInserted = diff.length && endIndex > startIndex && diff.trim() === diff && EmojiUtils.containsOnlyEmojis(diff); | ||
const { | ||
text: newComment, | ||
emojis, | ||
cursorPosition, | ||
} = EmojiUtils.replaceAndExtractEmojis(isEmojiInserted ? ComposerUtils.insertWhiteSpaceAtIndex(commentValue, endIndex) : commentValue, preferredSkinTone, preferredLocale); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line caused a regression iOS - Chat - The cursor moves one space backward when inserting text after an emoji |
||
if (!_.isEmpty(emojis)) { | ||
const newEmojis = EmojiUtils.getAddedEmojis(emojis, emojisPresentBefore.current); | ||
if (!_.isEmpty(newEmojis)) { | ||
|
@@ -255,6 +303,7 @@ function ComposerWithSuggestions({ | |
}, | ||
[ | ||
debouncedUpdateFrequentlyUsedEmojis, | ||
findNewlyAddedChars, | ||
preferredLocale, | ||
preferredSkinTone, | ||
reportID, | ||
|
@@ -309,17 +358,10 @@ function ComposerWithSuggestions({ | |
/** | ||
* Callback to add whatever text is chosen into the main input (used f.e as callback for the emoji picker) | ||
* @param {String} text | ||
* @param {Boolean} shouldAddTrailSpace | ||
*/ | ||
const replaceSelectionWithText = useCallback( | ||
(text, shouldAddTrailSpace = true) => { | ||
const updatedText = shouldAddTrailSpace ? `${text} ` : text; | ||
const selectionSpaceLength = shouldAddTrailSpace ? CONST.SPACE_LENGTH : 0; | ||
updateComment(ComposerUtils.insertText(commentRef.current, selection, updatedText)); | ||
setSelection((prevSelection) => ({ | ||
start: prevSelection.start + text.length + selectionSpaceLength, | ||
end: prevSelection.start + text.length + selectionSpaceLength, | ||
})); | ||
(text) => { | ||
updateComment(ComposerUtils.insertText(commentRef.current, selection, text)); | ||
}, | ||
[selection, updateComment], | ||
); | ||
|
@@ -524,6 +566,10 @@ function ComposerWithSuggestions({ | |
[blur, focus, prepareCommentAndResetComposer, replaceSelectionWithText], | ||
); | ||
|
||
useEffect(() => { | ||
lastTextRef.current = value; | ||
}, [value]); | ||
|
||
useEffect(() => { | ||
onValueChange(value); | ||
}, [onValueChange, value]); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like it belongs in expensify/common str.js, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment it is used only with composer and requires cursor position to exactly predict which character got appended when duplicate characters are present.