From 84d3af0f8d26e06efb01e47a504d510f22caa970 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 14 Jan 2021 20:56:18 -0500 Subject: Hotkeys (#1236) * Set up hotkey inputs * Improve key display * Add setInput * Add KeyboardShortcutController * Update how display handles hotkeys --- ext/mixed/js/display.js | 50 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) (limited to 'ext/mixed/js') 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); + } + } } -- cgit v1.2.3