trilium/src/public/javascripts/dialogs/import.js

149 lines
4.6 KiB
JavaScript
Raw Normal View History

2019-02-10 21:33:13 +08:00
import treeService from '../services/tree.js';
2019-02-11 02:36:03 +08:00
import utils from '../services/utils.js';
2019-02-10 21:33:13 +08:00
import treeUtils from "../services/tree_utils.js";
2019-02-10 22:33:56 +08:00
import server from "../services/server.js";
import infoService from "../services/info.js";
2019-02-10 23:36:25 +08:00
import messagingService from "../services/messaging.js";
2019-02-10 21:33:13 +08:00
const $dialog = $("#import-dialog");
const $form = $("#import-form");
2019-05-06 00:24:59 +08:00
const $noteTitle = $dialog.find(".import-note-title");
2019-02-10 22:33:56 +08:00
const $fileUploadInput = $("#import-file-upload-input");
2019-02-11 05:56:14 +08:00
const $importProgressCountWrapper = $("#import-progress-count-wrapper");
const $importProgressCount = $("#import-progress-count");
const $importButton = $("#import-button");
2019-02-24 19:24:28 +08:00
const $safeImportCheckbox = $("#safe-import-checkbox");
const $shrinkImagesCheckbox = $("#shrink-images-checkbox");
const $textImportedAsTextCheckbox = $("#text-imported-as-text-checkbox");
const $codeImportedAsCodeCheckbox = $("#code-imported-as-code-checkbox");
const $explodeArchivesCheckbox = $("#explode-archives-checkbox");
2019-02-10 21:33:13 +08:00
2019-02-11 02:36:03 +08:00
let importId;
2019-02-10 21:33:13 +08:00
async function showDialog() {
2019-02-11 02:36:03 +08:00
// each opening of the dialog resets the importId so we don't associate it with previous imports anymore
importId = '';
2019-02-11 05:56:14 +08:00
$importProgressCountWrapper.hide();
$importProgressCount.text('0');
$fileUploadInput.val('').change(); // to trigger Import button disabling listener below
2019-02-24 19:24:28 +08:00
2019-02-26 05:28:15 +08:00
$safeImportCheckbox.prop("checked", true);
$shrinkImagesCheckbox.prop("checked", true);
$textImportedAsTextCheckbox.prop("checked", true);
$codeImportedAsCodeCheckbox.prop("checked", true);
$explodeArchivesCheckbox.prop("checked", true);
2019-02-10 23:36:25 +08:00
2019-02-10 21:33:13 +08:00
glob.activeDialog = $dialog;
const currentNode = treeService.getActiveNode();
2019-02-10 21:33:13 +08:00
$noteTitle.text(await treeUtils.getNoteTitle(currentNode.data.noteId));
$dialog.modal();
}
$form.submit(() => {
const currentNode = treeService.getActiveNode();
2019-02-10 21:33:13 +08:00
// disabling so that import is not triggered again.
$importButton.attr("disabled", "disabled");
2019-02-10 22:33:56 +08:00
importIntoNote(currentNode.data.noteId);
2019-02-10 21:33:13 +08:00
return false;
});
async function importIntoNote(importNoteId) {
const files = Array.from($fileUploadInput[0].files); // shallow copy since we're resetting the upload button below
2019-02-10 22:33:56 +08:00
2019-02-11 02:36:03 +08:00
// we generate it here (and not on opening) for the case when you try to import multiple times from the same
// dialog (which shouldn't happen, but still ...)
importId = utils.randomString(10);
2019-02-26 05:28:15 +08:00
const options = {
safeImport: boolToString($safeImportCheckbox),
shrinkImages: boolToString($shrinkImagesCheckbox),
textImportedAsText: boolToString($textImportedAsTextCheckbox),
codeImportedAsCode: boolToString($codeImportedAsCodeCheckbox),
explodeArchives: boolToString($explodeArchivesCheckbox)
2019-02-26 05:28:15 +08:00
};
2019-02-24 19:24:28 +08:00
2019-02-26 05:28:15 +08:00
await uploadFiles(importNoteId, files, options);
$dialog.modal('hide');
}
async function uploadFiles(importNoteId, files, options) {
2019-05-23 03:59:14 +08:00
if (files.length === 0) {
return;
}
let noteId;
for (const file of files) {
const formData = new FormData();
formData.append('upload', file);
formData.append('importId', importId);
for (const key in options) {
formData.append(key, options[key]);
}
({noteId} = await $.ajax({
url: baseApiUrl + 'notes/' + importNoteId + '/import',
headers: server.getHeaders(),
data: formData,
dataType: 'json',
type: 'POST',
timeout: 60 * 60 * 1000,
contentType: false, // NEEDED, DON'T REMOVE THIS
processData: false, // NEEDED, DON'T REMOVE THIS
2019-02-26 05:28:15 +08:00
}));
}
infoService.showMessage("Import finished successfully.");
2019-04-01 01:13:17 +08:00
await treeService.reloadNote(importNoteId);
if (noteId) {
const node = await treeService.activateNote(noteId);
node.setExpanded(true);
}
}
2019-02-10 22:33:56 +08:00
2019-02-24 19:24:28 +08:00
function boolToString($el) {
return $el.is(":checked") ? "true" : "false";
}
messagingService.subscribeToMessages(async message => {
2019-02-11 02:53:57 +08:00
if (message.type === 'import-error') {
infoService.showError(message.message);
$dialog.modal('hide');
return;
}
2019-02-11 02:36:03 +08:00
if (!message.importId || message.importId !== importId) {
// incoming messages must correspond to this import instance
return;
}
2019-02-11 05:30:55 +08:00
if (message.type === 'import-progress-count') {
2019-02-11 05:56:14 +08:00
$importProgressCountWrapper.slideDown();
2019-02-10 22:33:56 +08:00
2019-02-11 05:56:14 +08:00
$importProgressCount.text(message.progressCount);
}
});
$fileUploadInput.change(() => {
if ($fileUploadInput.val()) {
$importButton.removeAttr("disabled");
}
else {
$importButton.attr("disabled", "disabled");
2019-02-10 23:36:25 +08:00
}
});
2019-02-10 21:33:13 +08:00
export default {
2019-02-26 05:28:15 +08:00
showDialog,
uploadFiles
2019-02-10 21:33:13 +08:00
}