diff options
author | Brandon Rainey <83629154+brandonrainey@users.noreply.github.com> | 2024-05-17 00:06:33 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-17 04:06:33 +0000 |
commit | a3ed56c3deb1a286a7e84c86064fd33fc33e8f61 (patch) | |
tree | d14b47e509adde3430c7f4b1fb57b951d8299662 /ext/js | |
parent | 6ade1eb2e0d8037472631a5e6718c33621303e31 (diff) |
refactored onKeyDown to be easier to read, and added test (#943)
* refactored onKeyDown to be easier to read, and added test to ensure preserved behavior
* added new keypress events to test
* refactored test to call method directly from SearchDisplayController, removed second test, removed old copyright
* added test for invalid keys, split keyboard events into valid and invalid
* added crtl + backspace as valid keypress, added 2 new invaid keypresses to test
Diffstat (limited to 'ext/js')
-rw-r--r-- | ext/js/display/search-display-controller.js | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/ext/js/display/search-display-controller.js b/ext/js/display/search-display-controller.js index 9b2311d1..e63b96e8 100644 --- a/ext/js/display/search-display-controller.js +++ b/ext/js/display/search-display-controller.js @@ -154,16 +154,15 @@ export class SearchDisplayController { * @param {KeyboardEvent} e */ _onKeyDown(e) { - const {activeElement} = document; - if ( - activeElement !== this._queryInput && - !this._isElementInput(activeElement) && - (!e.ctrlKey || e.key === 'Backspace') && - !e.metaKey && - !e.altKey && - (e.key.length === 1 || e.key === 'Backspace') && - e.key !== ' ' - ) { + const activeElement = document.activeElement; + + const isInputField = this._isElementInput(activeElement); + const isAllowedKey = e.key.length === 1 || e.key === 'Backspace'; + const isModifierKey = e.ctrlKey || e.metaKey || e.altKey; + const isSpaceKey = e.key === ' '; + const isCtrlBackspace = e.ctrlKey && e.key === 'Backspace'; + + if (!isInputField && (!isModifierKey || isCtrlBackspace) && isAllowedKey && !isSpaceKey) { this._queryInput.focus({preventScroll: true}); } } |