Skip to content

Commit 2c46bb5

Browse files
committed
(fix) add user tag message replacement
1 parent 0c7902b commit 2c46bb5

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/ChatWindow/Room.vue

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = []

src/utils/formatString.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ const pseudo_markdown = {
5858
// allowed_chars: '[a-z_]',
5959
// end: ':',
6060
// object: child => <Emojione type={child[0]} />
61-
// },
62-
// '@': {
63-
// allowed_chars: '[a-z_.-A-Z0-9]',
64-
// end: ' ',
65-
// object: child => <User data={child} />
6661
// }
6762
}
6863

0 commit comments

Comments
 (0)