From 5471f5b1bfabe41a53323946a5ed4097ede3d848 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 24 Apr 2020 11:23:16 +1000 Subject: [PATCH] adjust sort index on save --- rslib/src/notetype/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index b25b7df16..dabef8b14 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -143,6 +143,17 @@ impl NoteType { self.config.reqs = reqs; } + fn reposition_sort_idx(&mut self) { + let adjusted_idx = self.fields.iter().enumerate().find_map(|(idx, f)| { + if f.ord == Some(self.config.sort_field_idx) { + Some(idx) + } else { + None + } + }); + self.config.sort_field_idx = adjusted_idx.unwrap_or(0) as u32; + } + pub(crate) fn normalize_names(&mut self) { ensure_string_in_nfc(&mut self.name); for f in &mut self.fields { @@ -184,6 +195,7 @@ impl NoteType { self.normalize_names(); self.ensure_names_unique(); self.update_requirements(); + self.reposition_sort_idx(); // fixme: deal with duplicate note type names on update Ok(()) }