mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 01:06:35 -04:00
handle changed sort field index
This commit is contained in:
parent
ea8e0ef6a2
commit
d6706e1f0e
4 changed files with 28 additions and 10 deletions
|
@ -404,7 +404,7 @@ where
|
||||||
&self.mgr.media_folder,
|
&self.mgr.media_folder,
|
||||||
)? {
|
)? {
|
||||||
// note was modified, needs saving
|
// note was modified, needs saving
|
||||||
note.prepare_for_update(nt, usn)?;
|
note.prepare_for_update(nt, Some(usn))?;
|
||||||
self.ctx.storage.update_note(¬e)?;
|
self.ctx.storage.update_note(¬e)?;
|
||||||
collection_modified = true;
|
collection_modified = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,8 @@ impl Note {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prepare_for_update(&mut self, nt: &NoteType, usn: Usn) -> Result<()> {
|
/// Prepare note for saving to the database. If usn is provided, mtime will be bumped.
|
||||||
|
pub fn prepare_for_update(&mut self, nt: &NoteType, usn: Option<Usn>) -> Result<()> {
|
||||||
assert!(nt.id == self.ntid);
|
assert!(nt.id == self.ntid);
|
||||||
if nt.fields.len() != self.fields.len() {
|
if nt.fields.len() != self.fields.len() {
|
||||||
return Err(AnkiError::invalid_input(format!(
|
return Err(AnkiError::invalid_input(format!(
|
||||||
|
@ -87,8 +88,10 @@ impl Note {
|
||||||
};
|
};
|
||||||
self.sort_field = Some(sort_field.into());
|
self.sort_field = Some(sort_field.into());
|
||||||
self.checksum = Some(checksum);
|
self.checksum = Some(checksum);
|
||||||
|
if let Some(usn) = usn {
|
||||||
self.mtime = TimestampSecs::now();
|
self.mtime = TimestampSecs::now();
|
||||||
self.usn = usn;
|
self.usn = usn;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +193,7 @@ impl Collection {
|
||||||
did: DeckID,
|
did: DeckID,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.canonify_note_tags(note, ctx.usn)?;
|
self.canonify_note_tags(note, ctx.usn)?;
|
||||||
note.prepare_for_update(&ctx.notetype, ctx.usn)?;
|
note.prepare_for_update(&ctx.notetype, Some(ctx.usn))?;
|
||||||
self.storage.add_note(note)?;
|
self.storage.add_note(note)?;
|
||||||
self.generate_cards_for_new_note(ctx, note, did)
|
self.generate_cards_for_new_note(ctx, note, did)
|
||||||
}
|
}
|
||||||
|
@ -211,7 +214,7 @@ impl Collection {
|
||||||
note: &mut Note,
|
note: &mut Note,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.canonify_note_tags(note, ctx.usn)?;
|
self.canonify_note_tags(note, ctx.usn)?;
|
||||||
note.prepare_for_update(ctx.notetype, ctx.usn)?;
|
note.prepare_for_update(ctx.notetype, Some(ctx.usn))?;
|
||||||
self.generate_cards_for_existing_note(ctx, note)?;
|
self.generate_cards_for_existing_note(ctx, note)?;
|
||||||
self.storage.update_note(note)?;
|
self.storage.update_note(note)?;
|
||||||
|
|
||||||
|
|
|
@ -328,7 +328,11 @@ impl Collection {
|
||||||
}
|
}
|
||||||
self.transact(None, |col| {
|
self.transact(None, |col| {
|
||||||
if let Some(existing_notetype) = existing {
|
if let Some(existing_notetype) = existing {
|
||||||
col.update_notes_for_changed_fields(nt, existing_notetype.fields.len())?;
|
col.update_notes_for_changed_fields(
|
||||||
|
nt,
|
||||||
|
existing_notetype.fields.len(),
|
||||||
|
existing_notetype.config.sort_field_idx,
|
||||||
|
)?;
|
||||||
col.update_cards_for_changed_templates(nt, existing_notetype.templates.len())?;
|
col.update_cards_for_changed_templates(nt, existing_notetype.templates.len())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,12 +54,23 @@ impl Collection {
|
||||||
&mut self,
|
&mut self,
|
||||||
nt: &NoteType,
|
nt: &NoteType,
|
||||||
previous_field_count: usize,
|
previous_field_count: usize,
|
||||||
|
previous_sort_idx: u32,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let ords: Vec<_> = nt.fields.iter().map(|f| f.ord).collect();
|
let ords: Vec<_> = nt.fields.iter().map(|f| f.ord).collect();
|
||||||
if !ords_changed(&ords, previous_field_count) {
|
if !ords_changed(&ords, previous_field_count) {
|
||||||
|
if nt.config.sort_field_idx == previous_sort_idx {
|
||||||
|
// only need to update sort field
|
||||||
|
let nids = self.search_notes_only(&format!("mid:{}", nt.id))?;
|
||||||
|
for nid in nids {
|
||||||
|
let mut note = self.storage.get_note(nid)?.unwrap();
|
||||||
|
note.prepare_for_update(nt, None)?;
|
||||||
|
self.storage.update_note(¬e)?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// nothing to do
|
// nothing to do
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.storage.set_schema_modified()?;
|
self.storage.set_schema_modified()?;
|
||||||
|
|
||||||
|
@ -81,7 +92,7 @@ impl Collection {
|
||||||
})
|
})
|
||||||
.map(Into::into)
|
.map(Into::into)
|
||||||
.collect();
|
.collect();
|
||||||
note.prepare_for_update(nt, usn)?;
|
note.prepare_for_update(nt, Some(usn))?;
|
||||||
self.storage.update_note(¬e)?;
|
self.storage.update_note(¬e)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue