84
84
:text-formatting =" textFormatting"
85
85
:emojis-list =" emojisList"
86
86
:hide-options =" hideOptions"
87
+ @message-added =" onMessageAdded"
87
88
@message-action-handler =" messageActionHandler"
88
89
@open-file =" openFile"
89
90
@open-user-tag =" openUserTag"
@@ -336,7 +337,6 @@ import Message from '../Message/Message'
336
337
337
338
import filteredUsers from ' ../../utils/filter-items'
338
339
import Recorder from ' ../../utils/recorder'
339
- const { messagesValid } = require (' ../../utils/room-validation' )
340
340
const { detectMobile , iOSDevice } = require (' ../../utils/mobile-detection' )
341
341
const { isImageFile , isVideoFile } = require (' ../../utils/media-file' )
342
342
@@ -450,14 +450,7 @@ export default {
450
450
},
451
451
room (newVal , oldVal ) {
452
452
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 ()
461
454
}
462
455
},
463
456
roomMessage: {
@@ -466,14 +459,8 @@ export default {
466
459
if (val) this .message = this .roomMessage
467
460
}
468
461
},
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 ) => {
477
464
if (
478
465
this .showNewMessagesDivider &&
479
466
! message .seen &&
@@ -486,32 +473,8 @@ export default {
486
473
}
487
474
})
488
475
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
-
508
476
if (this .infiniteState ) {
509
477
this .infiniteState .loaded ()
510
- } else if (newVal .length && ! this .scrollIcon ) {
511
- setTimeout (() => {
512
- element .scrollTo ({ top: element .scrollHeight })
513
- this .loadingMessages = false
514
- }, 0 )
515
478
}
516
479
517
480
setTimeout (() => (this .loadingMoreMessages = false ), 0 )
@@ -557,15 +520,60 @@ export default {
557
520
},
558
521
559
522
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
+ },
560
569
onContainerScroll (e ) {
561
570
this .hideOptions = true
562
- setTimeout (() => {
563
- if (! e .target ) return
564
571
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
569
577
},
570
578
updateFooterList (tagChar ) {
571
579
if (! this .$refs [' roomTextarea' ]) return
0 commit comments