From 5bb1b5ef35118922f8c49a5cfebdbd8d81d1ba92 Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 23 Nov 2020 12:09:30 +0300 Subject: [PATCH] dedup code for modals in popup --- js/dom.js | 31 +++++++++--- popup.html | 8 +-- popup/popup.css | 5 -- popup/popup.js | 130 +++++++++++++++++++++--------------------------- 4 files changed, 84 insertions(+), 90 deletions(-) diff --git a/js/dom.js b/js/dom.js index 74649ab4..339a793d 100644 --- a/js/dom.js +++ b/js/dom.js @@ -387,9 +387,10 @@ function focusAccessibility() { } /** - * Switches to the next/previous keyboard-focusable element + * Switches to the next/previous keyboard-focusable element. + * Doesn't check `visibility` or `display` via getComputedStyle for simplicity. * @param {HTMLElement} rootElement - * @param {Number} step - for exmaple 1 or -1 + * @param {Number} step - for exmaple 1 or -1 (or 0 to focus the first focusable el in the box) * @returns {HTMLElement|false|undefined} - * HTMLElement: focus changed, * false: focus unchanged, @@ -397,16 +398,15 @@ function focusAccessibility() { */ function moveFocus(rootElement, step) { const elements = [...rootElement.getElementsByTagName('*')]; - const activeIndex = Math.max(0, elements.indexOf(document.activeElement)); + const activeEl = document.activeElement; + const activeIndex = step ? Math.max(step < 0 ? 0 : -1, elements.indexOf(activeEl)) : -1; const num = elements.length; - const {activeElement} = document; + if (!step) step = 1; for (let i = 1; i < num; i++) { - const elementIndex = (activeIndex + i * step + num) % num; - // we don't use positive tabindex so we stop at any valid value - const el = elements[elementIndex]; + const el = elements[(activeIndex + i * step + num) % num]; if (!el.disabled && el.tabIndex >= 0) { el.focus(); - return activeElement !== el && el; + return activeEl !== el && el; } } } @@ -462,3 +462,18 @@ function setupLivePrefs( } } } + +/* exported getEventKeyName */ +/** + * @param {KeyboardEvent} e + * @param {boolean} [letterAsCode] - use locale-independent KeyA..KeyZ for single-letter chars + */ +function getEventKeyName(e, letterAsCode) { + const mods = + (e.shiftKey ? 'Shift-' : '') + + (e.ctrlKey ? 'Ctrl-' : '') + + (e.altKey ? 'Alt-' : '') + + (e.metaKey ? 'Meta-' : ''); + return (mods === e.key + '-' ? '' : mods) + + (e.key.length === 1 && letterAsCode ? e.code : e.key); +} diff --git a/popup.html b/popup.html index 7c7a10fe..f41f4910 100644 --- a/popup.html +++ b/popup.html @@ -25,22 +25,22 @@
- + - + - +
-