diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index e53a6ffd5..ed183e40e 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -954,6 +954,7 @@ class EditorWebView(AnkiWebView): self._markInternal = False clip = self.editor.mw.app.clipboard() clip.dataChanged.connect(self._onClipboardChange) + gui_hooks.editor_web_view_did_init(self) def _onClipboardChange(self): if self._markInternal: diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 73e85eeef..eed241f7e 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -1036,6 +1036,30 @@ class _EditorDidUpdateTagsHook: editor_did_update_tags = _EditorDidUpdateTagsHook() +class _EditorWebViewDidInitHook: + _hooks: List[Callable[["aqt.editor.EditorWebView"], None]] = [] + + def append(self, cb: Callable[["aqt.editor.EditorWebView"], None]) -> None: + """(editor_web_view: aqt.editor.EditorWebView)""" + self._hooks.append(cb) + + def remove(self, cb: Callable[["aqt.editor.EditorWebView"], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def __call__(self, editor_web_view: aqt.editor.EditorWebView) -> None: + for hook in self._hooks: + try: + hook(editor_web_view) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +editor_web_view_did_init = _EditorWebViewDidInitHook() + + class _EditorWillShowContextMenuHook: _hooks: List[Callable[["aqt.editor.EditorWebView", QMenu], None]] = [] diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index fe675cb6b..dcf543979 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -460,6 +460,10 @@ def emptyNewCard(): return_type="str", legacy_hook="mungeEditingFontName", ), + Hook( + name="editor_web_view_did_init", + args=["editor_web_view: aqt.editor.EditorWebView"], + ), # Sound/video ################### Hook(name="av_player_will_play", args=["tag: anki.sound.AVTag"]),