mirror of
https://github.com/ankitects/anki.git
synced 2026-01-13 22:13:58 -05:00
- Introduced a new transact() method that wraps the return value in a separate struct that describes the changes that were made. - Changes are now gathered from the undo log, so we don't need to guess at what was changed - eg if update_note() is called with identical note contents, no changes are returned. Card changes will only be set if cards were actually generated by the update_note() call, and tag will only be set if a new tag was added. - mw.perform_op() has been updated to expect the op to return the changes, or a structure with the changes in it, and it will use them to fire the change hook, instead of fetching the changes from undo_status(), so there is no risk of race conditions. - the various calls to mw.perform_op() have been split into separate files like card_ops.py. Aside from making the code cleaner, this works around a rather annoying issue with mypy. Because we run it with no_strict_optional, mypy is happy to accept an operation that returns None, despite the type signature saying it requires changes to be returned. Turning no_strict_optional on for the whole codebase is not practical at the moment, but we can enable it for individual files. Still todo: - The cursor keeps moving back to the start of a field when typing - we need to ignore the refresh hook when we are the initiator. - The busy cursor icon should probably be delayed a few hundreds ms. - Still need to think about a nicer way of handling saveNow() - op_made_changes(), op_affects_study_queue() might be better embedded as properties in the object instead
111 lines
3.4 KiB
Rust
111 lines
3.4 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use super::ConfigEntry;
|
|
use crate::prelude::*;
|
|
|
|
#[derive(Debug)]
|
|
pub(crate) enum UndoableConfigChange {
|
|
Added(Box<ConfigEntry>),
|
|
Updated(Box<ConfigEntry>),
|
|
Removed(Box<ConfigEntry>),
|
|
}
|
|
|
|
impl Collection {
|
|
pub(crate) fn undo_config_change(&mut self, change: UndoableConfigChange) -> Result<()> {
|
|
match change {
|
|
UndoableConfigChange::Added(entry) => self.remove_config_undoable(&entry.key),
|
|
UndoableConfigChange::Updated(entry) => {
|
|
let current = self
|
|
.storage
|
|
.get_config_entry(&entry.key)?
|
|
.ok_or_else(|| AnkiError::invalid_input("config disappeared"))?;
|
|
self.update_config_entry_undoable(entry, current)
|
|
}
|
|
UndoableConfigChange::Removed(entry) => self.add_config_entry_undoable(entry),
|
|
}
|
|
}
|
|
|
|
pub(super) fn set_config_undoable(&mut self, entry: Box<ConfigEntry>) -> Result<()> {
|
|
if let Some(original) = self.storage.get_config_entry(&entry.key)? {
|
|
self.update_config_entry_undoable(entry, original)
|
|
} else {
|
|
self.add_config_entry_undoable(entry)
|
|
}
|
|
}
|
|
|
|
pub(super) fn remove_config_undoable(&mut self, key: &str) -> Result<()> {
|
|
if let Some(current) = self.storage.get_config_entry(key)? {
|
|
self.save_undo(UndoableConfigChange::Removed(current));
|
|
self.storage.remove_config(key)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn add_config_entry_undoable(&mut self, entry: Box<ConfigEntry>) -> Result<()> {
|
|
self.storage.set_config_entry(&entry)?;
|
|
self.save_undo(UndoableConfigChange::Added(entry));
|
|
Ok(())
|
|
}
|
|
|
|
fn update_config_entry_undoable(
|
|
&mut self,
|
|
entry: Box<ConfigEntry>,
|
|
original: Box<ConfigEntry>,
|
|
) -> Result<()> {
|
|
if entry.value != original.value {
|
|
self.save_undo(UndoableConfigChange::Updated(original));
|
|
self.storage.set_config_entry(&entry)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use crate::collection::open_test_collection;
|
|
|
|
#[test]
|
|
fn undo() -> Result<()> {
|
|
let mut col = open_test_collection();
|
|
// the op kind doesn't matter, we just need undo enabled
|
|
let op = Op::Bury;
|
|
// test key
|
|
let key = BoolKey::NormalizeNoteText;
|
|
|
|
// not set by default, but defaults to true
|
|
assert_eq!(col.get_bool(key), true);
|
|
|
|
// first set adds the key
|
|
col.transact(op, |col| col.set_bool(key, false))?;
|
|
assert_eq!(col.get_bool(key), false);
|
|
|
|
// mutate it twice
|
|
col.transact(op, |col| col.set_bool(key, true))?;
|
|
assert_eq!(col.get_bool(key), true);
|
|
col.transact(op, |col| col.set_bool(key, false))?;
|
|
assert_eq!(col.get_bool(key), false);
|
|
|
|
// when we remove it, it goes back to its default
|
|
col.transact(op, |col| col.remove_config(key))?;
|
|
assert_eq!(col.get_bool(key), true);
|
|
|
|
// undo the removal
|
|
col.undo()?;
|
|
assert_eq!(col.get_bool(key), false);
|
|
|
|
// undo the mutations
|
|
col.undo()?;
|
|
assert_eq!(col.get_bool(key), true);
|
|
col.undo()?;
|
|
assert_eq!(col.get_bool(key), false);
|
|
|
|
// and undo the initial add
|
|
col.undo()?;
|
|
assert_eq!(col.get_bool(key), true);
|
|
|
|
Ok(())
|
|
}
|
|
}
|