Skip to content

Commit e36a217

Browse files
committed
(fix) handle multiple usertags
1 parent abf4589 commit e36a217

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

src/components/FormatMessage/FormatMessage.vue

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,24 @@ export default {
135135
}
136136
},
137137
formatTags(content) {
138-
this.users.forEach(user => {
139-
const index = content.indexOf(user._id)
140-
const isTag = content.substring(index - 9, index) === '<usertag>'
141-
if (isTag) content = content.replace(user._id, `@${user.username}`)
138+
const firstTag = '<usertag>'
139+
const secondTag = '</usertag>'
140+
141+
const usertags = [...content.matchAll(new RegExp(firstTag, 'gi'))].map(
142+
a => a.index
143+
)
144+
145+
const initialContent = content
146+
147+
usertags.forEach(index => {
148+
const userId = initialContent.substring(
149+
index + firstTag.length,
150+
initialContent.indexOf(secondTag, index)
151+
)
152+
153+
const user = this.users.find(user => user._id === userId)
154+
155+
content = content.replaceAll(userId, `@${user?.username || 'unknown'}`)
142156
})
143157
144158
return content

src/lib/Room/Room.vue

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -993,31 +993,30 @@ export default {
993993
this.editedMessage = { ...message }
994994
995995
let messageContent = message.content
996+
const initialContent = messageContent
996997
997-
const res = [
998-
...messageContent.matchAll(new RegExp('<usertag>', 'gi'))
999-
].map(a => a.index)
998+
const firstTag = '<usertag>'
999+
const secondTag = '</usertag>'
10001000
1001-
if (res.length) {
1002-
const firstTag = '<usertag>'
1003-
const secondTag = '</usertag>'
1001+
const usertags = [
1002+
...messageContent.matchAll(new RegExp(firstTag, 'gi'))
1003+
].map(a => a.index)
10041004
1005-
res.forEach(r => {
1006-
const userId = messageContent.substring(
1007-
messageContent.indexOf(firstTag) + firstTag.length,
1008-
messageContent.indexOf(secondTag)
1009-
)
1005+
usertags.forEach(index => {
1006+
const userId = initialContent.substring(
1007+
index + firstTag.length,
1008+
initialContent.indexOf(secondTag, index)
1009+
)
10101010
1011-
const user = this.room.users.find(user => user._id === userId)
1011+
const user = this.room.users.find(user => user._id === userId)
10121012
1013-
messageContent = messageContent.replace(
1014-
`${firstTag}${userId}${secondTag}`,
1015-
`@${user.username || 'unknown'}`
1016-
)
1013+
messageContent = messageContent.replace(
1014+
`${firstTag}${userId}${secondTag}`,
1015+
`@${user?.username || 'unknown'}`
1016+
)
10171017
1018-
this.selectUserTag(user, true)
1019-
})
1020-
}
1018+
this.selectUserTag(user, true)
1019+
})
10211020
10221021
this.message = messageContent
10231022

0 commit comments

Comments
 (0)