12 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
ab68523be6 Merge PR #10 2023-03-24 18:16:25 +01:00
af073e0498 Build addon automatically on push
All checks were successful
continuous-integration/drone/tag Build is passing
Signed-off-by: Tobias Manske <tobias.manske@mailbox.org>
2023-03-24 18:02:29 +01:00
4c966c1f5f Format
Signed-off-by: Tobias Manske <tobias.manske@mailbox.org>
2023-03-24 18:02:28 +01:00
8e2815bc87 Add option to split side-by-side view. Also fixes split ratio handling
Signed-off-by: Tobias Manske <tobias.manske@mailbox.org>
2023-03-24 18:02:25 +01:00
734f24646e Remove the usage of deprecated function.
mv.pm.night_mode() is deprecated (see
1ed2cce648/qt/aqt/profiles.py (L537)).
Furthermore, on anki Version ⁨2.1.54, Python 3.9.10 Qt 6.3.1 PyQt 6.3.1, this function returns false, even when the dark mode is set.
2022-12-14 08:36:12 +01:00
58cd3cec42 Build changes for PR #7 2022-10-10 18:59:30 +02:00
a577e785be removed .DS_Store 2022-10-09 17:21:56 +02:00
ef413c20ee Implemented: config file, default open/close state, default split ratio, toolbar icon 2022-10-09 17:19:37 +02:00
30394c02d5 Allow customizing styles via a custom class
Previewer-specific styles can now be specified in card templates by
targeting the "editor-preview" class. E.g:
```
.editor-preview {
  font-size: 12px;
}
```

Closes #1
2022-10-08 00:22:16 +03:00
b41ac7ae1d Add light mode in addition to night mode
This utilizes Anki's theme_manager to determine if the default light version of a card should be used in the Editor Live Preview or if the night mode version should be used.
2022-08-03 19:14:08 +02:00
7 changed files with 184 additions and 29 deletions

32
.drone.yml Normal file
View File

@ -0,0 +1,32 @@
---
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

5
.gitignore vendored
View File

@ -165,3 +165,8 @@ tags
# End of https://www.toptal.com/developers/gitignore/api/vim,python
*.ankiaddon
.DS_Store
# 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 *
# 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

@ -6,9 +6,11 @@ from aqt.utils import *
from aqt.theme import theme_manager
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",
@ -17,7 +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)
if int(buildinfo.version.split(".")[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",
@ -26,64 +29,126 @@ class EditorPreview(object):
"js/reviewer.js",
]
def editor_init_hook(self, ed: editor.Editor):
ed.webview = AnkiWebView(title="editor_preview")
ed.editor_preview = AnkiWebView(title="editor_preview")
# This is taken out of clayout.py
ed.webview.stdHtml(
ed.editor_preview.stdHtml(
ed.mw.reviewer.revHtml(),
css=["css/reviewer.css"],
js=self.js,
context=ed,
)
if not config["showPreviewAutomatically"]:
ed.editor_preview.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))
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
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.addWidget(editor.web)
split.addWidget(editor.webview)
split.setStretchFactor(0, 0)
split.setStretchFactor(1, 1)
split.setStretchFactor(2, 1)
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):
b = editor.addButton(icon=None, cmd="_editor_toggle_preview", label='P', tip='Toggle Live Preview',
func=lambda o=editor: self.onEditorPreviewButton(o), disables=False
)
buttons.append(b)
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)
def onEditorPreviewButton(self, origin: editor.Editor):
if origin.webview.isHidden():
origin.webview.show()
if origin.editor_preview.isHidden():
origin.editor_preview.show()
else:
origin.webview.hide()
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")
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 = 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:
editor.webview.eval(self._obtainCardText(editor.note))
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()

6
src/config.json Normal file
View File

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

12
src/config.md Normal file
View File

@ -0,0 +1,12 @@
### Config
\- `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/>
<br/>

19
src/icons/file.svg Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 26.4.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 16 16" style="enable-background:new 0 0 16 16;" xml:space="preserve">
<style type="text/css">
.st0{stroke:#000000;stroke-width:0.5;stroke-miterlimit:10;}
</style>
<g>
<path class="st0" d="M8.7,14.1H2.2c-0.2,0-0.4-0.2-0.4-0.4V2.4c0-0.2,0.2-0.4,0.4-0.4h9.3c0.2,0,0.4,0.2,0.4,0.4v2.8
c0,0.2,0.2,0.4,0.4,0.4c0.2,0,0.4-0.2,0.4-0.4V2.4c0-0.7-0.6-1.3-1.3-1.3H2.2c-0.7,0-1.3,0.6-1.3,1.3v11.3c0,0.7,0.6,1.3,1.3,1.3
h6.5c0.2,0,0.4-0.2,0.4-0.4S9,14.1,8.7,14.1z"/>
<path class="st0" d="M15.4,14.2l-1.9-2c0.5-0.6,0.8-1.4,0.8-2.3c0-2-1.6-3.6-3.6-3.6S7.1,8,7.1,10s1.6,3.6,3.6,3.6
c0.8,0,1.6-0.3,2.2-0.7l1.9,2c0.1,0.1,0.2,0.1,0.3,0.1c0.1,0,0.2,0,0.3-0.1C15.6,14.7,15.6,14.4,15.4,14.2L15.4,14.2z M10.7,12.7
C9.2,12.7,8,11.5,8,10s1.2-2.7,2.7-2.7s2.7,1.2,2.7,2.7S12.2,12.7,10.7,12.7z M9.1,5.2c0-0.2-0.2-0.4-0.4-0.4H3.6
C3.4,4.8,3.2,5,3.2,5.2c0,0.2,0.2,0.4,0.4,0.4h5.1C9,5.6,9.1,5.4,9.1,5.2z M6.5,7.6c0-0.2-0.2-0.4-0.4-0.4H3.6
c-0.2,0-0.4,0.2-0.4,0.4S3.4,8,3.6,8h2.5C6.3,8,6.5,7.8,6.5,7.6z M3.6,9.6c-0.2,0-0.4,0.2-0.4,0.4c0,0.2,0.2,0.4,0.4,0.4h1.3
c0.2,0,0.4-0.2,0.4-0.4c0-0.2-0.2-0.4-0.4-0.4H3.6z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.4 KiB