Changeset View
Changeset View
Standalone View
Standalone View
lib/shared/typeahead-utils.js
Show All 9 Lines | export type TypeaheadMatchedStrings = { | ||||
+usernamePrefix: string, | +usernamePrefix: string, | ||||
}; | }; | ||||
export type Selection = { | export type Selection = { | ||||
+start: number, | +start: number, | ||||
+end: number, | +end: number, | ||||
}; | }; | ||||
function getTypeaheadRegexMatches( | |||||
text: string, | |||||
selection: Selection, | |||||
regex: RegExp, | |||||
): null | RegExp$matchResult { | |||||
if ( | |||||
selection.start === selection.end && | |||||
(selection.start === text.length || /\s/.test(text[selection.end])) | |||||
) { | |||||
return text.slice(0, selection.start).match(regex); | |||||
} | |||||
return null; | |||||
} | |||||
function getTypeaheadUserSuggestions( | function getTypeaheadUserSuggestions( | ||||
userSearchIndex: SearchIndex, | userSearchIndex: SearchIndex, | ||||
threadMembers: $ReadOnlyArray<RelativeMemberInfo>, | threadMembers: $ReadOnlyArray<RelativeMemberInfo>, | ||||
viewerID: ?string, | viewerID: ?string, | ||||
usernamePrefix: string, | usernamePrefix: string, | ||||
): $ReadOnlyArray<RelativeMemberInfo> { | ): $ReadOnlyArray<RelativeMemberInfo> { | ||||
const userIDs = userSearchIndex.getSearchResults(usernamePrefix); | const userIDs = userSearchIndex.getSearchResults(usernamePrefix); | ||||
const usersInThread = threadOtherMembers(threadMembers, viewerID); | const usersInThread = threadOtherMembers(threadMembers, viewerID); | ||||
Show All 23 Lines | ): { | ||||
const newText = | const newText = | ||||
textBeforeAtSymbol + '@' + stringForUserExplicit(user) + newSuffixText; | textBeforeAtSymbol + '@' + stringForUserExplicit(user) + newSuffixText; | ||||
const newSelectionStart = newText.length - newSuffixText.length + 1; | const newSelectionStart = newText.length - newSuffixText.length + 1; | ||||
return { newText, newSelectionStart }; | return { newText, newSelectionStart }; | ||||
} | } | ||||
export { getTypeaheadUserSuggestions, getNewTextAndSelection }; | export { | ||||
getTypeaheadUserSuggestions, | |||||
getNewTextAndSelection, | |||||
getTypeaheadRegexMatches, | |||||
}; |