@@ -598,24 +598,27 @@ export default {
598598
599599 this .textareaCursorPosition = this .$refs [' roomTextarea' ].selectionStart
600600
601- let n = this .textareaCursorPosition
601+ let position = this .textareaCursorPosition
602602
603603 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 ) !== ' '
607607 ) {
608- n --
608+ position --
609609 }
610610
611- const beforeTag = this .message .charAt (n - 2 )
611+ const beforeTag = this .message .charAt (position - 2 )
612612 const notLetterNumber = ! beforeTag .match (/ ^ [0-9a-zA-Z ] + $ / )
613613
614614 if (
615- this .message .charAt (n - 1 ) === ' @' &&
615+ this .message .charAt (position - 1 ) === ' @' &&
616616 (! beforeTag || beforeTag === ' ' || notLetterNumber)
617617 ) {
618- const query = this .message .substring (n, this .textareaCursorPosition )
618+ const query = this .message .substring (
619+ position,
620+ this .textareaCursorPosition
621+ )
619622
620623 this .filteredUsersTag = filteredUsers (
621624 this .room .users ,
@@ -628,11 +631,32 @@ export default {
628631 }
629632 },
630633 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+
632653 this .message =
633- this .message .substr (0 , cursorPosition + 1 ) +
654+ this .message .substr (0 , position ) +
634655 user .username +
635- this .message .substr (cursorPosition + 1 )
656+ space +
657+ this .message .substr (endPosition, this .message .length - 1 )
658+
659+ this .focusTextarea ()
636660 },
637661 resetUsersTag () {
638662 this .filteredUsersTag = []
0 commit comments