From 5e353a5612c0ec4e15678004d63f95e49eb57e6a Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 30 May 2020 10:30:21 +0200 Subject: [PATCH] improved drag & drop --- src/public/app/services/branches.js | 6 +++--- src/public/app/widgets/note_tree.js | 16 ++++++++++++++-- src/routes/api/branches.js | 22 +++++++++++++++------- src/routes/routes.js | 2 +- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/public/app/services/branches.js b/src/public/app/services/branches.js index 7be5da5e2..0ca1f532b 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 e7c8a6c52..fd2f0232d 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 b0cfe0578..ddb605188 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 568ee9704..838824630 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);