diff --git a/qt/aqt/addcards.py b/qt/aqt/addcards.py index 1a8bb3a65..0194c3296 100644 --- a/qt/aqt/addcards.py +++ b/qt/aqt/addcards.py @@ -135,11 +135,14 @@ class AddCards(QDialog): m = QMenu(self) for nid in self.history: if self.mw.col.findNotes("nid:%s" % nid): - fields = self.mw.col.getNote(nid).fields + note = self.mw.col.getNote(nid) + fields = note.fields txt = htmlToTextLine(", ".join(fields)) if len(txt) > 30: txt = txt[:30] + "..." - a = m.addAction(_('Edit "%s"') % txt) + line = _('Edit "%s"') % txt + line = gui_hooks.addcards_will_add_history_entry(line, note) + a = m.addAction(line) qconnect(a.triggered, lambda b, nid=nid: self.editHistory(nid)) else: a = m.addAction(_("(Note deleted)")) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index af8577ce0..138919fa7 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -149,6 +149,36 @@ class _AddCardsWillShowHistoryMenuHook: add_cards_will_show_history_menu = _AddCardsWillShowHistoryMenuHook() +class _AddcardsWillAddHistoryEntryFilter: + """Allows changing the history line in the add-card window.""" + + _hooks: List[Callable[[str, "anki.notes.Note"], str]] = [] + + def append(self, cb: Callable[[str, "anki.notes.Note"], str]) -> None: + """(line: str, note: anki.notes.Note)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[str, "anki.notes.Note"], str]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def count(self) -> int: + return len(self._hooks) + + def __call__(self, line: str, note: anki.notes.Note) -> str: + for filter in self._hooks: + try: + line = filter(line, note) + except: + # if the hook fails, remove it + self._hooks.remove(filter) + raise + return line + + +addcards_will_add_history_entry = _AddcardsWillAddHistoryEntryFilter() + + class _AddonConfigEditorWillDisplayJsonFilter: """Allows changing the text of the json configuration before actually displaying it to the user. For example, you can replace "\n" by diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index bbf8f8463..17ffa6720 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -485,6 +485,12 @@ hooks = [ replace return the reason to reject. Otherwise return the input.""", ), + Hook( + name="addcards_will_add_history_entry", + args=["line: str", "note: anki.notes.Note"], + return_type="str", + doc="""Allows changing the history line in the add-card window.""", + ), # Editing ################### Hook(