editor-preview/src/__init__.py

130 lines
4.3 KiB
Python
Raw Normal View History

2021-09-13 02:17:48 +02:00
import json
2021-09-14 10:45:43 +02:00
from anki import hooks, buildinfo
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
config = mw.addonManager.getConfig(__name__)
2021-09-13 02:17:48 +02:00
2021-09-13 02:17:48 +02:00
class EditorPreview(object):
js = [
"js/mathjax.js",
"js/vendor/mathjax/tex-chtml.js",
"js/reviewer.js",
]
2021-09-13 02:17:48 +02:00
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)
if int(buildinfo.version.split(".")[2]) < 45: # < 2.1.45
self.js = [
"js/vendor/jquery.min.js",
"js/vendor/css_browser_selector.min.js",
"js/mathjax.js",
"js/vendor/mathjax/tex-chtml.js",
"js/reviewer.js",
]
2021-09-14 10:45:43 +02:00
2021-09-13 02:17:48 +02:00
def editor_init_hook(self, ed: editor.Editor):
ed.editor_preview = AnkiWebView(title="editor_preview")
2021-09-13 02:17:48 +02:00
# This is taken out of clayout.py
ed.editor_preview.stdHtml(
2021-09-13 02:17:48 +02:00
ed.mw.reviewer.revHtml(),
css=["css/reviewer.css"],
js=self.js,
2021-09-13 02:17:48 +02:00
context=ed,
)
if not config["showPreviewAutomatically"]:
ed.editor_preview.hide()
2021-09-14 10:45:43 +02:00
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-13 02:17:48 +02:00
def _get_splitter(self, editor):
2021-09-14 10:45:43 +02:00
layout = editor.outerLayout
mainR, editorR = [int(r) for r in config["splitRatio"].split(":")]
location = config["location"]
2021-09-14 10:45:43 +02:00
split = QSplitter()
if location == "above":
split.setOrientation(Qt.Vertical)
split.addWidget(editor.editor_preview)
split.addWidget(editor.web)
sizes = [editorR, mainR]
elif location == "below":
split.setOrientation(Qt.Vertical)
split.addWidget(editor.web)
split.addWidget(editor.editor_preview)
sizes = [mainR, editorR]
elif location == "left":
split.setOrientation(Qt.Horizontal)
split.addWidget(editor.editor_preview)
split.addWidget(editor.web)
sizes = [editorR, mainR]
elif location == "right":
split.setOrientation(Qt.Horizontal)
split.addWidget(editor.web)
split.addWidget(editor.editor_preview)
sizes = [mainR, editorR]
else:
raise ValueError("Invalid value for config key location")
split.setSizes(sizes)
return split
def _inject_splitter(self, editor: editor.Editor):
layout = editor.outerLayout
2021-09-14 10:45:43 +02:00
web_index = layout.indexOf(editor.web)
layout.removeWidget(editor.web)
split = self._get_splitter(editor)
2021-09-14 10:45:43 +02:00
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):
addon_path = os.path.dirname(__file__)
icons_dir = os.path.join(addon_path, "icons")
b = editor.addButton(
icon=os.path.join(icons_dir, "file.svg"),
cmd="_editor_toggle_preview",
tip="Toggle Live Preview",
func=lambda o=editor: self.onEditorPreviewButton(o),
disables=False,
)
2021-09-14 10:45:43 +02:00
buttons.append(b)
def onEditorPreviewButton(self, origin: editor.Editor):
if origin.editor_preview.isHidden():
origin.editor_preview.show()
2021-09-14 10:45:43 +02:00
else:
origin.editor_preview.hide()
2021-09-14 10:45:43 +02:00
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")
if theme_manager.night_mode:
bodyclass = theme_manager.body_classes_for_card_ord(
c.ord, mw.pm.night_mode()
)
else:
bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
bodyclass += " editor-preview"
2021-09-13 02:17:48 +02:00
return f"_showAnswer({json.dumps(a)},'{bodyclass}');"
def onedit_hook(self, editor, origin):
if editor.note == origin:
editor.editor_preview.eval(self._obtainCardText(editor.note))
2021-09-13 02:17:48 +02:00
2021-09-13 02:17:48 +02:00
eprev = EditorPreview()