Last active
February 15, 2025 23:47
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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