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
337338import filteredUsers from ' ../../utils/filter-items'
338339import Recorder from ' ../../utils/recorder'
339- const { messagesValid } = require (' ../../utils/room-validation' )
340340const { detectMobile , iOSDevice } = require (' ../../utils/mobile-detection' )
341341const { 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