gab-social/app/javascript/gabsocial/utils/cursor_token_match.js

26 lines
562 B
JavaScript
Raw Normal View History

2019-08-03 06:51:48 +01:00
'use strict';
export function textAtCursorMatchesToken(str, caretPosition, searchTokens) {
let word;
let left = str.slice(0, caretPosition).search(/\S+$/);
let right = str.slice(caretPosition).search(/\s/);
if (right < 0) {
word = str.slice(left);
} else {
word = str.slice(left, right + caretPosition);
}
2020-03-14 17:31:29 +00:00
if (!word || word.trim().length < 2 || searchTokens.indexOf(word[0]) === -1) {
2019-08-03 06:51:48 +01:00
return [null, null];
}
word = word.trim().toLowerCase();
if (word.length > 0) {
return [left + 1, word];
}
return [null, null];
};