[Enhancement] Format messages in widget (#268)
* Use message formatter in widget * Rename the variable
This commit is contained in:
parent
c96e2c334c
commit
c6feea9f6d
6 changed files with 66 additions and 17 deletions
|
@ -42,6 +42,7 @@
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
/* eslint-disable no-named-as-default */
|
/* eslint-disable no-named-as-default */
|
||||||
|
import messageFormatterMixin from 'shared/mixins/messageFormatterMixin';
|
||||||
import getEmojiSVG from '../emoji/utils';
|
import getEmojiSVG from '../emoji/utils';
|
||||||
import timeMixin from '../../../mixins/time';
|
import timeMixin from '../../../mixins/time';
|
||||||
import BubbleText from './bubble/Text';
|
import BubbleText from './bubble/Text';
|
||||||
|
@ -56,7 +57,7 @@ export default {
|
||||||
BubbleMap,
|
BubbleMap,
|
||||||
BubbleAudio,
|
BubbleAudio,
|
||||||
},
|
},
|
||||||
mixins: [timeMixin],
|
mixins: [timeMixin, messageFormatterMixin],
|
||||||
props: {
|
props: {
|
||||||
data: {
|
data: {
|
||||||
type: Object,
|
type: Object,
|
||||||
|
@ -70,8 +71,7 @@ export default {
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
message() {
|
message() {
|
||||||
const linkifiedMessage = this.linkify(this.data.content);
|
return this.formatMessage(this.data.content);
|
||||||
return linkifiedMessage.replace(/\n/g, '<br>');
|
|
||||||
},
|
},
|
||||||
alignBubble() {
|
alignBubble() {
|
||||||
return this.data.message_type === 1 ? 'right' : 'left';
|
return this.data.message_type === 1 ? 'right' : 'left';
|
||||||
|
@ -106,14 +106,6 @@ export default {
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
getEmojiSVG,
|
getEmojiSVG,
|
||||||
linkify(text) {
|
|
||||||
if (!text) return text;
|
|
||||||
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
|
||||||
return text.replace(
|
|
||||||
urlRegex,
|
|
||||||
url => `<a href="${url}" target="_blank">${url}</a>`
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
27
app/javascript/shared/helpers/MessageFormatter.js
Normal file
27
app/javascript/shared/helpers/MessageFormatter.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
class MessageFormatter {
|
||||||
|
constructor(message) {
|
||||||
|
this.message = message || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
formatMessage() {
|
||||||
|
const linkifiedMessage = this.linkify();
|
||||||
|
return linkifiedMessage.replace(/\n/g, '<br>');
|
||||||
|
}
|
||||||
|
|
||||||
|
linkify() {
|
||||||
|
if (!this.message) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
||||||
|
return this.message.replace(
|
||||||
|
urlRegex,
|
||||||
|
url => `<a href="${url}" target="_blank">${url}</a>`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
get formattedMessage() {
|
||||||
|
return this.formatMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MessageFormatter;
|
13
app/javascript/shared/helpers/specs/MessageFormatter.spec.js
Normal file
13
app/javascript/shared/helpers/specs/MessageFormatter.spec.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import MessageFormatter from '../MessageFormatter';
|
||||||
|
|
||||||
|
describe('#MessageFormatter', () => {
|
||||||
|
describe('content with links', () => {
|
||||||
|
it('should format correctly', () => {
|
||||||
|
const message =
|
||||||
|
'Chatwoot is an opensource tool\nSee more at https://www.chatwoot.com';
|
||||||
|
expect(new MessageFormatter(message).formattedMessage).toEqual(
|
||||||
|
'Chatwoot is an opensource tool<br>See more at <a href="https://www.chatwoot.com" target="_blank">https://www.chatwoot.com</a>'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
10
app/javascript/shared/mixins/messageFormatterMixin.js
Normal file
10
app/javascript/shared/mixins/messageFormatterMixin.js
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import MessageFormatter from '../helpers/MessageFormatter';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
methods: {
|
||||||
|
formatMessage(message) {
|
||||||
|
const messageFormatter = new MessageFormatter(message);
|
||||||
|
return messageFormatter.formattedMessage;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
|
@ -1,12 +1,13 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="chat-bubble agent">
|
<div class="chat-bubble agent" v-html="formatMessage(message)"></div>
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import messageFormatterMixin from 'shared/mixins/messageFormatterMixin';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'AgentMessageBubble',
|
name: 'AgentMessageBubble',
|
||||||
|
mixins: [messageFormatterMixin],
|
||||||
props: {
|
props: {
|
||||||
message: String,
|
message: String,
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="chat-bubble user">
|
<div class="chat-bubble user" v-html="formatMessage(message)"></div>
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import messageFormatterMixin from 'shared/mixins/messageFormatterMixin';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'UserMessageBubble',
|
name: 'UserMessageBubble',
|
||||||
|
mixins: [messageFormatterMixin],
|
||||||
props: {
|
props: {
|
||||||
message: String,
|
message: String,
|
||||||
},
|
},
|
||||||
|
@ -28,6 +29,11 @@ export default {
|
||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
max-width: 80%;
|
max-width: 80%;
|
||||||
padding: $space-small $space-two;
|
padding: $space-small $space-two;
|
||||||
|
text-align: left;
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: $color-white;
|
||||||
|
}
|
||||||
|
|
||||||
&.user {
|
&.user {
|
||||||
border-bottom-right-radius: $space-smaller;
|
border-bottom-right-radius: $space-smaller;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue