From a3ed56c3deb1a286a7e84c86064fd33fc33e8f61 Mon Sep 17 00:00:00 2001 From: Brandon Rainey <83629154+brandonrainey@users.noreply.github.com> Date: Fri, 17 May 2024 00:06:33 -0400 Subject: 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 --- ext/js/display/search-display-controller.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'ext') 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}); } } -- cgit v1.2.3