my fork of the bluesky client
at main 37 lines 822 B view raw
1interface FoundMention { 2 value: string 3 index: number 4} 5 6export function getMentionAt( 7 text: string, 8 cursorPos: number, 9): FoundMention | undefined { 10 let re = /(^|\s)@([a-z0-9.]*)/gi 11 let match 12 while ((match = re.exec(text))) { 13 const spaceOffset = match[1].length 14 const index = match.index + spaceOffset 15 if ( 16 cursorPos >= index && 17 cursorPos <= index + match[0].length - spaceOffset 18 ) { 19 return {value: match[2], index} 20 } 21 } 22 return undefined 23} 24 25export function insertMentionAt( 26 text: string, 27 cursorPos: number, 28 mention: string, 29) { 30 const target = getMentionAt(text, cursorPos) 31 if (target) { 32 return `${text.slice(0, target.index)}@${mention} ${text.slice( 33 target.index + target.value.length + 1, // add 1 to include the "@" 34 )}` 35 } 36 return text 37}