Skip to content

Build your personal knowledge base with Trilium Notes

improved drag & drop

zadam5e353a5612c0
diff --git a/src/public/app/services/branches.js b/src/public/app/services/branches.js
index 7be5da5e2f..0ca1f532b9 100644
--- a/src/public/app/services/branches.js
+++ b/src/public/app/services/branches.js
@@ -45,7 +45,7 @@ async function moveAfterBranch(branchIdsToMove, afterBranchId) {
}
}
-async function moveToParentNote(branchIdsToMove, newParentNoteId) {
+async function moveToParentNote(branchIdsToMove, newParentBranchId) {
branchIdsToMove = filterRootNote(branchIdsToMove);
for (const branchIdToMove of branchIdsToMove) {
@@ -56,7 +56,7 @@ async function moveToParentNote(branchIdsToMove, newParentNoteId) {
continue;
}
- const resp = await server.put(`branches/${branchIdToMove}/move-to/${newParentNoteId}`);
+ const resp = await server.put(`branches/${branchIdToMove}/move-to/${newParentBranchId}`);
if (!resp.success) {
alert(resp.message);
@@ -225,4 +225,4 @@ export default {
moveNodeUpInHierarchy,
cloneNoteAfter,
cloneNoteTo
-};
\ 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 e7c8a6c52f..fd2f0232db 100644
--- a/src/public/app/widgets/note_tree.js
+++ b/src/public/app/widgets/note_tree.js
@@ -265,6 +265,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
const notes = this.getSelectedOrActiveNodes(node).map(node => ({
noteId: node.data.noteId,
+ branchId: node.data.branchId,
title: node.title
}));
@@ -304,17 +305,28 @@ export default class NoteTreeWidget extends TabAwareWidget {
});
}
else {
+ const jsonStr = dataTransfer.getData("text");
+ let notes = null;
+
+ try {
+ notes = JSON.parse(jsonStr);
+ }
+ catch (e) {
+ console.error(`Cannot parse ${jsonStr} into notes for drop`);
+ return;
+ }
+
// This function MUST be defined to enable dropping of items on the tree.
// data.hitMode is 'before', 'after', or 'over'.
- const selectedBranchIds = this.getSelectedOrActiveNodes().map(node => node.data.branchId);
+ const selectedBranchIds = notes.map(note => note.branchId);
if (data.hitMode === "before") {
branchService.moveBeforeBranch(selectedBranchIds, node.data.branchId);
} else if (data.hitMode === "after") {
branchService.moveAfterBranch(selectedBranchIds, node.data.branchId);
} else if (data.hitMode === "over") {
- branchService.moveToParentNote(selectedBranchIds, node.data.noteId);
+ branchService.moveToParentNote(selectedBranchIds, node.data.branchId);
} else {
throw new Error("Unknown hitMode=" + data.hitMode);
}
diff --git a/src/routes/api/branches.js b/src/routes/api/branches.js
index b0cfe0578c..ddb6051884 100644
--- a/src/routes/api/branches.js
+++ b/src/routes/api/branches.js
@@ -14,25 +14,33 @@ const TaskContext = require('../../services/task_context');
*/
async function moveBranchToParent(req) {
- const {branchId, parentNoteId} = req.params;
+ const {branchId, parentBranchId} = req.params;
+ const parentBranch = await repository.getBranch(parentBranchId);
const branchToMove = await repository.getBranch(branchId);
- if (branchToMove.parentNoteId === parentNoteId) {
+ if (!parentBranch || !branchToMove) {
+ return [400, `One or both branches ${branchId}, ${parentBranchId} have not been found`];
+ }
+
+ if (branchToMove.parentNoteId === parentBranch.noteId) {
return { success: true }; // no-op
}
- const validationResult = await treeService.validateParentChild(parentNoteId, branchToMove.noteId, branchId);
+ const validationResult = await treeService.validateParentChild(parentBranch.noteId, branchToMove.noteId, branchId);
if (!validationResult.success) {
return [200, validationResult];
}
- const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
+ const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentBranch.noteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 10;
- const newBranch = branchToMove.createClone(parentNoteId, newNotePos);
- newBranch.isExpanded = true;
+ // expanding so that the new placement of the branch is immediately visible
+ parentBranch.isExpanded = true;
+ await parentBranch.save();
+
+ const newBranch = branchToMove.createClone(parentBranch.noteId, newNotePos);
await newBranch.save();
branchToMove.isDeleted = true;
@@ -178,4 +186,4 @@ module.exports = {
setExpandedForSubtree,
deleteBranch,
setPrefix
-};
\ No newline at end of file
+};
diff --git a/src/routes/routes.js b/src/routes/routes.js
index 568ee97043..838824630d 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -123,7 +123,7 @@ function register(app) {
apiRoute(POST, '/api/tree/load', treeApiRoute.load);
apiRoute(PUT, '/api/branches/:branchId/set-prefix', branchesApiRoute.setPrefix);
- apiRoute(PUT, '/api/branches/:branchId/move-to/:parentNoteId', branchesApiRoute.moveBranchToParent);
+ apiRoute(PUT, '/api/branches/:branchId/move-to/:parentBranchId', branchesApiRoute.moveBranchToParent);
apiRoute(PUT, '/api/branches/:branchId/move-before/:beforeBranchId', branchesApiRoute.moveBranchBeforeNote);
apiRoute(PUT, '/api/branches/:branchId/move-after/:afterBranchId', branchesApiRoute.moveBranchAfterNote);
apiRoute(PUT, '/api/branches/:branchId/expanded/:expanded', branchesApiRoute.setExpanded);