editor-preview/src/__init__.py

81 lines
2.7 KiB
Python
Raw Normal View History

2021-09-13 02:17:48 +02:00
import json
2021-09-14 10:45:43 +02:00
2021-09-13 02:17:48 +02:00
from anki import hooks
2021-09-14 10:45:43 +02:00
from aqt import editor, gui_hooks, mw
from aqt.utils import *
2021-09-13 02:17:48 +02:00
from aqt.theme import theme_manager
2021-09-14 10:45:43 +02:00
from aqt.webview import AnkiWebView
2021-09-13 02:17:48 +02:00
class EditorPreview(object):
def __init__(self):
gui_hooks.editor_did_init.append(self.editor_init_hook)
2021-09-14 10:45:43 +02:00
gui_hooks.editor_did_init_buttons.append(self.editor_init_button_hook)
2021-09-13 02:17:48 +02:00
def editor_init_hook(self, ed: editor.Editor):
ed.webview = AnkiWebView(title="editor_preview")
# This is taken out of clayout.py
ed.webview.stdHtml(
ed.mw.reviewer.revHtml(),
css=["css/reviewer.css"],
js=[
"js/mathjax.js",
"js/vendor/mathjax/tex-chtml.js",
"js/reviewer.js",
],
context=ed,
)
# very arbitrary max size
# otherwise the browse window is not usable
2021-09-14 10:45:43 +02:00
# ed.webview.setMaximumHeight = 400
self._inject_splitter(ed)
2021-09-13 02:17:48 +02:00
gui_hooks.editor_did_fire_typing_timer.append(lambda o: self.onedit_hook(ed, o))
gui_hooks.editor_did_load_note.append(lambda o: None if o != ed else self.editor_note_hook(o))
2021-09-14 10:45:43 +02:00
def _inject_splitter(self, editor: editor.Editor):
layout = editor.outerLayout
split = QSplitter()
split.setOrientation(Qt.Vertical)
web_index = layout.indexOf(editor.web)
layout.removeWidget(editor.web)
split.addWidget(editor.web)
split.addWidget(editor.webview)
split.setStretchFactor(0, 0)
split.setStretchFactor(1, 1)
split.setStretchFactor(2, 1)
layout.insertWidget(web_index, split)
2021-09-13 02:17:48 +02:00
def editor_note_hook(self, editor):
self.onedit_hook(editor, editor.note)
2021-09-14 10:45:43 +02:00
def editor_init_button_hook(self, buttons, editor):
b = editor.addButton(icon=None, cmd="_editor_toggle_preview", label='P', tip='Toggle Live Preview',
func=lambda o=editor: self.onEditorPreviewButton(o), disables=False
)
buttons.append(b)
def onEditorPreviewButton(self, origin: editor.Editor):
if origin.webview.isHidden():
origin.webview.show()
else:
origin.webview.hide()
2021-09-13 02:17:48 +02:00
def _obtainCardText(self, note):
c = note.ephemeral_card()
a = 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, mw.pm.night_mode())
return f"_showAnswer({json.dumps(a)},'{bodyclass}');"
def onedit_hook(self, editor, origin):
if editor.note == origin:
editor.webview.eval(self._obtainCardText(editor.note))
eprev = EditorPreview()