Skip to content

Commit 70cfa00

Browse files
committed
(fix) scroll behaviour when adding messages
1 parent 98603b8 commit 70cfa00

File tree

2 files changed

+66
-47
lines changed

2 files changed

+66
-47
lines changed

src/ChatWindow/Message/Message.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ import MessageActions from './MessageActions'
189189
import MessageReactions from './MessageReactions'
190190
import AudioPlayer from './AudioPlayer'
191191
192+
const { messagesValid } = require('../../utils/room-validation')
192193
const {
193194
isImageFile,
194195
isVideoFile,
@@ -287,6 +288,16 @@ export default {
287288
}
288289
},
289290
291+
mounted() {
292+
if (!messagesValid(this.message)) {
293+
throw new Error(
294+
'Messages object is not valid! Must contain _id[String, Number], content[String, Number] and senderId[String, Number]'
295+
)
296+
}
297+
298+
this.$emit('message-added', { message: this.message, index: this.index })
299+
},
300+
290301
methods: {
291302
onHoverMessage() {
292303
this.imageHover = true

src/ChatWindow/Room/Room.vue

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
:text-formatting="textFormatting"
8585
:emojis-list="emojisList"
8686
:hide-options="hideOptions"
87+
@message-added="onMessageAdded"
8788
@message-action-handler="messageActionHandler"
8889
@open-file="openFile"
8990
@open-user-tag="openUserTag"
@@ -336,7 +337,6 @@ import Message from '../Message/Message'
336337
337338
import filteredUsers from '../../utils/filter-items'
338339
import Recorder from '../../utils/recorder'
339-
const { messagesValid } = require('../../utils/room-validation')
340340
const { detectMobile, iOSDevice } = require('../../utils/mobile-detection')
341341
const { isImageFile, isVideoFile } = require('../../utils/media-file')
342342
@@ -450,14 +450,7 @@ export default {
450450
},
451451
room(newVal, oldVal) {
452452
if (newVal.roomId && newVal.roomId !== oldVal.roomId) {
453-
this.loadingMessages = true
454-
this.scrollIcon = false
455-
this.scrollMessagesCount = 0
456-
this.resetMessage(true)
457-
if (this.roomMessage) {
458-
this.message = this.roomMessage
459-
setTimeout(() => this.onChangeInput(), 0)
460-
}
453+
this.onRoomChanged()
461454
}
462455
},
463456
roomMessage: {
@@ -466,14 +459,8 @@ export default {
466459
if (val) this.message = this.roomMessage
467460
}
468461
},
469-
messages(newVal, oldVal) {
470-
newVal.forEach((message, i) => {
471-
if (!messagesValid(message)) {
472-
throw new Error(
473-
'Messages object is not valid! Must contain _id[String, Number], content[String, Number] and senderId[String, Number]'
474-
)
475-
}
476-
462+
messages(val) {
463+
val.forEach((message, i) => {
477464
if (
478465
this.showNewMessagesDivider &&
479466
!message.seen &&
@@ -486,32 +473,8 @@ export default {
486473
}
487474
})
488475
489-
const element = this.$refs.scrollContainer
490-
if (!element) return
491-
492-
if (oldVal && newVal && oldVal.length === newVal.length - 1) {
493-
this.newMessages = []
494-
this.loadingMessages = false
495-
496-
if (this.getBottomScroll(element) < 60) {
497-
return this.scrollToBottom()
498-
} else {
499-
if (newVal[newVal.length - 1].senderId === this.currentUserId) {
500-
return this.scrollToBottom()
501-
} else {
502-
this.scrollIcon = true
503-
return this.scrollMessagesCount++
504-
}
505-
}
506-
}
507-
508476
if (this.infiniteState) {
509477
this.infiniteState.loaded()
510-
} else if (newVal.length && !this.scrollIcon) {
511-
setTimeout(() => {
512-
element.scrollTo({ top: element.scrollHeight })
513-
this.loadingMessages = false
514-
}, 0)
515478
}
516479
517480
setTimeout(() => (this.loadingMoreMessages = false), 0)
@@ -557,15 +520,60 @@ export default {
557520
},
558521
559522
methods: {
523+
onRoomChanged() {
524+
this.loadingMessages = true
525+
this.scrollIcon = false
526+
this.scrollMessagesCount = 0
527+
this.resetMessage(true)
528+
529+
if (this.roomMessage) {
530+
this.message = this.roomMessage
531+
setTimeout(() => this.onChangeInput(), 0)
532+
}
533+
534+
const unwatch = this.$watch(
535+
() => this.messages,
536+
val => {
537+
if (!val || !val.length) return
538+
539+
const element = this.$refs.scrollContainer
540+
if (!element) return
541+
542+
setTimeout(() => {
543+
element.scrollTo({ top: element.scrollHeight })
544+
unwatch()
545+
}, 0)
546+
}
547+
)
548+
},
549+
onMessageAdded({ message, index }) {
550+
this.newMessages = []
551+
552+
this.loadingMessages = false
553+
554+
if (index !== this.messages.length - 1) return
555+
556+
setTimeout(() => {
557+
if (this.getBottomScroll(this.$refs.scrollContainer) < 60) {
558+
this.scrollToBottom()
559+
} else {
560+
if (message.senderId === this.currentUserId) {
561+
this.scrollToBottom()
562+
} else {
563+
this.scrollIcon = true
564+
this.scrollMessagesCount++
565+
}
566+
}
567+
}, 0)
568+
},
560569
onContainerScroll(e) {
561570
this.hideOptions = true
562-
setTimeout(() => {
563-
if (!e.target) return
564571
565-
const bottomScroll = this.getBottomScroll(e.target)
566-
if (bottomScroll < 60) this.scrollMessagesCount = 0
567-
this.scrollIcon = bottomScroll > 500 || this.scrollMessagesCount
568-
}, 200)
572+
if (!e.target) return
573+
574+
const bottomScroll = this.getBottomScroll(e.target)
575+
if (bottomScroll < 60) this.scrollMessagesCount = 0
576+
this.scrollIcon = bottomScroll > 500 || this.scrollMessagesCount
569577
},
570578
updateFooterList(tagChar) {
571579
if (!this.$refs['roomTextarea']) return

0 commit comments

Comments
 (0)