From abec9bc39ec9fafdd0a5e1c2b9670918498b85fc Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 11 Jun 2020 15:27:45 +0200 Subject: [PATCH] Fixes #2064 and move message markup into template After some back-and-forth, I think it's still better to keep markup in templates (instead of having them directly inside the components) becaues it makes it easier for people to modify Converse.js (at the expense of some developer ergonomics). --- src/components/message.js | 40 ++---------------------------- src/templates/chat_message.js | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 src/templates/chat_message.js diff --git a/src/components/message.js b/src/components/message.js index 2edf8dc4b..4fc24a446 100644 --- a/src/components/message.js +++ b/src/components/message.js @@ -4,6 +4,7 @@ import './message-actions.js'; import MessageVersionsModal from '../modals/message-versions.js'; import dayjs from 'dayjs'; import filesize from "filesize"; +import tpl_chat_message from '../templates/chat_message.js'; import tpl_spinner from '../templates/spinner.js'; import { CustomElement } from './element.js'; import { __ } from '@converse/headless/i18n'; @@ -14,11 +15,9 @@ import { renderAvatar } from './../templates/directives/avatar'; const { Strophe } = converse.env; const u = converse.env.utils; -const i18n_edited = __('This message has been edited'); const i18n_show = __('Show more'); const i18n_show_less = __('Show less'); const i18n_uploading = __('Uploading file:'); -const i18n_new_messages = __('New messages'); class Message extends CustomElement { @@ -124,42 +123,7 @@ class Message extends CustomElement { } renderChatMessage () { - const is_groupchat_message = (this.message_type === 'groupchat'); - return html` - ${ this.is_first_unread ? html`

${ i18n_new_messages }
` : '' } -
- - ${ this.shouldShowAvatar() ? renderAvatar(this.getAvatarData()) : '' } -
- - ${ (this.is_me_message) ? html` - - ${this.hats.map(hat => html`${hat}`)} - ` : '' } - ${ this.is_me_message ? '**' : ''}${this.username} - ${ !this.is_me_message ? this.renderAvatarByline() : '' } - ${ this.is_encrypted ? html`` : '' } - -
-
- ${ this.is_retracted ? this.renderRetraction() : this.renderMessageText() } -
- ${ (this.received && !this.is_me_message && !is_groupchat_message) ? html`` : '' } - ${ (this.edited) ? html`` : '' } - -
-
-
`; + return tpl_chat_message(this); } shouldShowAvatar () { diff --git a/src/templates/chat_message.js b/src/templates/chat_message.js new file mode 100644 index 000000000..cdacd3c85 --- /dev/null +++ b/src/templates/chat_message.js @@ -0,0 +1,46 @@ +import { html } from "lit-html"; +import { __ } from '@converse/headless/i18n'; +import { renderAvatar } from './../templates/directives/avatar'; + +const i18n_edited = __('This message has been edited'); +const i18n_new_messages = __('New messages'); + + +export default (o) => { + const is_groupchat_message = (o.message_type === 'groupchat'); + return html` + ${ o.is_first_unread ? html`

${ i18n_new_messages }
` : '' } +
+ + ${ o.shouldShowAvatar() ? renderAvatar(o.getAvatarData()) : '' } +
+ + ${ (o.is_me_message) ? html` + + ${o.hats.map(hat => html`${hat}`)} + ` : '' } + ${ o.is_me_message ? '**' : ''}${o.username} + ${ !o.is_me_message ? o.renderAvatarByline() : '' } + ${ o.is_encrypted ? html`` : '' } + +
+
+ ${ o.is_retracted ? o.renderRetraction() : o.renderMessageText() } + ${ (o.received && !o.is_me_message && !is_groupchat_message) ? html`` : '' } + ${ (o.edited) ? html`` : '' } +
+ +
+
+
`; +}