forked from jupyterlab/jupyterlab
/
frontend.ts
319 lines (283 loc) · 8.9 KB
/
frontend.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { CommandLinker } from '@jupyterlab/apputils';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { ServiceManager } from '@jupyterlab/services';
import { IIterator } from '@phosphor/algorithm';
import { Application, IPlugin } from '@phosphor/application';
import { Token } from '@phosphor/coreutils';
import { Widget } from '@phosphor/widgets';
/**
* The type for all JupyterFrontEnd application plugins.
*
* @typeparam T - The type that the plugin `provides` upon being activated.
*/
export type JupyterFrontEndPlugin<T> = IPlugin<JupyterFrontEnd, T>;
/**
* The base Jupyter front-end application class.
*
* @typeparam `T` - The `shell` type. Defaults to `JupyterFrontEnd.IShell`.
*
* #### Notes
* This type is useful as a generic application against which front-end plugins
* can be authored. It inherits from the phosphor `Application`.
*/
export abstract class JupyterFrontEnd<
T extends JupyterFrontEnd.IShell = JupyterFrontEnd.IShell
> extends Application<T> {
/**
* Construct a new JupyterFrontEnd object.
*/
constructor(options: JupyterFrontEnd.IOptions<T>) {
super(options);
// The default restored promise if one does not exist in the options.
const restored = new Promise<void>(resolve => {
requestAnimationFrame(() => {
resolve();
});
});
this.commandLinker =
options.commandLinker || new CommandLinker({ commands: this.commands });
this.docRegistry = options.docRegistry || new DocumentRegistry();
this.restored =
options.restored ||
this.started.then(() => restored).catch(() => restored);
this.serviceManager = options.serviceManager || new ServiceManager();
this.commands.addCommand(Private.CONTEXT_MENU_INFO, {
label: 'Shift+Right Click for Browser Menu',
isEnabled: () => false,
execute: () => void 0
});
this.contextMenu.addItem({
command: Private.CONTEXT_MENU_INFO,
selector: 'body',
rank: Infinity
});
}
/**
* The name of this Jupyter front-end application.
*/
abstract readonly name: string;
/**
* A namespace/prefix plugins may use to denote their provenance.
*/
abstract readonly namespace: string;
/**
* The version of this Jupyter front-end application.
*/
abstract readonly version: string;
/**
* The command linker used by the application.
*/
readonly commandLinker: CommandLinker;
/**
* The document registry instance used by the application.
*/
readonly docRegistry: DocumentRegistry;
/**
* Promise that resolves when state is first restored.
*/
readonly restored: Promise<void>;
/**
* The service manager used by the application.
*/
readonly serviceManager: ServiceManager;
/**
* Walks up the DOM hierarchy of the target of the active `contextmenu`
* event, testing each HTMLElement ancestor for a user-supplied funcion. This can
* be used to find an HTMLElement on which to operate, given a context menu click.
*
* @param test - a function that takes an `HTMLElement` and returns a
* boolean for whether it is the element the requester is seeking.
*
* @returns an HTMLElement or undefined, if none is found.
*/
contextMenuHitTest(
test: (node: HTMLElement) => boolean
): HTMLElement | undefined {
if (
!this._contextMenuEvent ||
!(this._contextMenuEvent.target instanceof Node)
) {
return undefined;
}
let node = this._contextMenuEvent.target;
do {
if (node instanceof HTMLElement && test(node)) {
return node;
}
node = node.parentNode;
} while (node && node.parentNode && node !== node.parentNode);
return undefined;
// TODO: we should be able to use .composedPath() to simplify this function
// down to something like the below, but it seems like composedPath is
// sometimes returning an empty list.
/*
if (this._contextMenuEvent) {
this._contextMenuEvent
.composedPath()
.filter(x => x instanceof HTMLElement)
.find(test);
}
return undefined;
*/
}
/**
* A method invoked on a document `'contextmenu'` event.
*/
protected evtContextMenu(event: MouseEvent): void {
this._contextMenuEvent = event;
if (event.shiftKey) {
return;
}
const opened = this.contextMenu.open(event);
if (opened) {
const items = this.contextMenu.menu.items;
// If only the context menu information will be shown,
// with no real commands, close the context menu and
// allow the native one to open.
if (
items.length === 1 &&
items[0].command === Private.CONTEXT_MENU_INFO
) {
this.contextMenu.menu.close();
return;
}
// Stop propagation and allow the application context menu to show.
event.preventDefault();
event.stopPropagation();
}
}
private _contextMenuEvent: MouseEvent;
}
/**
* The namespace for `JupyterFrontEnd` class statics.
*/
export namespace JupyterFrontEnd {
/**
* The options used to initialize a JupyterFrontEnd.
*/
export interface IOptions<T extends IShell = IShell, U = any>
extends Application.IOptions<T> {
/**
* The document registry instance used by the application.
*/
docRegistry?: DocumentRegistry;
/**
* The command linker used by the application.
*/
commandLinker?: CommandLinker;
/**
* The service manager used by the application.
*/
serviceManager?: ServiceManager;
/**
* Promise that resolves when state is first restored, returning layout
* description.
*/
restored?: Promise<U>;
}
/**
* A minimal shell type for Jupyter front-end applications.
*/
export interface IShell extends Widget {
/**
* Activates a widget inside the application shell.
*
* @param id - The ID of the widget being activated.
*/
activateById(id: string): void;
/**
* Add a widget to the application shell.
*
* @param widget - The widget being added.
*
* @param area - Optional region in the shell into which the widget should
* be added.
*
* @param options - Optional flags the shell might use when opening the
* widget, as defined in the `DocumentRegistry`.
*/
add(
widget: Widget,
area?: string,
options?: DocumentRegistry.IOpenOptions
): void;
/**
* The focused widget in the application shell.
*
* #### Notes
* Different shell implementations have latitude to decide what "current"
* or "focused" mean, depending on their user interface characteristics.
*/
readonly currentWidget: Widget;
/**
* Returns an iterator for the widgets inside the application shell.
*
* @param area - Optional regions in the shell whose widgets are iterated.
*/
widgets(area?: string): IIterator<Widget>;
}
/**
* The application paths dictionary token.
*/
export const IPaths = new Token<IPaths>('@jupyterlab/application:IPaths');
/**
* An interface for URL and directory paths used by a Jupyter front-end.
*/
export interface IPaths {
/**
* The urls used by the application.
*/
readonly urls: {
readonly base: string;
readonly notFound?: string;
readonly app: string;
readonly static: string;
readonly settings: string;
readonly themes: string;
readonly tree: string;
readonly workspaces: string;
readonly hubPrefix?: string;
readonly hubHost?: string;
readonly hubUser?: string;
readonly hubServerName?: string;
};
/**
* The server directories used by the application, for user information
* only.
*
* #### Notes
* These are for user information and user interface hints only and should
* not be relied on in code. A server may set these to empty strings if it
* does not want to expose this information.
*
* Examples of appropriate use include displaying a help dialog for a user
* listing the paths, or a tooltip in a filebrowser displaying the server
* root. Examples of inapproriate use include using one of these paths in a
* terminal command, generating code using these paths, or using one of
* these paths in a request to the server (it would be better to write a
* server extension to handle these cases).
*/
readonly directories: {
readonly appSettings: string;
readonly schemas: string;
readonly static: string;
readonly templates: string;
readonly themes: string;
readonly userSettings: string;
readonly serverRoot: string;
readonly workspaces: string;
};
}
}
/**
* A namespace for module-private functionality.
*/
namespace Private {
/**
* An id for a private context-menu-info
* ersatz command.
*/
export const CONTEXT_MENU_INFO = '__internal:context-menu-info';
}