-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathindex.native.tsx
More file actions
144 lines (130 loc) · 6.32 KB
/
Copy pathindex.native.tsx
File metadata and controls
144 lines (130 loc) · 6.32 KB
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
import type {MarkdownStyle, MarkdownTextInput} from '@expensify/react-native-live-markdown';
import mimeDb from 'mime-db';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {NativeSyntheticEvent, TextInputChangeEvent, TextInputPasteEventData} from 'react-native';
import {StyleSheet} from 'react-native';
import type {ComposerProps, ComposerRef} from '@components/Composer/types';
import type {AnimatedMarkdownTextInputRef} from '@components/RNMarkdownTextInput';
import RNMarkdownTextInput from '@components/RNMarkdownTextInput';
import useIsInLandscapeMode from '@hooks/useIsInLandscapeMode';
import useMarkdownStyle from '@hooks/useMarkdownStyle';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {containsOnlyEmojis} from '@libs/EmojiUtils';
import {splitExtensionFromFileName} from '@libs/fileDownload/FileUtils';
import getLandscapeTextInputRefProxy from '@libs/getLandscapeTextInputRefProxy';
import Parser from '@libs/Parser';
import getFileSize from '@pages/Share/getFileSize';
import CONST from '@src/CONST';
import type {FileObject} from '@src/types/utils/Attachment';
const excludeNoStyles: Array<keyof MarkdownStyle> = [];
const excludeReportMentionStyle: Array<keyof MarkdownStyle> = ['mentionReport'];
function Composer({
onClear: onClearProp = () => {},
onPasteFile = () => {},
isDisabled = false,
maxLines,
isComposerFullSize = 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,
ref,
...props
}: ComposerProps) {
const textInputRef = useRef<MarkdownTextInput | null>(null);
const textContainsOnlyEmojis = useMemo(() => containsOnlyEmojis(Parser.htmlToText(Parser.replace(value ?? ''))), [value]);
const theme = useTheme();
const markdownStyle = useMarkdownStyle(textContainsOnlyEmojis, !isGroupPolicyReport ? excludeReportMentionStyle : excludeNoStyles);
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const isInLandscapeMode = useIsInLandscapeMode();
useEffect(() => {
if (!textInputRef.current?.setSelection || !selection || isComposerFullSize) {
return;
}
// We need the delay for setSelection to properly work for IOS in bridgeless mode due to a react native
// internal bug of dispatching the event before the component is ready for it.
// (see https://github.com/Expensify/App/pull/50520#discussion_r1861960311 for more context)
const timeoutID = setTimeout(() => {
// We are setting selection twice to trigger a scroll to the cursor on toggling to smaller composer size.
textInputRef.current?.setSelection((selection.start || 1) - 1, selection.start);
textInputRef.current?.setSelection(selection.start, selection.start);
}, 0);
return () => clearTimeout(timeoutID);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isComposerFullSize]);
/**
* Set the TextInput Ref
* @param {Element} el
*/
const setTextInputRef = useCallback(
(el: AnimatedMarkdownTextInputRef | null) => {
textInputRef.current = isInLandscapeMode ? getLandscapeTextInputRefProxy(el) : el;
if (typeof ref !== 'function' || textInputRef.current === null) {
return;
}
// 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(textInputRef.current as ComposerRef);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[isInLandscapeMode],
);
const onClear = useCallback(
({nativeEvent}: TextInputChangeEvent) => {
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} = splitExtensionFromFileName(baseFileName);
const fileExtension = originalFileExtension || (mimeDb[mimeType].extensions?.[0] ?? 'bin');
const fileName = `${stem}.${fileExtension}`;
let file: FileObject = {uri: fileURI, name: fileName, type: mimeType, size: 0};
getFileSize(file.uri ?? '')
.then((size) => (file = {...file, size}))
.finally(() => 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}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...props}
autoFocus={isInLandscapeMode ? false : props.autoFocus}
readOnly={isDisabled}
onPaste={pasteFile}
onClear={onClear}
disableFullscreenUI
/>
);
}
export default Composer;