trilium/src/public/app/dialogs/clone_to.js
zadam 915b1d1a45 Merge remote-tracking branch 'origin/stable'
# Conflicts:
#	libraries/ckeditor/ckeditor.js
#	libraries/ckeditor/ckeditor.js.map
#	package-lock.json
#	package.json
#	src/public/app/widgets/type_widgets/editable_text.js
2020-06-03 16:25:45 +02:00

69 lines
2.0 KiB
JavaScript

import noteAutocompleteService from "../services/note_autocomplete.js";
import utils from "../services/utils.js";
import treeService from "../services/tree.js";
import toastService from "../services/toast.js";
import treeCache from "../services/tree_cache.js";
import branchService from "../services/branches.js";
const $dialog = $("#clone-to-dialog");
const $form = $("#clone-to-form");
const $noteAutoComplete = $("#clone-to-note-autocomplete");
const $clonePrefix = $("#clone-prefix");
const $noteList = $("#clone-to-note-list");
let clonedNoteIds;
export async function showDialog(noteIds) {
clonedNoteIds = [];
for (const noteId of noteIds) {
if (!clonedNoteIds.includes(noteId)) {
clonedNoteIds.push(noteId);
}
}
utils.openDialog($dialog);
$noteAutoComplete.val('').trigger('focus');
$noteList.empty();
for (const noteId of clonedNoteIds) {
const note = await treeCache.getNote(noteId);
$noteList.append($("<li>").text(note.title));
}
noteAutocompleteService.initNoteAutocomplete($noteAutoComplete);
noteAutocompleteService.showRecentNotes($noteAutoComplete);
}
async function cloneNotesTo(notePath) {
const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
const targetBranchId = await treeCache.getBranchId(parentNoteId, noteId);
for (const cloneNoteId of clonedNoteIds) {
await branchService.cloneNoteTo(cloneNoteId, targetBranchId, $clonePrefix.val());
const clonedNote = await treeCache.getNote(cloneNoteId);
const targetNote = await treeCache.getBranch(targetBranchId).getNote();
toastService.showMessage(`Note "${clonedNote.title}" has been cloned into ${targetNote.title}`);
}
}
$form.on('submit', () => {
const notePath = $noteAutoComplete.getSelectedNotePath();
if (notePath) {
$dialog.modal('hide');
cloneNotesTo(notePath);
}
else {
console.error("No path to clone to.");
}
return false;
});