update empty cards hook

Closes #515
This commit is contained in:
Damien Elmes 2020-05-20 13:39:21 +10:00
parent 9876c30d89
commit 4fd8a4cf2d
3 changed files with 17 additions and 52 deletions

View File

@ -7,6 +7,7 @@ import re
import aqt import aqt
from anki.backend_pb2 import EmptyCardsReport, NoteWithEmptyCards from anki.backend_pb2 import EmptyCardsReport, NoteWithEmptyCards
from aqt import gui_hooks
from aqt.qt import QDialog, QDialogButtonBox, qconnect from aqt.qt import QDialog, QDialogButtonBox, qconnect
from aqt.utils import TR, restoreGeom, saveGeom, tooltip, tr from aqt.utils import TR, restoreGeom, saveGeom, tooltip, tr
@ -41,6 +42,8 @@ class EmptyCardsDialog(QDialog):
self.form.webview.title = "empty cards" self.form.webview.title = "empty cards"
self.form.webview.set_bridge_command(self._on_note_link_clicked, self) self.form.webview.set_bridge_command(self._on_note_link_clicked, self)
gui_hooks.empty_cards_will_show(self)
# make the note ids clickable # make the note ids clickable
html = re.sub( html = re.sub(
r"\[anki:nid:(\d+)\]", r"\[anki:nid:(\d+)\]",

View File

@ -1343,49 +1343,30 @@ class _EditorWillUseFontForFieldFilter:
editor_will_use_font_for_field = _EditorWillUseFontForFieldFilter() editor_will_use_font_for_field = _EditorWillUseFontForFieldFilter()
class _EmptyCardsWillBeDeletedFilter: class _EmptyCardsWillShowHook:
"""Allow to change the list of cards to delete. """Allows changing the list of cards to delete."""
For example, an add-on creating a method to delete only empty _hooks: List[Callable[["aqt.emptycards.EmptyCardsDialog"], None]] = []
new cards would be done as follow:
```
from anki.consts import CARD_TYPE_NEW
from anki.utils import ids2str
from aqt import mw
from aqt import gui_hooks
def filter(cids, col): def append(self, cb: Callable[["aqt.emptycards.EmptyCardsDialog"], None]) -> None:
return col.db.list( """(diag: aqt.emptycards.EmptyCardsDialog)"""
f"select id from cards where (type={CARD_TYPE_NEW} and (id in {ids2str(cids)))")
def emptyNewCard():
gui_hooks.append(filter)
mw.onEmptyCards()
gui_hooks.remove(filter)
```"""
_hooks: List[Callable[[List[int]], List[int]]] = []
def append(self, cb: Callable[[List[int]], List[int]]) -> None:
"""(cids: List[int])"""
self._hooks.append(cb) self._hooks.append(cb)
def remove(self, cb: Callable[[List[int]], List[int]]) -> None: def remove(self, cb: Callable[["aqt.emptycards.EmptyCardsDialog"], None]) -> None:
if cb in self._hooks: if cb in self._hooks:
self._hooks.remove(cb) self._hooks.remove(cb)
def __call__(self, cids: List[int]) -> List[int]: def __call__(self, diag: aqt.emptycards.EmptyCardsDialog) -> None:
for filter in self._hooks: for hook in self._hooks:
try: try:
cids = filter(cids) hook(diag)
except: except:
# if the hook fails, remove it # if the hook fails, remove it
self._hooks.remove(filter) self._hooks.remove(hook)
raise raise
return cids
empty_cards_will_be_deleted = _EmptyCardsWillBeDeletedFilter() empty_cards_will_show = _EmptyCardsWillShowHook()
class _MediaSyncDidProgressHook: class _MediaSyncDidProgressHook:

View File

@ -402,28 +402,9 @@ hooks = [
), ),
Hook(name="media_sync_did_start_or_stop", args=["running: bool"]), Hook(name="media_sync_did_start_or_stop", args=["running: bool"]),
Hook( Hook(
name="empty_cards_will_be_deleted", name="empty_cards_will_show",
args=["cids: List[int]"], args=["diag: aqt.emptycards.EmptyCardsDialog"],
return_type="List[int]", doc="""Allows changing the list of cards to delete.""",
doc="""Allow to change the list of cards to delete.
For example, an add-on creating a method to delete only empty
new cards would be done as follow:
```
from anki.consts import CARD_TYPE_NEW
from anki.utils import ids2str
from aqt import mw
from aqt import gui_hooks
def filter(cids, col):
return col.db.list(
f"select id from cards where (type={CARD_TYPE_NEW} and (id in {ids2str(cids)))")
def emptyNewCard():
gui_hooks.append(filter)
mw.onEmptyCards()
gui_hooks.remove(filter)
```""",
), ),
# Adding cards # Adding cards
################### ###################