From f99fe670ba9fa1e927cb9774be26d5f4e901718e Mon Sep 17 00:00:00 2001 From: nnduc Date: Thu, 19 Nov 2026 20:27:55 +0700 Subject: [PATCH] add copy function --- frontend/locales/extra.ini | 1 + frontend/locales/extra/vi.po | 3 ++ frontend/widgets/Chat/chat.js | 41 +++++++++++++++++++ frontend/widgets/ChatActions/chatactions.js | 39 +++++++++++++++++- src/widgets/chatactions.rs | 2 +- src/widgets_registry.rs | 2 + .../_chatactions_message_dialog.html | 17 ++++---- 7 files changed, 94 insertions(+), 11 deletions(-) diff --git a/frontend/locales/extra.ini b/frontend/locales/extra.ini index 794aabc..e5fe7d4 100644 --- a/frontend/locales/extra.ini +++ b/frontend/locales/extra.ini @@ -58,6 +58,7 @@ forward_title = Forward to… forward_filter = Search a discussion forward_error = This message cannot be forwarded forward_encrypted_file = This encrypted file can only be sent in an encrypted discussion +copy_error = This message cannot be copied ; Upstream strings still in use, their widgets are not part of this edition diff --git a/frontend/locales/extra/vi.po b/frontend/locales/extra/vi.po index aa811cc..707c1c1 100644 --- a/frontend/locales/extra/vi.po +++ b/frontend/locales/extra/vi.po @@ -3125,3 +3125,6 @@ msgstr "Không thể chuyển tiếp tin nhắn này" msgid "This encrypted file can only be sent in an encrypted discussion" msgstr "Tệp mã hóa này chỉ có thể gửi trong cuộc trò chuyện được mã hóa" + +msgid "This message cannot be copied" +msgstr "Không thể sao chép tin nhắn này" diff --git a/frontend/widgets/Chat/chat.js b/frontend/widgets/Chat/chat.js index 4052d82..d2d2de9 100644 --- a/frontend/widgets/Chat/chat.js +++ b/frontend/widgets/Chat/chat.js @@ -923,6 +923,41 @@ var Chat = { } ); }, + // A selection over several messages of the discussion: one message per + // line, without the times, ticks and buttons around them + copySelection: function (event) { + let selection = window.getSelection(); + if (!selection.rangeCount || selection.isCollapsed) return; + + let range = selection.getRangeAt(0); + let widget = document.querySelector('#chat_widget'); + if (!widget || !widget.contains(range.commonAncestorContainer)) return; + + // Not the OMEMO ciphertext shown while (or because it failed) decrypting + let texts = Array.from(widget.querySelectorAll('div.bubble > div.message > p:not(.encrypted)')) + .filter(p => range.intersectsNode(p)); + if (texts.length < 2) return; + + let lines = texts.map(p => { + let part = document.createRange(); + part.selectNodeContents(p); + if (part.compareBoundaryPoints(Range.START_TO_START, range) < 0) { + part.setStart(range.startContainer, range.startOffset); + } + if (part.compareBoundaryPoints(Range.END_TO_END, range) > 0) { + part.setEnd(range.endContainer, range.endOffset); + } + + let div = document.createElement('div'); + div.appendChild(part.cloneContents()); + div.querySelectorAll('img').forEach(img => img.replaceWith(img.alt ?? '')); + div.querySelectorAll('br').forEach(br => br.replaceWith('\n')); + return div.textContent; + }).filter(line => line.trim() != ''); + + event.clipboardData.setData('text/plain', lines.join('\n')); + event.preventDefault(); + }, setMessagePressBehaviour: function () { document.querySelectorAll('#chat_widget li div.bubble:not(.file) > div.message').forEach(message => { message.onmousedown = function (e) { @@ -1879,6 +1914,12 @@ MovimWebsocket.attach(function () { } }); +// Once, the widget scripts are reloaded +if (!window.movimChatCopy) { + window.movimChatCopy = true; + document.addEventListener('copy', (e) => Chat.copySelection(e)); +} + MovimEvents.registerWindow('focus', 'chat', () => { if (MovimWebsocket.connection) { var jid = MovimUtils.urlParts().params[0]; diff --git a/frontend/widgets/ChatActions/chatactions.js b/frontend/widgets/ChatActions/chatactions.js index 2bf75d1..a5a37f4 100644 --- a/frontend/widgets/ChatActions/chatactions.js +++ b/frontend/widgets/ChatActions/chatactions.js @@ -1,6 +1,7 @@ var ChatActions = { message: null, forwardError: null, + copyError: null, // OMEMO messages of the open conversation: {jid, entries: [[mid, id, messageid, published]], bodies} index: null, keywords: '', @@ -8,13 +9,31 @@ var ChatActions = { setMessage: function (message) { ChatActions.message = message; }, - initMessageDialog: function (forwardError) { + initMessageDialog: function (forwardError, copyError) { ChatActions.forwardError = forwardError; + ChatActions.copyError = copyError; // The system share sheet (mostly on mobile), to send to other apps if (navigator.share) { document.querySelector('#chat_actions li.share')?.classList.remove('hide'); } + + ChatActions.fillPreview(); + }, + // The server cannot read an OMEMO message, the preview shows its + // decrypted text from this browser, like in the discussion + fillPreview: async function () { + let p = document.querySelector('#message_preview p.encrypted[data-mid]'); + if (!p || !ChatActions.message?.encrypted || ChatActions.message.retracted) return; + + let text = await ChatActions.messageText(); + // Another dialog meanwhile + if (!text || !p.isConnected) return; + + if (typeof ChatOmemo == 'undefined' || !ChatOmemo.renderEncryptedFile(p, text)) { + p.innerHTML = MovimUtils.linkify(MovimUtils.htmlEscape(text)); + } + p.classList.remove('encrypted'); }, // The text of the message, decrypted by this browser for OMEMO @@ -33,6 +52,24 @@ var ChatActions = { return text.trim() ? text : null; }, + // Copy the text, decrypted for OMEMO. The link of an encrypted file + // carries its key, it is never put in the clipboard. + copy: async function () { + let text = await ChatActions.messageText(); + if (!text || (typeof AESGCM_REGEX != 'undefined' && AESGCM_REGEX.test(text.trim()))) { + Toast.send(ChatActions.copyError); + return; + } + + try { + await navigator.clipboard.writeText(text); + ChatActions_ajaxCopiedMessageText(); + } catch (e) { + MovimUtils.logError(e); + Toast.send(ChatActions.copyError); + } + }, + filterForward: function (keyword) { let words = ChatActions.normalize(keyword).split(/\s+/).filter(w => w); document.querySelectorAll('#forward_targets li').forEach(li => { diff --git a/src/widgets/chatactions.rs b/src/widgets/chatactions.rs index 7c2fe0e..9cd98f3 100644 --- a/src/widgets/chatactions.rs +++ b/src/widgets/chatactions.rs @@ -121,7 +121,7 @@ async fn show_message_dialog(ctx: &Ctx, mid: i64) { }), ); super::dialog(ctx, &html, false, false); - rpc!(ctx, "ChatActions.initMessageDialog", ctx.t("chatactions.forward_error", &[])); + rpc!(ctx, "ChatActions.initMessageDialog", ctx.t("chatactions.forward_error", &[]), ctx.t("chatactions.copy_error", &[])); } /// Where to forward a message: the open discussions (most recent first), diff --git a/src/widgets_registry.rs b/src/widgets_registry.rs index b834cfe..a93e12b 100644 --- a/src/widgets_registry.rs +++ b/src/widgets_registry.rs @@ -97,6 +97,8 @@ pub static WIDGETS: &[WidgetDef] = &[ ("ajaxShowSearchDialog", &["jid", "muc"]), ("ajaxSearchMessages", &["jid", "keywords", "muc", "encrypted"]), ("ajaxShowFiles", &["jid", "muc"]), + ("ajaxShowForwardDialog", &[]), + ("ajaxForwardTo", &["jid", "muc"]), ("ajaxCopiedMessageText", &[]), ("ajaxHttpDaemonRetract", &["mid"]), ("ajaxHttpDaemonModerate", &["mid"]), diff --git a/templates/ChatActions/_chatactions_message_dialog.html b/templates/ChatActions/_chatactions_message_dialog.html index 47742d3..8a71d7c 100644 --- a/templates/ChatActions/_chatactions_message_dialog.html +++ b/templates/ChatActions/_chatactions_message_dialog.html @@ -19,16 +19,15 @@

{{ __('message.react') }}

-
  • - - content_copy - -
    -

    {{ __('chatactions.copy_text') }}

    -
    -
  • {% endif %} +
  • + + content_copy + +
    +

    {{ __('chatactions.copy_text') }}

    +
    +
  • reply