-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathcommandpalette.ts
176 lines (157 loc) · 4.6 KB
/
commandpalette.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* -----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { searchIcon } from '@jupyterlab/ui-components';
import { Message } from '@lumino/messaging';
import { CommandPalette, Panel, Widget } from '@lumino/widgets';
/**
* Class name identifying the input group with search icon.
*/
const SEARCH_ICON_GROUP_CLASS = 'jp-SearchIconGroup';
/**
* Wrap the command palette in a modal to make it more usable.
*/
export class ModalCommandPalette extends Panel {
constructor(options: ModalCommandPalette.IOptions) {
super();
this.addClass('jp-ModalCommandPalette');
this.addClass('jp-ThemedContainer');
this.id = 'modal-command-palette';
this.palette = options.commandPalette;
this._commandPalette.commands.commandExecuted.connect(() => {
if (this.isAttached && this.isVisible) {
this.hideAndReset();
}
});
// required to properly receive blur and focus events;
// selection of items with mouse may not work without this.
this.node.tabIndex = 0;
}
get palette(): CommandPalette {
return this._commandPalette;
}
set palette(value: CommandPalette) {
this._commandPalette = value;
if (!this.searchIconGroup) {
this._commandPalette.inputNode.insertAdjacentElement(
'afterend',
this.createSearchIconGroup()
);
}
this.addWidget(value);
this.hideAndReset();
}
attach(): void {
Widget.attach(this, document.body);
}
detach(): void {
Widget.detach(this);
}
/**
* Hide the modal command palette and reset its search.
*/
hideAndReset(): void {
this.hide();
this._commandPalette.inputNode.value = '';
this._commandPalette.refresh();
}
/**
* Handle incoming events.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'keydown':
this._evtKeydown(event as KeyboardEvent);
break;
case 'blur': {
// if the focus shifted outside of this DOM element, hide and reset.
if (
// focus went away from child element
this.node.contains(event.target as HTMLElement) &&
// and it did NOT go to another child element but someplace else
!this.node.contains(
(event as MouseEvent).relatedTarget as HTMLElement
)
) {
event.stopPropagation();
this.hideAndReset();
}
break;
}
case 'contextmenu':
event.preventDefault();
event.stopPropagation();
break;
default:
break;
}
}
/**
* Find the element with search icon group.
*/
protected get searchIconGroup(): HTMLDivElement | undefined {
return this._commandPalette.node.getElementsByClassName(
SEARCH_ICON_GROUP_CLASS
)[0] as HTMLDivElement;
}
/**
* Create element with search icon group.
*/
protected createSearchIconGroup(): HTMLDivElement {
const inputGroup = document.createElement('div');
inputGroup.classList.add(SEARCH_ICON_GROUP_CLASS);
searchIcon.render(inputGroup);
return inputGroup;
}
/**
* A message handler invoked on an `'after-attach'` message.
*/
protected onAfterAttach(msg: Message): void {
this.node.addEventListener('keydown', this, true);
this.node.addEventListener('contextmenu', this, true);
}
/**
* A message handler invoked on an `'after-detach'` message.
*/
protected onAfterDetach(msg: Message): void {
this.node.removeEventListener('keydown', this, true);
this.node.removeEventListener('contextmenu', this, true);
}
protected onBeforeHide(msg: Message): void {
document.removeEventListener('blur', this, true);
}
protected onAfterShow(msg: Message): void {
document.addEventListener('blur', this, true);
}
/**
* A message handler invoked on an `'activate-request'` message.
*/
protected onActivateRequest(msg: Message): void {
if (this.isAttached) {
this.show();
this._commandPalette.activate();
}
}
/**
* Handle the `'keydown'` event for the widget.
*/
protected _evtKeydown(event: KeyboardEvent): void {
// Check for escape key
switch (event.keyCode) {
case 27: // Escape.
event.stopPropagation();
event.preventDefault();
this.hideAndReset();
break;
default:
break;
}
}
private _commandPalette: CommandPalette;
}
export namespace ModalCommandPalette {
export interface IOptions {
commandPalette: CommandPalette;
}
}