Skip to content

Build your personal knowledge base with Trilium Notes

fix moving/cloning notes broken in 0.42.4, closes #1066

zadam8c88ce6f651c
diff --git a/src/public/app/dialogs/clone_to.js b/src/public/app/dialogs/clone_to.js
index eab144aac2..075f3569d2 100644
--- a/src/public/app/dialogs/clone_to.js
+++ b/src/public/app/dialogs/clone_to.js
@@ -39,13 +39,14 @@ export async function showDialog(noteIds) {
}
async function cloneNotesTo(notePath) {
- const targetNoteId = treeService.getNoteIdFromNotePath(notePath);
+ const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
+ const targetBranchId = await treeCache.getBranchId(parentNoteId, noteId);
for (const cloneNoteId of clonedNoteIds) {
- await branchService.cloneNoteTo(cloneNoteId, targetNoteId, $clonePrefix.val());
+ await branchService.cloneNoteTo(cloneNoteId, targetBranchId, $clonePrefix.val());
const clonedNote = await treeCache.getNote(cloneNoteId);
- const targetNote = await treeCache.getNote(targetNoteId);
+ const targetNote = await treeCache.getBranch(targetBranchId).getNote();
toastService.showMessage(`Note "${clonedNote.title}" has been cloned into ${targetNote.title}`);
}
@@ -64,4 +65,4 @@ $form.on('submit', () => {
}
return false;
-});
\ No newline at end of file
+});
diff --git a/src/public/app/dialogs/move_to.js b/src/public/app/dialogs/move_to.js
index 4dbc6bcca5..16bff26291 100644
--- a/src/public/app/dialogs/move_to.js
+++ b/src/public/app/dialogs/move_to.js
@@ -32,10 +32,11 @@ export async function showDialog(branchIds) {
noteAutocompleteService.showRecentNotes($noteAutoComplete);
}
-async function moveNotesTo(parentNoteId) {
- await branchService.moveToParentNote(movedBranchIds, parentNoteId);
+async function moveNotesTo(parentBranchId) {
+ await branchService.moveToParentNote(movedBranchIds, parentBranchId);
- const parentNote = await treeCache.getNote(parentNoteId);
+ const parentBranch = treeCache.getBranch(parentBranchId);
+ const parentNote = await parentBranch.getNote();
toastService.showMessage(`Selected notes have been moved into ${parentNote.title}`);
}
@@ -46,13 +47,12 @@ $form.on('submit', () => {
if (notePath) {
$dialog.modal('hide');
- const noteId = treeService.getNoteIdFromNotePath(notePath);
-
- moveNotesTo(noteId);
+ const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
+ treeCache.getBranchId(parentNoteId, noteId).then(branchId => moveNotesTo(branchId));
}
else {
console.error("No path to move to.");
}
return false;
-});
\ No newline at end of file
+});
diff --git a/src/public/app/services/branches.js b/src/public/app/services/branches.js
index 0ca1f532b9..d64e6d1848 100644
--- a/src/public/app/services/branches.js
+++ b/src/public/app/services/branches.js
@@ -198,8 +198,8 @@ ws.subscribeToMessages(async message => {
}
});
-async function cloneNoteTo(childNoteId, parentNoteId, prefix) {
- const resp = await server.put('notes/' + childNoteId + '/clone-to/' + parentNoteId, {
+async function cloneNoteTo(childNoteId, parentBranchId, prefix) {
+ const resp = await server.put(`notes/${childNoteId}/clone-to/${parentBranchId}`, {
prefix: prefix
});
diff --git a/src/public/app/services/clipboard.js b/src/public/app/services/clipboard.js
index 3fc536b2ad..56c45ebea0 100644
--- a/src/public/app/services/clipboard.js
+++ b/src/public/app/services/clipboard.js
@@ -33,13 +33,13 @@ async function pasteAfter(afterBranchId) {
}
}
-async function pasteInto(parentNoteId) {
+async function pasteInto(parentBranchId) {
if (isClipboardEmpty()) {
return;
}
if (clipboardMode === 'cut') {
- await branchService.moveToParentNote(clipboardBranchIds, parentNoteId);
+ await branchService.moveToParentNote(clipboardBranchIds, parentBranchId);
clipboardBranchIds = [];
clipboardMode = null;
@@ -50,7 +50,7 @@ async function pasteInto(parentNoteId) {
for (const clipboardBranch of clipboardBranches) {
const clipboardNote = await clipboardBranch.getNote();
- await branchService.cloneNoteTo(clipboardNote.noteId, parentNoteId);
+ await branchService.cloneNoteTo(clipboardNote.noteId, parentBranchId);
}
// copy will keep clipboardBranchIds and clipboardMode so it's possible to paste into multiple places
@@ -89,4 +89,4 @@ export default {
cut,
copy,
isClipboardEmpty
-}
\ No newline at end of file
+}
diff --git a/src/public/app/widgets/note_tree.js b/src/public/app/widgets/note_tree.js
index 0a413fb0ac..c60b3981e7 100644
--- a/src/public/app/widgets/note_tree.js
+++ b/src/public/app/widgets/note_tree.js
@@ -746,6 +746,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
node.icon = this.getIcon(note, isFolder);
node.extraClasses = this.getExtraClasses(note);
node.title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title;
+ node.setExpanded(branch.isExpanded, {noEvents:true});
node.renderTitle();
}
@@ -1079,7 +1080,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
const toNode = node.getPrevSibling();
if (toNode !== null) {
- branchService.moveToParentNote([node.data.branchId], toNode.data.noteId);
+ branchService.moveToParentNote([node.data.branchId], toNode.data.branchId);
}
}
@@ -1164,7 +1165,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
}
pasteNotesFromClipboardCommand({node}) {
- clipboard.pasteInto(node.data.noteId);
+ clipboard.pasteInto(node.data.branchId);
}
pasteNotesAfterFromClipboard({node}) {
diff --git a/src/routes/api/cloning.js b/src/routes/api/cloning.js
index 9cd23fced0..42a208568a 100644
--- a/src/routes/api/cloning.js
+++ b/src/routes/api/cloning.js
@@ -3,10 +3,10 @@
const cloningService = require('../../services/cloning');
async function cloneNoteToParent(req) {
- const {noteId, parentNoteId} = req.params;
+ const {noteId, parentBranchId} = req.params;
const {prefix} = req.body;
- return await cloningService.cloneNoteToParent(noteId, parentNoteId, prefix);
+ return await cloningService.cloneNoteToParent(noteId, parentBranchId, prefix);
}
async function cloneNoteAfter(req) {
@@ -18,4 +18,4 @@ async function cloneNoteAfter(req) {
module.exports = {
cloneNoteToParent,
cloneNoteAfter
-};
\ No newline at end of file
+};
diff --git a/src/routes/routes.js b/src/routes/routes.js
index 838824630d..67cffc539a 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -152,7 +152,7 @@ function register(app) {
apiRoute(GET, '/api/edited-notes/:date', noteRevisionsApiRoute.getEditedNotesOnDate);
- apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentNoteId', cloningApiRoute.cloneNoteToParent);
+ apiRoute(PUT, '/api/notes/:noteId/clone-to/:parentBranchId', cloningApiRoute.cloneNoteToParent);
apiRoute(PUT, '/api/notes/:noteId/clone-after/:afterBranchId', cloningApiRoute.cloneNoteAfter);
route(GET, '/api/notes/:branchId/export/:type/:format/:version/:taskId', [auth.checkApiAuthOrElectron], exportRoute.exportBranch);
diff --git a/src/services/cloning.js b/src/services/cloning.js
index 206d428436..33b5476753 100644
--- a/src/services/cloning.js
+++ b/src/services/cloning.js
@@ -9,12 +9,14 @@ const Branch = require('../entities/branch');
const TaskContext = require("./task_context.js");
const utils = require('./utils');
-async function cloneNoteToParent(noteId, parentNoteId, prefix) {
- if (await isNoteDeleted(noteId) || await isNoteDeleted(parentNoteId)) {
+async function cloneNoteToParent(noteId, parentBranchId, prefix) {
+ const parentBranch = await repository.getBranch(parentBranchId);
+
+ if (await isNoteDeleted(noteId) || await isNoteDeleted(parentBranch.noteId)) {
return { success: false, message: 'Note is deleted.' };
}
- const validationResult = await treeService.validateParentChild(parentNoteId, noteId);
+ const validationResult = await treeService.validateParentChild(parentBranch.noteId, noteId);
if (!validationResult.success) {
return validationResult;
@@ -22,12 +24,13 @@ async function cloneNoteToParent(noteId, parentNoteId, prefix) {
const branch = await new Branch({
noteId: noteId,
- parentNoteId: parentNoteId,
+ parentNoteId: parentBranch.noteId,
prefix: prefix,
isExpanded: 0
}).save();
- await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
+ parentBranch.isExpanded = true; // the new target should be expanded so it immediately shows up to the user
+ await parentBranch.save();
return { success: true, branchId: branch.branchId };
}
@@ -111,4 +114,4 @@ module.exports = {
ensureNoteIsAbsentFromParent,
toggleNoteInParent,
cloneNoteAfter
-};
\ No newline at end of file
+};