Signed-off-by: Tobias Manske <tobias.manske@mailbox.org>
This commit is contained in:
Tobias Manske 2023-03-24 17:08:46 +01:00
parent 8e2815bc87
commit 4c966c1f5f
Signed by: tobias
GPG Key ID: E83C743C1FC2F79A

View File

@ -8,8 +8,9 @@ from aqt.webview import AnkiWebView
config = mw.addonManager.getConfig(__name__) config = mw.addonManager.getConfig(__name__)
class EditorPreview(object): class EditorPreview(object):
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",
@ -27,7 +28,6 @@ class EditorPreview(object):
"js/reviewer.js", "js/reviewer.js",
] ]
def editor_init_hook(self, ed: editor.Editor): def editor_init_hook(self, ed: editor.Editor):
ed.editor_preview = AnkiWebView(title="editor_preview") ed.editor_preview = AnkiWebView(title="editor_preview")
# This is taken out of clayout.py # This is taken out of clayout.py
@ -38,34 +38,36 @@ class EditorPreview(object):
context=ed, context=ed,
) )
if not config['showPreviewAutomatically']: if not config["showPreviewAutomatically"]:
ed.editor_preview.hide() ed.editor_preview.hide()
self._inject_splitter(ed) self._inject_splitter(ed)
gui_hooks.editor_did_fire_typing_timer.append(lambda o: self.onedit_hook(ed, o)) 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)) gui_hooks.editor_did_load_note.append(
lambda o: None if o != ed else self.editor_note_hook(o)
)
def _get_splitter(self, editor): def _get_splitter(self, editor):
layout = editor.outerLayout layout = editor.outerLayout
mainR, editorR = [int(r) for r in config['splitRatio'].split(":")] mainR, editorR = [int(r) for r in config["splitRatio"].split(":")]
location = config['location'] location = config["location"]
split = QSplitter() split = QSplitter()
if location == 'above': if location == "above":
split.setOrientation(Qt.Vertical) split.setOrientation(Qt.Vertical)
split.addWidget(editor.editor_preview) split.addWidget(editor.editor_preview)
split.addWidget(editor.web) split.addWidget(editor.web)
sizes = [editorR, mainR] sizes = [editorR, mainR]
elif location == 'below': elif location == "below":
split.setOrientation(Qt.Vertical) split.setOrientation(Qt.Vertical)
split.addWidget(editor.web) split.addWidget(editor.web)
split.addWidget(editor.editor_preview) split.addWidget(editor.editor_preview)
sizes = [mainR, editorR] sizes = [mainR, editorR]
elif location == 'left': elif location == "left":
split.setOrientation(Qt.Horizontal) split.setOrientation(Qt.Horizontal)
split.addWidget(editor.editor_preview) split.addWidget(editor.editor_preview)
split.addWidget(editor.web) split.addWidget(editor.web)
sizes = [editorR, mainR] sizes = [editorR, mainR]
elif location == 'right': elif location == "right":
split.setOrientation(Qt.Horizontal) split.setOrientation(Qt.Horizontal)
split.addWidget(editor.web) split.addWidget(editor.web)
split.addWidget(editor.editor_preview) split.addWidget(editor.editor_preview)
@ -76,7 +78,6 @@ class EditorPreview(object):
split.setSizes(sizes) split.setSizes(sizes)
return split return split
def _inject_splitter(self, editor: editor.Editor): def _inject_splitter(self, editor: editor.Editor):
layout = editor.outerLayout layout = editor.outerLayout
web_index = layout.indexOf(editor.web) web_index = layout.indexOf(editor.web)
@ -85,15 +86,18 @@ class EditorPreview(object):
split = self._get_splitter(editor) split = self._get_splitter(editor)
layout.insertWidget(web_index, split) layout.insertWidget(web_index, split)
def editor_note_hook(self, editor): def editor_note_hook(self, editor):
self.onedit_hook(editor, editor.note) self.onedit_hook(editor, editor.note)
def editor_init_button_hook(self, buttons, editor): def editor_init_button_hook(self, buttons, editor):
addon_path = os.path.dirname(__file__) addon_path = os.path.dirname(__file__)
icons_dir = os.path.join(addon_path, 'icons') 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', b = editor.addButton(
func=lambda o=editor: self.onEditorPreviewButton(o), disables=False 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,
) )
buttons.append(b) buttons.append(b)
@ -103,13 +107,14 @@ class EditorPreview(object):
else: else:
origin.editor_preview.hide() origin.editor_preview.hide()
def _obtainCardText(self, 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")
if theme_manager.night_mode: if theme_manager.night_mode:
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()
)
else: else:
bodyclass = theme_manager.body_classes_for_card_ord(c.ord) bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
bodyclass += " editor-preview" bodyclass += " editor-preview"
@ -120,4 +125,5 @@ class EditorPreview(object):
if editor.note == origin: if editor.note == origin:
editor.editor_preview.eval(self._obtainCardText(editor.note)) editor.editor_preview.eval(self._obtainCardText(editor.note))
eprev = EditorPreview() eprev = EditorPreview()