notetype removal undoable

This commit is contained in:
Damien Elmes 2021-04-30 16:01:47 +10:00
parent ea758f0092
commit 394fe86f8f
3 changed files with 19 additions and 14 deletions

View File

@ -228,10 +228,10 @@ class ModelManager:
self._remove_from_cache(NotetypeId(nt.id))
self.col._backend.remove_notetype(nt.id)
def remove(self, id: NotetypeId) -> None:
def remove(self, id: NotetypeId) -> OpChanges:
"Modifies schema."
self._remove_from_cache(id)
self.col._backend.remove_notetype(id)
return self.col._backend.remove_notetype(id)
def add(self, m: NotetypeDict) -> OpChangesWithId:
"Replaced with add_dict()"

View File

@ -11,8 +11,9 @@ from anki.lang import without_unicode_isolation
from anki.models import NotetypeDict, NotetypeId, NotetypeNameIdUseCount
from anki.notes import Note
from aqt import AnkiQt, gui_hooks
from aqt.operations.notetype import add_notetype_legacy
from aqt.operations.notetype import add_notetype_legacy, remove_notetype
from aqt.qt import *
from aqt.schema_change_tracker import ChangeTracker
from aqt.utils import (
HelpPage,
askUser,
@ -171,18 +172,14 @@ class Models(QDialog):
if not askUser(msg, parent=self):
return
self.col.modSchema(check=True)
tracker = ChangeTracker(self.mw)
if not tracker.mark_schema():
return
nt = self.current_notetype()
def save() -> Sequence[NotetypeNameIdUseCount]:
self.mm.rem(nt)
return self.col.models.all_use_counts()
def on_done(fut: Future) -> None:
self.updateModelsList(fut.result())
self.mw.taskman.with_progress(save, on_done, self)
remove_notetype(parent=self, notetype_id=nt["id"]).success(
lambda _: self.refresh_list()
).run_in_background()
def onAdvanced(self) -> None:
nt = self.current_notetype()

View File

@ -4,7 +4,7 @@
from __future__ import annotations
from anki.collection import OpChanges, OpChangesWithId
from anki.models import NotetypeDict
from anki.models import NotetypeDict, NotetypeId
from aqt import QWidget
from aqt.operations import CollectionOp
@ -23,3 +23,11 @@ def update_notetype_legacy(
notetype: NotetypeDict,
) -> CollectionOp[OpChanges]:
return CollectionOp(parent, lambda col: col.models.update_dict(notetype))
def remove_notetype(
*,
parent: QWidget,
notetype_id: NotetypeId,
) -> CollectionOp[OpChanges]:
return CollectionOp(parent, lambda col: col.models.remove(notetype_id))