Compare commits

..

1 Commits

Author SHA1 Message Date
33ed64c0c0
Build changes for PR #7 2022-10-10 18:55:54 +02:00
5 changed files with 37 additions and 131 deletions

View File

@ -1,31 +0,0 @@
---
kind: pipeline
type: docker
name: Build Anki Plugin
trigger:
event:
include:
- tag
steps:
- name: Build Archive
image: debian:bookworm
pull: always
commands:
- apt-get update && apt-get install -y zip
- ./build.sh
- name: Upload Artifact to Gitea
depends_on:
- Build Archive
image: plugins/gitea-release
settings:
api_key:
from_secret: gitea_api_token
checksum: sha256
base_url: https://git.tobiasmanske.de
files: editor-preview.ankiaddon
image_pull_secrets:
- registry

1
.gitignore vendored
View File

@ -117,7 +117,6 @@ venv/
ENV/ ENV/
env.bak/ env.bak/
venv.bak/ venv.bak/
.idea
# Spyder project settings # Spyder project settings
.spyderproject .spyderproject

View File

@ -1,5 +1,4 @@
import json import json
from typing import Set
from anki import hooks, buildinfo from anki import hooks, buildinfo
from aqt import editor, gui_hooks, mw from aqt import editor, gui_hooks, mw
@ -9,10 +8,8 @@ from aqt.webview import AnkiWebView
config = mw.addonManager.getConfig(__name__) config = mw.addonManager.getConfig(__name__)
class EditorPreview(object): class EditorPreview(object):
editors: Set[editor.Editor] = set() 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",
@ -21,18 +18,7 @@ class EditorPreview(object):
def __init__(self): def __init__(self):
gui_hooks.editor_did_init.append(self.editor_init_hook) gui_hooks.editor_did_init.append(self.editor_init_hook)
gui_hooks.editor_did_init_buttons.append(self.editor_init_button_hook) gui_hooks.editor_did_init_buttons.append(self.editor_init_button_hook)
gui_hooks.editor_did_load_note.append(self.editor_note_hook) if int(buildinfo.version.split(".")[2]) < 45: # < 2.1.45
gui_hooks.editor_did_fire_typing_timer.append(self.onedit_hook)
buildversion = buildinfo.version.split(".")
# Anki changed their versioning scheme in 2023 to year.month(.patch), causing things to explode here.
if int(buildversion[0]) >= 24 or (int(buildversion[0]) == 23 and int(buildversion[1]) == 12 and 2 < len(buildversion) and int(buildversion[2])) >= 1: # >= 23.12.1
self.js = [
"js/mathjax.js",
"js/vendor/mathjax/tex-chtml-full.js",
"js/reviewer.js",
]
elif int(buildversion[0]) < 23 and int(buildversion[2]) < 45: # < 2.1.45
self.js = [ self.js = [
"js/vendor/jquery.min.js", "js/vendor/jquery.min.js",
"js/vendor/css_browser_selector.min.js", "js/vendor/css_browser_selector.min.js",
@ -41,112 +27,71 @@ 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.webview = AnkiWebView(title="editor_preview")
# This is taken out of clayout.py # This is taken out of clayout.py
ed.editor_preview.stdHtml( ed.webview.stdHtml(
ed.mw.reviewer.revHtml(), ed.mw.reviewer.revHtml(),
css=["css/reviewer.css"], css=["css/reviewer.css"],
js=self.js, js=self.js,
context=ed, context=ed,
) )
if not config["showPreviewAutomatically"]: if not config['showPreviewAutomatically']:
ed.editor_preview.hide() ed.webview.hide()
self._inject_splitter(ed) self._inject_splitter(ed)
gui_hooks.editor_did_fire_typing_timer.append(lambda o: self.onedit_hook(ed, o))
def _get_splitter(self, editor): gui_hooks.editor_did_load_note.append(lambda o: None if o != ed else self.editor_note_hook(o))
mainR, editorR = [int(r) * 10000 for r in config["splitRatio"].split(":")]
location = config["location"]
split = QSplitter()
if location == "above":
split.setOrientation(Qt.Orientation.Vertical)
split.addWidget(editor.editor_preview)
split.addWidget(editor.wrapped_web)
sizes = [editorR, mainR]
elif location == "below":
split.setOrientation(Qt.Orientation.Vertical)
split.addWidget(editor.wrapped_web)
split.addWidget(editor.editor_preview)
sizes = [mainR, editorR]
elif location == "left":
split.setOrientation(Qt.Orientation.Horizontal)
split.addWidget(editor.editor_preview)
split.addWidget(editor.wrapped_web)
sizes = [editorR, mainR]
elif location == "right":
split.setOrientation(Qt.Orientation.Horizontal)
split.addWidget(editor.wrapped_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): def _inject_splitter(self, editor: editor.Editor):
layout = editor.web.parentWidget().layout() layout = editor.outerLayout
if layout is None: split = QSplitter()
layout = QVBoxLayout() split.setOrientation(Qt.Vertical)
editor.web.parentWidget().setLayout(layout)
web_index = layout.indexOf(editor.web) web_index = layout.indexOf(editor.web)
layout.removeWidget(editor.web) layout.removeWidget(editor.web)
split.addWidget(editor.web)
# Wrap a widget on the outer layer of the webview split.addWidget(editor.webview)
# So that other plugins can continue to modify the layout splitRatio = config['splitRatio']
editor.wrapped_web = QWidget() upperR, lowerR = [int(r) for r in splitRatio.split(":")]
wrapLayout = QHBoxLayout() split.setStretchFactor(0, upperR)
editor.wrapped_web.setLayout(wrapLayout) split.setStretchFactor(1, lowerR)
wrapLayout.addWidget(editor.web)
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.editors = set(filter(lambda it: it.note is not None, self.editors)) self.onedit_hook(editor, editor.note)
self.editors.add(editor)
# The initial loading of notes will also trigger an editing event
# which will cause a second refresh
# Caching the content of notes here will be used to determine if the content has changed
editor.cached_fields = list(editor.note.fields)
self.refresh(editor)
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( b = editor.addButton(icon=os.path.join(icons_dir, 'file.svg'), cmd="_editor_toggle_preview", tip='Toggle Live Preview',
icon=os.path.join(icons_dir, "file.svg"), func=lambda o=editor: self.onEditorPreviewButton(o), disables=False
cmd="_editor_toggle_preview", )
tip="Toggle Live Preview",
func=lambda o=editor: self.onEditorPreviewButton(o),
disables=False,
)
buttons.append(b) buttons.append(b)
def onEditorPreviewButton(self, origin: editor.Editor): def onEditorPreviewButton(self, origin: editor.Editor):
if origin.editor_preview.isHidden(): if origin.webview.isHidden():
origin.editor_preview.show() origin.webview.show()
else: else:
origin.editor_preview.hide() origin.webview.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")
bodyclass = theme_manager.body_classes_for_card_ord(c.ord, theme_manager.night_mode) 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" bodyclass += " editor-preview"
return f"_showAnswer({json.dumps(a)},'{bodyclass}');" return f"_showAnswer({json.dumps(a)},'{bodyclass}');"
def onedit_hook(self, note): def onedit_hook(self, editor, origin):
for editor in self.editors: if editor.note == origin:
if editor.note == note and editor.cached_fields != note.fields: editor.webview.eval(self._obtainCardText(editor.note))
editor.cached_fields = list(note.fields)
self.refresh(editor)
def refresh(self, editor):
editor.editor_preview.eval(self._obtainCardText(editor.note))
eprev = EditorPreview() eprev = EditorPreview()

View File

@ -1,5 +1 @@
{ {"showPreviewAutomatically": true, "splitRatio": "4:1"}
"showPreviewAutomatically": true,
"splitRatio": "1:1",
"location": "below"
}

View File

@ -2,8 +2,5 @@
\- `showPreviewAutomatically` [boolean (true | false)]:<br/> \- `showPreviewAutomatically` [boolean (true | false)]:<br/>
&nbsp;&nbsp;&nbsp;Defines if the preview window should show up automatically as you enter the Editor (default: true)<br/><br/> &nbsp;&nbsp;&nbsp;Defines if the preview window should show up automatically as you enter the Editor (default: true)<br/><br/>
\- `splitRatio` [int:int]:<br/> \- `splitRatio` [int:int]:<br/>
&nbsp;&nbsp;&nbsp;Defines the default split ratio of the main view and preview view (default: 1:1)<br/> &nbsp;&nbsp;&nbsp;Defines the default split ratio of the main view and preview view (default: 4:1)
<br/>
\- `location` [string (above | below | left | right)]:<br/>
&nbsp;&nbsp;&nbsp;Defines where to render the preview (default: below)
<br/> <br/>