From f88520d9258cfeb9c4c2b328da01ee628f452ce4 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Fri, 26 Jul 2024 10:51:55 +0200 Subject: [PATCH] Implements #146: copy message button for moderators We overload the copy message method that comes with Converse 11, to add the message metadata (nick and full date). --- client/utils/conversejs.ts | 2 +- conversejs/builtin.ts | 1 + conversejs/lib/plugins/livechat-specific.ts | 2 ++ .../livechat-specific/message-action.ts | 32 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 conversejs/lib/plugins/livechat-specific/message-action.ts diff --git a/client/utils/conversejs.ts b/client/utils/conversejs.ts index 89936971..758e14ee 100644 --- a/client/utils/conversejs.ts +++ b/client/utils/conversejs.ts @@ -167,7 +167,7 @@ async function displayConverseJS ( const converseJSParams: InitConverseJSParams = await (response).json() if (!pollListenerInitiliazed) { - // First time we got here, initiliaze this event: + // First time we got here, initialize this event: const i18nVoteOk = await clientOptions.peertubeHelpers.translate(LOC_POLL_VOTE_OK) pollListenerInitiliazed = true document.addEventListener('livechat-poll-vote', () => { diff --git a/conversejs/builtin.ts b/conversejs/builtin.ts index 3165f871..c1dd6689 100644 --- a/conversejs/builtin.ts +++ b/conversejs/builtin.ts @@ -34,6 +34,7 @@ declare global { env: { html: Function sizzle: Function + dayjs: Function } } initConversePlugins: typeof initConversePlugins diff --git a/conversejs/lib/plugins/livechat-specific.ts b/conversejs/lib/plugins/livechat-specific.ts index cfec5b8e..494f88bb 100644 --- a/conversejs/lib/plugins/livechat-specific.ts +++ b/conversejs/lib/plugins/livechat-specific.ts @@ -7,6 +7,7 @@ import { customizeToolbar } from './livechat-specific/toolbar' import { initReconnectionStuff } from './livechat-specific/reconnection' import { chatRoomOverrides } from './livechat-specific/chatroom' import { chatRoomMessageOverrides } from './livechat-specific/chatroom-message' +import { customizeMessageAction } from './livechat-specific/message-action' export const livechatSpecificsPlugin = { dependencies: ['converse-muc', 'converse-muc-views'], @@ -22,6 +23,7 @@ export const livechatSpecificsPlugin = { customizeHeading(this) customizeToolbar(this) + customizeMessageAction(this) _converse.api.listen.on('chatRoomViewInitialized', function (this: any, _model: any): void { // Remove the spinner if present... diff --git a/conversejs/lib/plugins/livechat-specific/message-action.ts b/conversejs/lib/plugins/livechat-specific/message-action.ts new file mode 100644 index 00000000..87bb70ca --- /dev/null +++ b/conversejs/lib/plugins/livechat-specific/message-action.ts @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: 2024 John Livingston +// +// SPDX-License-Identifier: AGPL-3.0-only + +/** + * Do some customization on Message actions custom elements: + * * override the copy text method to add meta data + * + * @param plugin The plugin object + */ +export function customizeMessageAction (plugin: any): void { + const _converse = plugin._converse + const MessageActions = _converse.api.elements.registry['converse-message-actions'] + if (MessageActions) { + class MessageActionsOverloaded extends MessageActions { + async onMessageCopyButtonClicked (ev?: Event): Promise { + ev?.preventDefault?.() + let txt = '' + try { + txt += this.model.getDisplayName() as string + txt += ' - ' + const date = new Date(this.model.get('edited') || this.model.get('time')) + txt += date.toLocaleDateString() + ' ' + date.toLocaleTimeString() + txt += '\n' + } catch {} + txt += this.model.getMessageText() as string + await navigator.clipboard.writeText(txt) + } + } + _converse.api.elements.define('converse-message-actions', MessageActionsOverloaded) + } +}