From c6feea9f6d3ff7da053051fd94ca4ffdb5734d6e Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Sun, 24 Nov 2019 00:29:55 +0530 Subject: [PATCH] [Enhancement] Format messages in widget (#268) * Use message formatter in widget * Rename the variable --- .../widgets/conversation/Message.vue | 14 +++------- .../shared/helpers/MessageFormatter.js | 27 +++++++++++++++++++ .../helpers/specs/MessageFormatter.spec.js | 13 +++++++++ .../shared/mixins/messageFormatterMixin.js | 10 +++++++ .../widget/components/AgentMessageBubble.vue | 7 ++--- .../widget/components/UserMessageBubble.vue | 12 ++++++--- 6 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 app/javascript/shared/helpers/MessageFormatter.js create mode 100644 app/javascript/shared/helpers/specs/MessageFormatter.spec.js create mode 100644 app/javascript/shared/mixins/messageFormatterMixin.js diff --git a/app/javascript/dashboard/components/widgets/conversation/Message.vue b/app/javascript/dashboard/components/widgets/conversation/Message.vue index 59283b4c1..818eef088 100644 --- a/app/javascript/dashboard/components/widgets/conversation/Message.vue +++ b/app/javascript/dashboard/components/widgets/conversation/Message.vue @@ -42,6 +42,7 @@ diff --git a/app/javascript/shared/helpers/MessageFormatter.js b/app/javascript/shared/helpers/MessageFormatter.js new file mode 100644 index 000000000..32aeef48f --- /dev/null +++ b/app/javascript/shared/helpers/MessageFormatter.js @@ -0,0 +1,27 @@ +class MessageFormatter { + constructor(message) { + this.message = message || ''; + } + + formatMessage() { + const linkifiedMessage = this.linkify(); + return linkifiedMessage.replace(/\n/g, '
'); + } + + linkify() { + if (!this.message) { + return ''; + } + const urlRegex = /(https?:\/\/[^\s]+)/g; + return this.message.replace( + urlRegex, + url => `${url}` + ); + } + + get formattedMessage() { + return this.formatMessage(); + } +} + +export default MessageFormatter; diff --git a/app/javascript/shared/helpers/specs/MessageFormatter.spec.js b/app/javascript/shared/helpers/specs/MessageFormatter.spec.js new file mode 100644 index 000000000..f6ffb34f8 --- /dev/null +++ b/app/javascript/shared/helpers/specs/MessageFormatter.spec.js @@ -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
See more at https://www.chatwoot.com' + ); + }); + }); +}); diff --git a/app/javascript/shared/mixins/messageFormatterMixin.js b/app/javascript/shared/mixins/messageFormatterMixin.js new file mode 100644 index 000000000..4c6cb0c1d --- /dev/null +++ b/app/javascript/shared/mixins/messageFormatterMixin.js @@ -0,0 +1,10 @@ +import MessageFormatter from '../helpers/MessageFormatter'; + +export default { + methods: { + formatMessage(message) { + const messageFormatter = new MessageFormatter(message); + return messageFormatter.formattedMessage; + }, + }, +}; diff --git a/app/javascript/widget/components/AgentMessageBubble.vue b/app/javascript/widget/components/AgentMessageBubble.vue index 6d270dc85..54319761a 100755 --- a/app/javascript/widget/components/AgentMessageBubble.vue +++ b/app/javascript/widget/components/AgentMessageBubble.vue @@ -1,12 +1,13 @@