hook: history_line

I expect to use this hook to create an add-on which will simply remove
the ",,". By ignoring empty fields I expect to make this easier to
use.
This commit is contained in:
Arthur Milchior 2020-06-03 23:29:09 +02:00
parent a0c1b68b86
commit c81a9b3d61
3 changed files with 41 additions and 2 deletions

View file

@ -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)"))

View file

@ -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

View file

@ -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(