2021-12-02 17:49:13 +01:00
|
|
|
// @flow
|
2021-12-06 19:06:40 +01:00
|
|
|
import { EMOTES_48px as EMOTES } from 'constants/emotes';
|
2021-12-02 17:49:13 +01:00
|
|
|
import { matchSorter } from 'match-sorter';
|
2021-12-06 20:39:39 +01:00
|
|
|
import { SEARCH_OPTIONS } from 'constants/search';
|
2021-12-02 17:49:13 +01:00
|
|
|
import * as KEYCODES from 'constants/keycodes';
|
2021-12-06 18:28:36 +01:00
|
|
|
import Autocomplete from '@mui/material/Autocomplete';
|
2021-12-07 15:51:55 +01:00
|
|
|
import BusyIndicator from 'component/common/busy-indicator';
|
2021-12-06 21:38:13 +01:00
|
|
|
import EMOJIS from 'emoji-dictionary';
|
2021-12-07 19:17:29 +01:00
|
|
|
import LbcSymbol from 'component/common/lbc-symbol';
|
2021-12-07 15:51:55 +01:00
|
|
|
import Popper from '@mui/material/Popper';
|
2021-12-02 17:49:13 +01:00
|
|
|
import React from 'react';
|
2021-12-10 21:03:53 +01:00
|
|
|
import replaceAll from 'core-js-pure/features/string/replace-all';
|
2021-12-02 17:49:13 +01:00
|
|
|
import TextareaSuggestionsItem from 'component/textareaSuggestionsItem';
|
2021-12-06 18:28:36 +01:00
|
|
|
import TextField from '@mui/material/TextField';
|
2021-12-06 20:39:39 +01:00
|
|
|
import useLighthouse from 'effects/use-lighthouse';
|
2021-12-02 17:49:13 +01:00
|
|
|
import useThrottle from 'effects/use-throttle';
|
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const SUGGESTION_REGEX = new RegExp(
|
2021-12-10 16:12:18 +01:00
|
|
|
'((?:^| |\n)@[^\\s=&#$@%?:;/\\"<>%{}|^~[]*(?::[\\w]+)?)|((?:^| |\n):[\\w+-]*:?)',
|
2021-12-06 19:06:40 +01:00
|
|
|
'gm'
|
|
|
|
);
|
|
|
|
|
|
|
|
/** Regex Explained step-by-step:
|
|
|
|
*
|
2021-12-10 16:12:18 +01:00
|
|
|
* 1) ()|() = different capturing groups (either Mention or Emote)
|
2021-12-06 19:06:40 +01:00
|
|
|
* 2) (?:^| |\n) = only allow for: sentence beginning, space or newline before the match (no words or symbols)
|
|
|
|
* 3) [^\s=&#$@%?:;/\\"<>%{}|^~[]* = anything, except the characters inside
|
|
|
|
* 4) (?::[\w]+)? = A mention can be matched with a ':' as a claim modifier with words or digits after as ID digits,
|
|
|
|
* or else it's everything before the ':' (will then match the winning uri for the mention behind since has no canonical ID)
|
|
|
|
* 5) :\w*:? = the emote Regex, possible to be matched with a ':' at the end to consider previously typed emotes
|
|
|
|
*
|
|
|
|
*/
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 20:39:39 +01:00
|
|
|
const SEARCH_SIZE = 10;
|
|
|
|
const LIGHTHOUSE_MIN_CHARACTERS = 3;
|
|
|
|
const INPUT_DEBOUNCE_MS = 1000;
|
|
|
|
|
2021-12-06 21:38:13 +01:00
|
|
|
const EMOJI_MIN_CHARACTERS = 2;
|
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
type Props = {
|
2021-12-06 18:28:36 +01:00
|
|
|
canonicalCommentors?: Array<string>,
|
|
|
|
canonicalCreatorUri?: string,
|
2021-12-06 20:39:39 +01:00
|
|
|
canonicalSearch?: Array<string>,
|
2021-12-06 18:28:36 +01:00
|
|
|
canonicalSubscriptions?: Array<string>,
|
2021-12-07 19:17:29 +01:00
|
|
|
canonicalTop?: string,
|
2021-12-02 17:49:13 +01:00
|
|
|
className?: string,
|
2021-12-06 18:28:36 +01:00
|
|
|
commentorUris?: Array<string>,
|
|
|
|
disabled?: boolean,
|
2021-12-07 15:51:55 +01:00
|
|
|
hasNewResolvedResults?: boolean,
|
2021-12-06 18:28:36 +01:00
|
|
|
id: string,
|
2021-12-02 17:49:13 +01:00
|
|
|
inputRef: any,
|
|
|
|
isLivestream?: boolean,
|
|
|
|
maxLength?: number,
|
|
|
|
placeholder?: string,
|
2021-12-07 19:17:29 +01:00
|
|
|
searchQuery?: string,
|
2021-12-06 20:39:39 +01:00
|
|
|
showMature: boolean,
|
2021-12-02 17:49:13 +01:00
|
|
|
type?: string,
|
2021-12-06 18:28:36 +01:00
|
|
|
uri?: string,
|
2021-12-02 17:49:13 +01:00
|
|
|
value: any,
|
2021-12-06 18:28:36 +01:00
|
|
|
doResolveUris: (Array<string>) => void,
|
2021-12-07 19:17:29 +01:00
|
|
|
doSetMentionSearchResults: (string, Array<string>) => void,
|
2021-12-06 18:28:36 +01:00
|
|
|
onBlur: (any) => any,
|
2021-12-02 17:49:13 +01:00
|
|
|
onChange: (any) => any,
|
2021-12-06 18:28:36 +01:00
|
|
|
onFocus: (any) => any,
|
2021-12-02 17:49:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function TextareaWithSuggestions(props: Props) {
|
|
|
|
const {
|
|
|
|
canonicalCommentors,
|
|
|
|
canonicalCreatorUri,
|
2021-12-06 20:39:39 +01:00
|
|
|
canonicalSearch,
|
2021-12-06 18:28:36 +01:00
|
|
|
canonicalSubscriptions: canonicalSubs,
|
2021-12-07 19:17:29 +01:00
|
|
|
canonicalTop,
|
2021-12-02 17:49:13 +01:00
|
|
|
className,
|
|
|
|
commentorUris,
|
2021-12-06 18:28:36 +01:00
|
|
|
disabled,
|
2021-12-07 15:51:55 +01:00
|
|
|
hasNewResolvedResults,
|
2021-12-06 18:28:36 +01:00
|
|
|
id,
|
2021-12-02 17:49:13 +01:00
|
|
|
inputRef,
|
|
|
|
isLivestream,
|
|
|
|
maxLength,
|
|
|
|
placeholder,
|
2021-12-07 19:17:29 +01:00
|
|
|
searchQuery,
|
2021-12-06 20:39:39 +01:00
|
|
|
showMature,
|
2021-12-02 17:49:13 +01:00
|
|
|
type,
|
2021-12-06 19:06:40 +01:00
|
|
|
value: messageValue,
|
2021-12-06 18:28:36 +01:00
|
|
|
doResolveUris,
|
2021-12-07 15:51:55 +01:00
|
|
|
doSetMentionSearchResults,
|
2021-12-06 18:28:36 +01:00
|
|
|
onBlur,
|
2021-12-02 17:49:13 +01:00
|
|
|
onChange,
|
2021-12-06 18:28:36 +01:00
|
|
|
onFocus,
|
2021-12-02 17:49:13 +01:00
|
|
|
} = props;
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
const inputDefaultProps = { className, placeholder, maxLength, type, disabled };
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
const [suggestionValue, setSuggestionValue] = React.useState(undefined);
|
2021-12-06 18:28:36 +01:00
|
|
|
const [highlightedSuggestion, setHighlightedSuggestion] = React.useState('');
|
|
|
|
const [shouldClose, setClose] = React.useState();
|
2021-12-06 20:39:39 +01:00
|
|
|
const [debouncedTerm, setDebouncedTerm] = React.useState('');
|
|
|
|
// const [mostSupported, setMostSupported] = React.useState('');
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
const suggestionTerm = suggestionValue && suggestionValue.term;
|
2021-12-06 19:06:40 +01:00
|
|
|
const isEmote = suggestionValue && suggestionValue.isEmote;
|
2021-12-06 20:39:39 +01:00
|
|
|
const isMention = suggestionValue && !suggestionValue.isEmote;
|
2021-12-09 21:17:29 +01:00
|
|
|
const invalidTerm = suggestionTerm && isMention && suggestionTerm.charAt(1) === ':';
|
2021-12-06 20:39:39 +01:00
|
|
|
|
|
|
|
const additionalOptions = { isBackgroundSearch: false, [SEARCH_OPTIONS.CLAIM_TYPE]: SEARCH_OPTIONS.INCLUDE_CHANNELS };
|
|
|
|
const { results, loading } = useLighthouse(debouncedTerm, showMature, SEARCH_SIZE, additionalOptions, 0);
|
|
|
|
const stringifiedResults = JSON.stringify(results);
|
|
|
|
|
|
|
|
const hasMinLength = suggestionTerm && isMention && suggestionTerm.length >= LIGHTHOUSE_MIN_CHARACTERS;
|
|
|
|
const isTyping = isMention && debouncedTerm !== suggestionTerm;
|
2021-12-07 15:51:55 +01:00
|
|
|
const showPlaceholder =
|
2021-12-09 21:17:29 +01:00
|
|
|
isMention && !invalidTerm && (isTyping || loading || (results && results.length > 0 && !hasNewResolvedResults));
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
const shouldFilter = (uri, previous) => uri !== canonicalCreatorUri && (!previous || !previous.includes(uri));
|
|
|
|
const filteredCommentors = canonicalCommentors && canonicalCommentors.filter((uri) => shouldFilter(uri));
|
|
|
|
const filteredSubs = canonicalSubs && canonicalSubs.filter((uri) => shouldFilter(uri, filteredCommentors));
|
2021-12-07 19:17:29 +01:00
|
|
|
const filteredTop =
|
|
|
|
canonicalTop &&
|
|
|
|
shouldFilter(canonicalTop, filteredSubs) &&
|
|
|
|
shouldFilter(canonicalTop, filteredCommentors) &&
|
|
|
|
canonicalTop;
|
2021-12-06 20:39:39 +01:00
|
|
|
const filteredSearch =
|
|
|
|
canonicalSearch &&
|
2021-12-07 19:17:29 +01:00
|
|
|
canonicalSearch.filter(
|
|
|
|
(uri) => shouldFilter(uri, filteredSubs) && shouldFilter(uri, filteredCommentors) && uri !== filteredTop
|
|
|
|
);
|
2021-12-06 18:28:36 +01:00
|
|
|
|
2021-12-06 21:38:13 +01:00
|
|
|
let emoteNames;
|
|
|
|
let emojiNames;
|
2021-12-06 18:28:36 +01:00
|
|
|
const allOptions = [];
|
2021-12-06 19:06:40 +01:00
|
|
|
if (isEmote) {
|
2022-01-05 21:20:38 +01:00
|
|
|
emoteNames = EMOTES.map(({ name }) => name);
|
2021-12-06 21:38:13 +01:00
|
|
|
const hasMinEmojiLength = suggestionTerm && suggestionTerm.length > EMOJI_MIN_CHARACTERS;
|
2021-12-07 17:05:10 +01:00
|
|
|
// Filter because our emotes are priority from default emojis, like :eggplant:
|
|
|
|
emojiNames = hasMinEmojiLength ? EMOJIS.names.filter((name) => !emoteNames.includes(`:${name}:`)) : [];
|
2021-12-06 21:38:13 +01:00
|
|
|
const emotesAndEmojis = [...emoteNames, ...emojiNames];
|
|
|
|
|
|
|
|
allOptions.push(...emotesAndEmojis);
|
2021-12-06 19:06:40 +01:00
|
|
|
} else {
|
|
|
|
if (canonicalCreatorUri) allOptions.push(canonicalCreatorUri);
|
|
|
|
if (filteredSubs) allOptions.push(...filteredSubs);
|
|
|
|
if (filteredCommentors) allOptions.push(...filteredCommentors);
|
2021-12-07 19:17:29 +01:00
|
|
|
if (filteredTop) allOptions.push(filteredTop);
|
2021-12-06 20:39:39 +01:00
|
|
|
if (filteredSearch) allOptions.push(...filteredSearch);
|
2021-12-06 19:06:40 +01:00
|
|
|
}
|
2021-12-06 18:28:36 +01:00
|
|
|
|
|
|
|
const allOptionsGrouped =
|
|
|
|
allOptions.length > 0
|
|
|
|
? allOptions.map((option) => {
|
2021-12-06 19:06:40 +01:00
|
|
|
const groupName = isEmote
|
2021-12-06 21:38:13 +01:00
|
|
|
? (emoteNames.includes(option) && __('Emotes')) || (emojiNames.includes(option) && __('Emojis'))
|
2021-12-06 19:06:40 +01:00
|
|
|
: (canonicalCreatorUri === option && __('Creator')) ||
|
|
|
|
(filteredSubs && filteredSubs.includes(option) && __('Following')) ||
|
2021-12-06 20:39:39 +01:00
|
|
|
(filteredCommentors && filteredCommentors.includes(option) && __('From Comments')) ||
|
2021-12-07 19:17:29 +01:00
|
|
|
(filteredTop && filteredTop === option && 'Top') ||
|
2021-12-06 20:39:39 +01:00
|
|
|
(filteredSearch && filteredSearch.includes(option) && __('From Search'));
|
2021-12-06 18:28:36 +01:00
|
|
|
|
2021-12-06 21:38:13 +01:00
|
|
|
let emoteLabel;
|
|
|
|
if (isEmote) {
|
|
|
|
// $FlowFixMe
|
2021-12-10 21:03:53 +01:00
|
|
|
emoteLabel = `:${replaceAll(option, ':', '')}:`;
|
2021-12-06 21:38:13 +01:00
|
|
|
}
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
return {
|
2021-12-06 21:38:13 +01:00
|
|
|
label: emoteLabel || option.replace('lbry://', '').replace('#', ':'),
|
2021-12-06 18:28:36 +01:00
|
|
|
group: groupName,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
: [];
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-07 19:17:29 +01:00
|
|
|
const allMatches =
|
|
|
|
useSuggestionMatch(
|
|
|
|
suggestionTerm || '',
|
|
|
|
allOptionsGrouped.map(({ label }) => label)
|
|
|
|
) || [];
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
/** --------- **/
|
|
|
|
/** Functions **/
|
|
|
|
/** --------- **/
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
function handleInputChange(value: string) {
|
|
|
|
onChange({ target: { value } });
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
const cursorIndex = inputRef && inputRef.current && inputRef.current.selectionStart;
|
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const suggestionMatches = value.match(SUGGESTION_REGEX);
|
|
|
|
|
|
|
|
if (!suggestionMatches) {
|
2021-12-07 19:17:29 +01:00
|
|
|
if (suggestionValue) setSuggestionValue(null);
|
2021-12-06 19:06:40 +01:00
|
|
|
return; // Exit here and avoid unnecessary behavior
|
|
|
|
}
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const exec = SUGGESTION_REGEX.exec(value);
|
|
|
|
|
|
|
|
const previousLastIndexes = [];
|
2021-12-10 16:12:18 +01:00
|
|
|
let isEmote = exec && exec[2];
|
2021-12-06 19:06:40 +01:00
|
|
|
let currentSuggestionIndex = exec && exec.index;
|
|
|
|
let currentLastIndex = exec && SUGGESTION_REGEX.lastIndex;
|
|
|
|
let currentSuggestionValue =
|
|
|
|
cursorIndex >= currentSuggestionIndex &&
|
|
|
|
cursorIndex <= currentLastIndex &&
|
|
|
|
suggestionMatches &&
|
|
|
|
suggestionMatches[0];
|
|
|
|
|
|
|
|
if (suggestionMatches && suggestionMatches.length > 1) {
|
|
|
|
currentSuggestionValue = suggestionMatches.find((match, index) => {
|
|
|
|
const previousLastIndex = previousLastIndexes[index - 1] || 0;
|
|
|
|
const valueWithoutPrevious = value.substring(previousLastIndex);
|
|
|
|
|
|
|
|
const tempRe = new RegExp(SUGGESTION_REGEX);
|
|
|
|
const tempExec = tempRe.exec(valueWithoutPrevious);
|
|
|
|
|
|
|
|
if (tempExec) {
|
2021-12-10 16:12:18 +01:00
|
|
|
isEmote = tempExec && tempExec[2];
|
2021-12-06 19:06:40 +01:00
|
|
|
currentSuggestionIndex = previousLastIndex + tempExec.index;
|
|
|
|
currentLastIndex = previousLastIndex + tempRe.lastIndex;
|
|
|
|
previousLastIndexes.push(currentLastIndex);
|
|
|
|
}
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
// the current mention term will be the one on the text cursor's range,
|
|
|
|
// in case of there being more in the same comment message
|
2021-12-06 19:06:40 +01:00
|
|
|
if (previousLastIndexes) {
|
|
|
|
return cursorIndex >= currentSuggestionIndex && cursorIndex <= currentLastIndex;
|
2021-12-02 17:49:13 +01:00
|
|
|
}
|
|
|
|
});
|
2021-12-06 19:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (currentSuggestionValue) {
|
|
|
|
const token = isEmote ? ':' : '@';
|
|
|
|
const tokenIndex = currentSuggestionValue.indexOf(token);
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2022-01-13 19:36:07 +01:00
|
|
|
if (inputRef && inputRef.current) inputRef.current.setAttribute('typing-term', '');
|
2021-12-02 17:49:13 +01:00
|
|
|
// $FlowFixMe
|
2021-12-06 19:06:40 +01:00
|
|
|
setSuggestionValue({
|
|
|
|
beforeTerm: currentSuggestionValue.substring(0, tokenIndex), // in case of a space or newline
|
|
|
|
term: currentSuggestionValue.substring(tokenIndex),
|
|
|
|
index: currentSuggestionIndex,
|
|
|
|
lastIndex: currentLastIndex,
|
|
|
|
isEmote,
|
|
|
|
});
|
2021-12-07 19:17:29 +01:00
|
|
|
} else if (suggestionValue) {
|
2022-01-14 22:07:21 +01:00
|
|
|
inputRef.current.removeAttribute('typing-term');
|
2021-12-07 19:17:29 +01:00
|
|
|
setSuggestionValue(null);
|
2021-12-02 17:49:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleSelect = React.useCallback(
|
2022-01-13 19:36:07 +01:00
|
|
|
(selectedValue: string, key?: number) => {
|
2021-12-02 17:49:13 +01:00
|
|
|
if (!suggestionValue) return;
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
const elem = inputRef && inputRef.current;
|
2021-12-06 19:06:40 +01:00
|
|
|
const newCursorPos = suggestionValue.beforeTerm.length + suggestionValue.index + selectedValue.length + 1;
|
2021-12-06 18:28:36 +01:00
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const contentBegin = messageValue.substring(0, suggestionValue.index);
|
|
|
|
const replaceValue = suggestionValue.beforeTerm + selectedValue;
|
|
|
|
const contentEnd =
|
|
|
|
messageValue.length > suggestionValue.lastIndex
|
|
|
|
? messageValue.substring(suggestionValue.lastIndex, messageValue.length)
|
|
|
|
: ' ';
|
|
|
|
|
|
|
|
const newValue = contentBegin + replaceValue + contentEnd;
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
onChange({ target: { value: newValue } });
|
2021-12-07 19:17:29 +01:00
|
|
|
setSuggestionValue(null);
|
2022-01-13 19:36:07 +01:00
|
|
|
|
|
|
|
// no keycode === was selected with TAB (function was called by effect) or on click
|
|
|
|
// ENTER is handled on commentCreate after attempting to send on livestream
|
|
|
|
if (!key && inputRef && inputRef.current) inputRef.current.removeAttribute('typing-term');
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
elem.focus();
|
|
|
|
elem.setSelectionRange(newCursorPos, newCursorPos);
|
2021-12-02 17:49:13 +01:00
|
|
|
},
|
2021-12-06 19:06:40 +01:00
|
|
|
[messageValue, inputRef, onChange, suggestionValue]
|
2021-12-02 17:49:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/** ------- **/
|
|
|
|
/** Effects **/
|
|
|
|
/** ------- **/
|
|
|
|
|
2021-12-06 20:39:39 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!isMention) return;
|
|
|
|
|
2021-12-09 21:17:29 +01:00
|
|
|
if (isTyping && suggestionTerm && !invalidTerm) {
|
2021-12-07 19:17:29 +01:00
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setDebouncedTerm(!hasMinLength ? '' : suggestionTerm);
|
|
|
|
}, INPUT_DEBOUNCE_MS);
|
2021-12-06 20:39:39 +01:00
|
|
|
|
2021-12-07 19:17:29 +01:00
|
|
|
return () => clearTimeout(timer);
|
|
|
|
}
|
2021-12-09 21:17:29 +01:00
|
|
|
}, [hasMinLength, invalidTerm, isMention, isTyping, suggestionTerm]);
|
2021-12-06 20:39:39 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!stringifiedResults) return;
|
|
|
|
|
|
|
|
const arrayResults = JSON.parse(stringifiedResults);
|
2021-12-07 19:17:29 +01:00
|
|
|
if (debouncedTerm && arrayResults && arrayResults.length > 0) {
|
|
|
|
doResolveUris([debouncedTerm, ...arrayResults]);
|
|
|
|
doSetMentionSearchResults(debouncedTerm, arrayResults);
|
2021-12-06 20:39:39 +01:00
|
|
|
}
|
2021-12-07 19:17:29 +01:00
|
|
|
}, [debouncedTerm, doResolveUris, doSetMentionSearchResults, stringifiedResults, suggestionTerm]);
|
2021-12-06 20:39:39 +01:00
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
// Only resolve commentors on Livestreams when first trying to mention/looking for it
|
2021-12-02 17:49:13 +01:00
|
|
|
React.useEffect(() => {
|
2021-12-06 18:28:36 +01:00
|
|
|
if (isLivestream && commentorUris && suggestionTerm) doResolveUris(commentorUris);
|
|
|
|
}, [commentorUris, doResolveUris, isLivestream, suggestionTerm]);
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
// Allow selecting with TAB key
|
2021-12-02 17:49:13 +01:00
|
|
|
React.useEffect(() => {
|
2021-12-06 19:06:40 +01:00
|
|
|
if (!suggestionTerm) return; // only if there is a term, or else can't tab to navigate page
|
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
function handleKeyDown(e: SyntheticKeyboardEvent<*>) {
|
|
|
|
const { keyCode } = e;
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
if (highlightedSuggestion && keyCode === KEYCODES.TAB) {
|
2021-12-02 17:49:13 +01:00
|
|
|
e.preventDefault();
|
2021-12-06 19:06:40 +01:00
|
|
|
handleSelect(highlightedSuggestion.label);
|
2021-12-02 17:49:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
|
|
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
2021-12-06 19:06:40 +01:00
|
|
|
}, [handleSelect, highlightedSuggestion, suggestionTerm]);
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2022-01-14 22:07:21 +01:00
|
|
|
// Prevent keyboard keys Up and Down being overriden by MUI listeners when not in use
|
2022-01-14 18:55:42 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
const inputElement = inputRef && inputRef.current;
|
|
|
|
|
|
|
|
function overrideKeyHandling(event) {
|
2022-01-14 22:07:21 +01:00
|
|
|
const { keyCode } = event;
|
|
|
|
|
|
|
|
if (!suggestionTerm && (keyCode === KEYCODES.UP || keyCode === KEYCODES.DOWN)) {
|
2022-01-14 18:55:42 +01:00
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputElement) {
|
|
|
|
inputElement.addEventListener('keydown', overrideKeyHandling);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (inputElement) {
|
|
|
|
inputElement.removeEventListener('keydown', overrideKeyHandling);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [inputRef, suggestionTerm]);
|
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
/** ------ **/
|
|
|
|
/** Render **/
|
|
|
|
/** ------ **/
|
|
|
|
|
2021-12-07 12:57:31 +01:00
|
|
|
const renderGroup = (groupName: string, children: any) => (
|
2021-12-07 13:48:28 +01:00
|
|
|
<div key={groupName} className="textareaSuggestions__group">
|
2021-12-06 18:25:27 +01:00
|
|
|
<label className="textareaSuggestions__label">
|
2021-12-07 19:17:29 +01:00
|
|
|
{groupName === 'Top' ? (
|
|
|
|
<LbcSymbol prefix={__('Winning Search for %matching_term%', { matching_term: searchQuery })} />
|
|
|
|
) : suggestionTerm && suggestionTerm.length > 1 ? (
|
|
|
|
__('%group_name% matching %matching_term%', { group_name: groupName, matching_term: suggestionTerm })
|
|
|
|
) : (
|
|
|
|
groupName
|
|
|
|
)}
|
2021-12-06 18:25:27 +01:00
|
|
|
</label>
|
2021-12-06 18:28:36 +01:00
|
|
|
{children}
|
2021-12-02 17:49:13 +01:00
|
|
|
<hr className="textareaSuggestions__topSeparator" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
const renderInput = (params: any) => {
|
|
|
|
const { InputProps, disabled, fullWidth, id, inputProps: autocompleteInputProps } = params;
|
|
|
|
const inputProps = { ...autocompleteInputProps, ...inputDefaultProps };
|
|
|
|
const autocompleteProps = { InputProps, disabled, fullWidth, id, inputProps };
|
|
|
|
|
|
|
|
return <TextField inputRef={inputRef} multiline select={false} {...autocompleteProps} />;
|
|
|
|
};
|
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const renderOption = (optionProps: any, label: string) => {
|
2022-01-05 21:20:38 +01:00
|
|
|
const emoteFound = isEmote && EMOTES.find(({ name }) => name === label);
|
2021-12-06 19:06:40 +01:00
|
|
|
const emoteValue = emoteFound ? { name: label, url: emoteFound.url } : undefined;
|
2021-12-06 21:38:13 +01:00
|
|
|
const emojiFound = isEmote && EMOJIS.getUnicode(label);
|
|
|
|
const emojiValue = emojiFound ? { name: label, unicode: emojiFound } : undefined;
|
2021-12-06 19:06:40 +01:00
|
|
|
|
2021-12-07 13:48:28 +01:00
|
|
|
return <TextareaSuggestionsItem key={label} uri={label} emote={emoteValue || emojiValue} {...optionProps} />;
|
2021-12-06 19:06:40 +01:00
|
|
|
};
|
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
return (
|
2021-12-06 18:28:36 +01:00
|
|
|
<Autocomplete
|
2021-12-07 14:14:48 +01:00
|
|
|
PopperComponent={AutocompletePopper}
|
2021-12-06 18:28:36 +01:00
|
|
|
autoHighlight
|
|
|
|
disableClearable
|
2021-12-07 19:17:29 +01:00
|
|
|
filterOptions={(options) => options.filter(({ label }) => allMatches.includes(label))}
|
2021-12-06 18:28:36 +01:00
|
|
|
freeSolo
|
|
|
|
fullWidth
|
2021-12-07 19:17:29 +01:00
|
|
|
getOptionLabel={(option) => option.label || ''}
|
2021-12-06 18:28:36 +01:00
|
|
|
groupBy={(option) => option.group}
|
|
|
|
id={id}
|
2021-12-06 19:06:40 +01:00
|
|
|
inputValue={messageValue}
|
2021-12-07 19:17:29 +01:00
|
|
|
loading={allMatches.length === 0 || showPlaceholder}
|
2021-12-07 15:51:55 +01:00
|
|
|
loadingText={showPlaceholder ? <BusyIndicator message={__('Searching...')} /> : __('Nothing found')}
|
2021-12-06 19:06:40 +01:00
|
|
|
onBlur={() => onBlur && onBlur()}
|
2021-12-06 18:28:36 +01:00
|
|
|
/* Different from onInputChange, onChange is only used for the selected value,
|
|
|
|
so here it is acting simply as a selection handler (see it as onSelect) */
|
2022-01-13 19:36:07 +01:00
|
|
|
onChange={(event, value) => handleSelect(value.label, event.keyCode)}
|
2021-12-06 18:28:36 +01:00
|
|
|
onClose={(event, reason) => reason !== 'selectOption' && setClose(true)}
|
2021-12-06 19:06:40 +01:00
|
|
|
onFocus={() => onFocus && onFocus()}
|
2021-12-06 18:28:36 +01:00
|
|
|
onHighlightChange={(event, option) => setHighlightedSuggestion(option)}
|
|
|
|
onInputChange={(event, value, reason) => reason === 'input' && handleInputChange(value)}
|
|
|
|
onOpen={() => suggestionTerm && setClose(false)}
|
|
|
|
/* 'open' is for the popper box component, set to check for a valid term
|
2021-12-07 15:51:55 +01:00
|
|
|
or else it will be displayed all the time as empty (no options) */
|
2021-12-06 18:28:36 +01:00
|
|
|
open={!!suggestionTerm && !shouldClose}
|
|
|
|
options={allOptionsGrouped}
|
|
|
|
renderGroup={({ group, children }) => renderGroup(group, children)}
|
|
|
|
renderInput={(params) => renderInput(params)}
|
2021-12-06 19:06:40 +01:00
|
|
|
renderOption={(optionProps, option) => renderOption(optionProps, option.label)}
|
2021-12-06 18:28:36 +01:00
|
|
|
/>
|
2021-12-02 17:49:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-07 14:14:48 +01:00
|
|
|
function AutocompletePopper(props: any) {
|
|
|
|
return <Popper {...props} placement="top" />;
|
|
|
|
}
|
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
function useSuggestionMatch(term: string, list: Array<string>) {
|
|
|
|
const throttledTerm = useThrottle(term);
|
|
|
|
|
|
|
|
return React.useMemo(() => {
|
|
|
|
return !throttledTerm || throttledTerm.trim() === ''
|
|
|
|
? undefined
|
|
|
|
: matchSorter(list, term, { keys: [(item) => item] });
|
|
|
|
}, [list, term, throttledTerm]);
|
|
|
|
}
|