diff --git a/lib/shared/typeahead-utils.js b/lib/shared/typeahead-utils.js --- a/lib/shared/typeahead-utils.js +++ b/lib/shared/typeahead-utils.js @@ -26,4 +26,27 @@ ); } -export { getTypeaheadUserSuggestions }; +function getNewTextAndSelection( + textBeforeAtSymbol: string, + entireText: string, + usernamePrefix: string, + user: RelativeMemberInfo, +): { + newText: string, + newSelectionStart: number, +} { + const totalMatchLength = + textBeforeAtSymbol.length + usernamePrefix.length + 1; // 1 for @ char + + let newSuffixText = entireText.slice(totalMatchLength); + newSuffixText = (newSuffixText[0] !== ' ' ? ' ' : '') + newSuffixText; + + const newText = + textBeforeAtSymbol + '@' + stringForUserExplicit(user) + newSuffixText; + + const newSelectionStart = newText.length - newSuffixText.length + 1; + + return { newText, newSelectionStart }; +} + +export { getTypeaheadUserSuggestions, getNewTextAndSelection }; diff --git a/web/utils/typeahead-utils.js b/web/utils/typeahead-utils.js --- a/web/utils/typeahead-utils.js +++ b/web/utils/typeahead-utils.js @@ -4,6 +4,7 @@ import * as React from 'react'; import { oldValidUsernameRegexString } from 'lib/shared/account-utils'; +import { getNewTextAndSelection } from 'lib/shared/typeahead-utils'; import { stringForUserExplicit } from 'lib/shared/user-utils'; import type { SetState } from 'lib/types/hook-types'; import type { RelativeMemberInfo } from 'lib/types/thread-types'; @@ -101,24 +102,15 @@ .map(suggestedUser => ({ key: suggestedUser.id, execute: () => { - const newPrefixText = textBeforeAtSymbol; - - const totalMatchLength = - textBeforeAtSymbol.length + usernamePrefix.length + 1; // 1 for @ char - - let newSuffixText = inputStateDraft.slice(totalMatchLength); - newSuffixText = (newSuffixText[0] !== ' ' ? ' ' : '') + newSuffixText; - - const newText = - newPrefixText + - '@' + - stringForUserExplicit(suggestedUser) + - newSuffixText; + const { newText, newSelectionStart } = getNewTextAndSelection( + textBeforeAtSymbol, + inputStateDraft, + usernamePrefix, + suggestedUser, + ); inputStateSetDraft(newText); - inputStateSetTextCursorPosition( - newText.length - newSuffixText.length + 1, - ); + inputStateSetTextCursorPosition(newSelectionStart); }, actionButtonContent: stringForUserExplicit(suggestedUser), }));