/
ComposerMessageInput.tsx
220 lines (184 loc) · 5.73 KB
/
ComposerMessageInput.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
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
import React from 'react'
import debounce from 'debounce'
import { ActionEmitter, KeybindAction } from '../../keybindings'
import { getLogger } from '../../../shared/logger'
const log = getLogger('renderer/composer/ComposerMessageInput')
type ComposerMessageInputProps = {
chatId: number
sendMessage: () => void
enterKeySends: boolean
updateDraftText: (text: string, InputChatId: number) => void
}
type ComposerMessageInputState = {
text: string
chatId: number
// error?:boolean|Error
loadingDraft: boolean
}
export default class ComposerMessageInput extends React.Component<
ComposerMessageInputProps,
ComposerMessageInputState
> {
composerSize: number
setCursorPosition: number | false
textareaRef: React.RefObject<HTMLTextAreaElement>
saveDraft: () => void
constructor(props: ComposerMessageInputProps) {
super(props)
this.state = {
text: '',
chatId: props.chatId,
loadingDraft: true,
}
this.composerSize = 48
this.setComposerSize = this.setComposerSize.bind(this)
this.setCursorPosition = false
this.onKeyDown = this.onKeyDown.bind(this)
this.onChange = this.onChange.bind(this)
this.insertStringAtCursorPosition = this.insertStringAtCursorPosition.bind(
this
)
this.saveDraft = debounce(() => {
const { text, chatId } = this.state
this.props.updateDraftText(text, chatId)
}, 500)
this.textareaRef = React.createRef()
this.focus = this.focus.bind(this)
}
componentDidMount() {
ActionEmitter.registerHandler(KeybindAction.Composer_Focus, this.focus)
}
componentWillUnmount() {
ActionEmitter.unRegisterHandler(KeybindAction.Composer_Focus, this.focus)
}
static getDerivedStateFromProps(
props: ComposerMessageInputProps,
currentState: ComposerMessageInputState
) {
if (currentState.chatId !== props.chatId) {
return { chatId: props.chatId, text: '', loadingDraft: true }
}
return null
}
setText(text: string) {
this.setState({ text })
this.setState({ loadingDraft: false })
}
setComposerSize(size: number) {
this.composerSize = size
}
focus() {
log.debug('Focus composer message input')
setTimeout(() => this.textareaRef?.current?.focus())
}
getText() {
return this.state.text
}
clearText() {
this.setState({ text: '' })
}
componentDidUpdate(
_prevProps: ComposerMessageInputProps,
prevState: ComposerMessageInputState
) {
if (this.setCursorPosition) {
this.textareaRef.current.selectionStart = this.setCursorPosition
this.textareaRef.current.selectionEnd = this.setCursorPosition
// Focus on the current selection, hack for focusing on newlines
this.textareaRef.current.blur()
this.textareaRef.current.focus()
this.setCursorPosition = false
}
if (prevState.chatId === this.state.chatId) {
this.resizeTextareaAndComposer()
if (prevState.text !== this.state.text) {
if (!this.state.loadingDraft) {
this.saveDraft()
}
}
}
}
onChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
this.setState({ text: e.target.value /*error: false*/ })
if (!this.state.loadingDraft) {
this.saveDraft()
}
}
keyEventToAction(e: React.KeyboardEvent<HTMLTextAreaElement>) {
const enterKeySends = this.props.enterKeySends
// ENTER + SHIFT
if (e.keyCode === 13 && e.shiftKey) {
return 'NEWLINE'
// ENTER + CTRL
} else if (e.keyCode === 13 && e.ctrlKey) {
return 'SEND'
// ENTER
} else if (e.keyCode === 13 && !e.shiftKey) {
return enterKeySends ? 'SEND' : 'NEWLINE'
}
}
onKeyDown(e: React.KeyboardEvent<HTMLTextAreaElement>) {
const action = this.keyEventToAction(e)
if (!action) return
if (action === 'NEWLINE') {
this.insertStringAtCursorPosition('\n')
} else if (action === 'SEND') {
this.props.sendMessage()
}
e.preventDefault()
e.stopPropagation()
}
resizeTextareaAndComposer() {
const maxScrollHeight = 9 * 24
const el = this.textareaRef.current
// We need to set the textarea height first to `auto` to get the real needed
// scrollHeight. Ugly hack.
el.style.height = 'auto'
const scrollHeight = el.scrollHeight
if (scrollHeight + 16 === this.composerSize) {
el.style.height = scrollHeight + 'px'
return
}
if (scrollHeight > maxScrollHeight && el.classList.contains('scroll')) {
el.style.height = maxScrollHeight + 'px'
return
}
if (scrollHeight < maxScrollHeight) {
this.setComposerSize(scrollHeight + 16)
el.style.height = scrollHeight + 'px'
el.classList.remove('scroll')
} else {
this.setComposerSize(maxScrollHeight + 16)
el.style.height = maxScrollHeight + 'px'
el.classList.add('scroll')
}
}
insertStringAtCursorPosition(str: string) {
const textareaElem = this.textareaRef.current
const { selectionStart, selectionEnd } = textareaElem
const textValue = this.state.text
const textBeforeCursor = textValue.slice(0, selectionStart)
const textAfterCursor = textValue.slice(selectionEnd)
const updatedText = textBeforeCursor + str + textAfterCursor
this.setCursorPosition = textareaElem.selectionStart + str.length
this.setState({ text: updatedText })
}
render() {
return (
<textarea
className='message-input-area'
id='composer-textarea'
ref={this.textareaRef}
rows={1}
// intent={this.state.error ? 'danger' : 'none'}
// large
value={this.state.text}
onKeyDown={this.onKeyDown}
onChange={this.onChange}
placeholder={window.static_translate('write_message_desktop')}
disabled={this.state.loadingDraft}
dir='auto'
/>
)
}
}