Custom editor keymap
<10kB. Updated 2021-10-14. Only supports Anki 2.1.x.
Description
It has been pointed out to me that equivalent functionality is already available through https://ankiweb.net/shared/info/1899278645 and https://ankiweb.net/shared/info/396502676 so this addon is superfluous. Oh well, it is here now and functioning as far as I can tell,
Use at your own peril.
Allows customizing the editor keymap, i.e. what character(s) are inserted on the press of a specific key and modifiers. Example: configure
ArrowRight+Alt to insert "→" symbol.
- Configuration in Tools→Add-ons→Custom editor keymap→Config.
- The key is the javascript KeyboardEvent.key. The easiest way is to use https://keycode.info or similar to find key name (use event.key). <nop> as key will result in key-value pair being ignored.
- The value is any string (including HTML) that will be inserted. Empty string ("") will result in nothing being inserted and no further processing of the event in the javascript.
- The key to map must be first, then any modifiers (Shift, Ctrl, Alt, AltGraph, Meta) joined with "+", ex. ArrowLeft+Shift+Alt.
- Additional modifier HTML will insert the character(s) as HTML (default is text), i.e. <b>FAT</b> inserted as HTML will be rendered as FAT while inserted as text will rendered as <b>FAT</b>.
- Additional modifier is CMD will do a javascript eval() of the value
.
- Additional modifier FILE, which will interpret the value as a file name (path based in add-on directory) and use the file contents as value.
- Removing the key-value pair entirely will leave the "default"/normal mapping intact.
- There is minimal error checking of the config.
- Replacement is done in javascript in the editor fields (the addon injects javascript on editor_will_load_note). The key event will be processed by Anki before reaching the js (for instance mapping "b+Ctrl": "sample" will result in bold state being toggled before insertion of sample).
Sample config content
{
"<nop>": "",
"ArrowRight+Alt": "→",
"ArrowRight+Alt+Shift": "⇒",
"ArrowLeft+Alt": "←",
"ArrowLeft+Alt+Shift": "⇐",
"ArrowUp+Alt": "↑",
"ArrowUp+Alt+Shift": "⇑",
"ArrowDown+Alt": "↓",
"ArrowDown+Alt+Shift": "⇓",
"ArrowLeft+Ctrl+Alt+Shift": "↔",
"ArrowRight+Ctrl+Alt+Shift": "⇔",
"F7+CMD": "var t = document.activeElement.shadowRoot.innerHTML.match(/(?<={{c)\\\\d+(?=::)/g) || [\"0\"]; wrap(\"{{c\" + (Math.max.apply(null, t.map(i=>Number(i))) + 1) + \"::\", \"}}\");",
"X+Ctrl+Shift+Alt+CMD": "var els = document.activeElement.shadowRoot.querySelectorAll(\"div, p, span\"); els.forEach(el => { if(!el.hasAttributes()) { var par = el.parentNode; while (el.firstChild) par.insertBefore(el.firstChild, el); par.removeChild(el); } });"
}
CHANGELOG
- 2021-10-30 Added modifier FILE. Primary use case is combining with CMD to easier create more complex JS stored in a file with sensible formatting (the JSON of the config file doesn't allow line breaks). Note that the quoting of the file is not 100% robust (the file contents are stored in a variable using JS backtick string so using backticks in the file might mess things up). This is effectively the equivalent of "JS snippets", suggested way to write these are:
- Install AnkiWebView Inspector (https://ankiweb.net/shared/info/31746032)
- Develop and test the script in the inspector console (no need to restart Anki, try parts etc.)
- Copy-paste into file in CEK add-on directory
- Insert shortcut with FILE and CMD modifiers and the filename as value
- 2021-10-13 Added sample js not because it changes the addon but I thought it may be useful for others that want to strip unnecessary/blank div, p and span tags that gets inserted sometimes (only strips those tags that have no attributes set and inserts the content in the parent) - use at your own risk.
- 2021-10-08 Added modifier CMD.
Download
As add-ons are programs downloaded from the internet, they are
potentially
malicious. You should only download add-ons you trust.
Supported Anki versions:
- 2.1.46+ (updated 2021-10-14)
To download this add-on, please copy and paste the following code
into Anki 2.1:
617159433
If you were linked to this page from the internet, please open Anki
on
your computer, go to the
Tools menu and then Add-ons>Browse & Install to paste in
the
code.
All Anki 2.1.x
Add-Ons
Contact Author
Reviews
on
1633618736
Great add-on!