2014-03-13 22:07:56 +01:00
|
|
|
var copy_and_paste = (function () {
|
|
|
|
|
2017-10-06 10:46:08 +02:00
|
|
|
var exports = {};
|
2014-03-13 22:07:56 +01:00
|
|
|
|
|
|
|
function find_boundary_tr(initial_tr, iterate_row) {
|
2016-12-02 17:09:31 +01:00
|
|
|
var j;
|
|
|
|
var skip_same_td_check = false;
|
2014-03-13 22:07:56 +01:00
|
|
|
var tr = initial_tr;
|
|
|
|
|
|
|
|
// If the selection boundary is somewhere that does not have a
|
|
|
|
// parent tr, we should let the browser handle the copy-paste
|
|
|
|
// entirely on its own
|
|
|
|
if (tr.length === 0) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2014-03-13 22:07:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the selection boundary is on a table row that does not have an
|
|
|
|
// associated message id (because the user clicked between messages),
|
|
|
|
// then scan downwards until we hit a table row with a message id.
|
|
|
|
// To ensure we can't enter an infinite loop, bail out (and let the
|
|
|
|
// browser handle the copy-paste on its own) if we don't hit what we
|
|
|
|
// are looking for within 10 rows.
|
2016-11-30 19:05:04 +01:00
|
|
|
for (j = 0; (!tr.is('.message_row')) && j < 10; j += 1) {
|
2014-03-13 22:07:56 +01:00
|
|
|
tr = iterate_row(tr);
|
|
|
|
}
|
|
|
|
if (j === 10) {
|
2018-03-13 13:04:16 +01:00
|
|
|
return;
|
2014-03-13 22:07:56 +01:00
|
|
|
} else if (j !== 0) {
|
|
|
|
// If we updated tr, then we are not dealing with a selection
|
|
|
|
// that is entirely within one td, and we can skip the same td
|
|
|
|
// check (In fact, we need to because it won't work correctly
|
|
|
|
// in this case)
|
|
|
|
skip_same_td_check = true;
|
|
|
|
}
|
|
|
|
return [rows.id(tr), skip_same_td_check];
|
|
|
|
}
|
|
|
|
|
2018-03-20 19:25:01 +01:00
|
|
|
function construct_recipient_header(message_row) {
|
|
|
|
var message_header_content = rows.get_message_recipient_header(message_row)
|
|
|
|
.text()
|
|
|
|
.replace(/\s+/g, " ")
|
|
|
|
.replace(/^\s/, "").replace(/\s$/, "");
|
|
|
|
return $('<p>').append($('<strong>').text(message_header_content));
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:49:42 +01:00
|
|
|
function construct_copy_div(div, start_id, end_id) {
|
2018-03-20 19:32:38 +01:00
|
|
|
var start_row = current_msg_list.get_row(start_id);
|
|
|
|
var start_recipient_row = rows.get_message_recipient_row(start_row);
|
|
|
|
var start_recipient_row_id = rows.id_for_recipient_row(start_recipient_row);
|
|
|
|
var should_include_start_recipient_header = false;
|
|
|
|
|
|
|
|
var last_recipient_row_id = start_recipient_row_id;
|
|
|
|
for (var row = start_row;
|
2018-03-20 18:49:42 +01:00
|
|
|
rows.id(row) <= end_id;
|
|
|
|
row = rows.next_visible(row)) {
|
2018-03-20 19:32:38 +01:00
|
|
|
var recipient_row_id = rows.id_for_recipient_row(rows.get_message_recipient_row(row));
|
|
|
|
// if we found a message from another recipient,
|
|
|
|
// it means that we have messages from several recipients,
|
|
|
|
// so we have to add new recipient's bar to final copied message
|
|
|
|
// and wouldn't forget to add start_recipient's bar at the beginning of final message
|
|
|
|
if (recipient_row_id !== last_recipient_row_id) {
|
2018-03-20 19:25:01 +01:00
|
|
|
div.append(construct_recipient_header(row));
|
2018-03-20 19:32:38 +01:00
|
|
|
last_recipient_row_id = recipient_row_id;
|
|
|
|
should_include_start_recipient_header = true;
|
2018-03-20 18:49:42 +01:00
|
|
|
}
|
|
|
|
var message = current_msg_list.get(rows.id(row));
|
|
|
|
var message_firstp = $(message.content).slice(0, 1);
|
|
|
|
message_firstp.prepend(message.sender_full_name + ": ");
|
|
|
|
div.append(message_firstp);
|
|
|
|
div.append($(message.content).slice(1));
|
|
|
|
}
|
2018-03-20 19:32:38 +01:00
|
|
|
|
|
|
|
if (should_include_start_recipient_header) {
|
|
|
|
div.prepend(construct_recipient_header(start_row));
|
|
|
|
}
|
2018-03-20 18:49:42 +01:00
|
|
|
}
|
2014-03-13 22:07:56 +01:00
|
|
|
|
2016-12-02 14:06:06 +01:00
|
|
|
function copy_handler() {
|
2014-03-13 22:07:56 +01:00
|
|
|
var selection = window.getSelection();
|
2016-12-02 17:09:31 +01:00
|
|
|
var i;
|
|
|
|
var range;
|
|
|
|
var ranges = [];
|
|
|
|
var startc;
|
|
|
|
var endc;
|
|
|
|
var initial_end_tr;
|
|
|
|
var start_id;
|
|
|
|
var end_id;
|
|
|
|
var start_data;
|
|
|
|
var end_data;
|
2014-03-13 22:07:56 +01:00
|
|
|
var skip_same_td_check = false;
|
2016-12-02 17:09:31 +01:00
|
|
|
var div = $('<div>');
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 0; i < selection.rangeCount; i += 1) {
|
2014-03-13 22:07:56 +01:00
|
|
|
range = selection.getRangeAt(i);
|
|
|
|
ranges.push(range);
|
|
|
|
|
|
|
|
startc = $(range.startContainer);
|
|
|
|
start_data = find_boundary_tr($(startc.parents('.selectable_row, .message_header')[0]), function (row) {
|
|
|
|
return row.next();
|
|
|
|
});
|
|
|
|
if (start_data === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
start_id = start_data[0];
|
|
|
|
|
|
|
|
endc = $(range.endContainer);
|
|
|
|
// If the selection ends in the bottom whitespace, we should act as
|
|
|
|
// though the selection ends on the final message
|
|
|
|
// Chrome seems to like selecting the compose_close button
|
|
|
|
// when you go off the end of the last message
|
|
|
|
if (endc.attr('id') === "bottom_whitespace" || endc.attr('id') === "compose_close") {
|
|
|
|
initial_end_tr = $(".message_row:last");
|
|
|
|
skip_same_td_check = true;
|
|
|
|
} else {
|
|
|
|
initial_end_tr = $(endc.parents('.selectable_row')[0]);
|
|
|
|
}
|
|
|
|
end_data = find_boundary_tr(initial_end_tr, function (row) {
|
|
|
|
return row.prev();
|
|
|
|
});
|
|
|
|
if (end_data === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
end_id = end_data[0];
|
|
|
|
|
|
|
|
if (start_data[1] || end_data[1]) {
|
|
|
|
skip_same_td_check = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we should let the browser handle the copy-paste entirely on its own
|
|
|
|
// (In this case, there is no need for our special copy code)
|
|
|
|
if (!skip_same_td_check &&
|
|
|
|
startc.parents('.selectable_row>div')[0] === endc.parents('.selectable_row>div')[0]) {
|
|
|
|
return;
|
2016-12-02 21:34:35 +01:00
|
|
|
}
|
2014-03-13 22:07:56 +01:00
|
|
|
|
2018-03-20 18:49:42 +01:00
|
|
|
// Construct a div for what we want to copy (div)
|
|
|
|
construct_copy_div(div, start_id, end_id);
|
2014-03-13 22:07:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (window.bridge !== undefined) {
|
|
|
|
// If the user is running the desktop app,
|
|
|
|
// convert emoji images to plain text for
|
|
|
|
// copy-paste purposes.
|
|
|
|
ui.replace_emoji_with_text(div);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select div so that the browser will copy it
|
|
|
|
// instead of copying the original selection
|
2016-12-03 03:08:47 +01:00
|
|
|
div.css({position: 'absolute', left: '-99999px'})
|
2014-03-13 22:07:56 +01:00
|
|
|
.attr('id', 'copytempdiv');
|
|
|
|
$('body').append(div);
|
|
|
|
selection.selectAllChildren(div[0]);
|
|
|
|
|
|
|
|
// After the copy has happened, delete the div and
|
|
|
|
// change the selection back to the original selection
|
|
|
|
window.setTimeout(function () {
|
|
|
|
selection = window.getSelection();
|
|
|
|
selection.removeAllRanges();
|
|
|
|
_.each(ranges, function (range) {
|
|
|
|
selection.addRange(range);
|
|
|
|
});
|
|
|
|
$('#copytempdiv').remove();
|
|
|
|
},0);
|
|
|
|
}
|
|
|
|
|
2017-11-23 21:17:08 +01:00
|
|
|
exports.paste_handler_converter = function (paste_html) {
|
2017-11-28 11:13:33 +01:00
|
|
|
var converters = {
|
|
|
|
converters: [
|
|
|
|
{
|
|
|
|
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
|
|
|
|
replacement: function (content) {
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
filter: ['em', 'i'],
|
|
|
|
replacement: function (content) {
|
|
|
|
return '*' + content + '*';
|
|
|
|
},
|
|
|
|
},
|
2018-02-22 09:44:41 +01:00
|
|
|
{
|
|
|
|
// Checks for raw links without custom text or title.
|
|
|
|
filter: function (node) {
|
|
|
|
return node.nodeName === "A" &&
|
|
|
|
node.href === node.innerHTML &&
|
|
|
|
node.href === node.title;
|
|
|
|
},
|
|
|
|
replacement: function (content) {
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Checks for escaped ordered list syntax.
|
|
|
|
filter: function (node) {
|
|
|
|
return /(\d+)\\\. /.test(node.innerHTML);
|
|
|
|
},
|
|
|
|
replacement: function (content) {
|
|
|
|
return content.replace(/(\d+)\\\. /g, '$1. ');
|
|
|
|
},
|
|
|
|
},
|
2017-11-28 11:13:33 +01:00
|
|
|
],
|
|
|
|
};
|
|
|
|
var markdown_html = toMarkdown(paste_html, converters);
|
2017-11-23 21:17:08 +01:00
|
|
|
|
|
|
|
// Now that we've done the main conversion, we want to remove
|
|
|
|
// any HTML tags that weren't converted to markdown-style
|
|
|
|
// text, since Bugdown doesn't support those.
|
|
|
|
var div = document.createElement("div");
|
|
|
|
div.innerHTML = markdown_html;
|
|
|
|
// Using textContent for modern browsers, innerText works for Internet Explorer
|
|
|
|
return div.textContent || div.innerText || "";
|
|
|
|
};
|
|
|
|
|
2017-10-06 10:46:08 +02:00
|
|
|
exports.paste_handler = function (event) {
|
|
|
|
var clipboardData = event.originalEvent.clipboardData;
|
|
|
|
|
|
|
|
var paste_html = clipboardData.getData('text/html');
|
2018-03-11 23:18:22 +01:00
|
|
|
if (paste_html) {
|
2017-10-06 10:46:08 +02:00
|
|
|
event.preventDefault();
|
2017-11-23 21:17:08 +01:00
|
|
|
var text = exports.paste_handler_converter(paste_html);
|
2017-10-06 10:46:08 +02:00
|
|
|
compose_ui.insert_syntax_and_focus(text);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-13 22:07:56 +01:00
|
|
|
$(function () {
|
2017-06-30 00:42:39 +02:00
|
|
|
$(document).on('copy', copy_handler);
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").bind('paste', exports.paste_handler);
|
2014-03-13 22:07:56 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = copy_and_paste;
|
|
|
|
}
|