Fix maybe_coalesce_note_undo_entry() (#2992)

* Fix maybe_coalesce_note_undo_entry()

* Use .count()

* Only check .first()

* Explicitly match on [note change, collection modification] (dae)
This commit is contained in:
Abdo 2024-02-11 09:04:10 +03:00 committed by GitHub
parent 3110e2d235
commit db02c95eb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 18 deletions

View file

@ -2,6 +2,7 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use super::NoteTags; use super::NoteTags;
use crate::collection::undo::UndoableCollectionChange;
use crate::prelude::*; use crate::prelude::*;
use crate::undo::UndoableChange; use crate::undo::UndoableChange;
@ -64,22 +65,22 @@ impl Collection {
if changes.op != Op::UpdateNote { if changes.op != Op::UpdateNote {
return; return;
} }
let Some(previous_op) = self.previous_undo_op() else {
if let Some(previous_op) = self.previous_undo_op() { return;
};
if previous_op.kind != Op::UpdateNote { if previous_op.kind != Op::UpdateNote {
return; return;
} }
let Some(current_op) = self.current_undo_op() else {
return;
};
if let ( if let (
Some(UndoableChange::Note(UndoableNoteChange::Updated(previous))), [UndoableChange::Note(UndoableNoteChange::Updated(previous)), UndoableChange::Collection(UndoableCollectionChange::Modified(_))],
Some(UndoableChange::Note(UndoableNoteChange::Updated(current))), [UndoableChange::Note(UndoableNoteChange::Updated(current)), UndoableChange::Collection(UndoableCollectionChange::Modified(_))],
) = ( ) = (&previous_op.changes[..], &current_op.changes[..])
previous_op.changes.last(), {
self.current_undo_op().and_then(|op| op.changes.last()),
) {
if previous.id == current.id && previous_op.timestamp.elapsed_secs() < 60 { if previous.id == current.id && previous_op.timestamp.elapsed_secs() < 60 {
self.pop_last_change(); self.clear_last_op();
}
} }
} }
} }

View file

@ -266,14 +266,14 @@ impl Collection {
} }
/// Used for coalescing successive note updates. /// Used for coalescing successive note updates.
pub(crate) fn pop_last_change(&mut self) -> Option<UndoableChange> { pub(crate) fn clear_last_op(&mut self) {
self.state self.state
.undo .undo
.current_step .current_step
.as_mut() .as_mut()
.expect("no operation active") .expect("no operation active")
.changes .changes
.pop() .clear()
} }
/// Return changes made by the current op. Must only be called in a /// Return changes made by the current op. Must only be called in a
@ -546,4 +546,19 @@ mod test {
Ok(()) Ok(())
} }
#[test]
fn coalesce_note_undo_entries() -> Result<()> {
let mut col = Collection::new();
let nt = col.get_notetype_by_name("Basic")?.unwrap();
let mut note = nt.new_note();
col.add_note(&mut note, DeckId(1))?;
note.set_field(0, "foo")?;
col.update_note(&mut note)?;
note.set_field(0, "bar")?;
col.update_note(&mut note)?;
assert_eq!(col.state.undo.undo_steps.len(), 2);
Ok(())
}
} }