Allow pasting images from the clipboard to upload an image

(imported from commit 0d0ebecdb35521819433691c2d3a07fb13572eb4)
This commit is contained in:
Leo Franchi
2013-04-19 13:09:40 -04:00
parent 7902ecf9b5
commit c4fa29a9d5

View File

@@ -76,6 +76,8 @@
caller = this;
this.on('drop', drop).on('dragstart', opts.dragStart).on('dragenter', dragEnter).on('dragover', dragOver).on('dragleave', dragLeave);
this.on('paste', paste);
$(document).on('drop', docDrop).on('dragenter', docEnter).on('dragover', docOver).on('dragleave', docLeave);
$('#' + opts.fallback_id).change(function(e) {
@@ -107,6 +109,38 @@
return false;
}
function paste(event) {
// Take the first image pasted in the clipboard
var match_re = /image.*/;
var item;
$.each(event.originalEvent.clipboardData.items, function (idx, this_event) {
if (this_event.type.match(match_re)) {
item = this_event;
return false;
}
});
if (item === undefined) {
return;
}
// Call the user callback to initialize the drop event
if( opts.drop.call(this, event) === false ) return false;
// Read the data of the drop in as binary data, and send it to the server
var data = item.getAsFile();
var reader = new FileReader();
reader.onload = function(event) {
function finished_callback(serverResponse, timeDiff, xhr) {
return opts.uploadFinished(-1, undefined, serverResponse, timeDiff, xhr);
}
var url_params = "?mimetype=" + encodeURIComponent(data.type);
do_xhr("pasted_image", event.target.result, data.type, {}, url_params, finished_callback, function () {});
};
reader.readAsBinaryString(data);
}
function getBuilder(filename, filedata, mime, boundary) {
var dashdash = '--',
crlf = '\r\n',