mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 09:16:38 -04:00
undoing of notetype fields
- fix stale cache issue - update add cards screen in response to op changes
This commit is contained in:
parent
b9417fc583
commit
9a46ad6352
6 changed files with 25 additions and 19 deletions
|
@ -908,6 +908,8 @@ table.review-log {{ {revlog_style} }}
|
||||||
If UndoEmpty is received, caller should try undo_legacy()."""
|
If UndoEmpty is received, caller should try undo_legacy()."""
|
||||||
out = self._backend.undo()
|
out = self._backend.undo()
|
||||||
self.clear_python_undo()
|
self.clear_python_undo()
|
||||||
|
if out.changes.notetype:
|
||||||
|
self.models._clear_cache()
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def undo_legacy(self) -> LegacyUndoResult:
|
def undo_legacy(self) -> LegacyUndoResult:
|
||||||
|
|
|
@ -51,6 +51,7 @@ class AddCards(QDialog):
|
||||||
self._load_new_note()
|
self._load_new_note()
|
||||||
self.history: List[NoteId] = []
|
self.history: List[NoteId] = []
|
||||||
self._last_added_note: Optional[Note] = None
|
self._last_added_note: Optional[Note] = None
|
||||||
|
gui_hooks.operation_did_execute.append(self.on_operation_did_execute)
|
||||||
restoreGeom(self, "add")
|
restoreGeom(self, "add")
|
||||||
addCloseShortcut(self)
|
addCloseShortcut(self)
|
||||||
gui_hooks.add_cards_did_init(self)
|
gui_hooks.add_cards_did_init(self)
|
||||||
|
@ -151,6 +152,18 @@ class AddCards(QDialog):
|
||||||
note.fields[n] = old_note.fields[n]
|
note.fields[n] = old_note.fields[n]
|
||||||
self.setAndFocusNote(note)
|
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:
|
def _new_note(self) -> Note:
|
||||||
return self.col.new_note(
|
return self.col.new_note(
|
||||||
self.col.models.get(self.notetype_chooser.selected_notetype_id)
|
self.col.models.get(self.notetype_chooser.selected_notetype_id)
|
||||||
|
@ -248,6 +261,7 @@ class AddCards(QDialog):
|
||||||
av_player.stop_and_clear_queue()
|
av_player.stop_and_clear_queue()
|
||||||
self.editor.cleanup()
|
self.editor.cleanup()
|
||||||
self.notetype_chooser.cleanup()
|
self.notetype_chooser.cleanup()
|
||||||
|
gui_hooks.operation_did_execute.remove(self.on_operation_did_execute)
|
||||||
self.mw.maybeReset()
|
self.mw.maybeReset()
|
||||||
saveGeom(self, "add")
|
saveGeom(self, "add")
|
||||||
aqt.dialogs.markClosed("AddCards")
|
aqt.dialogs.markClosed("AddCards")
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
# Copyright: Ankitects Pty Ltd and contributors
|
# Copyright: Ankitects Pty Ltd and contributors
|
||||||
# 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
|
||||||
|
|
||||||
from concurrent.futures import Future
|
|
||||||
|
|
||||||
import aqt
|
import aqt
|
||||||
|
from anki.collection import OpChanges
|
||||||
from anki.consts import *
|
from anki.consts import *
|
||||||
from anki.errors import TemplateError
|
|
||||||
from anki.lang import without_unicode_isolation
|
from anki.lang import without_unicode_isolation
|
||||||
from anki.models import NotetypeDict
|
from anki.models import NotetypeDict
|
||||||
from aqt import AnkiQt, gui_hooks
|
from aqt import AnkiQt, gui_hooks
|
||||||
|
from aqt.operations.notetype import update_notetype_legacy
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
from aqt.schema_change_tracker import ChangeTracker
|
from aqt.schema_change_tracker import ChangeTracker
|
||||||
from aqt.utils import (
|
from aqt.utils import (
|
||||||
|
@ -234,21 +233,13 @@ class FieldDialog(QDialog):
|
||||||
def accept(self) -> None:
|
def accept(self) -> None:
|
||||||
self.saveField()
|
self.saveField()
|
||||||
|
|
||||||
def save() -> None:
|
def on_done(changes: OpChanges) -> 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()
|
|
||||||
tooltip("Changes saved.", parent=self.mw)
|
tooltip("Changes saved.", parent=self.mw)
|
||||||
QDialog.accept(self)
|
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:
|
def onHelp(self) -> None:
|
||||||
openHelp(HelpPage.CUSTOMIZING_FIELDS)
|
openHelp(HelpPage.CUSTOMIZING_FIELDS)
|
||||||
|
|
|
@ -219,10 +219,7 @@ class Models(QDialog):
|
||||||
# Cleanup
|
# Cleanup
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
# need to flush model on change or reject
|
|
||||||
|
|
||||||
def reject(self) -> None:
|
def reject(self) -> None:
|
||||||
self.mw.reset()
|
|
||||||
saveGeom(self, "models")
|
saveGeom(self, "models")
|
||||||
QDialog.reject(self)
|
QDialog.reject(self)
|
||||||
|
|
||||||
|
|
|
@ -485,7 +485,6 @@ impl Collection {
|
||||||
let normalize = self.get_bool(BoolKey::NormalizeNoteText);
|
let normalize = self.get_bool(BoolKey::NormalizeNoteText);
|
||||||
notetype.prepare_for_update(original.as_ref())?;
|
notetype.prepare_for_update(original.as_ref())?;
|
||||||
self.ensure_notetype_name_unique(notetype, usn)?;
|
self.ensure_notetype_name_unique(notetype, usn)?;
|
||||||
self.state.notetype_cache.remove(¬etype.id);
|
|
||||||
|
|
||||||
if let Some(original) = original {
|
if let Some(original) = original {
|
||||||
self.update_notes_for_changed_fields(
|
self.update_notes_for_changed_fields(
|
||||||
|
@ -498,6 +497,7 @@ impl Collection {
|
||||||
self.update_notetype_undoable(notetype, original)?;
|
self.update_notetype_undoable(notetype, original)?;
|
||||||
} else {
|
} else {
|
||||||
// adding with existing id for old undo code, bypass undo
|
// adding with existing id for old undo code, bypass undo
|
||||||
|
self.state.notetype_cache.remove(¬etype.id);
|
||||||
self.storage
|
self.storage
|
||||||
.add_or_update_notetype_with_existing_id(¬etype)?;
|
.add_or_update_notetype_with_existing_id(¬etype)?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ impl Collection {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn remove_notetype_only_undoable(&mut self, notetype: Notetype) -> Result<()> {
|
pub(crate) fn remove_notetype_only_undoable(&mut self, notetype: Notetype) -> Result<()> {
|
||||||
|
self.state.notetype_cache.remove(¬etype.id);
|
||||||
self.storage.remove_notetype(notetype.id)?;
|
self.storage.remove_notetype(notetype.id)?;
|
||||||
self.save_undo(UndoableNotetypeChange::Removed(Box::new(notetype)));
|
self.save_undo(UndoableNotetypeChange::Removed(Box::new(notetype)));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -46,6 +47,7 @@ impl Collection {
|
||||||
notetype: &Notetype,
|
notetype: &Notetype,
|
||||||
original: Notetype,
|
original: Notetype,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
self.state.notetype_cache.remove(¬etype.id);
|
||||||
self.save_undo(UndoableNotetypeChange::Updated(Box::new(original)));
|
self.save_undo(UndoableNotetypeChange::Updated(Box::new(original)));
|
||||||
self.storage
|
self.storage
|
||||||
.add_or_update_notetype_with_existing_id(notetype)
|
.add_or_update_notetype_with_existing_id(notetype)
|
||||||
|
|
Loading…
Reference in a new issue