1 Commits

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

View File

@ -1,32 +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 slugify
- ./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:
- "*.ankiaddon"
image_pull_secrets:
- registry

1
.gitignore vendored
View File

@ -169,4 +169,3 @@ tags
# Stores actual addon config if the src directory is symlinked into an anki installation during development
src/meta.json
src/manifest.json

View File

@ -1,27 +1,11 @@
#!/bin/bash
set -e
set -o nounset
SLUG=$(slugify "${DRONE_COMMIT_REF}")
if [ -d src/__pycache__ ]; then
rm -r src/__pycache__
fi
cd src
# Create Manifest
cat - > manifest.json <<EOF
{
"name": "Editor Live Preview - ${SLUG}",
"package": "editor-live-preview-${SLUG}"
}
EOF
zip -r ../editor-preview-external.ankiaddon --exclude meta.json -- *
zip -r ../editor-preview-ankiweb.ankiaddon --exclude meta.json --exclude manifest.json -- *
zip -r ../editor-preview.ankiaddon --exclude meta.json -- *
cd ..
echo "Ankiweb Contents"
unzip -l editor-preview-ankiweb.ankiaddon
echo "External Contents"
unzip -l editor-preview-external.ankiaddon
unzip -l editor-preview.ankiaddon

View File

@ -8,9 +8,8 @@ from aqt.webview import AnkiWebView
config = mw.addonManager.getConfig(__name__)
class EditorPreview(object):
js = [
js=[
"js/mathjax.js",
"js/vendor/mathjax/tex-chtml.js",
"js/reviewer.js",
@ -19,8 +18,7 @@ class EditorPreview(object):
def __init__(self):
gui_hooks.editor_did_init.append(self.editor_init_hook)
gui_hooks.editor_did_init_buttons.append(self.editor_init_button_hook)
self.scrollLock = config["enableScrollLockByDefault"]
if int(buildinfo.version.split(".")[2]) < 45: # < 2.1.45
if int(buildinfo.version.split(".")[2]) < 45: # < 2.1.45
self.js = [
"js/vendor/jquery.min.js",
"js/vendor/css_browser_selector.min.js",
@ -29,126 +27,71 @@ class EditorPreview(object):
"js/reviewer.js",
]
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
ed.editor_preview.stdHtml(
ed.webview.stdHtml(
ed.mw.reviewer.revHtml(),
css=["css/reviewer.css"],
js=self.js,
context=ed,
)
if not config["showPreviewAutomatically"]:
ed.editor_preview.hide()
if not config['showPreviewAutomatically']:
ed.webview.hide()
self._inject_splitter(ed)
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)
)
def _get_splitter(self, editor):
layout = editor.outerLayout
mainR, editorR = [int(r) for r in config["splitRatio"].split(":")]
location = config["location"]
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
gui_hooks.editor_did_load_note.append(lambda o: None if o != ed else self.editor_note_hook(o))
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 = self._get_splitter(editor)
split.addWidget(editor.web)
split.addWidget(editor.webview)
splitRatio = config['splitRatio']
upperR, lowerR = [int(r) for r in splitRatio.split(":")]
split.setStretchFactor(0, upperR)
split.setStretchFactor(1, lowerR)
layout.insertWidget(web_index, split)
def isScrollLocked(self):
return self.scrollLock
def toggleScrollLock(self):
self.scrollLock = not self.scrollLock
def editor_note_hook(self, editor):
# Dont lock the editor in place when loading a new note
# i.e. when using the cards browser
self.onedit_hook(editor, editor.note, keep_scrollbar_value=False)
self.onedit_hook(editor, editor.note)
def editor_init_button_hook(self, buttons, editor):
addon_path = os.path.dirname(__file__)
icons_dir = os.path.join(addon_path, "icons")
previewButton = 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,
)
scrollLockButton = editor.addButton(
# icon=os.path.join(icons_dir, "file.svg"),
icon=None,
label="🔒",
cmd="_editor_toggle_scroll_lock",
tip="Toggle Scroll Locking",
func=lambda o=editor: self.onEditorScrollLockButton(o),
disables=True,
)
buttons.append(previewButton)
buttons.append(scrollLockButton)
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
)
buttons.append(b)
def onEditorPreviewButton(self, origin: editor.Editor):
if origin.editor_preview.isHidden():
origin.editor_preview.show()
if origin.webview.isHidden():
origin.webview.show()
else:
origin.editor_preview.hide()
origin.webview.hide()
def onEditorScrollLockButton(self, origin: editor.Editor):
self.toggleScrollLock()
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, 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"
return f"_showAnswer({json.dumps(a)},'{bodyclass}');"
def onedit_hook(self, editor, origin, keep_scrollbar_value=True):
def onedit_hook(self, editor, origin):
if editor.note == origin:
page: QWebEnginePage = editor.editor_preview.page()
scrollbar_value = page.scrollPosition()
editor.editor_preview.eval(self._obtainCardText(editor.note))
if keep_scrollbar_value and self.isScrollLocked():
page.runJavaScript(
f"window.scrollTo({scrollbar_value.x()}, {scrollbar_value.y()});"
)
editor.webview.eval(self._obtainCardText(editor.note))
eprev = EditorPreview()

View File

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

View File

@ -2,11 +2,5 @@
\- `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/>
\- `splitRatio` [int:int]:<br/>
&nbsp;&nbsp;&nbsp;Defines the default split ratio of the main view and preview view (default: 1:1)<br/>
<br/>
\- `location` [string (above | below | left | right)]:<br/>
&nbsp;&nbsp;&nbsp;Defines where to render the preview (default: below)
<br/>
\- `showPreviewAutomatically` [boolean (true | false)]:<br/>
&nbsp;&nbsp;&nbsp;Whether to keep the scrollbar at the same location when text changed or not (default: true)<br/>
&nbsp;&nbsp;&nbsp;Defines the default split ratio of the main view and preview view (default: 4:1)
<br/>