From abc6e9cf0f7524f9be720dcdaeda4e7a46d3eb30 Mon Sep 17 00:00:00 2001 From: Wallunen <44848747+Wallunen@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:26:54 +0300 Subject: [PATCH] Fix supertab freezing the app Removes the infinite, app-freezing while-loop and handles the now-reachable errors. --- ts/services/addGlobalKeyboardShortcuts.ts | 39 ++++++++++------------- ts/util/focusableSelectors.ts | 8 ++--- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/ts/services/addGlobalKeyboardShortcuts.ts b/ts/services/addGlobalKeyboardShortcuts.ts index e151b0045c..c9b937a31d 100644 --- a/ts/services/addGlobalKeyboardShortcuts.ts +++ b/ts/services/addGlobalKeyboardShortcuts.ts @@ -42,7 +42,7 @@ export function addGlobalKeyboardShortcuts(): void { return; } - // Super tab :) + // Super tab :( if ( (commandOrCtrl && key === 'F6') || (commandOrCtrl && !shiftKey && (key === 't' || key === 'T')) @@ -55,10 +55,7 @@ export function addGlobalKeyboardShortcuts(): void { const focusedIndexes: Array = []; targets.forEach((target, index) => { - if ( - (focusedElement != null && target === focusedElement) || - target.contains(focusedElement) - ) { + if (target.contains(focusedElement)) { focusedIndexes.push(index); } }); @@ -76,20 +73,13 @@ export function addGlobalKeyboardShortcuts(): void { const focusedIndex = focusedIndexes.at(-1) ?? -1; const lastIndex = targets.length - 1; - const increment = shiftKey ? -1 : 1; - let index; - if (focusedIndex < 0 || focusedIndex >= lastIndex) { - index = 0; + let index: number | undefined; + if (focusedIndex < 0) { + index = shiftKey ? lastIndex : 0; } else { - index = focusedIndex + increment; - } - - while (!targets[index]) { - index += increment; - if (index > lastIndex || index < 0) { - index = 0; - } + const increment = shiftKey ? lastIndex : 1; + index = (focusedIndex + increment) % targets.length; } const node = targets[index]; @@ -98,12 +88,15 @@ export function addGlobalKeyboardShortcuts(): void { if (firstFocusableElement) { firstFocusableElement.focus(); } else { - const nodeInfo = Array.from(node.attributes) - .map(attr => `${attr.name}=${attr.value}`) - .join(','); - log.warn( - `supertab: could not find focus for DOM node ${node.nodeName}<${nodeInfo}>` - ); + if (node) { + const nodeInfo = Array.from(node.attributes) + .map(attr => `${attr.name}=${attr.value}`) + .join(','); + log.warn( + `supertab: could not find focus for DOM node ${node.nodeName}<${nodeInfo}>` + ); + } + window.enterMouseMode(); const { activeElement } = document; if ( diff --git a/ts/util/focusableSelectors.ts b/ts/util/focusableSelectors.ts index a683fbcb10..44cd3c8af8 100644 --- a/ts/util/focusableSelectors.ts +++ b/ts/util/focusableSelectors.ts @@ -36,9 +36,9 @@ export const focusableSelector = focusableSelectors.join(', '); * is focusable. */ export function matchOrQueryFocusable( - element: HTMLElement -): HTMLElement | null { - return element.matches(focusableSelector) + element: HTMLElement | null | undefined +): HTMLElement | null | undefined { + return element?.matches(focusableSelector) ? element - : element.querySelector(focusableSelector); + : element?.querySelector(focusableSelector); }