Merge pull request #523 from Arthur-Milchior/hook_note_will_load

Hook editor_note_will_load
This commit is contained in:
Damien Elmes 2020-03-25 09:43:16 +10:00 committed by GitHub
commit 818b9193a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

View File

@ -456,6 +456,7 @@ class Editor:
json.dumps(focusTo),
json.dumps(self.note.id),
)
js = gui_hooks.editor_will_load_note(js, self.note, self)
self.web.evalWithCallback(js, oncallback)
def fonts(self) -> List[Tuple[str, int, bool]]:

View File

@ -1207,6 +1207,40 @@ class _EditorWebViewDidInitHook:
editor_web_view_did_init = _EditorWebViewDidInitHook()
class _EditorWillLoadNoteFilter:
"""Allows changing the javascript commands to load note before
executing it and do change in the QT editor."""
_hooks: List[Callable[[str, "anki.notes.Note", "aqt.editor.Editor"], str]] = []
def append(
self, cb: Callable[[str, "anki.notes.Note", "aqt.editor.Editor"], str]
) -> None:
"""(js: str, note: anki.notes.Note, editor: aqt.editor.Editor)"""
self._hooks.append(cb)
def remove(
self, cb: Callable[[str, "anki.notes.Note", "aqt.editor.Editor"], str]
) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def __call__(
self, js: str, note: anki.notes.Note, editor: aqt.editor.Editor
) -> str:
for filter in self._hooks:
try:
js = filter(js, note, editor)
except:
# if the hook fails, remove it
self._hooks.remove(filter)
raise
return js
editor_will_load_note = _EditorWillLoadNoteFilter()
class _EditorWillShowContextMenuHook:
_hooks: List[Callable[["aqt.editor.EditorWebView", QMenu], None]] = []

View File

@ -498,6 +498,13 @@ def emptyNewCard():
args=["editor_web_view: aqt.editor.EditorWebView"],
),
Hook(name="editor_did_init", args=["editor: aqt.editor.Editor"],),
Hook(
name="editor_will_load_note",
args=["js: str", "note: anki.notes.Note", "editor: aqt.editor.Editor"],
return_type="str",
doc="""Allows changing the javascript commands to load note before
executing it and do change in the QT editor.""",
),
# Sound/video
###################
Hook(name="av_player_will_play", args=["tag: anki.sound.AVTag"]),