2 Commits

Author SHA1 Message Date
360c8b8668 Add debug builds
All checks were successful
continuous-integration/drone/tag Build is passing
2023-03-24 23:37:24 +01:00
c3f536017a WIP: Keep scroll position steady
All checks were successful
continuous-integration/drone/tag Build is passing
2023-03-24 21:48:44 +01:00
6 changed files with 67 additions and 19 deletions

View File

@ -13,7 +13,7 @@ steps:
image: debian:bookworm
pull: always
commands:
- apt-get update && apt-get install -y zip
- apt-get update && apt-get install -y zip slugify
- ./build.sh
- name: Upload Artifact to Gitea
@ -25,7 +25,8 @@ steps:
from_secret: gitea_api_token
checksum: sha256
base_url: https://git.tobiasmanske.de
files: editor-preview.ankiaddon
files:
- "*.ankiaddon"
image_pull_secrets:
- registry

2
.gitignore vendored
View File

@ -117,7 +117,6 @@ venv/
ENV/
env.bak/
venv.bak/
.idea
# Spyder project settings
.spyderproject
@ -170,3 +169,4 @@ 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,11 +1,27 @@
#!/bin/bash
set -e
set -o nounset
SLUG=$(slugify "${DRONE_COMMIT_REF}")
if [ -d src/__pycache__ ]; then
rm -r src/__pycache__
fi
cd src
zip -r ../editor-preview.ankiaddon --exclude meta.json -- *
# 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 -- *
cd ..
unzip -l editor-preview.ankiaddon
echo "Ankiweb Contents"
unzip -l editor-preview-ankiweb.ankiaddon
echo "External Contents"
unzip -l editor-preview-external.ankiaddon

View File

@ -19,10 +19,8 @@ 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)
buildversion = buildinfo.version.split(".")
# Anki changed their versioning scheme in 2023 to year.month(.patch), causing things to explode here.
if not int(buildversion[0]) >= 23 and int(buildversion[2]) < 45: # < 2.1.45
self.scrollLock = config["enableScrollLockByDefault"]
if int(buildinfo.version.split(".")[2]) < 45: # < 2.1.45
self.js = [
"js/vendor/jquery.min.js",
"js/vendor/css_browser_selector.min.js",
@ -56,22 +54,22 @@ class EditorPreview(object):
location = config["location"]
split = QSplitter()
if location == "above":
split.setOrientation(Qt.Orientation.Vertical)
split.setOrientation(Qt.Vertical)
split.addWidget(editor.editor_preview)
split.addWidget(editor.web)
sizes = [editorR, mainR]
elif location == "below":
split.setOrientation(Qt.Orientation.Vertical)
split.setOrientation(Qt.Vertical)
split.addWidget(editor.web)
split.addWidget(editor.editor_preview)
sizes = [mainR, editorR]
elif location == "left":
split.setOrientation(Qt.Orientation.Horizontal)
split.setOrientation(Qt.Horizontal)
split.addWidget(editor.editor_preview)
split.addWidget(editor.web)
sizes = [editorR, mainR]
elif location == "right":
split.setOrientation(Qt.Orientation.Horizontal)
split.setOrientation(Qt.Horizontal)
split.addWidget(editor.web)
split.addWidget(editor.editor_preview)
sizes = [mainR, editorR]
@ -89,20 +87,38 @@ class EditorPreview(object):
split = self._get_splitter(editor)
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):
self.onedit_hook(editor, editor.note)
# 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)
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(
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,
)
buttons.append(b)
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)
def onEditorPreviewButton(self, origin: editor.Editor):
if origin.editor_preview.isHidden():
@ -110,18 +126,29 @@ class EditorPreview(object):
else:
origin.editor_preview.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)
bodyclass = theme_manager.body_classes_for_card_ord(
c.ord, theme_manager.night_mode
)
bodyclass += " editor-preview"
return f"_showAnswer({json.dumps(a)},'{bodyclass}');"
def onedit_hook(self, editor, origin):
def onedit_hook(self, editor, origin, keep_scrollbar_value=True):
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()});"
)
eprev = EditorPreview()

View File

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

View File

@ -7,3 +7,6 @@
\- `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/>
<br/>