2018-08-04 05:09:01 +02:00
|
|
|
// https://github.com/tootsuite/mastodon/blob/master/app/javascript/mastodon/components/autosuggest_textarea.js
|
2022-04-29 16:47:49 +02:00
|
|
|
const textAtCursorMatch = (
|
|
|
|
str: string,
|
|
|
|
cursorPosition: number,
|
|
|
|
separators: Array<string> = ['@', '#', ':']
|
|
|
|
): [number | null, string | null] => {
|
|
|
|
let word: string
|
2018-08-04 05:09:01 +02:00
|
|
|
|
2020-05-17 09:31:37 +02:00
|
|
|
const left = str.slice(0, cursorPosition).search(/\S+$/)
|
|
|
|
const right = str.slice(cursorPosition).search(/\s/)
|
2018-08-04 05:09:01 +02:00
|
|
|
|
|
|
|
if (right < 0) {
|
|
|
|
word = str.slice(left)
|
|
|
|
} else {
|
|
|
|
word = str.slice(left, right + cursorPosition)
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:07:13 +01:00
|
|
|
if (!word || word.trim().length < 3 || separators.indexOf(word[0]) === -1) {
|
2018-08-04 05:09:01 +02:00
|
|
|
return [null, null]
|
|
|
|
}
|
|
|
|
|
|
|
|
word = word.trim().toLowerCase()
|
|
|
|
|
|
|
|
if (word.length > 0) {
|
|
|
|
return [left + 1, word]
|
|
|
|
} else {
|
|
|
|
return [null, null]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default textAtCursorMatch
|