Skip to content

Commit d6ea02f

Browse files
authored
Merge pull request #10 from antoine92190/text-formatting
Add text formatting
2 parents 8f2be87 + 8384164 commit d6ea02f

File tree

8 files changed

+290
-74
lines changed

8 files changed

+290
-74
lines changed

README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ You can import it as a custom component:
101101
| showFiles | Boolean | - | true |
102102
| showEmojis | Boolean | - | true |
103103
| showReactionEmojis | Boolean | - | true |
104-
| textMessages (7) | Object | - | null |
105-
| responsiveBreakpoint (8) | Number | - | 900 |
106-
| singleRoom (9) | Number | - | false |
107-
| theme (10) | Sring | - | light |
108-
| styles (11) | Object | - | (10) |
104+
| textFormatting (7) | Boolean | - | true |
105+
| textMessages (8) | Object | - | null |
106+
| responsiveBreakpoint (9) | Number | - | 900 |
107+
| singleRoom (10) | Number | - | false |
108+
| theme (11) | Sring | - | light |
109+
| styles (12) | Object | - | (10) |
109110

110111
(1) `currentUserId` is required to display UI and trigger actions according to the user using the chat (ex: messages position on the right, etc.)
111112

@@ -188,13 +189,23 @@ textMessages="{
188189
}"
189190
```
190191

191-
(8) `responsiveBreakpoint` can be used to collapse the rooms list on the left when then viewport size goes below the specified width.
192+
(8) `textFormatting` can be used to add text formatting. Currently, bold, italic, strikethrough and underline formatting are available and can be used in conjonction. You can disable text formatting by passing the prop as `:textFormatting="false"`.
192193

193-
(9) `singleRoom` can be used if you never want to show the rooms list on the left. You still need to pass the `rooms` prop as an array with a single element.
194+
| Style | Syntax | Example | Output |
195+
| ----------------- | --------------- | -------------------------------------- | -------------------------------------- |
196+
| Bold | `* *` | `*This is bold text*` | **This is bold text** |
197+
| Italic | `_ _` | `_This text is italicized_` | _This text is italicized_ |
198+
| Strikethrough | `~ ~` | `~This was mistaken text~` | ~~This was mistaken text~~ |
199+
| Underline | `° °` | `°This text is underlined°` | <ins>This text is underlined</ins> |
200+
| Nested formatting | `* *` and `_ _` | `*This text is _extremely_ important*` | **This text is _extremely_ important** |
194201

195-
(10) `theme` can be used to change the chat theme. Currently, only `light` and `dark` are available.
202+
(9) `responsiveBreakpoint` can be used to collapse the rooms list on the left when then viewport size goes below the specified width.
196203

197-
(11) `styles` can be used to customize your own theme. Ex:
204+
(10) `singleRoom` can be used if you never want to show the rooms list on the left. You still need to pass the `rooms` prop as an array with a single element.
205+
206+
(11) `theme` can be used to change the chat theme. Currently, only `light` and `dark` are available.
207+
208+
(12) `styles` can be used to customize your own theme. Ex:
198209

199210
```javascript
200211
styles="{

src/ChatWindow/ChatWindow.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
:textMessages="t"
1111
:showAddRoom="showAddRoom"
1212
:showRoomsList="showRoomsList"
13+
:textFormatting="textFormatting"
1314
:isMobile="isMobile"
1415
@fetchRoom="fetchRoom"
1516
@addRoom="addRoom"
@@ -33,6 +34,7 @@
3334
:textMessages="t"
3435
:singleRoom="singleRoom"
3536
:showRoomsList="showRoomsList"
37+
:textFormatting="textFormatting"
3638
:isMobile="isMobile"
3739
:loadingRooms="loadingRooms"
3840
:roomInfo="$listeners.roomInfo"
@@ -94,6 +96,7 @@ export default {
9496
showFiles: { type: Boolean, default: true },
9597
showEmojis: { type: Boolean, default: true },
9698
showReactionEmojis: { type: Boolean, default: true },
99+
textFormatting: { type: Boolean, default: true },
97100
newMessage: { type: Object, default: null }
98101
},
99102

src/ChatWindow/FormatMessage.vue

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<template>
2+
<div>
3+
<div v-if="textFormatting">
4+
<template v-for="(message, i) in linkifiedMessage">
5+
<component
6+
:is="message.types.indexOf('url') !== -1 ? 'a' : 'span'"
7+
:key="i"
8+
:class="{
9+
'text-bold': message.types.indexOf('bold') !== -1,
10+
'text-italic': message.types.indexOf('italic') !== -1,
11+
'text-strike': message.types.indexOf('strike') !== -1,
12+
'text-underline': message.types.indexOf('underline') !== -1
13+
}"
14+
:href="message.href"
15+
target="_blank"
16+
>{{ message.value }}</component
17+
>
18+
</template>
19+
</div>
20+
<div v-else>{{ content }}</div>
21+
</div>
22+
</template>
23+
24+
<script>
25+
import formatString from '../utils/formatString'
26+
27+
export default {
28+
name: 'format-message',
29+
30+
props: {
31+
content: { type: [String, Number], required: true },
32+
formatLinks: { type: Boolean, default: true },
33+
textFormatting: { type: Boolean, required: true }
34+
},
35+
36+
computed: {
37+
linkifiedMessage() {
38+
return formatString(this.content, this.formatLinks)
39+
}
40+
}
41+
}
42+
</script>
43+
44+
<style></style>

src/ChatWindow/Message.vue

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@
6262
</div>
6363

6464
<div v-else-if="!message.file">
65-
<span v-for="(message, i) in linkifiedMessage" :key="i">
66-
<span v-if="message.bind" v-html="message.content"></span>
67-
<span v-else>{{ message.content }}</span>
68-
</span>
65+
<format-message
66+
:content="this.message.content"
67+
:textFormatting="textFormatting"
68+
></format-message>
6969
</div>
7070

7171
<div class="image-container" v-else-if="isImage">
@@ -101,10 +101,10 @@
101101
</div>
102102
</transition>
103103
</div>
104-
<span v-for="(message, i) in linkifiedMessage" :key="i">
105-
<span v-if="message.bind" v-html="message.content"></span>
106-
<span v-else>{{ message.content }}</span>
107-
</span>
104+
<format-message
105+
:content="this.message.content"
106+
:textFormatting="textFormatting"
107+
></format-message>
108108
</div>
109109

110110
<div v-else class="file-message">
@@ -229,12 +229,11 @@ import vClickOutside from 'v-click-outside'
229229
import SvgIcon from './SvgIcon'
230230
import Loader from './Loader'
231231
import EmojiPicker from './EmojiPicker'
232-
233-
import formatString from '../utils/formatString'
232+
import FormatMessage from './FormatMessage'
234233
235234
export default {
236235
name: 'message',
237-
components: { SvgIcon, Loader, EmojiPicker },
236+
components: { SvgIcon, Loader, EmojiPicker, FormatMessage },
238237
239238
directives: {
240239
clickOutside: vClickOutside.directive
@@ -252,6 +251,7 @@ export default {
252251
roomFooterRef: { type: HTMLDivElement },
253252
newMessages: { type: Array },
254253
showReactionEmojis: { type: Boolean, required: true },
254+
textFormatting: { type: Boolean, required: true },
255255
emojisList: { type: Object, required: true },
256256
hideOptions: { type: Boolean, required: true }
257257
},
@@ -310,9 +310,6 @@ export default {
310310
},
311311
312312
computed: {
313-
linkifiedMessage() {
314-
return formatString(this.message.content, true)
315-
},
316313
showDate() {
317314
return (
318315
this.index > 0 &&

src/ChatWindow/Room.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
:roomFooterRef="$refs.roomFooter"
9595
:newMessages="newMessages"
9696
:showReactionEmojis="showReactionEmojis"
97+
:textFormatting="textFormatting"
9798
:emojisList="emojisList"
9899
:hideOptions="hideOptions"
99100
@messageActionHandler="messageActionHandler"
@@ -264,6 +265,7 @@ export default {
264265
showFiles: { type: Boolean, required: true },
265266
showEmojis: { type: Boolean, required: true },
266267
showReactionEmojis: { type: Boolean, required: true },
268+
textFormatting: { type: Boolean, required: true },
267269
loadingRooms: { type: Boolean, required: true },
268270
roomInfo: { type: Function }
269271
},

src/ChatWindow/RoomsList.vue

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@
6363
<span v-if="room.lastMessage.seen">
6464
<svg-icon name="checkmark" class="icon-check" />
6565
</span>
66-
<span v-for="(message, i) in getLastMessage(room)" :key="i">
67-
<span v-if="message.bind" v-html="message.content"></span>
68-
<span v-else>{{ message.content }}</span>
69-
</span>
66+
<format-message
67+
:content="getLastMessage(room)"
68+
:formatLinks="false"
69+
:textFormatting="textFormatting"
70+
></format-message>
7071
</div>
7172
</div>
7273
</div>
@@ -77,19 +78,20 @@
7778
<script>
7879
import Loader from './Loader'
7980
import SvgIcon from './SvgIcon'
81+
import FormatMessage from './FormatMessage'
8082
8183
import filteredUsers from '../utils/filterItems'
82-
import formatString from '../utils/formatString'
8384
8485
export default {
8586
name: 'rooms-list',
86-
components: { Loader, SvgIcon },
87+
components: { Loader, SvgIcon, FormatMessage },
8788
8889
props: {
8990
currentUserId: { type: [String, Number], required: true },
9091
textMessages: { type: Object, required: true },
9192
showRoomsList: { type: Boolean, required: true },
9293
showAddRoom: { type: Boolean, required: true },
94+
textFormatting: { type: Boolean, required: true },
9395
isMobile: { type: Boolean, required: true },
9496
rooms: { type: Array, required: true },
9597
loadingRooms: { type: Boolean, required: true },
@@ -137,18 +139,18 @@ export default {
137139
},
138140
getLastMessage(room) {
139141
if (room.users.length <= 2) {
140-
return formatString(room.lastMessage.content)
142+
return room.lastMessage.content
141143
}
142144
143145
const user = room.users.find(
144146
user => user._id === room.lastMessage.sender_id
145147
)
146148
147149
if (user._id === this.currentUserId) {
148-
return formatString(room.lastMessage.content)
150+
return room.lastMessage.content
149151
}
150152
151-
return `${user.username} - ${formatString(room.lastMessage.content)}`
153+
return `${user.username} - ${room.lastMessage.content}`
152154
}
153155
}
154156
}

src/styles/helper.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,19 @@
5151
margin-right: 15px;
5252
border-radius: 50%;
5353
}
54+
55+
.text-bold {
56+
font-weight: bold;
57+
}
58+
59+
.text-italic {
60+
font-style: italic;
61+
}
62+
63+
.text-strike {
64+
text-decoration: line-through;
65+
}
66+
67+
.text-underline {
68+
text-decoration: underline;
69+
}

0 commit comments

Comments
 (0)