diff --git a/src/public/javascripts/desktop.js b/src/public/javascripts/desktop.js index 659245c07..12df3f593 100644 --- a/src/public/javascripts/desktop.js +++ b/src/public/javascripts/desktop.js @@ -12,7 +12,6 @@ import exportDialog from './dialogs/export.js'; import cloning from './services/cloning.js'; import contextMenu from './services/tree_context_menu.js'; import dragAndDropSetup from './services/drag_and_drop.js'; -import exportService from './services/export.js'; import link from './services/link.js'; import messagingService from './services/messaging.js'; import noteDetailService from './services/note_detail.js'; diff --git a/src/public/javascripts/dialogs/export.js b/src/public/javascripts/dialogs/export.js index 7b6008196..6f15cee85 100644 --- a/src/public/javascripts/dialogs/export.js +++ b/src/public/javascripts/dialogs/export.js @@ -1,6 +1,7 @@ import treeService from '../services/tree.js'; import treeUtils from "../services/tree_utils.js"; -import exportService from "../services/export.js"; +import utils from "../services/utils.js"; +import protectedSessionHolder from "../services/protected_session_holder.js"; const $dialog = $("#export-dialog"); const $form = $("#export-form"); @@ -46,13 +47,19 @@ $form.submit(() => { const currentNode = treeService.getCurrentNode(); - exportService.exportBranch(currentNode.data.branchId, exportType, exportFormat); + exportBranch(currentNode.data.branchId, exportType, exportFormat); $dialog.modal('hide'); return false; }); +function exportBranch(branchId, type, format) { + const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}?protectedSessionId=` + encodeURIComponent(protectedSessionHolder.getProtectedSessionId()); + + utils.download(url); +} + $('input[name=export-type]').change(function () { if (this.value === 'subtree') { if ($("input[name=export-subtree-format]:checked").length === 0) { diff --git a/src/public/javascripts/dialogs/import.js b/src/public/javascripts/dialogs/import.js index 4af89849b..67c062fc2 100644 --- a/src/public/javascripts/dialogs/import.js +++ b/src/public/javascripts/dialogs/import.js @@ -1,10 +1,12 @@ import treeService from '../services/tree.js'; -import exportService from "../services/export.js"; import treeUtils from "../services/tree_utils.js"; +import server from "../services/server.js"; +import infoService from "../services/info.js"; const $dialog = $("#import-dialog"); const $form = $("#import-form"); const $noteTitle = $dialog.find(".note-title"); +const $fileUploadInput = $("#import-file-upload-input"); async function showDialog() { glob.activeDialog = $dialog; @@ -18,13 +20,44 @@ async function showDialog() { $form.submit(() => { const currentNode = treeService.getCurrentNode(); - exportService.importIntoNote(currentNode.data.noteId).then(() => { - $dialog.modal('hide'); - }); + importIntoNote(currentNode.data.noteId); return false; }); +function importIntoNote(importNoteId) { + const formData = new FormData(); + formData.append('upload', $fileUploadInput[0].files[0]); + + // this is done to reset the field otherwise triggering import same file again would not work + // https://github.com/zadam/trilium/issues/388 + $fileUploadInput.val(''); + + $.ajax({ + url: baseApiUrl + 'notes/' + importNoteId + '/import', + headers: server.getHeaders(), + data: formData, + dataType: 'json', + type: 'POST', + contentType: false, // NEEDED, DON'T REMOVE THIS + processData: false, // NEEDED, DON'T REMOVE THIS + }) + .fail((xhr, status, error) => alert('Import error: ' + xhr.responseText)) + .done(async note => { + $dialog.modal('hide'); + + infoService.showMessage("Import finished successfully.") + + await treeService.reload(); + + if (note) { + const node = await treeService.activateNote(note.noteId); + + node.setExpanded(true); + } + }); +} + export default { showDialog } \ No newline at end of file diff --git a/src/public/javascripts/services/export.js b/src/public/javascripts/services/export.js deleted file mode 100644 index 578028fdd..000000000 --- a/src/public/javascripts/services/export.js +++ /dev/null @@ -1,46 +0,0 @@ -import treeService from './tree.js'; -import protectedSessionHolder from './protected_session_holder.js'; -import utils from './utils.js'; -import server from './server.js'; - -const $fileUploadInput = $("#import-file-upload-input"); - -function exportBranch(branchId, type, format) { - const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}?protectedSessionId=` + encodeURIComponent(protectedSessionHolder.getProtectedSessionId()); - - utils.download(url); -} - -async function importIntoNote(importNoteId) { - const formData = new FormData(); - formData.append('upload', $fileUploadInput[0].files[0]); - - // this is done to reset the field otherwise triggering import same file again would not work - // https://github.com/zadam/trilium/issues/388 - $fileUploadInput.val(''); - - await $.ajax({ - url: baseApiUrl + 'notes/' + importNoteId + '/import', - headers: server.getHeaders(), - data: formData, - dataType: 'json', - type: 'POST', - contentType: false, // NEEDED, DON'T REMOVE THIS - processData: false, // NEEDED, DON'T REMOVE THIS - }) - .fail((xhr, status, error) => alert('Import error: ' + xhr.responseText)) - .done(async note => { - await treeService.reload(); - - if (note) { - const node = await treeService.activateNote(note.noteId); - - node.setExpanded(true); - } - }); -} - -export default { - exportBranch, - importIntoNote -}; \ No newline at end of file