mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-27 21:09:41 +08:00
133 lines
4.0 KiB
JavaScript
133 lines
4.0 KiB
JavaScript
import treeUtils from "../services/tree_utils.js";
|
|
import utils from "../services/utils.js";
|
|
import ws from "../services/ws.js";
|
|
import infoService from "../services/info.js";
|
|
|
|
const $dialog = $("#export-dialog");
|
|
const $form = $("#export-form");
|
|
const $noteTitle = $dialog.find(".export-note-title");
|
|
const $subtreeFormats = $("#export-subtree-formats");
|
|
const $singleFormats = $("#export-single-formats");
|
|
const $subtreeType = $("#export-type-subtree");
|
|
const $singleType = $("#export-type-single");
|
|
const $exportButton = $("#export-button");
|
|
const $opmlVersions = $("#opml-versions");
|
|
|
|
let taskId = '';
|
|
let branchId = null;
|
|
|
|
export async function showDialog(node, defaultType) {
|
|
utils.closeActiveDialog();
|
|
|
|
// each opening of the dialog resets the taskId so we don't associate it with previous exports anymore
|
|
taskId = '';
|
|
$exportButton.removeAttr("disabled");
|
|
|
|
if (defaultType === 'subtree') {
|
|
$subtreeType.prop("checked", true).change();
|
|
|
|
// to show/hide OPML versions
|
|
$("input[name=export-subtree-format]:checked").change();
|
|
}
|
|
else if (defaultType === 'single') {
|
|
$singleType.prop("checked", true).change();
|
|
}
|
|
else {
|
|
throw new Error("Unrecognized type " + defaultType);
|
|
}
|
|
|
|
$("#opml-v2").prop("checked", true); // setting default
|
|
|
|
glob.activeDialog = $dialog;
|
|
|
|
$dialog.modal();
|
|
|
|
branchId = node.data.branchId;
|
|
|
|
const noteTitle = await treeUtils.getNoteTitle(node.data.noteId);
|
|
|
|
$noteTitle.html(noteTitle);
|
|
}
|
|
|
|
$form.submit(() => {
|
|
$dialog.modal('hide');
|
|
|
|
const exportType = $dialog.find("input[name='export-type']:checked").val();
|
|
|
|
if (!exportType) {
|
|
// this shouldn't happen as we always choose default export type
|
|
alert("Choose export type first please");
|
|
return;
|
|
}
|
|
|
|
const exportFormat = exportType === 'subtree'
|
|
? $("input[name=export-subtree-format]:checked").val()
|
|
: $("input[name=export-single-format]:checked").val();
|
|
|
|
const exportVersion = exportFormat === 'opml' ? $dialog.find("input[name='opml-version']:checked").val() : "1.0";
|
|
|
|
exportBranch(branchId, exportType, exportFormat, exportVersion);
|
|
|
|
return false;
|
|
});
|
|
|
|
function exportBranch(branchId, type, format, version) {
|
|
taskId = utils.randomString(10);
|
|
|
|
const url = utils.getHost() + `/api/notes/${branchId}/export/${type}/${format}/${version}/${taskId}`;
|
|
|
|
utils.download(url);
|
|
}
|
|
|
|
$('input[name=export-type]').change(function () {
|
|
if (this.value === 'subtree') {
|
|
if ($("input[name=export-subtree-format]:checked").length === 0) {
|
|
$("input[name=export-subtree-format]:first").prop("checked", true);
|
|
}
|
|
|
|
$subtreeFormats.slideDown();
|
|
$singleFormats.slideUp();
|
|
}
|
|
else {
|
|
if ($("input[name=export-single-format]:checked").length === 0) {
|
|
$("input[name=export-single-format]:first").prop("checked", true);
|
|
}
|
|
|
|
$subtreeFormats.slideUp();
|
|
$singleFormats.slideDown();
|
|
}
|
|
});
|
|
|
|
$('input[name=export-subtree-format]').change(function () {
|
|
if (this.value === 'opml') {
|
|
$opmlVersions.slideDown();
|
|
}
|
|
else {
|
|
$opmlVersions.slideUp();
|
|
}
|
|
});
|
|
|
|
function makeToast(id, message) {
|
|
return {
|
|
id: id,
|
|
title: "Export status",
|
|
message: message,
|
|
icon: "arrow-square-up-right"
|
|
};
|
|
}
|
|
|
|
ws.subscribeToMessages(async message => {
|
|
if (message.type === 'task-error' && message.taskType === 'export') {
|
|
infoService.closePersistent(message.taskId);
|
|
infoService.showError(message.message);
|
|
}
|
|
else if (message.type === 'task-progress-count' && message.taskType === 'export') {
|
|
infoService.showPersistent(makeToast(message.taskId, "Export in progress: " + message.progressCount));
|
|
}
|
|
else if (message.type === 'task-succeeded' && message.taskType === 'export') {
|
|
const toast = makeToast(message.taskId, "Import finished successfully.");
|
|
toast.closeAfter = 5000;
|
|
|
|
infoService.showPersistent(toast);
|
|
}
|
|
}); |