@@ -598,24 +598,27 @@ export default {
598
598
599
599
this .textareaCursorPosition = this .$refs [' roomTextarea' ].selectionStart
600
600
601
- let n = this .textareaCursorPosition
601
+ let position = this .textareaCursorPosition
602
602
603
603
while (
604
- n > 0 &&
605
- this .message .charAt (n - 1 ) !== ' @' &&
606
- this .message .charAt (n - 1 ) !== ' '
604
+ position > 0 &&
605
+ this .message .charAt (position - 1 ) !== ' @' &&
606
+ this .message .charAt (position - 1 ) !== ' '
607
607
) {
608
- n --
608
+ position --
609
609
}
610
610
611
- const beforeTag = this .message .charAt (n - 2 )
611
+ const beforeTag = this .message .charAt (position - 2 )
612
612
const notLetterNumber = ! beforeTag .match (/ ^ [0-9a-zA-Z ] + $ / )
613
613
614
614
if (
615
- this .message .charAt (n - 1 ) === ' @' &&
615
+ this .message .charAt (position - 1 ) === ' @' &&
616
616
(! beforeTag || beforeTag === ' ' || notLetterNumber)
617
617
) {
618
- const query = this .message .substring (n, this .textareaCursorPosition )
618
+ const query = this .message .substring (
619
+ position,
620
+ this .textareaCursorPosition
621
+ )
619
622
620
623
this .filteredUsersTag = filteredUsers (
621
624
this .room .users ,
@@ -628,11 +631,32 @@ export default {
628
631
}
629
632
},
630
633
selectUserTag (user ) {
631
- const cursorPosition = this .$refs [' roomTextarea' ].selectionStart - 1
634
+ const cursorPosition = this .$refs [' roomTextarea' ].selectionStart
635
+
636
+ let position = cursorPosition
637
+ while (position > 0 && this .message .charAt (position - 1 ) !== ' @' ) {
638
+ position--
639
+ }
640
+
641
+ let endPosition = position
642
+ while (
643
+ this .message .charAt (endPosition) &&
644
+ this .message .charAt (endPosition).trim ()
645
+ ) {
646
+ endPosition++
647
+ }
648
+
649
+ const space = this .message .substr (endPosition, endPosition).length
650
+ ? ' '
651
+ : ' '
652
+
632
653
this .message =
633
- this .message .substr (0 , cursorPosition + 1 ) +
654
+ this .message .substr (0 , position ) +
634
655
user .username +
635
- this .message .substr (cursorPosition + 1 )
656
+ space +
657
+ this .message .substr (endPosition, this .message .length - 1 )
658
+
659
+ this .focusTextarea ()
636
660
},
637
661
resetUsersTag () {
638
662
this .filteredUsersTag = []
0 commit comments