forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuggestionEmoji.js
259 lines (222 loc) · 9.33 KB
/
SuggestionEmoji.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React, {useState, useCallback, useRef, useImperativeHandle} from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import CONST from '../../../../CONST';
import useArrowKeyFocusManager from '../../../../hooks/useArrowKeyFocusManager';
import * as SuggestionsUtils from '../../../../libs/SuggestionUtils';
import * as EmojiUtils from '../../../../libs/EmojiUtils';
import EmojiSuggestions from '../../../../components/EmojiSuggestions';
import ONYXKEYS from '../../../../ONYXKEYS';
import useLocalize from '../../../../hooks/useLocalize';
import * as SuggestionProps from './suggestionProps';
/**
* Check if this piece of string looks like an emoji
* @param {String} str
* @param {Number} pos
* @returns {Boolean}
*/
const isEmojiCode = (str, pos) => {
const leftWords = str.slice(0, pos).split(CONST.REGEX.SPECIAL_CHAR_OR_EMOJI);
const leftWord = _.last(leftWords);
return CONST.REGEX.HAS_COLON_ONLY_AT_THE_BEGINNING.test(leftWord) && leftWord.length > 2;
};
const defaultSuggestionsValues = {
suggestedEmojis: [],
colonSignIndex: -1,
shouldShowSuggestionMenu: false,
};
const propTypes = {
/** Preferred skin tone */
preferredSkinTone: PropTypes.number,
/** A ref to this component */
forwardedRef: PropTypes.shape({current: PropTypes.shape({})}),
/** Function to clear the input */
resetKeyboardInput: PropTypes.func.isRequired,
/** Callback when a emoji was inserted */
onInsertedEmoji: PropTypes.func.isRequired,
...SuggestionProps.baseProps,
};
const defaultProps = {
preferredSkinTone: CONST.EMOJI_DEFAULT_SKIN_TONE,
forwardedRef: null,
};
function SuggestionEmoji({
preferredSkinTone,
value,
setValue,
selection,
setSelection,
updateComment,
isComposerFullSize,
isAutoSuggestionPickerLarge,
forwardedRef,
resetKeyboardInput,
onInsertedEmoji,
measureParentContainer,
}) {
const [suggestionValues, setSuggestionValues] = useState(defaultSuggestionsValues);
const isEmojiSuggestionsMenuVisible = !_.isEmpty(suggestionValues.suggestedEmojis) && suggestionValues.shouldShowEmojiSuggestionMenu;
const [highlightedEmojiIndex, setHighlightedEmojiIndex] = useArrowKeyFocusManager({
isActive: isEmojiSuggestionsMenuVisible,
maxIndex: SuggestionsUtils.getMaxArrowIndex(suggestionValues.suggestedEmojis.length, isAutoSuggestionPickerLarge),
shouldExcludeTextAreaNodes: false,
});
const {preferredLocale} = useLocalize();
// Used to decide whether to block the suggestions list from showing to prevent flickering
const shouldBlockCalc = useRef(false);
/**
* Replace the code of emoji and update selection
* @param {Number} selectedEmoji
*/
const insertSelectedEmoji = useCallback(
(highlightedEmojiIndexInner) => {
const commentBeforeColon = value.slice(0, suggestionValues.colonIndex);
const emojiObject = suggestionValues.suggestedEmojis[highlightedEmojiIndexInner];
const emojiCode = emojiObject.types && emojiObject.types[preferredSkinTone] ? emojiObject.types[preferredSkinTone] : emojiObject.code;
const commentAfterColonWithEmojiNameRemoved = value.slice(selection.end);
updateComment(`${commentBeforeColon}${emojiCode} ${SuggestionsUtils.trimLeadingSpace(commentAfterColonWithEmojiNameRemoved)}`, true);
// In some Android phones keyboard, the text to search for the emoji is not cleared
// will be added after the user starts typing again on the keyboard. This package is
// a workaround to reset the keyboard natively.
resetKeyboardInput();
setSelection({
start: suggestionValues.colonIndex + emojiCode.length + CONST.SPACE_LENGTH,
end: suggestionValues.colonIndex + emojiCode.length + CONST.SPACE_LENGTH,
});
setSuggestionValues((prevState) => ({...prevState, suggestedEmojis: []}));
onInsertedEmoji(emojiObject);
},
[onInsertedEmoji, preferredSkinTone, resetKeyboardInput, selection.end, setSelection, suggestionValues.colonIndex, suggestionValues.suggestedEmojis, updateComment, value],
);
/**
* Clean data related to suggestions
*/
const resetSuggestions = useCallback(() => {
setSuggestionValues(defaultSuggestionsValues);
}, []);
const updateShouldShowSuggestionMenuToFalse = useCallback(() => {
setSuggestionValues((prevState) => {
if (prevState.shouldShowSuggestionMenu) {
return {...prevState, shouldShowSuggestionMenu: false};
}
return prevState;
});
}, []);
/**
* Listens for keyboard shortcuts and applies the action
*
* @param {Object} e
*/
const triggerHotkeyActions = useCallback(
(e) => {
const suggestionsExist = suggestionValues.suggestedEmojis.length > 0;
if (((!e.shiftKey && e.key === CONST.KEYBOARD_SHORTCUTS.ENTER.shortcutKey) || e.key === CONST.KEYBOARD_SHORTCUTS.TAB.shortcutKey) && suggestionsExist) {
e.preventDefault();
if (suggestionValues.suggestedEmojis.length > 0) {
insertSelectedEmoji(highlightedEmojiIndex);
}
return true;
}
if (e.key === CONST.KEYBOARD_SHORTCUTS.ESCAPE.shortcutKey) {
e.preventDefault();
if (suggestionsExist) {
resetSuggestions();
}
return true;
}
},
[highlightedEmojiIndex, insertSelectedEmoji, resetSuggestions, suggestionValues.suggestedEmojis.length],
);
/**
* Calculates and cares about the content of an Emoji Suggester
*/
const calculateEmojiSuggestion = useCallback(
(selectionEnd) => {
if (shouldBlockCalc.current || !value) {
shouldBlockCalc.current = false;
resetSuggestions();
return;
}
const leftString = value.substring(0, selectionEnd);
const colonIndex = leftString.lastIndexOf(':');
const isCurrentlyShowingEmojiSuggestion = isEmojiCode(value, selectionEnd);
const nextState = {
suggestedEmojis: [],
colonIndex,
shouldShowEmojiSuggestionMenu: false,
};
const newSuggestedEmojis = EmojiUtils.suggestEmojis(leftString, preferredLocale);
if (newSuggestedEmojis.length && isCurrentlyShowingEmojiSuggestion) {
nextState.suggestedEmojis = newSuggestedEmojis;
nextState.shouldShowEmojiSuggestionMenu = !_.isEmpty(newSuggestedEmojis);
}
setSuggestionValues((prevState) => ({...prevState, ...nextState}));
setHighlightedEmojiIndex(0);
},
[value, preferredLocale, setHighlightedEmojiIndex, resetSuggestions],
);
const onSelectionChange = useCallback(
(e) => {
/**
* we pass here e.nativeEvent.selection.end directly to calculateEmojiSuggestion
* because in other case calculateEmojiSuggestion will have an old calculation value
* of suggestion instead of current one
*/
calculateEmojiSuggestion(e.nativeEvent.selection.end);
},
[calculateEmojiSuggestion],
);
const setShouldBlockSuggestionCalc = useCallback(
(shouldBlockSuggestionCalc) => {
shouldBlockCalc.current = shouldBlockSuggestionCalc;
},
[shouldBlockCalc],
);
useImperativeHandle(
forwardedRef,
() => ({
resetSuggestions,
onSelectionChange,
triggerHotkeyActions,
setShouldBlockSuggestionCalc,
updateShouldShowSuggestionMenuToFalse,
}),
[onSelectionChange, resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse],
);
if (!isEmojiSuggestionsMenuVisible) {
return null;
}
return (
<EmojiSuggestions
onClose={() => setSuggestionValues((prevState) => ({...prevState, suggestedEmojis: []}))}
highlightedEmojiIndex={highlightedEmojiIndex}
emojis={suggestionValues.suggestedEmojis}
comment={value}
updateComment={(newComment) => setValue(newComment)}
colonIndex={suggestionValues.colonIndex}
prefix={value.slice(suggestionValues.colonIndex + 1, selection.start)}
onSelect={insertSelectedEmoji}
isComposerFullSize={isComposerFullSize}
preferredSkinToneIndex={preferredSkinTone}
isEmojiPickerLarge={isAutoSuggestionPickerLarge}
measureParentContainer={measureParentContainer}
/>
);
}
SuggestionEmoji.propTypes = propTypes;
SuggestionEmoji.defaultProps = defaultProps;
SuggestionEmoji.displayName = 'SuggestionEmoji';
const SuggestionEmojiWithRef = React.forwardRef((props, ref) => (
<SuggestionEmoji
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));
export default withOnyx({
preferredSkinTone: {
key: ONYXKEYS.PREFERRED_EMOJI_SKIN_TONE,
selector: EmojiUtils.getPreferredSkinToneIndex,
},
})(SuggestionEmojiWithRef);