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"');
|
|
|
|
dialogOpenHarddrive.settingsBlock = '<p>Handles the "Import from hard drive" and the "Convert HTML to Markdown" dialog boxes.</p>';
|
2013-06-16 10:47:45 +00:00
|
|
|
|
|
|
|
var fileMgr = undefined;
|
|
|
|
dialogOpenHarddrive.onFileMgrCreated = function(fileMgrParameter) {
|
|
|
|
fileMgr = fileMgrParameter;
|
|
|
|
};
|
|
|
|
|
|
|
|
var extensionMgr = undefined;
|
|
|
|
dialogOpenHarddrive.onExtensionMgrCreated = function(extensionMgrParameter) {
|
|
|
|
extensionMgr = extensionMgrParameter;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
$("#modal-import-harddrive-markdown, #modal-import-harddrive-html").modal("hide");
|
|
|
|
_.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/)) {
|
|
|
|
extensionMgr.onError(importedFile.name + " is a binary file.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
content = contentWrapper ? contentWrapper(content) : content;
|
|
|
|
if(content === undefined) {
|
|
|
|
extensionMgr.onError(importedFile.name + " is not a valid HTML file.");
|
|
|
|
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);
|
2013-07-10 22:43:14 +00:00
|
|
|
$('#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-06-16 18:32:02 +00:00
|
|
|
extensionMgr.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;
|
|
|
|
|
|
|
|
});
|