From 14bb068626e987ded8a118f705533eb0e3e35ee4 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 11 Apr 2026 19:36:50 +0300 Subject: [PATCH] fix(tree): deleting ancestor of the current note doesn't correctly navigate --- apps/client/src/services/branches.ts | 27 +++++++++++++++++++++------ apps/client/src/widgets/note_tree.ts | 9 +-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/apps/client/src/services/branches.ts b/apps/client/src/services/branches.ts index 8f31060242..15d24a10d7 100644 --- a/apps/client/src/services/branches.ts +++ b/apps/client/src/services/branches.ts @@ -120,7 +120,7 @@ async function deleteNotes(branchIdsToDelete: string[], forceDeleteAllClones = f if (moveToParent) { try { - await activateParentNotePath(); + await activateParentNotePath(branchIdsToDelete); } catch (e) { console.error(e); } @@ -152,13 +152,28 @@ async function deleteNotes(branchIdsToDelete: string[], forceDeleteAllClones = f return true; } -async function activateParentNotePath() { - // this is not perfect, maybe we should find the next/previous sibling, but that's more complex +async function activateParentNotePath(branchIdsToDelete: string[]) { const activeContext = appContext.tabManager.getActiveContext(); - const parentNotePathArr = activeContext?.notePathArray.slice(0, -1); + const activeNotePath = activeContext?.notePathArray ?? []; - if (parentNotePathArr && parentNotePathArr.length > 0) { - activeContext?.setNote(parentNotePathArr.join("/")); + // Find the deleted branch that appears earliest in the active note's path + let earliestIndex = activeNotePath.length; + for (const branchId of branchIdsToDelete) { + const branch = froca.getBranch(branchId); + if (branch) { + const index = activeNotePath.indexOf(branch.noteId); + if (index !== -1 && index < earliestIndex) { + earliestIndex = index; + } + } + } + + // Navigate to the parent of the highest deleted ancestor + if (earliestIndex < activeNotePath.length) { + const parentPath = activeNotePath.slice(0, earliestIndex); + if (parentPath.length > 0) { + await activeContext?.setNote(parentPath.join("/")); + } } } diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts index 2617be3e01..5c2aadb1ea 100644 --- a/apps/client/src/widgets/note_tree.ts +++ b/apps/client/src/widgets/note_tree.ts @@ -1614,14 +1614,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget { return; } - // Only navigate to parent if deleting the currently active note - const activeNoteId = appContext.tabManager.getActiveContext()?.noteId; - const isDeletingActiveNote = branchIds.some((branchId) => { - const branch = froca.getBranch(branchId); - return branch?.noteId === activeNoteId; - }); - - await branchService.deleteNotes(branchIds, false, isDeletingActiveNote); + await branchService.deleteNotes(branchIds); this.clearSelectedNodes(); }