Skip to content

Commit 34fc46c

Browse files
committed
update the focus manager to rely on document.activeElement rather than tracking the currently focused element
1 parent 63e238f commit 34fc46c

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/devtools/utils/table-cell-editor.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class JSONEditor implements ICellEditorComp {
119119
this.editor.textarea.style.cursor = "text";
120120

121121
this.focusManager = new EditorFocusManager(
122-
this.editor.textarea,
122+
editorContainer,
123123
this.cancelBtn,
124124
this.saveBtn,
125125
);
@@ -260,45 +260,51 @@ export class JSONEditor implements ICellEditorComp {
260260
* in the popup
261261
*/
262262
class EditorFocusManager {
263-
elements: HTMLElement[];
264-
focusIndex: number | null;
265263
constructor(
266-
textarea: HTMLTextAreaElement,
267-
cancel: HTMLButtonElement,
268-
save: HTMLButtonElement,
269-
) {
270-
this.elements = [textarea, cancel, save];
271-
this.focusIndex = null;
264+
public editorContainer: HTMLDivElement,
265+
public cancel: HTMLButtonElement,
266+
public save: HTMLButtonElement,
267+
) {}
268+
269+
get elements(): HTMLElement[] {
270+
return [this.editorContainer, this.cancel, this.save];
272271
}
273272

274273
focusNext() {
275-
if (
276-
this.focusIndex === null ||
277-
this.focusIndex >= this.elements.length - 1
278-
) {
279-
this.focusElement(0);
280-
} else {
281-
const idx = Math.max(this.focusIndex + 1, 0);
282-
this.focusElement(idx);
274+
if (document.activeElement instanceof HTMLElement) {
275+
const idx = this.elements.indexOf(document.activeElement);
276+
if (idx >= 0) {
277+
const next = idx >= this.elements.length - 1 ? 0 : idx + 1;
278+
this.focusElement(this.elements[next]);
279+
return;
280+
}
283281
}
282+
this.focusElement(this.editorContainer);
284283
}
285284

286285
focusPrevious() {
287-
if (this.focusIndex === null || this.focusIndex <= 0) {
288-
this.focusElement(this.elements.length - 1);
289-
} else {
290-
const idx = Math.min(this.focusIndex - 1, this.elements.length - 1);
291-
this.focusElement(idx);
286+
if (document.activeElement instanceof HTMLElement) {
287+
const idx = this.elements.indexOf(document.activeElement);
288+
if (idx >= 0) {
289+
const prev = idx <= 0 ? this.elements.length - 1 : idx - 1;
290+
this.focusElement(this.elements[prev]);
291+
return;
292+
}
292293
}
294+
this.focusElement(this.editorContainer);
293295
}
294296

295-
private focusElement(idx: number) {
296-
const elt = this.elements[idx];
297-
elt.focus();
298-
if (elt instanceof HTMLTextAreaElement) {
299-
elt.select();
297+
private focusElement(elt: HTMLElement) {
298+
if (elt === this.editorContainer) {
299+
const textarea =
300+
this.editorContainer.shadowRoot?.querySelector("textarea");
301+
if (textarea) {
302+
textarea.focus();
303+
textarea.select();
304+
}
305+
} else {
306+
elt.focus();
300307
}
301-
this.focusIndex = idx;
302308
}
303309
}
304310

0 commit comments

Comments
 (0)