Stackedit/res/extensions/dialogOpenHarddrive.js

108 lines
3.9 KiB
JavaScript
Raw Normal View History

2013-06-16 10:47:45 +00:00
define([
"jquery",
"underscore",
2013-06-16 18:29:54 +00:00
"utils",
2013-06-22 23:48:57 +00:00
"classes/Extension",
2013-06-16 10:47:45 +00:00
"toMarkdown",
"config",
2013-06-22 23:48:57 +00:00
], function($, _, utils, Extension, toMarkdown) {
2013-06-16 10:47:45 +00:00
2013-06-22 23:48:57 +00:00
var dialogOpenHarddrive = new Extension("dialogOpenHarddrive", 'Dialog "Open from"');
2013-06-16 10:47:45 +00:00
var fileMgr = undefined;
dialogOpenHarddrive.onFileMgrCreated = function(fileMgrParameter) {
fileMgr = fileMgrParameter;
};
2013-07-30 08:46:36 +00:00
var eventMgr = undefined;
dialogOpenHarddrive.onEventMgrCreated = function(eventMgrParameter) {
eventMgr = eventMgrParameter;
2013-06-16 10:47:45 +00:00
};
var contentWrapper = undefined;
var converter = undefined;
var htmlContentWrapper = function(content) {
return converter.makeMd(content);
};
function handleFileImport(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = (evt.dataTransfer || evt.target).files;
2013-08-11 00:52:05 +00:00
$(".modal-import-harddrive-markdown, .modal-import-harddrive-html").modal("hide");
2013-06-16 10:47:45 +00:00
_.each(files, function(file) {
2013-07-17 00:10:19 +00:00
if($(evt.target).is("#wmd-input") && file.name.match(/.(jpe?g|png|gif)$/)) {
return;
}
2013-06-16 10:47:45 +00:00
var reader = new FileReader();
reader.onload = (function(importedFile) {
return function(e) {
var content = e.target.result;
if(content.match(/\uFFFD/)) {
2013-07-30 08:46:36 +00:00
eventMgr.onError(importedFile.name + " is a binary file.");
2013-06-16 10:47:45 +00:00
return;
}
content = contentWrapper ? contentWrapper(content) : content;
if(content === undefined) {
2013-07-30 08:46:36 +00:00
eventMgr.onError(importedFile.name + " is not a valid HTML file.");
2013-06-16 10:47:45 +00:00
return;
}
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_FILE_MAX_CONTENT_SIZE);
reader.readAsText(blob);
});
}
function handleMarkdownImport(evt) {
contentWrapper = undefined;
handleFileImport(evt);
}
function handleHtmlImport(evt) {
contentWrapper = htmlContentWrapper;
handleFileImport(evt);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
}
dialogOpenHarddrive.onReady = function() {
// Create toMarkdown converter
converter = new toMarkdown.converter();
$("#input-file-import-harddrive-markdown").change(handleMarkdownImport);
$('#dropzone-import-harddrive-markdown, #wmd-input').each(function() {
2013-06-16 10:47:45 +00:00
this.addEventListener('dragover', handleDragOver, false);
this.addEventListener('drop', handleMarkdownImport, false);
});
$("#input-file-import-harddrive-html").change(handleHtmlImport);
$('#dropzone-import-harddrive-html').each(function() {
this.addEventListener('dragover', handleDragOver, false);
this.addEventListener('drop', handleHtmlImport, false);
});
2013-06-16 18:29:54 +00:00
$(".action-convert-html").click(function(e) {
var content = utils.getInputTextValue("#input-convert-html", e);
if(content === undefined) {
return;
}
content = converter.makeMd(content);
if(content === undefined) {
2013-07-30 08:46:36 +00:00
eventMgr.onError("Invalid HTML code.");
2013-06-16 18:29:54 +00:00
return;
}
var fileDesc = fileMgr.createFile(undefined, content);
fileMgr.selectFile(fileDesc);
});
2013-06-16 10:47:45 +00:00
};
return dialogOpenHarddrive;
});