Add editor_will_munge_html hook

This commit is contained in:
Henrik Giesel 2020-08-08 23:14:55 +02:00
parent 06a69c25e6
commit 7f9e560396
2 changed files with 36 additions and 0 deletions

View File

@ -1564,6 +1564,36 @@ class _EditorWillLoadNoteFilter:
editor_will_load_note = _EditorWillLoadNoteFilter() editor_will_load_note = _EditorWillLoadNoteFilter()
class _EditorWillMungeHtmlFilter:
"""Allows manipulating the text that will be saved by the editor"""
_hooks: List[Callable[[str, "aqt.editor.Editor"], str]] = []
def append(self, cb: Callable[[str, "aqt.editor.Editor"], str]) -> None:
"""(txt: str, editor: aqt.editor.Editor)"""
self._hooks.append(cb)
def remove(self, cb: Callable[[str, "aqt.editor.Editor"], str]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def count(self) -> int:
return len(self._hooks)
def __call__(self, txt: str, editor: aqt.editor.Editor) -> str:
for filter in self._hooks:
try:
txt = filter(txt, editor)
except:
# if the hook fails, remove it
self._hooks.remove(filter)
raise
return txt
editor_will_munge_html = _EditorWillMungeHtmlFilter()
class _EditorWillShowContextMenuHook: class _EditorWillShowContextMenuHook:
_hooks: List[Callable[["aqt.editor.EditorWebView", QMenu], None]] = [] _hooks: List[Callable[["aqt.editor.EditorWebView", QMenu], None]] = []

View File

@ -568,6 +568,12 @@ hooks = [
args=["note: anki.notes.Note"], args=["note: anki.notes.Note"],
legacy_hook="tagsUpdated", legacy_hook="tagsUpdated",
), ),
Hook(
name="editor_will_munge_html",
args=["txt: str", "editor: aqt.editor.Editor"],
return_type="str",
doc="""Allows manipulating the text that will be saved by the editor""",
),
Hook( Hook(
name="editor_will_use_font_for_field", name="editor_will_use_font_for_field",
args=["font: str"], args=["font: str"],