/
index.ts
581 lines (499 loc) · 16.9 KB
/
index.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
each
} from '@phosphor/algorithm';
import {
Menu, Widget
} from '@phosphor/widgets';
import {
JupyterLab, JupyterLabPlugin
} from '@jupyterlab/application';
import {
ICommandPalette, showDialog, Dialog
} from '@jupyterlab/apputils';
import {
IMainMenu, IMenuExtender, EditMenu, FileMenu, KernelMenu,
MainMenu, RunMenu, SettingsMenu, ViewMenu, TabsMenu
} from '@jupyterlab/mainmenu';
/**
* A namespace for command IDs of semantic extension points.
*/
export
namespace CommandIDs {
export
const undo = 'editmenu:undo';
export
const redo = 'editmenu:redo';
export
const clearCurrent = 'editmenu:clear-current';
export
const clearAll = 'editmenu:clear-all';
export
const find = 'editmenu:find';
export
const findAndReplace = 'editmenu:find-and-replace';
export
const closeAndCleanup = 'filemenu:close-and-cleanup';
export
const createConsole = 'filemenu:create-console';
export
const interruptKernel = 'kernelmenu:interrupt';
export
const restartKernel = 'kernelmenu:restart';
export
const restartKernelAndClear = 'kernelmenu:restart-and-clear';
export
const changeKernel = 'kernelmenu:change';
export
const shutdownKernel = 'kernelmenu:shutdown';
export
const shutdownAllKernels = 'kernelmenu:shutdownAll';
export
const wordWrap = 'viewmenu:word-wrap';
export
const lineNumbering = 'viewmenu:line-numbering';
export
const matchBrackets = 'viewmenu:match-brackets';
export
const run = 'runmenu:run';
export
const runAll = 'runmenu:run-all';
export
const restartAndRunAll = 'runmenu:restart-and-run-all';
export
const runAbove = 'runmenu:run-above';
export
const runBelow = 'runmenu:run-below';
}
/**
* A service providing an interface to the main menu.
*/
const menuPlugin: JupyterLabPlugin<IMainMenu> = {
id: '@jupyterlab/mainmenu-extension:plugin',
requires: [ICommandPalette],
provides: IMainMenu,
activate: (app: JupyterLab, palette: ICommandPalette): IMainMenu => {
let menu = new MainMenu(app.commands);
menu.id = 'jp-MainMenu';
let logo = new Widget();
logo.addClass('jp-MainAreaPortraitIcon');
logo.addClass('jp-JupyterIcon');
logo.id = 'jp-MainLogo';
// Create the application menus.
createEditMenu(app, menu.editMenu);
createFileMenu(app, menu.fileMenu);
createKernelMenu(app, menu.kernelMenu);
createRunMenu(app, menu.runMenu);
createSettingsMenu(app, menu.settingsMenu);
createViewMenu(app, menu.viewMenu);
createTabsMenu(app, menu.tabsMenu);
palette.addItem({
command: CommandIDs.shutdownAllKernels,
category: 'Kernel Operations'
});
app.shell.addToTopArea(logo);
app.shell.addToTopArea(menu);
return menu;
}
};
/**
* Create the basic `Edit` menu.
*/
export
function createEditMenu(app: JupyterLab, menu: EditMenu): void {
const commands = menu.menu.commands;
// Add the undo/redo commands the the Edit menu.
commands.addCommand(CommandIDs.undo, {
label: 'Undo',
isEnabled:
Private.delegateEnabled(app, menu.undoers, 'undo'),
execute:
Private.delegateExecute(app, menu.undoers, 'undo')
});
commands.addCommand(CommandIDs.redo, {
label: 'Redo',
isEnabled:
Private.delegateEnabled(app, menu.undoers, 'redo'),
execute:
Private.delegateExecute(app, menu.undoers, 'redo')
});
menu.addGroup([
{ command: CommandIDs.undo },
{ command: CommandIDs.redo }
], 0);
// Add the clear commands to the Edit menu.
commands.addCommand(CommandIDs.clearCurrent, {
label: () => {
const noun =
Private.delegateLabel(app, menu.clearers, 'noun');
const enabled =
Private.delegateEnabled(app, menu.clearers, 'clearCurrent')();
return `Clear${enabled ? ` ${noun}` : ''}`;
},
isEnabled:
Private.delegateEnabled(app, menu.clearers, 'clearCurrent'),
execute:
Private.delegateExecute(app, menu.clearers, 'clearCurrent')
});
commands.addCommand(CommandIDs.clearAll, {
label: () => {
const noun = Private.delegateLabel(app, menu.clearers, 'pluralNoun');
const enabled = Private.delegateEnabled(app, menu.clearers, 'clearAll')();
return `Clear All${enabled ? ` ${noun}` : ''}`;
},
isEnabled:
Private.delegateEnabled(app, menu.clearers, 'clearAll'),
execute:
Private.delegateExecute(app, menu.clearers, 'clearAll')
});
menu.addGroup([
{ command: CommandIDs.clearCurrent },
{ command: CommandIDs.clearAll },
], 10);
// Add the find/replace commands the the Edit menu.
commands.addCommand(CommandIDs.find, {
label: 'Find…',
isEnabled:
Private.delegateEnabled(app, menu.findReplacers, 'find'),
execute:
Private.delegateExecute(app, menu.findReplacers, 'find')
});
commands.addCommand(CommandIDs.findAndReplace, {
label: 'Find and Replace…',
isEnabled:
Private.delegateEnabled(app, menu.findReplacers, 'findAndReplace'),
execute:
Private.delegateExecute(app, menu.findReplacers, 'findAndReplace')
});
menu.addGroup([
{ command: CommandIDs.find },
{ command: CommandIDs.findAndReplace }
], 200);
}
/**
* Create the basic `File` menu.
*/
export
function createFileMenu(app: JupyterLab, menu: FileMenu): void {
const commands = menu.menu.commands;
// Add a delegator command for closing and cleaning up an activity.
commands.addCommand(CommandIDs.closeAndCleanup, {
label: () => {
const action =
Private.delegateLabel(app, menu.closeAndCleaners, 'action');
const name =
Private.delegateLabel(app, menu.closeAndCleaners, 'name');
return `Close and ${action ? ` ${action} ${name}` : 'Shutdown'}`;
},
isEnabled:
Private.delegateEnabled(app, menu.closeAndCleaners, 'closeAndCleanup'),
execute:
Private.delegateExecute(app, menu.closeAndCleaners, 'closeAndCleanup')
});
// Add a delegator command for creating a console for an activity.
commands.addCommand(CommandIDs.createConsole, {
label: () => {
const name = Private.delegateLabel(app, menu.consoleCreators, 'name');
const label = `New Console for ${name ? name : 'Activity' }`;
return label;
},
isEnabled: Private.delegateEnabled(app, menu.consoleCreators, 'createConsole'),
execute: Private.delegateExecute(app, menu.consoleCreators, 'createConsole')
});
// Add the new group
const newGroup = [
{ type: 'submenu' as Menu.ItemType, submenu: menu.newMenu.menu },
{ command: 'filebrowser:create-main-launcher' },
];
const newViewGroup = [
{ command: 'docmanager:clone' },
{ command: CommandIDs.createConsole }
];
// Add the close group
const closeGroup = [
'docmanager:close',
'filemenu:close-and-cleanup',
'docmanager:close-all-files'
].map(command => { return { command }; });
// Add save group.
const saveGroup = [
'docmanager:save',
'docmanager:save-as',
'docmanager:save-all'
].map(command => { return { command }; });
// Add the re group.
const reGroup = [
'docmanager:restore-checkpoint',
'docmanager:rename'
].map(command => { return { command }; });
menu.addGroup(newGroup, 0);
menu.addGroup(newViewGroup, 1);
menu.addGroup(closeGroup, 2);
menu.addGroup(saveGroup, 3);
menu.addGroup(reGroup, 4);
}
/**
* Create the basic `Kernel` menu.
*/
export
function createKernelMenu(app: JupyterLab, menu: KernelMenu): void {
const commands = menu.menu.commands;
commands.addCommand(CommandIDs.interruptKernel, {
label: 'Interrupt Kernel',
isEnabled: Private.delegateEnabled(app, menu.kernelUsers, 'interruptKernel'),
execute: Private.delegateExecute(app, menu.kernelUsers, 'interruptKernel')
});
commands.addCommand(CommandIDs.restartKernel, {
label: 'Restart Kernel…',
isEnabled: Private.delegateEnabled(app, menu.kernelUsers, 'restartKernel'),
execute: Private.delegateExecute(app, menu.kernelUsers, 'restartKernel')
});
commands.addCommand(CommandIDs.restartKernelAndClear, {
label: () => {
const noun = Private.delegateLabel(app, menu.kernelUsers, 'noun');
const enabled =
Private.delegateEnabled(app, menu.kernelUsers, 'restartKernelAndClear')();
return `Restart Kernel and Clear${enabled ? ` ${noun}` : ''}…`;
},
isEnabled:
Private.delegateEnabled(app, menu.kernelUsers, 'restartKernelAndClear'),
execute:
Private.delegateExecute(app, menu.kernelUsers, 'restartKernelAndClear')
});
commands.addCommand(CommandIDs.changeKernel, {
label: 'Change Kernel…',
isEnabled: Private.delegateEnabled(app, menu.kernelUsers, 'changeKernel'),
execute: Private.delegateExecute(app, menu.kernelUsers, 'changeKernel')
});
commands.addCommand(CommandIDs.shutdownKernel, {
label: 'Shutdown Kernel',
isEnabled: Private.delegateEnabled(app, menu.kernelUsers, 'shutdownKernel'),
execute: Private.delegateExecute(app, menu.kernelUsers, 'shutdownKernel')
});
commands.addCommand(CommandIDs.shutdownAllKernels, {
label: 'Shutdown All Kernels…',
isEnabled: () => {
return app.serviceManager.sessions.running().next() !== undefined;
},
execute: () => {
showDialog({
title: 'Shutdown All?',
body: 'Shut down all kernels?',
buttons: [
Dialog.cancelButton(), Dialog.warnButton({ label: 'SHUTDOWN' })
]
}).then(result => {
if (result.button.accept) {
return app.serviceManager.sessions.shutdownAll();
}
});
}
});
const restartGroup = [
CommandIDs.restartKernel,
CommandIDs.restartKernelAndClear,
CommandIDs.restartAndRunAll,
].map(command => { return { command }; });
menu.addGroup([{ command: CommandIDs.interruptKernel }], 0);
menu.addGroup(restartGroup, 1);
menu.addGroup([{ command: CommandIDs.shutdownKernel },
{ command: CommandIDs.shutdownAllKernels }], 2);
menu.addGroup([{ command: CommandIDs.changeKernel }], 3);
}
/**
* Create the basic `View` menu.
*/
export
function createViewMenu(app: JupyterLab, menu: ViewMenu): void {
const commands = menu.menu.commands;
commands.addCommand(CommandIDs.lineNumbering, {
label: 'Show Line Numbers',
isEnabled: Private.delegateEnabled(app, menu.editorViewers, 'toggleLineNumbers'),
isToggled: Private.delegateToggled(app, menu.editorViewers, 'lineNumbersToggled'),
execute: Private.delegateExecute(app, menu.editorViewers, 'toggleLineNumbers')
});
commands.addCommand(CommandIDs.matchBrackets, {
label: 'Match Brackets',
isEnabled: Private.delegateEnabled(app, menu.editorViewers, 'toggleMatchBrackets'),
isToggled: Private.delegateToggled(app, menu.editorViewers, 'matchBracketsToggled'),
execute: Private.delegateExecute(app, menu.editorViewers, 'toggleMatchBrackets')
});
commands.addCommand(CommandIDs.wordWrap, {
label: 'Wrap Words',
isEnabled: Private.delegateEnabled(app, menu.editorViewers, 'toggleWordWrap'),
isToggled: Private.delegateToggled(app, menu.editorViewers, 'wordWrapToggled'),
execute: Private.delegateExecute(app, menu.editorViewers, 'toggleWordWrap')
});
menu.addGroup([
{ command: 'application:toggle-left-area' },
{ command: 'application:toggle-right-area' }
], 0);
const editorViewerGroup = [
CommandIDs.lineNumbering,
CommandIDs.matchBrackets,
CommandIDs.wordWrap
].map( command => { return { command }; });
menu.addGroup(editorViewerGroup, 10);
// Add the command for toggling single-document mode.
menu.addGroup([
{ command: 'application:toggle-presentation-mode'},
{ command: 'application:toggle-mode' }
], 1000);
}
export
function createRunMenu(app: JupyterLab, menu: RunMenu): void {
const commands = menu.menu.commands;
commands.addCommand(CommandIDs.run, {
label: () => {
const noun = Private.delegateLabel(app, menu.codeRunners, 'noun');
const enabled =
Private.delegateEnabled(app, menu.codeRunners, 'run')();
return `Run Selected${enabled ? ` ${noun}` : ''}`;
},
isEnabled: Private.delegateEnabled(app, menu.codeRunners, 'run'),
execute: Private.delegateExecute(app, menu.codeRunners, 'run')
});
commands.addCommand(CommandIDs.runAll, {
label: () => {
const noun = Private.delegateLabel(app, menu.codeRunners, 'noun');
const enabled =
Private.delegateEnabled(app, menu.codeRunners, 'runAll')();
return `Run All${enabled ? ` ${noun}` : ''}`;
},
isEnabled: Private.delegateEnabled(app, menu.codeRunners, 'runAll'),
execute: Private.delegateExecute(app, menu.codeRunners, 'runAll')
});
commands.addCommand(CommandIDs.restartAndRunAll, {
label: () => {
const noun = Private.delegateLabel(app, menu.codeRunners, 'noun');
const enabled =
Private.delegateEnabled(app, menu.codeRunners, 'restartAndRunAll')();
return `Restart Kernel and Run All${enabled ? ` ${noun}` : ''}…`;
},
isEnabled: Private.delegateEnabled(app, menu.codeRunners, 'restartAndRunAll'),
execute: Private.delegateExecute(app, menu.codeRunners, 'restartAndRunAll')
});
const runAllGroup = [
CommandIDs.runAll,
CommandIDs.restartAndRunAll
].map( command => { return { command }; });
menu.addGroup([{ command: CommandIDs.run }], 0);
menu.addGroup(runAllGroup, 999);
}
export
function createSettingsMenu(app: JupyterLab, menu: SettingsMenu): void {
menu.addGroup([{ command: 'settingeditor:open' }], 1000);
}
export
function createTabsMenu(app: JupyterLab, menu: TabsMenu): void {
const commands = app.commands;
// Add commands for cycling the active tabs.
menu.addGroup([
{ command: 'application:activate-next-tab' },
{ command: 'application:activate-previous-tab' }
], 0);
let tabGroup: Menu.IItemOptions[] = [];
// Utility function to create a command to activate
// a given tab, or get it if it already exists.
const createMenuItem = (widget: Widget): Menu.IItemOptions => {
const commandID = `tabmenu:activate-${widget.id}`;
if (!commands.hasCommand(commandID)) {
commands.addCommand(commandID, {
label: () => widget.title.label,
isVisible: () => !widget.isDisposed,
isEnabled: () => !widget.isDisposed,
isToggled: () => app.shell.currentWidget === widget,
execute: () => app.shell.activateById(widget.id)
});
}
return { command: commandID };
};
app.restored.then(() => {
// Iterate over the current widgets in the
// main area, and add them to the tab group
// of the menu.
const populateTabs = () => {
menu.removeGroup(tabGroup);
tabGroup.length = 0;
each(app.shell.widgets('main'), widget => {
tabGroup.push(createMenuItem(widget));
});
menu.addGroup(tabGroup, 1);
};
populateTabs();
app.shell.layoutModified.connect(() => { populateTabs(); });
});
}
export default menuPlugin;
/**
* A namespace for Private data.
*/
namespace Private {
/**
* Given a widget and a set containing IMenuExtenders,
* check the tracker and return the extender, if any,
* that holds the widget.
*/
function findExtender<E extends IMenuExtender<Widget>>(widget: Widget, s: Set<E>): E {
let extender: E;
s.forEach(value => {
if (value.tracker.has(widget)) {
extender = value;
}
});
return extender;
}
/**
* A utility function that delegates a portion of a label to an IMenuExtender.
*/
export
function delegateLabel<E extends IMenuExtender<Widget>>(app: JupyterLab, s: Set<E>, label: keyof E): string {
let widget = app.shell.currentWidget;
const extender = findExtender(widget, s);
if (!extender) {
return '';
}
return extender[label];
}
/**
* A utility function that delegates command execution
* to an IMenuExtender.
*/
export
function delegateExecute<E extends IMenuExtender<Widget>>(app: JupyterLab, s: Set<E>, executor: keyof E): () => Promise<any> {
return () => {
let widget = app.shell.currentWidget;
const extender = findExtender(widget, s);
if (!extender) {
return Promise.resolve(void 0);
}
return extender[executor](widget);
};
}
/**
* A utility function that delegates whether a command is enabled
* to an IMenuExtender.
*/
export
function delegateEnabled<E extends IMenuExtender<Widget>>(app: JupyterLab, s: Set<E>, executor: keyof E): () => boolean {
return () => {
let widget = app.shell.currentWidget;
const extender = findExtender(widget, s);
return !!extender && !!extender[executor] &&
(extender.isEnabled ? extender.isEnabled(widget) : true);
};
}
/**
* A utility function that delegates whether a command is toggled
* for an IMenuExtender.
*/
export
function delegateToggled<E extends IMenuExtender<Widget>>(app: JupyterLab, s: Set<E>, toggled: keyof E): () => boolean {
return () => {
let widget = app.shell.currentWidget;
const extender = findExtender(widget, s);
return !!extender && !!extender[toggled] && !!extender[toggled](widget);
};
}
}