From 6ef9c27234d3b505fbe9be961e60fbbb454917b3 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 15 Feb 2025 12:48:55 +0100 Subject: [PATCH] Fix note inconsistencies --- README.md | 2 -- gdn-app/src-tauri/src/store.rs | 32 +++++++++++++++++--------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index cd403fd..366f390 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,6 @@ variations or spellings are incorrect. ## Todos - TODO Fix wide notes adding horizontal scroll bar -- TODO Fix deleting note not unlinking it (and maybe other child inconsistencies - as well) - TODO Rename "note not found" to "loading..." - TODO Add history widget to top of screen - TODO Show pinned message at bottom of screen diff --git a/gdn-app/src-tauri/src/store.rs b/gdn-app/src-tauri/src/store.rs index 6b1452b..2bac8e6 100644 --- a/gdn-app/src-tauri/src/store.rs +++ b/gdn-app/src-tauri/src/store.rs @@ -55,7 +55,14 @@ impl Store { self.curr_id += 1; } - fn update_parents(&mut self) { + fn make_consistent_and_tick(&mut self) { + // Remove child notes that don't exist + let children = self.notes.keys().copied().collect::>(); + for info in &mut self.notes.values_mut() { + info.children.retain(|child| children.contains(child)); + } + + // Update parents to match new child notes self.parents.clear(); for (id, info) in &self.notes { for child in &info.children { @@ -67,6 +74,8 @@ impl Store { .or_default() += 1; } } + + self.tick(); } pub fn create(&mut self, text: String) -> NoteId { @@ -77,16 +86,14 @@ impl Store { }; self.notes.insert(id, info); - self.update_parents(); - self.tick(); + self.make_consistent_and_tick(); id } pub fn delete(&mut self, id: NoteId) -> Option { let info = self.notes.remove(&id)?; - self.update_parents(); - self.tick(); + self.make_consistent_and_tick(); Some(info) } @@ -106,7 +113,7 @@ impl Store { return None; } note.children = children; - self.tick(); + self.make_consistent_and_tick(); Some(()) } @@ -165,8 +172,7 @@ impl Store { let index = Self::resolve_child_position(¬e.children, child_position); note.children.insert(index, child_id); - self.update_parents(); - self.tick(); + self.make_consistent_and_tick(); Some(()) } @@ -183,8 +189,7 @@ impl Store { let index = Self::resolve_child_iteration(¬e.children, child_id, child_iteration)?; note.children.remove(index); - self.update_parents(); - self.tick(); + self.make_consistent_and_tick(); Some(()) } @@ -224,15 +229,12 @@ impl Store { .children .insert(to_idx, child_id); - self.update_parents(); - self.tick(); + self.make_consistent_and_tick(); Some(()) } pub fn clear(&mut self) { self.notes.clear(); - - self.update_parents(); - self.tick(); + self.make_consistent_and_tick(); } }