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();
for (key in form) {
val = form[key];
if (val) {
if (val instanceof Blob) {
fd.append(key, val, val.name);
} else if (val) {
fd.append(key, val);
}
}
@ -5678,6 +5680,9 @@
el: link,
order: 10
});
if ($.engine === 'webkit') {
$.on(d, 'paste', QR.paste);
}
$.on(d, 'dragover', QR.dragOver);
$.on(d, 'drop', QR.dropFile);
$.on(d, 'dragstart dragend', QR.drag);
@ -5980,12 +5985,33 @@
QR.fileInput(e.dataTransfer.files);
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) {
var file, length, max, post, _i, _len, _ref, _ref1;
if (!(files instanceof FileList)) {
if (files instanceof Event) {
files = __slice.call(this.files);
QR.nodes.fileInput.value = null;
}
QR.nodes.fileInput.value = null;
length = files.length;
if (!length) {
return;

View File

@ -17,6 +17,7 @@ beta
QR changes:
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.
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.

View File

@ -39,7 +39,10 @@ $.extend $,
return new FormData form
fd = new FormData()
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
ajax: (url, callbacks, opts={}) ->
{type, headers, upCallbacks, form} = opts

View File

@ -32,6 +32,8 @@ QR =
el: link
order: 10
if $.engine is 'webkit'
$.on d, 'paste', QR.paste
$.on d, 'dragover', QR.dragOver
$.on d, 'drop', QR.dropFile
$.on d, 'dragstart dragend', QR.drag
@ -296,10 +298,21 @@ QR =
QR.open()
QR.fileInput e.dataTransfer.files
$.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) ->
unless files instanceof FileList
if files instanceof Event # file input
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
return unless length
max = QR.nodes.fileInput.max