Add support for pasting files. Close #903.

This commit is contained in:
Nicolas Stepien 2013-02-27 04:18:39 +01:00
parent 5c1f2128af
commit 70fdb95586
4 changed files with 49 additions and 6 deletions

View File

@ -661,7 +661,9 @@
fd = new FormData(); fd = new FormData();
for (key in form) { for (key in form) {
val = form[key]; val = form[key];
if (val) { if (val instanceof Blob) {
fd.append(key, val, val.name);
} else if (val) {
fd.append(key, val); fd.append(key, val);
} }
} }
@ -5678,6 +5680,9 @@
el: link, el: link,
order: 10 order: 10
}); });
if ($.engine === 'webkit') {
$.on(d, 'paste', QR.paste);
}
$.on(d, 'dragover', QR.dragOver); $.on(d, 'dragover', QR.dragOver);
$.on(d, 'drop', QR.dropFile); $.on(d, 'drop', QR.dropFile);
$.on(d, 'dragstart dragend', QR.drag); $.on(d, 'dragstart dragend', QR.drag);
@ -5980,12 +5985,33 @@
QR.fileInput(e.dataTransfer.files); QR.fileInput(e.dataTransfer.files);
return $.addClass(QR.nodes.el, 'dump'); return $.addClass(QR.nodes.el, 'dump');
}, },
paste: function(e) {
var blob, files, item, _i, _len, _ref;
files = [];
_ref = e.clipboardData.items;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
if (item.kind === 'file') {
blob = item.getAsFile();
blob.name = 'file';
if (blob.type) {
blob.name += '.' + blob.type.split('/')[1];
}
files.push(blob);
}
}
if (!files.length) {
return;
}
QR.open();
return QR.fileInput(files);
},
fileInput: function(files) { fileInput: function(files) {
var file, length, max, post, _i, _len, _ref, _ref1; var file, length, max, post, _i, _len, _ref, _ref1;
if (!(files instanceof FileList)) { if (files instanceof Event) {
files = __slice.call(this.files); files = __slice.call(this.files);
}
QR.nodes.fileInput.value = null; QR.nodes.fileInput.value = null;
}
length = files.length; length = files.length;
if (!length) { if (!length) {
return; return;

View File

@ -17,6 +17,7 @@ beta
QR changes: QR changes:
Opening text files will insert their content in the comment field. Opening text files will insert their content in the comment field.
Pasting files/images (e.g. from another website) in Chrome will open them in the QR.
Cooldown start time is now more accurate, which means shorter cooldown period and faster auto-posting. Cooldown start time is now more accurate, which means shorter cooldown period and faster auto-posting.
Clicking the submit button while uploading will abort the upload and won't start re-uploading automatically anymore. Clicking the submit button while uploading will abort the upload and won't start re-uploading automatically anymore.
Closing the QR while uploading will abort the upload and won't close the QR anymore. Closing the QR while uploading will abort the upload and won't close the QR anymore.

View File

@ -39,7 +39,10 @@ $.extend $,
return new FormData form return new FormData form
fd = new FormData() fd = new FormData()
for key, val of form for key, val of form
fd.append key, val if val if val instanceof Blob
fd.append key, val, val.name
else if val
fd.append key, val
fd fd
ajax: (url, callbacks, opts={}) -> ajax: (url, callbacks, opts={}) ->
{type, headers, upCallbacks, form} = opts {type, headers, upCallbacks, form} = opts

View File

@ -32,6 +32,8 @@ QR =
el: link el: link
order: 10 order: 10
if $.engine is 'webkit'
$.on d, 'paste', QR.paste
$.on d, 'dragover', QR.dragOver $.on d, 'dragover', QR.dragOver
$.on d, 'drop', QR.dropFile $.on d, 'drop', QR.dropFile
$.on d, 'dragstart dragend', QR.drag $.on d, 'dragstart dragend', QR.drag
@ -296,8 +298,19 @@ QR =
QR.open() QR.open()
QR.fileInput e.dataTransfer.files QR.fileInput e.dataTransfer.files
$.addClass QR.nodes.el, 'dump' $.addClass QR.nodes.el, 'dump'
paste: (e) ->
files = []
for item in e.clipboardData.items
if item.kind is 'file'
blob = item.getAsFile()
blob.name = 'file'
blob.name += '.' + blob.type.split('/')[1] if blob.type
files.push blob
return unless files.length
QR.open()
QR.fileInput files
fileInput: (files) -> fileInput: (files) ->
unless files instanceof FileList if files instanceof Event # file input
files = [@files...] files = [@files...]
QR.nodes.fileInput.value = null # Don't hold the files from being modified on windows QR.nodes.fileInput.value = null # Don't hold the files from being modified on windows
{length} = files {length} = files