forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.native.tsx
160 lines (143 loc) · 6.66 KB
/
index.native.tsx
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
import type {MarkdownStyle} from '@expensify/react-native-live-markdown';
import mimeDb from 'mime-db';
import type {ForwardedRef} from 'react';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {NativeSyntheticEvent, TextInput, TextInputChangeEventData, TextInputPasteEventData} from 'react-native';
import {StyleSheet} from 'react-native';
import type {FileObject} from '@components/AttachmentModal';
import type {ComposerProps} from '@components/Composer/types';
import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput';
import RNMarkdownTextInput from '@components/RNMarkdownTextInput';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useMarkdownStyle from '@hooks/useMarkdownStyle';
import useResetComposerFocus from '@hooks/useResetComposerFocus';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as EmojiUtils from '@libs/EmojiUtils';
import * as FileUtils from '@libs/fileDownload/FileUtils';
import CONST from '@src/CONST';
const excludeNoStyles: Array<keyof MarkdownStyle> = [];
const excludeReportMentionStyle: Array<keyof MarkdownStyle> = ['mentionReport'];
function Composer(
{
onClear: onClearProp = () => {},
onPasteFile = () => {},
isDisabled = false,
maxLines,
isComposerFullSize = false,
autoFocus = false,
style,
// On native layers we like to have the Text Input not focused so the
// user can read new chats without the keyboard in the way of the view.
// On Android the selection prop is required on the TextInput but this prop has issues on IOS
selection,
value,
isGroupPolicyReport = false,
...props
}: ComposerProps,
ref: ForwardedRef<TextInput>,
) {
const textInput = useRef<AnimatedMarkdownTextInputRef | null>(null);
const {isFocused, shouldResetFocusRef} = useResetComposerFocus(textInput);
const textContainsOnlyEmojis = useMemo(() => EmojiUtils.containsOnlyEmojis(value ?? ''), [value]);
const theme = useTheme();
const markdownStyle = useMarkdownStyle(value, !isGroupPolicyReport ? excludeReportMentionStyle : excludeNoStyles);
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {inputCallbackRef, inputRef: autoFocusInputRef} = useAutoFocusInput();
useEffect(() => {
if (autoFocus === !!autoFocusInputRef.current) {
return;
}
inputCallbackRef(autoFocus ? textInput.current : null);
}, [autoFocus, inputCallbackRef, autoFocusInputRef]);
useEffect(() => {
if (!textInput.current || !textInput.current.setSelection || !selection || isComposerFullSize) {
return;
}
// We need the delay for setSelection to properly work for IOS.
const timeoutID = setTimeout(() => {
// We are setting selection twice to trigger a scroll to the cursor on toggling to smaller composer size.
textInput.current?.setSelection((selection.start || 1) - 1, selection.start);
textInput.current?.setSelection(selection.start, selection.start);
}, 0);
return () => clearTimeout(timeoutID);
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [isComposerFullSize]);
/**
* Set the TextInput Ref
* @param {Element} el
*/
const setTextInputRef = useCallback((el: AnimatedMarkdownTextInputRef) => {
// eslint-disable-next-line react-compiler/react-compiler
textInput.current = el;
if (typeof ref !== 'function' || textInput.current === null) {
return;
}
if (autoFocus) {
inputCallbackRef(el);
}
// This callback prop is used by the parent component using the constructor to
// get a ref to the inner textInput element e.g. if we do
// <constructor ref={el => this.textInput = el} /> this will not
// return a ref to the component, but rather the HTML element by default
ref(textInput.current);
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
const onClear = useCallback(
({nativeEvent}: NativeSyntheticEvent<TextInputChangeEventData>) => {
onClearProp(nativeEvent.text);
},
[onClearProp],
);
const pasteFile = useCallback(
(e: NativeSyntheticEvent<TextInputPasteEventData>) => {
const clipboardContent = e.nativeEvent.items.at(0);
if (clipboardContent?.type === 'text/plain') {
return;
}
const mimeType = clipboardContent?.type ?? '';
const fileURI = clipboardContent?.data;
const baseFileName = fileURI?.split('/').pop() ?? 'file';
const {fileName: stem, fileExtension: originalFileExtension} = FileUtils.splitExtensionFromFileName(baseFileName);
const fileExtension = originalFileExtension || (mimeDb[mimeType].extensions?.[0] ?? 'bin');
const fileName = `${stem}.${fileExtension}`;
const file: FileObject = {uri: fileURI, name: fileName, type: mimeType};
onPasteFile(file);
},
[onPasteFile],
);
const maxHeightStyle = useMemo(() => StyleUtils.getComposerMaxHeightStyle(maxLines, isComposerFullSize), [StyleUtils, isComposerFullSize, maxLines]);
const composerStyle = useMemo(() => StyleSheet.flatten([style, textContainsOnlyEmojis ? styles.onlyEmojisTextLineHeight : {}]), [style, textContainsOnlyEmojis, styles]);
return (
<RNMarkdownTextInput
id={CONST.COMPOSER.NATIVE_ID}
multiline
autoComplete="off"
placeholderTextColor={theme.placeholderText}
ref={setTextInputRef}
value={value}
rejectResponderTermination={false}
smartInsertDelete={false}
textAlignVertical="center"
style={[composerStyle, maxHeightStyle]}
markdownStyle={markdownStyle}
autoFocus={autoFocus}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...props}
readOnly={isDisabled}
onPaste={pasteFile}
onBlur={(e) => {
if (!isFocused) {
// eslint-disable-next-line react-compiler/react-compiler
shouldResetFocusRef.current = true; // detect the input is blurred when the page is hidden
}
props?.onBlur?.(e);
}}
onClear={onClear}
/>
);
}
Composer.displayName = 'Composer';
export default React.forwardRef(Composer);