-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Fix mention suggestion highlight doesn't move when the text selection moves #39917
Changes from 7 commits
3bead8e
898262f
9576865
6bcc48b
0b5fe42
ca7c6aa
a33064d
c531b83
c13c898
42f0f7d
27f7254
1ea2c63
a3eee55
307bb9c
25a5d85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import Str from 'expensify-common/lib/str'; | |
import lodashSortBy from 'lodash/sortBy'; | ||
import type {ForwardedRef} from 'react'; | ||
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react'; | ||
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native'; | ||
import * as Expensicons from '@components/Icon/Expensicons'; | ||
import type {Mention} from '@components/MentionSuggestions'; | ||
import MentionSuggestions from '@components/MentionSuggestions'; | ||
|
@@ -219,19 +220,8 @@ function SuggestionMention( | |
return; | ||
} | ||
|
||
const valueAfterTheCursor = value.substring(selectionEnd); | ||
const indexOfFirstSpecialCharOrEmojiAfterTheCursor = valueAfterTheCursor.search(CONST.REGEX.MENTION_BREAKER); | ||
|
||
let suggestionEndIndex; | ||
if (indexOfFirstSpecialCharOrEmojiAfterTheCursor === -1) { | ||
// We didn't find a special char/whitespace/emoji after the cursor, so we will use the entire string | ||
suggestionEndIndex = value.length; | ||
} else { | ||
suggestionEndIndex = indexOfFirstSpecialCharOrEmojiAfterTheCursor + selectionEnd; | ||
} | ||
|
||
const afterLastBreakLineIndex = value.lastIndexOf('\n', selectionEnd - 1) + 1; | ||
const leftString = value.substring(afterLastBreakLineIndex, suggestionEndIndex); | ||
const leftString = value.substring(afterLastBreakLineIndex, selectionEnd); | ||
const words = leftString.split(CONST.REGEX.SPACE_OR_EMOJI); | ||
const lastWord: string = words.at(-1) ?? ''; | ||
const secondToLastWord = words[words.length - 3]; | ||
|
@@ -261,9 +251,7 @@ function SuggestionMention( | |
mentionPrefix: prefix, | ||
}; | ||
|
||
const isCursorBeforeTheMention = valueAfterTheCursor.startsWith(suggestionWord); | ||
|
||
if (!isCursorBeforeTheMention && isMentionCode(suggestionWord)) { | ||
if (isMentionCode(suggestionWord)) { | ||
const suggestions = getMentionOptions(personalDetails, prefix); | ||
nextState.suggestedMentions = suggestions; | ||
nextState.shouldShowSuggestionMenu = !!suggestions.length; | ||
|
@@ -279,8 +267,12 @@ function SuggestionMention( | |
); | ||
|
||
useEffect(() => { | ||
if (!isComposerFocused) { | ||
return; | ||
} | ||
calculateMentionSuggestion(selection.end); | ||
}, [selection, calculateMentionSuggestion]); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isComposerFocused]); | ||
bernhardoj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const updateShouldShowSuggestionMenuToFalse = useCallback(() => { | ||
setSuggestionValues((prevState) => { | ||
|
@@ -300,16 +292,29 @@ function SuggestionMention( | |
|
||
const getSuggestions = useCallback(() => suggestionValues.suggestedMentions, [suggestionValues]); | ||
|
||
const onSelectionChange = useCallback( | ||
(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => { | ||
/** | ||
* we pass here e.nativeEvent.selection.end directly to calculateEmojiSuggestion | ||
* because in other case calculateEmojiSuggestion will have an old calculation value | ||
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. Please update the comment. 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. Updated |
||
* of suggestion instead of current one | ||
*/ | ||
calculateMentionSuggestion(e.nativeEvent.selection.end); | ||
}, | ||
[calculateMentionSuggestion], | ||
); | ||
bernhardoj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
useImperativeHandle( | ||
ref, | ||
() => ({ | ||
resetSuggestions, | ||
triggerHotkeyActions, | ||
setShouldBlockSuggestionCalc, | ||
onSelectionChange, | ||
updateShouldShowSuggestionMenuToFalse, | ||
getSuggestions, | ||
}), | ||
[resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions], | ||
[onSelectionChange, resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions], | ||
); | ||
|
||
if (!isMentionSuggestionsMenuVisible) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,7 +111,8 @@ function Suggestions( | |
|
||
const onSelectionChange = useCallback((e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => { | ||
const emojiHandler = suggestionEmojiRef.current?.onSelectionChange?.(e); | ||
return emojiHandler; | ||
const mentionHandler = suggestionMentionRef.current?.onSelectionChange?.(e); | ||
return emojiHandler ?? mentionHandler; | ||
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. I tried to test a corner case with precondition - have a user with By pasting text As prod has same issue and I'm not sure if this is expected, so I think we can also skip this one in this PR. 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. Yeah, I always wondering whether both suggestions will show or not since we don't return anything inside |
||
}, []); | ||
|
||
const updateShouldShowSuggestionMenuToFalse = useCallback(() => { | ||
|
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.
Just left a comment in case someone is confused about this change.
I think it makes sense to use
selection.end
, see below comparison