This commit is contained in:
Tobias Manske 2021-09-09 18:37:52 +02:00
parent 20609401d2
commit 48bb1931a0
Signed by: tobias
GPG Key ID: D5914DC71F2F9352

View File

@ -3,48 +3,50 @@ import json
from anki import hooks from anki import hooks
from aqt import editor from aqt import editor
from aqt.webview import AnkiWebView from aqt.webview import AnkiWebView
from aqt.utils import showInfo
from aqt.theme import theme_manager from aqt.theme import theme_manager
from aqt import addcards
from aqt import gui_hooks from aqt import gui_hooks
from aqt import mw from aqt import mw
class Editor_Preview(object): class EditorPreview(object):
def addcards_hook(self, ac: addcards): def __init__(self):
layout = ac.form.fieldsArea.layout() gui_hooks.editor_did_init.append(self.editor_init_hook)
ac.web_view = AnkiWebView(title="preview") gui_hooks.editor_did_load_note.append(self.editor_note_hook)
layout.addWidget(ac.web_view) gui_hooks.editor_did_fire_typing_timer.append(self.onedit_hook)
ac.web_view.stdHtml(
ac.mw.reviewer.revHtml(), def editor_init_hook(self, ed: editor.Editor):
self.webview = AnkiWebView(title="editor_preview")
# This is taken out of clayout.py
self.webview.stdHtml(
ed.mw.reviewer.revHtml(),
css=["css/reviewer.css"], css=["css/reviewer.css"],
js=[ js=[
"js/mathjax.js", "js/mathjax.js",
"js/vendor/mathjax/tex-chtml.js", "js/vendor/mathjax/tex-chtml.js",
"js/reviewer.js", "js/reviewer.js",
], ],
context=ac, context=ed,
) )
note = ac.editor.note layout = ed.outerLayout
if ac.editor.card: # very arbitrary max size
ord = ac.editor.card.ord # otherwise the browse window is not usable
else: self.webview.setMaximumHeight = 400
ord = 0 layout.addWidget(self.webview, 1)
c = note.ephemeral_card()
a = ac.mw.prepare_card_text_for_display(c.answer())
a = gui_hooks.card_will_show(a, c, "clayoutAnswer")
bodyclass = theme_manager.body_classes_for_card_ord(c.ord, ac.mw.pm.night_mode())
ac.web_view.eval(f"_showAnswer({json.dumps(a)},'{bodyclass}');")
self.web_view = ac.web_view
def onedit_hook(self, note): def editor_note_hook(self, editor):
self.onedit_hook(editor.note)
def _obtainCardText(self, note):
c = note.ephemeral_card() c = note.ephemeral_card()
a = mw.prepare_card_text_for_display(c.answer()) a = mw.prepare_card_text_for_display(c.answer())
a = gui_hooks.card_will_show(a, c, "clayoutAnswer") a = gui_hooks.card_will_show(a, c, "clayoutAnswer")
bodyclass = theme_manager.body_classes_for_card_ord(c.ord, mw.pm.night_mode()) bodyclass = theme_manager.body_classes_for_card_ord(c.ord, mw.pm.night_mode())
self.web_view.eval(f"_showAnswer({json.dumps(a)},'{bodyclass}');")
return f"_showAnswer({json.dumps(a)},'{bodyclass}');"
def onedit_hook(self, note):
self.webview.eval(self._obtainCardText(note))
eprev = Editor_Preview() eprev = EditorPreview()
gui_hooks.add_cards_did_init.append(eprev.addcards_hook)
gui_hooks.editor_did_fire_typing_timer.append(eprev.onedit_hook)