Skip to content

Commit abc6e9c

Browse files
committed
Fix supertab freezing the app
Removes the infinite, app-freezing while-loop and handles the now-reachable errors.
1 parent 02e7a9e commit abc6e9c

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

ts/services/addGlobalKeyboardShortcuts.ts

+16-23
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function addGlobalKeyboardShortcuts(): void {
4242
return;
4343
}
4444

45-
// Super tab :)
45+
// Super tab :(
4646
if (
4747
(commandOrCtrl && key === 'F6') ||
4848
(commandOrCtrl && !shiftKey && (key === 't' || key === 'T'))
@@ -55,10 +55,7 @@ export function addGlobalKeyboardShortcuts(): void {
5555
const focusedIndexes: Array<number> = [];
5656

5757
targets.forEach((target, index) => {
58-
if (
59-
(focusedElement != null && target === focusedElement) ||
60-
target.contains(focusedElement)
61-
) {
58+
if (target.contains(focusedElement)) {
6259
focusedIndexes.push(index);
6360
}
6461
});
@@ -76,20 +73,13 @@ export function addGlobalKeyboardShortcuts(): void {
7673
const focusedIndex = focusedIndexes.at(-1) ?? -1;
7774

7875
const lastIndex = targets.length - 1;
79-
const increment = shiftKey ? -1 : 1;
8076

81-
let index;
82-
if (focusedIndex < 0 || focusedIndex >= lastIndex) {
83-
index = 0;
77+
let index: number | undefined;
78+
if (focusedIndex < 0) {
79+
index = shiftKey ? lastIndex : 0;
8480
} else {
85-
index = focusedIndex + increment;
86-
}
87-
88-
while (!targets[index]) {
89-
index += increment;
90-
if (index > lastIndex || index < 0) {
91-
index = 0;
92-
}
81+
const increment = shiftKey ? lastIndex : 1;
82+
index = (focusedIndex + increment) % targets.length;
9383
}
9484

9585
const node = targets[index];
@@ -98,12 +88,15 @@ export function addGlobalKeyboardShortcuts(): void {
9888
if (firstFocusableElement) {
9989
firstFocusableElement.focus();
10090
} else {
101-
const nodeInfo = Array.from(node.attributes)
102-
.map(attr => `${attr.name}=${attr.value}`)
103-
.join(',');
104-
log.warn(
105-
`supertab: could not find focus for DOM node ${node.nodeName}<${nodeInfo}>`
106-
);
91+
if (node) {
92+
const nodeInfo = Array.from(node.attributes)
93+
.map(attr => `${attr.name}=${attr.value}`)
94+
.join(',');
95+
log.warn(
96+
`supertab: could not find focus for DOM node ${node.nodeName}<${nodeInfo}>`
97+
);
98+
}
99+
107100
window.enterMouseMode();
108101
const { activeElement } = document;
109102
if (

ts/util/focusableSelectors.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export const focusableSelector = focusableSelectors.join(', ');
3636
* is focusable.
3737
*/
3838
export function matchOrQueryFocusable(
39-
element: HTMLElement
40-
): HTMLElement | null {
41-
return element.matches(focusableSelector)
39+
element: HTMLElement | null | undefined
40+
): HTMLElement | null | undefined {
41+
return element?.matches(focusableSelector)
4242
? element
43-
: element.querySelector(focusableSelector);
43+
: element?.querySelector(focusableSelector);
4444
}

0 commit comments

Comments
 (0)