Merge pull request #1464 from thelounge/xpaw/clipboard

Format messages on copy
This commit is contained in:
Pavel Djundik 2017-09-03 14:19:21 +03:00 committed by GitHub
commit 704dae9dec
3 changed files with 50 additions and 1 deletions

View File

@ -174,6 +174,7 @@ kbd {
color: rgba(0, 0, 0, 0.35) !important; color: rgba(0, 0, 0, 0.35) !important;
} }
#js-copy-hack,
#help, #help,
#windows .header .title, #windows .header .title,
#windows .header .topic, #windows .header .topic,
@ -185,6 +186,16 @@ kbd {
cursor: text; 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 */ /* Icons */
#viewport .lt::before, #viewport .lt::before,
@ -1001,7 +1012,6 @@ kbd {
#chat .time, #chat .time,
#chat .from, #chat .from,
#chat .content { #chat .content {
display: block;
padding: 2px 0; padding: 2px 0;
flex: 0 0 auto; flex: 0 0 auto;
} }

38
client/js/clipboard.js Normal file
View File

@ -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);

View File

@ -21,6 +21,7 @@ const options = require("./options");
const utils = require("./utils"); const utils = require("./utils");
require("./autocompletion"); require("./autocompletion");
require("./webpush"); require("./webpush");
require("./clipboard");
$(function() { $(function() {
var sidebar = $("#sidebar, #footer"); var sidebar = $("#sidebar, #footer");