From 56f806146c5ba79377f26f082bfca25dc8417bac Mon Sep 17 00:00:00 2001 From: Sam Penny <33956017+sam1penny@users.noreply.github.com> Date: Sat, 30 Jul 2022 11:41:42 +0100 Subject: [PATCH] added editor hook: add_cards_might_add_note (#1992) * added hook add_cards_might_add_note * fix failing test --- qt/aqt/addcards.py | 5 +++++ qt/tools/genhooks_gui.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index ef117cc55..5b25dc9ba 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -296,6 +296,11 @@ class AddCards(QMainWindow): showWarning(problem, help=HelpPage.ADDING_CARD_AND_NOTE) return False + optional_problems: list[str] = [] + gui_hooks.add_cards_might_add_note(optional_problems, note) + if not all(askUser(op) for op in optional_problems): + return False + return True def keyPressEvent(self, evt: QKeyEvent) -> None: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 39a020db3..e0300e732 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -904,6 +904,26 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) replace return the reason to reject. Otherwise return the input.""", ), + Hook( + name="add_cards_might_add_note", + args=["optional_problems: list[str]", "note: anki.notes.Note"], + doc=""" + Allows you to provide an optional reason to reject a note. A + yes / no dialog will open displaying the problem, to which the + user can decide if they would like to add the note anyway. + + optional_problems is a list containing the optional reasons for which + you might reject a note. If your add-on wants to add a reason, + it should append the reason to the list. + + An example add-on that asks the user for confirmation before adding a + card without tags: + + def might_reject_empty_tag(optional_problems, note): + if not any(note.tags): + optional_problems.append("Add cards without tags?") + """, + ), Hook( name="addcards_will_add_history_entry", args=["line: str", "note: anki.notes.Note"],