undoing of notetype fields

- fix stale cache issue
- update add cards screen in response to op changes
This commit is contained in:
Damien Elmes 2021-04-30 17:15:59 +10:00
parent b9417fc583
commit 9a46ad6352
6 changed files with 25 additions and 19 deletions

View File

@ -908,6 +908,8 @@ table.review-log {{ {revlog_style} }}
If UndoEmpty is received, caller should try undo_legacy()."""
out = self._backend.undo()
self.clear_python_undo()
if out.changes.notetype:
self.models._clear_cache()
return out
def undo_legacy(self) -> LegacyUndoResult:

View File

@ -51,6 +51,7 @@ class AddCards(QDialog):
self._load_new_note()
self.history: List[NoteId] = []
self._last_added_note: Optional[Note] = None
gui_hooks.operation_did_execute.append(self.on_operation_did_execute)
restoreGeom(self, "add")
addCloseShortcut(self)
gui_hooks.add_cards_did_init(self)
@ -151,6 +152,18 @@ class AddCards(QDialog):
note.fields[n] = old_note.fields[n]
self.setAndFocusNote(note)
def on_operation_did_execute(
self, changes: OpChanges, handler: Optional[object]
) -> None:
if (changes.notetype or changes.deck) and handler is not self.editor:
self.on_notetype_change(
NotetypeId(
self.col.defaults_for_adding(
current_review_card=self.mw.reviewer.card
).notetype_id
)
)
def _new_note(self) -> Note:
return self.col.new_note(
self.col.models.get(self.notetype_chooser.selected_notetype_id)
@ -248,6 +261,7 @@ class AddCards(QDialog):
av_player.stop_and_clear_queue()
self.editor.cleanup()
self.notetype_chooser.cleanup()
gui_hooks.operation_did_execute.remove(self.on_operation_did_execute)
self.mw.maybeReset()
saveGeom(self, "add")
aqt.dialogs.markClosed("AddCards")

View File

@ -1,14 +1,13 @@
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from concurrent.futures import Future
import aqt
from anki.collection import OpChanges
from anki.consts import *
from anki.errors import TemplateError
from anki.lang import without_unicode_isolation
from anki.models import NotetypeDict
from aqt import AnkiQt, gui_hooks
from aqt.operations.notetype import update_notetype_legacy
from aqt.qt import *
from aqt.schema_change_tracker import ChangeTracker
from aqt.utils import (
@ -234,21 +233,13 @@ class FieldDialog(QDialog):
def accept(self) -> None:
self.saveField()
def save() -> None:
self.mm.save(self.model)
def on_done(fut: Future) -> None:
try:
fut.result()
except TemplateError as e:
# fixme: i18n
showWarning(f"Unable to save changes: {str(e)}")
return
self.mw.reset()
def on_done(changes: OpChanges) -> None:
tooltip("Changes saved.", parent=self.mw)
QDialog.accept(self)
self.mw.taskman.with_progress(save, on_done, self)
update_notetype_legacy(parent=self.mw, notetype=self.model).success(
on_done
).run_in_background()
def onHelp(self) -> None:
openHelp(HelpPage.CUSTOMIZING_FIELDS)

View File

@ -219,10 +219,7 @@ class Models(QDialog):
# Cleanup
##########################################################################
# need to flush model on change or reject
def reject(self) -> None:
self.mw.reset()
saveGeom(self, "models")
QDialog.reject(self)

View File

@ -485,7 +485,6 @@ impl Collection {
let normalize = self.get_bool(BoolKey::NormalizeNoteText);
notetype.prepare_for_update(original.as_ref())?;
self.ensure_notetype_name_unique(notetype, usn)?;
self.state.notetype_cache.remove(&notetype.id);
if let Some(original) = original {
self.update_notes_for_changed_fields(
@ -498,6 +497,7 @@ impl Collection {
self.update_notetype_undoable(notetype, original)?;
} else {
// adding with existing id for old undo code, bypass undo
self.state.notetype_cache.remove(&notetype.id);
self.storage
.add_or_update_notetype_with_existing_id(&notetype)?;
}

View File

@ -27,6 +27,7 @@ impl Collection {
}
pub(crate) fn remove_notetype_only_undoable(&mut self, notetype: Notetype) -> Result<()> {
self.state.notetype_cache.remove(&notetype.id);
self.storage.remove_notetype(notetype.id)?;
self.save_undo(UndoableNotetypeChange::Removed(Box::new(notetype)));
Ok(())
@ -46,6 +47,7 @@ impl Collection {
notetype: &Notetype,
original: Notetype,
) -> Result<()> {
self.state.notetype_cache.remove(&notetype.id);
self.save_undo(UndoableNotetypeChange::Updated(Box::new(original)));
self.storage
.add_or_update_notetype_with_existing_id(notetype)