From 3a70346eb37ce234e49460ed0c97acb3affe68cc Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 03:26:24 +0300 Subject: fix various unwanted focus issues on search page Don't focus input if a modifier or specific keys are pressed --- ext/bg/js/search.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 5c0bffed..f0bbf203 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -96,7 +96,34 @@ class DisplaySearch extends Display { } onKeyDown(e) { - if (!super.onKeyDown(e)) { + const key = Display.getKeyFromEvent(e); + + let activeModifierMap = { + 'Control': e.ctrlKey, + 'Meta': e.metaKey + }; + // true if no known modifier is pressed + activeModifierMap[undefined] = !Object.values(activeModifierMap).includes(true); + + const ignoreKeys = { + undefined: ['Tab'], + 'Control': ['C', 'A', 'V'], + 'Meta': ['C', 'A', 'V'], + 'OS': [], + 'Alt': [], + 'Shift': [] + } + + let preventFocus = false; + for (const [modifier, keys] of Object.entries(ignoreKeys)) { + const modifierActive = activeModifierMap[modifier]; + if (key === modifier || (modifierActive && keys.includes(key))) { + preventFocus = true; + break; + } + } + + if (!super.onKeyDown(e) && !preventFocus) { this.query.focus({preventScroll: true}); } } -- cgit v1.2.3