Skip to content

Commit ac98bab

Browse files
committed
(UI) improve textarea resize with image
1 parent ff014ed commit ac98bab

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/ChatWindow/Room.vue

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@
156156
'textarea-image': imageFile
157157
}"
158158
:style="{
159-
height: `${imageDimensions.height - 12}px`,
160-
'min-height': `${
161-
imageDimensions.height > 20 ? imageDimensions.height - 14 : 20
162-
}px`,
163-
'padding-left': `${imageDimensions.width + 6}px`
159+
'min-height': `${imageDimensions ? imageDimensions.height : 20}px`,
160+
'padding-left': `${imageDimensions ? imageDimensions.width : 16}px`
164161
}"
165162
v-model="message"
166163
@input="onChangeInput"
@@ -262,7 +259,7 @@ export default {
262259
loadingMoreMessages: false,
263260
file: null,
264261
imageFile: null,
265-
imageDimensions: { height: '20px', width: '10px' },
262+
imageDimensions: null,
266263
menuOpened: false,
267264
emojiOpened: false,
268265
emojisList: {},
@@ -337,12 +334,14 @@ export default {
337334
},
338335
imageFile() {
339336
setTimeout(() => {
340-
if (!this.$refs.imageFile)
341-
return (this.imageDimensions = { height: 20, width: 10 })
342-
343-
this.imageDimensions = {
344-
height: this.$refs.imageFile.height + 2,
345-
width: this.$refs.imageFile.width + 20
337+
if (!this.$refs.imageFile) {
338+
this.imageDimensions = null
339+
setTimeout(() => this.resizeTextarea(), 0)
340+
} else {
341+
this.imageDimensions = {
342+
height: this.$refs.imageFile.height - 10,
343+
width: this.$refs.imageFile.width + 26
344+
}
346345
}
347346
}, 20)
348347
}
@@ -487,23 +486,20 @@ export default {
487486
if (this.isImageCheck(this.file)) this.imageFile = message.file.url
488487
this.message = message.content
489488
490-
setTimeout(() => this.resizeTextarea(this.$refs['roomTextarea']), 0)
489+
setTimeout(() => this.resizeTextarea(), 0)
491490
},
492491
scrollToBottom() {
493492
const element = this.$refs.scrollContainer
494493
element.scrollTo({ top: element.scrollHeight, behavior: 'smooth' })
495494
},
496-
onChangeInput(el) {
497-
this.autoGrow(el)
495+
onChangeInput() {
496+
this.resizeTextarea()
498497
this.$emit('typingMessage', this.message)
499498
},
500-
autoGrow(el) {
501-
this.resizeTextarea(el.srcElement)
502-
},
503-
resizeTextarea(textarea) {
504-
textarea.style.height = 0
505-
const offset = this.imageFile ? 2 : 0
506-
textarea.style.height = textarea.scrollHeight - 24 + offset + 'px'
499+
resizeTextarea() {
500+
const el = this.$refs['roomTextarea']
501+
el.style.height = 0
502+
el.style.height = el.scrollHeight - 24 + 'px'
507503
},
508504
addEmoji(emoji) {
509505
this.message += emoji.icon

0 commit comments

Comments
 (0)