-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.js
246 lines (216 loc) · 8.71 KB
/
index.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
import React from 'react';
import {TextInput, StyleSheet} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
const propTypes = {
// Maximum number of lines in the text input
maxLines: PropTypes.number,
// The default value of the comment box
defaultValue: PropTypes.string.isRequired,
// Callback method to handle pasting a file
onPasteFile: PropTypes.func,
// A ref to forward to the text input
forwardedRef: PropTypes.func.isRequired,
// General styles to apply to the text input
// eslint-disable-next-line react/forbid-prop-types
style: PropTypes.any,
// If the input should clear, it actually gets intercepted instead of .clear()
shouldClear: PropTypes.bool,
// When the input has cleared whoever owns this input should know about it
onClear: PropTypes.func,
// Callback to fire when a file has been dragged into the text input
onDragEnter: PropTypes.func,
// Callback to fire when the user is no longer dragging over the text input
onDragLeave: PropTypes.func,
// Callback to fire when a file is dropped on the text input
onDrop: PropTypes.func,
// Whether or not this TextInput is disabled.
isDisabled: PropTypes.bool,
};
const defaultProps = {
maxLines: -1,
onPasteFile: () => {},
shouldClear: false,
onClear: () => {},
style: null,
onDragEnter: () => {},
onDragLeave: () => {},
onDrop: () => {},
isDisabled: false,
};
const IMAGE_EXTENSIONS = {
'image/bmp': 'bmp',
'image/gif': 'gif',
'image/jpeg': 'jpg',
'image/png': 'png',
'image/svg+xml': 'svg',
'image/tiff': 'tiff',
'image/webp': 'webp',
};
/**
* On web we like to have the Text Input field always focused so the user can easily type a new chat
*/
class TextInputFocusable extends React.Component {
constructor(props) {
super(props);
this.state = {
numberOfLines: 1,
selection: {
start: this.props.defaultValue.length,
end: this.props.defaultValue.length,
},
};
}
componentDidMount() {
this.focusInput();
this.updateNumberOfLines();
// 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
if (this.props.forwardedRef && _.isFunction(this.props.forwardedRef)) {
this.props.forwardedRef(this.textInput);
}
// There is no onPaste or onDrag for TextInput in react-native so we will add event
// listeners here and unbind when the component unmounts
if (this.textInput) {
// Firefox will not allow dropping unless we call preventDefault on the dragover event
this.textInput.addEventListener('dragover', e => e.preventDefault());
this.textInput.addEventListener('dragenter', this.props.onDragEnter);
this.textInput.addEventListener('dragleave', this.props.onDragLeave);
this.textInput.addEventListener('drop', this.props.onDrop);
this.textInput.addEventListener('paste', this.checkForAttachment.bind(this));
}
}
componentDidUpdate(prevProps) {
if (!prevProps.shouldClear && this.props.shouldClear) {
this.textInput.clear();
// eslint-disable-next-line react/no-did-update-set-state
this.setState({numberOfLines: 1});
this.props.onClear();
}
if (prevProps.defaultValue !== this.props.defaultValue) {
this.updateNumberOfLines();
this.moveCursorToEnd();
}
}
componentWillUnmount() {
if (this.textInput) {
this.textInput.addEventListener('dragover', e => e.preventDefault());
this.textInput.removeEventListener('dragenter', this.props.onDragEnter);
this.textInput.removeEventListener('dragleave', this.props.onDragLeave);
this.textInput.removeEventListener('drop', this.props.onDrop);
this.textInput.removeEventListener('paste', this.checkForAttachment.bind(this));
}
}
/**
* Calculates the max number of lines the text input can have
*
* @param {Number} lineHeight
* @param {Number} paddingTopAndBottom
* @param {Number} scrollHeight
*
* @returns {Number}
*/
getNumberOfLines(lineHeight, paddingTopAndBottom, scrollHeight) {
const maxLines = this.props.maxLines;
let newNumberOfLines = Math.ceil((scrollHeight - paddingTopAndBottom) / lineHeight);
newNumberOfLines = maxLines <= 0 ? newNumberOfLines : Math.min(newNumberOfLines, maxLines);
return newNumberOfLines;
}
/**
* Check the paste event for an attachment, parse the data and
* call onPasteFile from props with the selected file
*
* @param {ClipboardEvent} event
*/
checkForAttachment(event) {
const {files, types} = event.clipboardData;
const TEXT_HTML = 'text/html';
if (files.length > 0) {
// Prevent the default so we do not post the file name into the text box
event.preventDefault();
this.props.onPasteFile(event.clipboardData.files[0]);
} else if (types.includes(TEXT_HTML)) {
const domparser = new DOMParser();
const embededImages = domparser.parseFromString(event.clipboardData.getData(TEXT_HTML), TEXT_HTML).images;
if (embededImages.length > 0) {
event.preventDefault();
fetch(embededImages[0].src)
.then((response) => {
if (!response.ok) { throw Error(response.statusText); }
return response.blob();
})
.then((x) => {
const extension = IMAGE_EXTENSIONS[x.type];
if (!extension) {
throw new Error('No extension found for mime type');
}
return new File([x], `pasted_image.${extension}`, {});
})
.then(this.props.onPasteFile)
.catch((error) => {
console.debug(error);
alert(`There was a problem getting the image you pasted. \n${error.message}`);
});
}
}
}
/**
* Check the current scrollHeight of the textarea (minus any padding) and
* divide by line height to get the total number of rows for the textarea.
*/
updateNumberOfLines() {
const computedStyle = window.getComputedStyle(this.textInput);
const lineHeight = parseInt(computedStyle.lineHeight, 10) || 20;
const paddingTopAndBottom = parseInt(computedStyle.paddingBottom, 10)
+ parseInt(computedStyle.paddingTop, 10);
// We have to reset the rows back to the minimum before updating so that the scrollHeight is not
// affected by the previous row setting. If we don't, rows will be added but not removed on backspace/delete.
this.setState({numberOfLines: 1}, () => {
this.setState({
numberOfLines: this.getNumberOfLines(lineHeight, paddingTopAndBottom, this.textInput.scrollHeight),
});
});
}
/**
* Move cursor to end by setting start and end
* to length of the input value.
*/
moveCursorToEnd() {
this.setState({
selection: {
start: this.props.defaultValue.length,
end: this.props.defaultValue.length,
},
});
}
focusInput() {
this.textInput.focus();
}
render() {
const propStyles = StyleSheet.flatten(this.props.style);
propStyles.outline = 'none';
const propsWithoutStyles = _.omit(this.props, 'style');
return (
<TextInput
ref={el => this.textInput = el}
selection={this.state.selection}
onChange={() => {
this.updateNumberOfLines();
}}
numberOfLines={this.state.numberOfLines}
style={propStyles}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...propsWithoutStyles}
disabled={this.props.isDisabled}
/>
);
}
}
TextInputFocusable.propTypes = propTypes;
TextInputFocusable.defaultProps = defaultProps;
export default React.forwardRef((props, ref) => (
/* eslint-disable-next-line react/jsx-props-no-spreading */
<TextInputFocusable {...props} forwardedRef={ref} />
));