From 7ee808169db2c246a39e5ae3a22a2febd8c0adfb Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sat, 26 Aug 2017 19:36:18 +0300 Subject: [PATCH] Format messages on copy Fixes #1146 --- client/css/style.css | 12 +++++++++++- client/js/clipboard.js | 38 ++++++++++++++++++++++++++++++++++++++ client/js/lounge.js | 1 + 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 client/js/clipboard.js diff --git a/client/css/style.css b/client/css/style.css index 775aff25..4ead5c6c 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -174,6 +174,7 @@ kbd { color: rgba(0, 0, 0, 0.35) !important; } +#js-copy-hack, #help, #windows .header .title, #windows .header .topic, @@ -185,6 +186,16 @@ kbd { cursor: text; } +#js-copy-hack { + position: absolute; + left: -999999px; +} + +#chat #js-copy-hack .condensed:not(.closed) .msg, +#chat #js-copy-hack > .msg { + display: block; +} + /* Icons */ #viewport .lt::before, @@ -1001,7 +1012,6 @@ kbd { #chat .time, #chat .from, #chat .content { - display: block; padding: 2px 0; flex: 0 0 auto; } diff --git a/client/js/clipboard.js b/client/js/clipboard.js new file mode 100644 index 00000000..8b8ba81b --- /dev/null +++ b/client/js/clipboard.js @@ -0,0 +1,38 @@ +"use strict"; + +const $ = require("jquery"); +const chat = document.getElementById("chat"); + +function copyMessages() { + const selection = window.getSelection(); + + // If selection does not span multiple elements, do nothing + if (selection.anchorNode === selection.focusNode) { + return; + } + + const range = selection.getRangeAt(0); + const documentFragment = range.cloneContents(); + const div = document.createElement("div"); + + $(documentFragment) + .find(".from .user") + .each((_, el) => { + el = $(el); + el.text(`<${el.text()}>`); + }); + + div.id = "js-copy-hack"; + div.appendChild(documentFragment); + chat.appendChild(div); + + selection.selectAllChildren(div); + + window.setTimeout(() => { + chat.removeChild(div); + selection.removeAllRanges(); + selection.addRange(range); + }, 0); +} + +$(chat).on("copy", ".messages", copyMessages); diff --git a/client/js/lounge.js b/client/js/lounge.js index 4e7590b4..324f6f47 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -21,6 +21,7 @@ const options = require("./options"); const utils = require("./utils"); require("./autocompletion"); require("./webpush"); +require("./clipboard"); $(function() { var sidebar = $("#sidebar, #footer");