aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-14 20:56:18 -0500
committerGitHub <noreply@github.com>2021-01-14 20:56:18 -0500
commit84d3af0f8d26e06efb01e47a504d510f22caa970 (patch)
tree0dd7306bbe75d52526ac037562d9e79a33a939ec /ext/mixed
parent39741bf1d2336091a4d3592ea9ad037ef1f47f79 (diff)
Hotkeys (#1236)
* Set up hotkey inputs * Improve key display * Add setInput * Add KeyboardShortcutController * Update how display handles hotkeys
Diffstat (limited to 'ext/mixed')
-rw-r--r--ext/mixed/js/display.js50
1 files changed, 22 insertions, 28 deletions
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index e361a3a1..45019039 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -132,23 +132,6 @@ class Display extends EventDispatcher {
['playAudio', () => { this._playAudioCurrent(); }],
['copyHostSelection', () => this._copyHostSelection()]
]);
- this.registerHotkeys([
- {key: 'Escape', modifiers: [], action: 'close'},
- {key: 'PageUp', modifiers: ['alt'], action: 'previousEntry3'},
- {key: 'PageDown', modifiers: ['alt'], action: 'nextEntry3'},
- {key: 'End', modifiers: ['alt'], action: 'lastEntry'},
- {key: 'Home', modifiers: ['alt'], action: 'firstEntry'},
- {key: 'ArrowUp', modifiers: ['alt'], action: 'previousEntry'},
- {key: 'ArrowDown', modifiers: ['alt'], action: 'nextEntry'},
- {key: 'KeyB', modifiers: ['alt'], action: 'historyBackward'},
- {key: 'KeyF', modifiers: ['alt'], action: 'historyForward'},
- {key: 'KeyK', modifiers: ['alt'], action: 'addNoteKanji'},
- {key: 'KeyE', modifiers: ['alt'], action: 'addNoteTermKanji'},
- {key: 'KeyR', modifiers: ['alt'], action: 'addNoteTermKana'},
- {key: 'KeyP', modifiers: ['alt'], action: 'playAudio'},
- {key: 'KeyV', modifiers: ['alt'], action: 'viewNote'},
- {key: 'KeyC', modifiers: ['ctrl'], action: 'copyHostSelection'}
- ]);
this.registerMessageHandlers([
['setMode', {async: false, handler: this._onMessageSetMode.bind(this)}]
]);
@@ -313,6 +296,7 @@ class Display extends EventDispatcher {
this._options = options;
this._ankiFieldTemplates = templates;
+ this._updateHotkeys(options);
this._updateDocumentOptions(options);
this._updateTheme(options.general.popupTheme);
this.setCustomCss(options.general.customPopupCss);
@@ -401,17 +385,6 @@ class Display extends EventDispatcher {
}
}
- registerHotkeys(hotkeys) {
- for (const {key, modifiers, action} of hotkeys) {
- let handlers = this._hotkeys.get(key);
- if (typeof handlers === 'undefined') {
- handlers = [];
- this._hotkeys.set(key, handlers);
- }
- handlers.push({modifiers: new Set(modifiers), action});
- }
- }
-
registerMessageHandlers(handlers) {
for (const [name, handlerInfo] of handlers) {
this._messageHandlers.set(name, handlerInfo);
@@ -1902,4 +1875,25 @@ class Display extends EventDispatcher {
height = Math.max(Math.max(0, handleSize.height), height);
await this._invokeOwner('setFrameSize', {width, height});
}
+
+ _registerHotkey(key, modifiers, action) {
+ if (!this._actions.has(action)) { return false; }
+
+ let handlers = this._hotkeys.get(key);
+ if (typeof handlers === 'undefined') {
+ handlers = [];
+ this._hotkeys.set(key, handlers);
+ }
+ handlers.push({modifiers: new Set(modifiers), action});
+ return true;
+ }
+
+ _updateHotkeys(options) {
+ this._hotkeys.clear();
+
+ for (const {action, key, modifiers, scopes, enabled} of options.inputs.hotkeys) {
+ if (!enabled || !scopes.includes(this._pageType)) { continue; }
+ this._registerHotkey(key, modifiers, action);
+ }
+ }
}