Skip to content

Instantly share code, notes, and snippets.

@hikaru-y
Last active February 15, 2025 23:47
Show Gist options
  • Save hikaru-y/1140d8c7acecae5c1197cba83b96bc7b to your computer and use it in GitHub Desktop.
Save hikaru-y/1140d8c7acecae5c1197cba83b96bc7b to your computer and use it in GitHub Desktop.
A small Anki add-on that enables indentation with the Tab key and outdentation with Shift+Tab in the editor.
from aqt import gui_hooks, mw
from aqt.editor import Editor
from aqt.webview import WebContent
def on_webview_will_set_content(web_content: WebContent, context: object) -> None:
if isinstance(context, Editor):
web_content.js.append(f"/_addons/{__name__.split('.')[0]}/indent_with_tab.js")
assert mw
mw.addonManager.setWebExports(__name__, r".+\.js")
gui_hooks.webview_will_set_content.append(on_webview_will_set_content)
document.addEventListener("keydown", (event) => {
if (!event.target?.closest(".editor-field")) {
return;
}
if (event.key === "Tab") {
if (event.ctrlKey) {
return;
}
event.preventDefault();
if (event.shiftKey) {
document.execCommand("outdent");
} else {
document.execCommand("indent");
}
}
});
{
"name": "Indent with Tab",
"package": "indent_with_tab"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment