diff --git a/src/routes/api/branches.js b/src/routes/api/branches.js index ddc89bd25..b9263340b 100644 --- a/src/routes/api/branches.js +++ b/src/routes/api/branches.js @@ -68,19 +68,31 @@ function moveBranchBeforeNote(req) { return [200, validationResult]; } + const originalBeforeNotePosition = beforeBranch.notePosition; + // we don't change utcDateModified so other changes are prioritized in case of conflict // also we would have to sync all those modified branches otherwise hash checks would fail + sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0", - [beforeBranch.parentNoteId, beforeBranch.notePosition]); + [beforeBranch.parentNoteId, originalBeforeNotePosition]); + + // also need to update becca positions + const parentNote = becca.getNote(beforeBranch.parentNoteId); + + for (const childBranch of parentNote.getChildBranches()) { + if (childBranch.notePosition >= originalBeforeNotePosition) { + childBranch.notePosition += 10; + } + } entityChangesService.addNoteReorderingEntityChange(beforeBranch.parentNoteId); if (branchToMove.parentNoteId === beforeBranch.parentNoteId) { - branchToMove.notePosition = beforeBranch.notePosition; + branchToMove.notePosition = originalBeforeNotePosition; branchToMove.save(); } else { - const newBranch = branchToMove.createClone(beforeBranch.parentNoteId, beforeBranch.notePosition); + const newBranch = branchToMove.createClone(beforeBranch.parentNoteId, originalBeforeNotePosition); newBranch.save(); branchToMove.markAsDeleted(); @@ -101,14 +113,25 @@ function moveBranchAfterNote(req) { return [200, validationResult]; } + const originalAfterNotePosition = afterNote.notePosition; + // we don't change utcDateModified so other changes are prioritized in case of conflict // also we would have to sync all those modified branches otherwise hash checks would fail sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0", - [afterNote.parentNoteId, afterNote.notePosition]); + [afterNote.parentNoteId, originalAfterNotePosition]); + + // also need to update becca positions + const parentNote = becca.getNote(afterNote.parentNoteId); + + for (const childBranch of parentNote.getChildBranches()) { + if (childBranch.notePosition > originalAfterNotePosition) { + childBranch.notePosition += 10; + } + } entityChangesService.addNoteReorderingEntityChange(afterNote.parentNoteId); - const movedNotePosition = afterNote.notePosition + 10; + const movedNotePosition = originalAfterNotePosition + 10; if (branchToMove.parentNoteId === afterNote.parentNoteId) { branchToMove.notePosition = movedNotePosition;