compose: Preserve links when pasting html.

This uses the to-markdown.js library to do all the hard work of
parsing HTML and turning it into markdown and not e.g. uploaded files.

Tweaked by tabbott to better scope when it activates to just include
pastes of HTML content.

Fixes #5853.
This commit is contained in:
Garvit
2017-10-06 14:16:08 +05:30
committed by Tim Abbott
parent 9fa58a3c16
commit 52069998eb
5 changed files with 60 additions and 233 deletions

View File

@@ -1,6 +1,6 @@
var copy_and_paste = (function () {
var exports = {}; // we don't actually export anything yet, but that's ok
var exports = {};
function find_boundary_tr(initial_tr, iterate_row) {
var j;
@@ -140,11 +140,31 @@ function copy_handler() {
},0);
}
exports.paste_handler = function (event) {
var clipboardData = event.originalEvent.clipboardData;
var paste_html = clipboardData.getData('text/html');
if (paste_html) {
event.preventDefault();
var markdown_html = toMarkdown(paste_html);
// 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
var text = div.textContent || div.innerText || "";
compose_ui.insert_syntax_and_focus(text);
}
};
$(function () {
$(document).on('copy', copy_handler);
$("#new_message_content").bind('paste', exports.paste_handler);
});
return exports;
}());