Debian Codegraph @

js/keyhandler.ts

raw
1 import { BasicKeyHandler, DataGrid } from '@lumino/datagrid';
2
3 /**
4 * ipydatagrid specific KeyHandler class for handling
5 * key events. Only polymorphed events are listed here,
6 * the rest are ihnerited and used as is from the
7 * parent class.
8 */
9 export class KeyHandler extends BasicKeyHandler {
10 constructor() {
11 super();
12 }
13
14 /**
15 * Handle the `'Escape'` key press for the data grid.
16 *
17 * @param grid - The data grid of interest.
18 *
19 * @param event - The keyboard event of interest.
20 */
21 protected onEscape(grid: DataGrid, event: KeyboardEvent): void {
22 // Bail if no selection model exists
23 if (!grid.selectionModel) {
24 return;
25 }
26
27 // Returns the first selection from the model, if selections exist
28 const selections = grid.selectionModel.selections().next();
29
30 if (selections) {
31 grid.selectionModel.clear();
32 } else {
33 // Chrome's implementation of focus() requires the
34 // target element to have the tabIndex attribute set.
35 // https://bugs.chromium.org/p/chromium/issues/detail?id=467043
36 if (document.body.tabIndex === -1) {
37 document.body.tabIndex = 0;
38 }
39 document.body.focus();
40 }
41 }
42 }