Skip to content

Commit b1bc43d

Browse files
committed
(feat) add reply message flow
1 parent 2667e04 commit b1bc43d

File tree

7 files changed

+241
-60
lines changed

7 files changed

+241
-60
lines changed

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,16 @@ colors="{
144144
messageUsernameColor: '#9ca6af',
145145
messageTimestampColor: '#828c94',
146146
messageDateBg: 'rgba(33, 148, 243, 0.15)',
147-
messageDateColor: '#b8bdcc',
147+
messageDateColor: '#505a62',
148148
messageTextColor: '#0a0a0a',
149-
roomLastMessage: '#a2aeb8',
150-
roomTimestamp: '#67717a',
149+
messageReplyBg: '#f0f0f0',
150+
messageReplyUsernameColor: '#9ca6af',
151+
messageReplyContentColor: '#0a0a0a',
152+
roomLastMessage: '#67717a',
153+
roomTimestamp: '#a2aeb8',
151154
textColor: '#0a0a0a',
152155
inputBg: '#fff',
156+
footerBg: '#f0f0f0',
153157
spinnerColor: '#333',
154158
borderColor: '#d3dde7',
155159
iconsColor: {
@@ -169,8 +173,9 @@ colors="{
169173
pencilEdited: '#9e9e9e',
170174
trash: '#f44336',
171175
checkmark: '#0696c7',
172-
eye: '#9ca6af'
173-
}
176+
eye: '#fff',
177+
reply: '#000'
178+
}
174179
}"
175180
```
176181

@@ -224,15 +229,15 @@ messages="[
224229

225230
## Events API
226231

227-
| Event | Params | Fires when |
228-
| --------------------- | --------------------------------------------- | ----------------------------------------------------- |
229-
| fetchMessages (1) | `{ room, options }` | A user has scrolled on top to load more messages |
230-
| sendMessage | `{ roomId, content, file (3) }` | A user has sent a message |
231-
| editMessage | `{ roomId, messageId, newContent, file (3) }` | A user has edited a message |
232-
| deleteMessage | `{ roomId, messageId }` | A user has deleted a message |
233-
| openFile | `{ message }` | A user has clicked to view or download a file |
234-
| addRoom | - | A user clicks on the plus icon next to searchbar |
235-
| menuActionHandler (2) | `{ roomId, action }` | A user clicks on the vertical dots icon inside a room |
232+
| Event | Params | Fires when |
233+
| --------------------- | --------------------------------------------------------------- | ----------------------------------------------------- |
234+
| fetchMessages (1) | `{ room, options }` | A user has scrolled on top to load more messages |
235+
| sendMessage | `{ roomId, content, file (3), replyMessage (4) }` | A user has sent a message |
236+
| editMessage | `{ roomId, messageId, newContent, file (3), replyMessage (4) }` | A user has edited a message |
237+
| deleteMessage | `{ roomId, messageId }` | A user has deleted a message |
238+
| openFile | `{ message }` | A user has clicked to view or download a file |
239+
| addRoom | - | A user clicks on the plus icon next to searchbar |
240+
| menuActionHandler (2) | `{ roomId, action }` | A user clicks on the vertical dots icon inside a room |
236241

237242
(1) `fetchMessages` should be a method implementing a pagination system. Its purpose is to load older messages of a conversation when the user scroll on top
238243

@@ -255,6 +260,8 @@ menuActionHandler({ roomId, action }) {
255260

256261
(3) All file params contain: `{ blob, localURL, name, size, type }`
257262

263+
(4) `replyMessage` object is available when the user replied to another message by clicking the corresponding icon, and contains the message information that was clicked
264+
258265
## Using with Firestore
259266

260267
### Source code

demo/src/ChatContainer.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export default {
209209
)
210210
211211
room.roomName =
212-
roomContacts.map(user => user.username).join(', ') || 'Myslef'
212+
roomContacts.map(user => user.username).join(', ') || 'Myself'
213213
214214
const roomAvatar =
215215
roomContacts.length === 1 && roomContacts[0].avatar
@@ -360,7 +360,7 @@ export default {
360360
}
361361
},
362362
363-
async sendMessage({ content, roomId, file }) {
363+
async sendMessage({ content, roomId, file, replyMessage }) {
364364
const message = {
365365
sender_id: this.currentUserId,
366366
content,
@@ -376,6 +376,17 @@ export default {
376376
}
377377
}
378378
379+
if (replyMessage) {
380+
message.replyMessage = {
381+
_id: replyMessage._id,
382+
content: replyMessage.content,
383+
sender_id:
384+
replyMessage.sender_id === 'me'
385+
? this.currentUserId
386+
: replyMessage.sender_id
387+
}
388+
}
389+
379390
const { id } = await roomsRef
380391
.doc(roomId)
381392
.collection('messages')

src/ChatWindow/ChatMessage.vue

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121
@mouseover="onHoverMessage(message)"
2222
@mouseleave="onLeaveMessage"
2323
>
24+
<transition name="fade">
25+
<div
26+
v-if="messageReply && !message.deleted"
27+
class="svg-button button-reply"
28+
:class="{
29+
'reply-current': message.sender_id === 'me',
30+
'reply-agent': message.sender_id !== 'me'
31+
}"
32+
@click.stop="replyMessage(message)"
33+
>
34+
<svg-icon name="reply" />
35+
</div>
36+
</transition>
37+
2438
<div
2539
class="message-card"
2640
:class="{
@@ -40,6 +54,14 @@
4054
<span>{{ message.username }}</span>
4155
</div>
4256

57+
<div
58+
v-if="!message.deleted && message.replyMessage"
59+
class="reply-message"
60+
>
61+
<div class="reply-username">{{ replyUsername }}</div>
62+
<div class="reply-content">{{ message.replyMessage.content }}</div>
63+
</div>
64+
4365
<div v-if="message.deleted">
4466
<span>{{ textMessages.MESSAGE_DELETED }}</span>
4567
</div>
@@ -131,7 +153,8 @@ export default {
131153
openMessageId: {},
132154
hoverMessageId: null,
133155
imageLoading: false,
134-
imageHover: false
156+
imageHover: false,
157+
messageReply: false
135158
}
136159
},
137160
@@ -162,6 +185,11 @@ export default {
162185
this.message.seen &&
163186
!this.message.deleted
164187
)
188+
},
189+
replyUsername() {
190+
const { sender_id } = this.message.replyMessage
191+
const replyUser = this.roomUsers.find(user => user._id === sender_id)
192+
return replyUser ? replyUser.username : ''
165193
}
166194
},
167195
@@ -184,19 +212,24 @@ export default {
184212
},
185213
onHoverMessage() {
186214
this.imageHover = true
215+
this.messageReply = true
187216
if (this.canEditMessage()) this.hoverMessageId = this.message._id
188217
},
189218
canEditMessage() {
190219
return this.message.sender_id === 'me' && !this.message.deleted
191220
},
192221
onLeaveMessage() {
193222
this.imageHover = false
223+
this.messageReply = false
194224
this.openMessageId.toggle = false
195225
this.hoverMessageId = null
196226
},
197227
editMessage() {
198228
this.$emit('editMessage', this.message)
199229
},
230+
replyMessage() {
231+
this.$emit('replyMessage', this.message)
232+
},
200233
deleteMessage() {
201234
this.$emit('deleteMessage', this.message)
202235
this.onLeaveMessage()
@@ -231,7 +264,7 @@ export default {
231264
text-transform: uppercase;
232265
padding: 4px;
233266
color: var(--chat-color-message-date);
234-
background: var(--chat-color-message-date-bg);
267+
background: var(--chat-bg-color-message-date);
235268
display: block;
236269
overflow-wrap: break-word;
237270
position: relative;
@@ -254,6 +287,7 @@ export default {
254287
}
255288
256289
.message-container {
290+
position: relative;
257291
padding: 3px 10px;
258292
align-items: end;
259293
min-width: 100px;
@@ -349,6 +383,43 @@ export default {
349383
}
350384
}
351385
386+
.reply-message {
387+
background: var(--chat-bg-color-message-reply);
388+
border-radius: 4px;
389+
margin: -1px -5px 8px;
390+
391+
.reply-username {
392+
padding: 5px 5px 0 5px;
393+
color: var(--chat-color-message-reply-username);
394+
font-size: 13px;
395+
line-height: 15px;
396+
margin-bottom: 2px;
397+
}
398+
399+
.reply-content {
400+
padding: 0 5px 5px 5px;
401+
color: var(--chat-color-message-reply-content);
402+
}
403+
}
404+
405+
.button-reply {
406+
position: absolute;
407+
top: 4px;
408+
409+
svg {
410+
height: 20px;
411+
width: 20px;
412+
}
413+
}
414+
415+
.reply-agent {
416+
right: -14px;
417+
}
418+
419+
.reply-current {
420+
left: -14px;
421+
}
422+
352423
.text-username {
353424
font-size: 13px;
354425
color: var(--chat-color-message-username);

0 commit comments

Comments
 (0)