[Glitch] Change autosuggestions to include second word in web UI
Port e4e19ba26487e03c9db62275efe960dc124aa280 to glitch-soc Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
57153179bf
commit
eda507e673
@ -0,0 +1,29 @@
|
|||||||
|
export const textAtCursorMatchesToken = (
|
||||||
|
str: string,
|
||||||
|
caretPosition: number,
|
||||||
|
searchTokens: string[],
|
||||||
|
) => {
|
||||||
|
let word: string;
|
||||||
|
|
||||||
|
const regex = new RegExp(`[${searchTokens.join('')}\\w]+(\\s[\\w]+)?$`);
|
||||||
|
const left = str.slice(0, caretPosition).search(regex);
|
||||||
|
const right = str.slice(caretPosition).search(/\s/);
|
||||||
|
|
||||||
|
if (right < 0) {
|
||||||
|
word = str.slice(left);
|
||||||
|
} else {
|
||||||
|
word = str.slice(left, right + caretPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
word = word.trim();
|
||||||
|
|
||||||
|
if (word.length < 3 || (word[0] && !searchTokens.includes(word[0]))) {
|
||||||
|
return [null, null];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (word.length > 0) {
|
||||||
|
return [left + 1, word];
|
||||||
|
} else {
|
||||||
|
return [null, null];
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -12,31 +12,7 @@ import AutosuggestAccountContainer from '../features/compose/containers/autosugg
|
|||||||
import { AutosuggestEmoji } from './autosuggest_emoji';
|
import { AutosuggestEmoji } from './autosuggest_emoji';
|
||||||
import { AutosuggestHashtag } from './autosuggest_hashtag';
|
import { AutosuggestHashtag } from './autosuggest_hashtag';
|
||||||
import { LocalCustomEmojiProvider } from './emoji/context';
|
import { LocalCustomEmojiProvider } from './emoji/context';
|
||||||
|
import { textAtCursorMatchesToken } from './autosuggest/utils';
|
||||||
const 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!word || word.trim().length < 3 || searchTokens.indexOf(word[0]) === -1) {
|
|
||||||
return [null, null];
|
|
||||||
}
|
|
||||||
|
|
||||||
word = word.trim();
|
|
||||||
|
|
||||||
if (word.length > 0) {
|
|
||||||
return [left + 1, word];
|
|
||||||
} else {
|
|
||||||
return [null, null];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export default class AutosuggestInput extends ImmutablePureComponent {
|
export default class AutosuggestInput extends ImmutablePureComponent {
|
||||||
|
|
||||||
|
|||||||
@ -13,31 +13,7 @@ import AutosuggestAccountContainer from '../features/compose/containers/autosugg
|
|||||||
import { AutosuggestEmoji } from './autosuggest_emoji';
|
import { AutosuggestEmoji } from './autosuggest_emoji';
|
||||||
import { AutosuggestHashtag } from './autosuggest_hashtag';
|
import { AutosuggestHashtag } from './autosuggest_hashtag';
|
||||||
import { LocalCustomEmojiProvider } from './emoji/context';
|
import { LocalCustomEmojiProvider } from './emoji/context';
|
||||||
|
import { textAtCursorMatchesToken } from './autosuggest/utils';
|
||||||
const textAtCursorMatchesToken = (str, caretPosition) => {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!word || word.trim().length < 3 || ['@', '@', ':', '#', '#'].indexOf(word[0]) === -1) {
|
|
||||||
return [null, null];
|
|
||||||
}
|
|
||||||
|
|
||||||
word = word.trim();
|
|
||||||
|
|
||||||
if (word.length > 0) {
|
|
||||||
return [left + 1, word];
|
|
||||||
} else {
|
|
||||||
return [null, null];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const AutosuggestTextarea = forwardRef(({
|
const AutosuggestTextarea = forwardRef(({
|
||||||
value,
|
value,
|
||||||
@ -64,7 +40,7 @@ const AutosuggestTextarea = forwardRef(({
|
|||||||
const tokenStartRef = useRef(0);
|
const tokenStartRef = useRef(0);
|
||||||
|
|
||||||
const handleChange = useCallback((e) => {
|
const handleChange = useCallback((e) => {
|
||||||
const [ tokenStart, token ] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart);
|
const [ tokenStart, token ] = textAtCursorMatchesToken(e.target.value, e.target.selectionStart, ['@', '@', ':', '#', '#']);
|
||||||
|
|
||||||
if (token !== null && lastTokenRef.current !== token) {
|
if (token !== null && lastTokenRef.current !== token) {
|
||||||
tokenStartRef.current = tokenStart;
|
tokenStartRef.current = tokenStart;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user