It might however still be nice to host to github or another git hosting service that way people could contribute, Iâm not certain whether my changes are still helpful since I changed quite a lot and had no need for symbols but with the changes I made to your code snippets are now reloaded live, i.e. if you change a file in your user_files those changes are reflected immediately in the editor. I wasnât able to get this to work for the config file itself, although Iâm not certain itâs even possible. What is missing in my code and I do believe in yours is the ability to undo changes. Have you got any idea how to do that?
In any case here are my changes, take the code from them as you like or if you open a git repository I could see if I can open a pull request:
init.py
import os
import sys
from collections import namedtuple
from functools import partial
from anki.hooks import addHook
from aqt import gui_hooks, mw, utils
from aqt.qt import QCursor, QKeySequence, QMenu
JS_SECTION = "JS snippets"
JS_MENU_SCUT = None
# Build the JS commands from pre/post and file contents
def build_js(snippet) -> str:
file = ""
pre = snippet.pre
post = snippet.post
fname = snippet.fname
file = ""
if fname:
for f in fname.split(";"):
path = os.path.join(os.path.dirname(__file__), f"user_files/{f}")
with open(path) as fh:
file += fh.read().strip()
# return pre + file + post
return ";".join([pre, file, post])
# Add shortcuts to editor
def setup_cuts(snippets, scs, edit):
def fn(s):
edit.web.eval(build_js(s))
return
scs += [(s.cut, partial(fn, s), True) for s in snippets]
menu = QMenu("Custom", edit.web)
map(lambda s: menu.addAction(s.name, partial(fn, s), s.cut), snippets)
menu.popup(QCursor.pos())
# DEBUG:
# for i in scs:
# if isinstance(i[0], QKeySequence):
# func = inspect.getsourcelines(i[1])[0]
# print(f"INFO:{func}")
# print(i)
# print("\n")
# Only set up if not already loaded
if not 2065559429 in sys.modules:
config = mw.addonManager.getConfig(__name__)
# Setup base shortcuts
JS_MENU_SCUT = config["Snippet menu shortcut"]
config_snippets = config["Snippets"]
Snippet = namedtuple("Snippet", ["name", "cut", "fname", "pre", "post"])
def get_snippets():
return [
Snippet(sc["Name"], sc["Shortcut"], sc["File"], sc["Pre"], sc["Post"])
for sc in mw.addonManager.getConfig(__name__)["Snippets"]
]
# addHook(
# "EditorWebView.contextMenuEvent", mouse_context
# ) # Legacy hook but it does fire
# gui_hooks.editor_will_show_context_menu.append(on_pop_context) # New style hooks doesn't fire until Image Occlusion Enhanced is fixed
gui_hooks.editor_did_init_shortcuts.append(partial(setup_cuts, get_snippets()))
Config File
{
"name": "js_snippets",
"config": {
"Snippet context submenu": "false",
"Snippet menu shortcut": "Ctrl+J",
"Snippets": [
{
"File": "test.js",
"Name": "Test",
"Post": "",
"Pre": "",
"Shortcut": "Alt+T"
},
{
"File": "util.js;bracket.js",
"Name": "Bracket",
"Post": "",
"Pre": "",
"Shortcut": "Alt+B"
},
{
"File": "util.js;jaxc.js",
"Name": "Jaxc",
"Post": "",
"Pre": "",
"Shortcut": "Alt+J"
},
{
"File": "util.js;ht.js",
"Name": "Highlight Text",
"Post": "",
"Pre": "",
"Shortcut": "Alt+H,T"
},
{
"File": "util.js;hm.js",
"Name": "Highlight Math",
"Post": "",
"Pre": "",
"Shortcut": "Alt+H,M"
},
{
"File": "util.js;math.js",
"Name": "Math",
"Post": "",
"Pre": "",
"Shortcut": "Alt+M"
},
{
"File": "color.js",
"Name": "Color Green",
"Post": "setColor('green')",
"Pre": "",
"Shortcut": "Ctrl+G,G"
},
{
"File": "color.js",
"Name": "Color Black",
"Post": "setColor('black')",
"Pre": "",
"Shortcut": "Ctrl+G,B"
}
],
"Symbol context submenu": "true",
"Symbol menu shortcut": "Ctrl+S",
"Symbols": [
{ "HTML": "false", "Shortcut": "Alt+Left", "Symbol": "\u2190" },
{ "HTML": "false", "Shortcut": "Alt+Right", "Symbol": "\u2192" },
{ "HTML": "false", "Shortcut": "Alt+Up", "Symbol": "\u2191" },
{ "HTML": "false", "Shortcut": "Alt+Down", "Symbol": "\u2193" }
]
}
}