forked from seung-lab/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rely on DOM focus to dispatch keyboard events
Previously, a single set of keyboard event handlers were added to the window object, and keyboard events were dispatched using a custom mechanism based on a global stack of keyboard handlers. With this change, the builtin focus mechanism is used instead, which is both simpler and more general. In order to ensure that the builtin keyboard focus remains on what the user expects, this change also introduces a a new mechanism for automatic focusing of elements.
- Loading branch information
Showing
4 changed files
with
88 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import debounce from 'lodash/debounce'; | ||
import {RefCounted} from 'neuroglancer/util/disposable'; | ||
import LinkedListOperations from 'neuroglancer/util/linked_list.0.ts'; | ||
|
||
class AutomaticFocusList { | ||
next0: AutomaticallyFocusedElement|null; | ||
prev0: AutomaticallyFocusedElement|null; | ||
|
||
constructor() { | ||
LinkedListOperations.initializeHead(<any>this); | ||
} | ||
} | ||
|
||
const automaticFocusList = new AutomaticFocusList(); | ||
|
||
const maybeUpdateFocus = debounce(() => { | ||
const {activeElement} = document; | ||
if (activeElement === null || activeElement === document.body) { | ||
const node = LinkedListOperations.front<AutomaticallyFocusedElement>(<any>automaticFocusList); | ||
if (node !== null) { | ||
node.element.focus(); | ||
} | ||
} | ||
}); | ||
|
||
window.addEventListener('focus', () => { | ||
maybeUpdateFocus(); | ||
}, true); | ||
|
||
window.addEventListener('blur', () => { | ||
maybeUpdateFocus(); | ||
}, true); | ||
|
||
export class AutomaticallyFocusedElement extends RefCounted { | ||
prev0: AutomaticallyFocusedElement|null = null; | ||
next0: AutomaticallyFocusedElement|null = null; | ||
|
||
constructor(public element: HTMLElement) { | ||
super(); | ||
element.tabIndex = -1; | ||
this.registerEventListener(element, 'mouseenter', () => { | ||
element.focus(); | ||
}); | ||
// Insert at the end of the list. | ||
LinkedListOperations.insertBefore(<any>automaticFocusList, this); | ||
this.registerEventListener(element, 'focus', () => { | ||
// Move to the beginning of the list. | ||
LinkedListOperations.pop<AutomaticallyFocusedElement>(this); | ||
LinkedListOperations.insertAfter(<any>automaticFocusList, this); | ||
}); | ||
maybeUpdateFocus(); | ||
} | ||
|
||
disposed() { | ||
super.disposed(); | ||
LinkedListOperations.pop<AutomaticallyFocusedElement>(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters