enable ctrl-V support

This commit is contained in:
mappu 2017-10-15 19:49:46 +13:00
parent a3fc9092e3
commit 2f4fe0a55f
3 changed files with 91 additions and 20 deletions

View File

@ -153,14 +153,8 @@ var contented = (function() {
var $f = $("<div>").html(widgetHtml); var $f = $("<div>").html(widgetHtml);
$f.find(".contented-extratext").text(extraText); $f.find(".contented-extratext").text(extraText);
var ourClose = function () { // Tab buttons
$f.remove(); // remove from dom
onClose(); // upstream close
};
$f.find(".contented-close").click(function () {
ourClose();
})
var hasSetupDrawingBoardYet = false; var hasSetupDrawingBoardYet = false;
var setType = function (type) { var setType = function (type) {
$f.find(".contented-upload-type").removeClass("contented-upload-type-active"); $f.find(".contented-upload-type").removeClass("contented-upload-type-active");
@ -169,6 +163,12 @@ var contented = (function() {
$f.find(".contented-upload-if").removeClass("contented-active"); $f.find(".contented-upload-if").removeClass("contented-active");
$f.find(".contented-if-" + type).addClass("contented-active"); $f.find(".contented-if-" + type).addClass("contented-active");
if (type == "drag") {
enablePasteHandler();
} else {
disablePasteHandler();
}
if (type == "drawing" && !hasSetupDrawingBoardYet) { if (type == "drawing" && !hasSetupDrawingBoardYet) {
setupDrawingBoard(); setupDrawingBoard();
hasSetupDrawingBoardYet = true; hasSetupDrawingBoardYet = true;
@ -179,12 +179,7 @@ var contented = (function() {
setType($(this).attr('data-upload-type')); setType($(this).attr('data-upload-type'));
}); });
if (!contented.supportsDrop()) { // Widget positioning
// switch default
setType('file');
}
//
var $element = $(element); var $element = $(element);
var offset = $element.offset(); var offset = $element.offset();
@ -201,6 +196,8 @@ var contented = (function() {
'max-height': $element.height() + "px" 'max-height': $element.height() + "px"
}); });
// Drag and drop support
$f.find('.contented').on('dragover dragenter', function (e) { $f.find('.contented').on('dragover dragenter', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@ -225,6 +222,8 @@ var contented = (function() {
handleUploadFrom($(".contented-file-selector")[0].files); handleUploadFrom($(".contented-file-selector")[0].files);
}); });
// Pastebin
$f.find('.contented-paste-upload').on('click', function(e) { $f.find('.contented-paste-upload').on('click', function(e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@ -232,7 +231,64 @@ var contented = (function() {
handleUploadFrom([blob]); handleUploadFrom([blob]);
}); });
// Ctrl+V uploads
var pasteHandler = function(e) {
e.preventDefault();
e.stopPropagation();
var items = (e.clipboardData || e.originalEvent.clipboardData).items;
var items_length = items.length;
var blobs = [];
var handled = 0;
var haveHandled = function() {
handled += 1;
if (handled == items_length) {
if (blobs.length > 0) {
handleUploadFrom( blobs );
} else {
// alert("Pasted 0 files");
}
}
};
for (var i = 0; i < items.length; ++i) {
var item = items[i];
var mimeType = item.type;
if (item.kind === 'file') {
blobs.push(item.getAsFile());
haveHandled();
} else if (item.kind === 'string') {
item.getAsString(function(s) {
blobs.push( new Blob([s], {type : mimeType}) );
haveHandled();
});
} else {
// file|string are the only supported types in
// all browsers at the time of writing
// Ignore future possibilities
haveHandled();
}
}
};
var enablePasteHandler = function() {
document.addEventListener('paste', pasteHandler);
};
var disablePasteHandler = function() {
document.removeEventListener('paste', pasteHandler);
};
// Embed in DOM, load default area
$("body").append($f); $("body").append($f);
if (!contented.supportsDrop()) {
setType('file');
} else {
setType('drag');
}
// Drawing board // Drawing board
@ -285,6 +341,17 @@ var contented = (function() {
}; };
// Close button
var ourClose = function () {
$f.remove(); // remove from dom
disablePasteHandler();
onClose(); // upstream close
};
$f.find(".contented-close").click(function () {
ourClose();
})
// Progress bar // Progress bar
var setProgressCaption = function(message) { var setProgressCaption = function(message) {
@ -294,7 +361,9 @@ var contented = (function() {
$f.find(".contented-progress-element").css('width', (frc * 100) + "%"); $f.find(".contented-progress-element").css('width', (frc * 100) + "%");
}; };
var handleUploadFrom = function (files) { // Common upload handler
var handleUploadFrom = function(files) {
setProgressCaption("Uploading, please wait..."); setProgressCaption("Uploading, please wait...");
setProgressPercentage(0); setProgressPercentage(0);
@ -346,6 +415,8 @@ var contented = (function() {
}); });
} }
// .
}); });

View File

@ -133,7 +133,7 @@
<div class="contented-content-area"> <div class="contented-content-area">
<div class="contented-upload-if contented-if-drag contented-active"> <div class="contented-upload-if contented-if-drag contented-active">
<label>Drop files to upload <span class="contented-extratext"></span></label> <label>Drop files or Ctrl-V to upload <span class="contented-extratext"></span></label>
</div> </div>
<div class="contented-upload-if contented-if-file"> <div class="contented-upload-if contented-if-file">

File diff suppressed because one or more lines are too long