Compare commits
	
		
			4 Commits
		
	
	
		
			kvn1351-mu
			...
			v20230324-
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						af073e0498
	
				 | 
					
					
						|||
| 
						
						
							
						
						4c966c1f5f
	
				 | 
					
					
						|||
| 
						
						
							
						
						8e2815bc87
	
				 | 
					
					
						|||
| 
						
						
							
						
						58cd3cec42
	
				 | 
					
					
						
							
								
								
									
										31
									
								
								.drone.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								.drone.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					---
 | 
				
			||||||
 | 
					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
 | 
				
			||||||
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -166,3 +166,6 @@ tags
 | 
				
			|||||||
# End of https://www.toptal.com/developers/gitignore/api/vim,python
 | 
					# End of https://www.toptal.com/developers/gitignore/api/vim,python
 | 
				
			||||||
*.ankiaddon
 | 
					*.ankiaddon
 | 
				
			||||||
.DS_Store
 | 
					.DS_Store
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Stores actual addon config if the src directory is symlinked into an anki installation during development
 | 
				
			||||||
 | 
					src/meta.json
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								build.sh
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								build.sh
									
									
									
									
									
								
							@@ -6,6 +6,6 @@ if [ -d src/__pycache__ ]; then
 | 
				
			|||||||
fi
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
cd src
 | 
					cd src
 | 
				
			||||||
zip -r ../editor-preview.ankiaddon *
 | 
					zip -r ../editor-preview.ankiaddon --exclude meta.json -- *
 | 
				
			||||||
cd ..
 | 
					cd ..
 | 
				
			||||||
unzip -l  editor-preview.ankiaddon
 | 
					unzip -l  editor-preview.ankiaddon
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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",
 | 
				
			||||||
@@ -18,7 +19,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)
 | 
				
			||||||
        if int(buildinfo.version.split(".")[2]) < 45: # < 2.1.45
 | 
					        if int(buildinfo.version.split(".")[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",
 | 
				
			||||||
@@ -27,63 +28,93 @@ 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.webview = 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
 | 
				
			||||||
        ed.webview.stdHtml(
 | 
					        ed.editor_preview.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.webview.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):
 | 
				
			||||||
 | 
					        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):
 | 
					    def _inject_splitter(self, editor: editor.Editor):
 | 
				
			||||||
        layout = editor.outerLayout
 | 
					        layout = editor.outerLayout
 | 
				
			||||||
        split = QSplitter()
 | 
					 | 
				
			||||||
        split.setOrientation(Qt.Vertical)
 | 
					 | 
				
			||||||
        web_index = layout.indexOf(editor.web)
 | 
					        web_index = layout.indexOf(editor.web)
 | 
				
			||||||
        layout.removeWidget(editor.web)
 | 
					        layout.removeWidget(editor.web)
 | 
				
			||||||
        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)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        split = self._get_splitter(editor)
 | 
				
			||||||
 | 
					        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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def onEditorPreviewButton(self, origin: editor.Editor):
 | 
					    def onEditorPreviewButton(self, origin: editor.Editor):
 | 
				
			||||||
        if origin.webview.isHidden():
 | 
					        if origin.editor_preview.isHidden():
 | 
				
			||||||
            origin.webview.show()
 | 
					            origin.editor_preview.show()
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            origin.webview.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"
 | 
				
			||||||
@@ -92,6 +123,7 @@ class EditorPreview(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def onedit_hook(self, editor, origin):
 | 
					    def onedit_hook(self, editor, origin):
 | 
				
			||||||
        if editor.note == origin:
 | 
					        if editor.note == origin:
 | 
				
			||||||
            editor.webview.eval(self._obtainCardText(editor.note))
 | 
					            editor.editor_preview.eval(self._obtainCardText(editor.note))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
eprev = EditorPreview()
 | 
					eprev = EditorPreview()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1 +1,5 @@
 | 
				
			|||||||
{"showPreviewAutomatically": true, "splitRatio": "4:1"}
 | 
					{
 | 
				
			||||||
 | 
					  "showPreviewAutomatically": true,
 | 
				
			||||||
 | 
					  "splitRatio": "1:1",
 | 
				
			||||||
 | 
					  "location": "below"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,5 +2,8 @@
 | 
				
			|||||||
\- `showPreviewAutomatically` [boolean (true | false)]:<br/>
 | 
					\- `showPreviewAutomatically` [boolean (true | false)]:<br/>
 | 
				
			||||||
   Defines if the preview window should show up automatically as you enter the Editor (default: true)<br/><br/>
 | 
					   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/>
 | 
				
			||||||
   Defines the default split ratio of the main view and preview view (default: 4:1)
 | 
					   Defines the default split ratio of the main view and preview view (default: 1:1)<br/>
 | 
				
			||||||
 | 
					<br/>
 | 
				
			||||||
 | 
					\- `location` [string (above | below | left | right)]:<br/>
 | 
				
			||||||
 | 
					   Defines where to render the preview (default: below)
 | 
				
			||||||
<br/>
 | 
					<br/>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user