Stackedit/js/mediaImporter.js

91 lines
3.6 KiB
JavaScript
Raw Normal View History

2013-06-02 00:38:23 +00:00
define([
"jquery",
"underscore",
2013-06-22 23:48:57 +00:00
"classes/Provider",
2013-06-02 00:38:23 +00:00
"core",
2013-07-16 23:54:56 +00:00
"extensionMgr",
2013-06-10 21:22:32 +00:00
"providers/gplusProvider"
2013-07-16 23:54:56 +00:00
], function($, _, Provider, core, extensionMgr) {
2013-06-02 00:38:23 +00:00
var mediaImporter = {};
// Create a map with providerId: providerModule
var providerMap = _.chain(arguments).map(function(argument) {
2013-06-22 23:48:57 +00:00
return argument instanceof Provider && [
2013-06-02 00:38:23 +00:00
argument.providerId,
argument
];
}).compact().object().value();
core.onReady(function() {
_.each(providerMap, function(provider) {
// Import image action links (if any)
$(".action-import-image-" + provider.providerId).click(function() {
// Take the insertLinkCallback from core module
var insertLinkCallback = core.insertLinkCallback;
// Unset it to be sure core module will not call it
core.insertLinkCallback = undefined;
provider.importImage(function(error, imageLink) {
if(error) {
insertLinkCallback(null);
return;
}
insertLinkCallback(imageLink || null);
});
});
});
2013-07-16 23:54:56 +00:00
function handleImgImport(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = (evt.dataTransfer || evt.target).files;
var file = _.first(files);
if(file.name.match(/.(jpe?g|png|gif|bmp)$/)) {
var reader = new FileReader();
reader.onload = (function(importedFile) {
return function(e) {
var content = new Uint8Array(e.target.result);
providerMap["gplus"].uploadImage(file.name, content, function(error, imageLink) {
if(error) {
return;
}
// Generate an insertLinkCallback by clicking the
// pagedown button but without showing the dialog
core.catchModal = true;
$("#wmd-image-button").click();
core.catchModal = false;
// Take the insertLinkCallback from core module
var insertLinkCallback = core.insertLinkCallback;
// Unset it to be sure core module will not call it
core.insertLinkCallback = undefined;
insertLinkCallback(imageLink || null);
});
// var title = importedFile.name;
// var dotPosition = title.lastIndexOf(".");
// title = dotPosition !== -1 ? title.substring(0,
// dotPosition) : title;
// var fileDesc = fileMgr.createFile(title, content);
// fileMgr.selectFile(fileDesc);
};
})(file);
var blob = file.slice(0, IMPORT_IMG_MAX_CONTENT_SIZE);
reader.readAsArrayBuffer(blob);
}
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
}
$('#wmd-input').each(function() {
this.addEventListener('dragover', handleDragOver, false);
this.addEventListener('drop', handleImgImport, false);
});
2013-06-02 00:38:23 +00:00
});
return mediaImporter;
});