Fix note inconsistencies

This commit is contained in:
Joscha 2025-02-15 12:48:55 +01:00
parent cc0d242424
commit 6ef9c27234
2 changed files with 17 additions and 17 deletions

View file

@ -14,8 +14,6 @@ variations or spellings are incorrect.
## Todos ## Todos
- TODO Fix wide notes adding horizontal scroll bar - 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 Rename "note not found" to "loading..."
- TODO Add history widget to top of screen - TODO Add history widget to top of screen
- TODO Show pinned message at bottom of screen - TODO Show pinned message at bottom of screen

View file

@ -55,7 +55,14 @@ impl Store {
self.curr_id += 1; 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::<HashSet<_>>();
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(); self.parents.clear();
for (id, info) in &self.notes { for (id, info) in &self.notes {
for child in &info.children { for child in &info.children {
@ -67,6 +74,8 @@ impl Store {
.or_default() += 1; .or_default() += 1;
} }
} }
self.tick();
} }
pub fn create(&mut self, text: String) -> NoteId { pub fn create(&mut self, text: String) -> NoteId {
@ -77,16 +86,14 @@ impl Store {
}; };
self.notes.insert(id, info); self.notes.insert(id, info);
self.update_parents(); self.make_consistent_and_tick();
self.tick();
id id
} }
pub fn delete(&mut self, id: NoteId) -> Option<NoteInfo> { pub fn delete(&mut self, id: NoteId) -> Option<NoteInfo> {
let info = self.notes.remove(&id)?; let info = self.notes.remove(&id)?;
self.update_parents(); self.make_consistent_and_tick();
self.tick();
Some(info) Some(info)
} }
@ -106,7 +113,7 @@ impl Store {
return None; return None;
} }
note.children = children; note.children = children;
self.tick(); self.make_consistent_and_tick();
Some(()) Some(())
} }
@ -165,8 +172,7 @@ impl Store {
let index = Self::resolve_child_position(&note.children, child_position); let index = Self::resolve_child_position(&note.children, child_position);
note.children.insert(index, child_id); note.children.insert(index, child_id);
self.update_parents(); self.make_consistent_and_tick();
self.tick();
Some(()) Some(())
} }
@ -183,8 +189,7 @@ impl Store {
let index = Self::resolve_child_iteration(&note.children, child_id, child_iteration)?; let index = Self::resolve_child_iteration(&note.children, child_id, child_iteration)?;
note.children.remove(index); note.children.remove(index);
self.update_parents(); self.make_consistent_and_tick();
self.tick();
Some(()) Some(())
} }
@ -224,15 +229,12 @@ impl Store {
.children .children
.insert(to_idx, child_id); .insert(to_idx, child_id);
self.update_parents(); self.make_consistent_and_tick();
self.tick();
Some(()) Some(())
} }
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.notes.clear(); self.notes.clear();
self.make_consistent_and_tick();
self.update_parents();
self.tick();
} }
} }