forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
485 lines (390 loc) · 12 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ILayoutRestorer, JupyterLab, JupyterLabPlugin
} from '@jupyterlab/application';
import {
Clipboard, InstanceTracker, ToolbarButton
} from '@jupyterlab/apputils';
import {
IStateDB, PageConfig, PathExt, URLExt
} from '@jupyterlab/coreutils';
import {
IDocumentManager
} from '@jupyterlab/docmanager';
import {
DocumentRegistry
} from '@jupyterlab/docregistry';
import {
FileBrowserModel, FileBrowser, IFileBrowserFactory
} from '@jupyterlab/filebrowser';
import {
Launcher
} from '@jupyterlab/launcher';
import {
Contents
} from '@jupyterlab/services';
import {
map, toArray
} from '@phosphor/algorithm';
import {
CommandRegistry
} from '@phosphor/commands';
import {
Menu
} from '@phosphor/widgets';
/**
* The command IDs used by the file browser plugin.
*/
namespace CommandIDs {
export
const copy = 'filebrowser:copy';
// For main browser only.
export
const createLauncher = 'filebrowser:create-main-launcher';
export
const cut = 'filebrowser:cut';
export
const del = 'filebrowser:delete';
export
const download = 'filebrowser:download';
export
const duplicate = 'filebrowser:duplicate';
// For main browser only.
export
const hideBrowser = 'filebrowser:hide-main';
// For main browser only.
export
const navigate = 'filebrowser:navigate-main';
export
const open = 'filebrowser:open';
export
const paste = 'filebrowser:paste';
export
const rename = 'filebrowser:rename';
// For main browser only.
export
const share = 'filebrowser:share-main';
// For main browser only.
export
const showBrowser = 'filebrowser:activate-main';
export
const shutdown = 'filebrowser:shutdown';
// For main browser only.
export
const toggleBrowser = 'filebrowser:toggle-main';
}
/**
* The default file browser extension.
*/
const browser: JupyterLabPlugin<void> = {
activate: activateBrowser,
id: '@jupyterlab/filebrowser-extension:browser',
requires: [IFileBrowserFactory, ILayoutRestorer],
autoStart: true
};
/**
* The default file browser factory provider.
*/
const factory: JupyterLabPlugin<IFileBrowserFactory> = {
activate: activateFactory,
id: '@jupyterlab/filebrowser-extension:factory',
provides: IFileBrowserFactory,
requires: [IDocumentManager, IStateDB]
};
/**
* The file browser namespace token.
*/
const namespace = 'filebrowser';
/**
* Export the plugins as default.
*/
const plugins: JupyterLabPlugin<any>[] = [factory, browser];
export default plugins;
/**
* Activate the file browser factory provider.
*/
function activateFactory(app: JupyterLab, docManager: IDocumentManager, state: IStateDB): IFileBrowserFactory {
const { commands } = app;
const tracker = new InstanceTracker<FileBrowser>({ namespace });
const createFileBrowser = (id: string, options: IFileBrowserFactory.IOptions = { }) => {
const model = new FileBrowserModel({
manager: docManager,
driveName: options.driveName || '',
state: options.state === null ? null : options.state || state
});
const widget = new FileBrowser({
id, model, commands: options.commands || commands
});
const { registry } = docManager;
// Add a launcher toolbar item.
let launcher = new ToolbarButton({
className: 'jp-AddIcon',
onClick: () => {
return createLauncher(commands, widget);
},
tooltip: 'New Launcher'
});
launcher.addClass('jp-MaterialIcon');
widget.toolbar.insertItem(0, 'launch', launcher);
// Add a context menu handler to the file browser's directory listing.
let node = widget.node.getElementsByClassName('jp-DirListing-content')[0];
node.addEventListener('contextmenu', (event: MouseEvent) => {
event.preventDefault();
const model = widget.modelForClick(event);
if (model) {
const menu = createContextMenu(model, commands, registry);
menu.open(event.clientX, event.clientY);
}
});
// Track the newly created file browser.
tracker.add(widget);
return widget;
};
const defaultBrowser = createFileBrowser('filebrowser');
return { createFileBrowser, defaultBrowser, tracker };
}
/**
* Activate the default file browser in the sidebar.
*/
function activateBrowser(app: JupyterLab, factory: IFileBrowserFactory, restorer: ILayoutRestorer): void {
const browser = factory.defaultBrowser;
const { commands, shell } = app;
// Let the application restorer track the primary file browser (that is
// automatically created) for restoration of application state (e.g. setting
// the file browser as the current side bar widget).
//
// All other file browsers created by using the factory function are
// responsible for their own restoration behavior, if any.
restorer.add(browser, namespace);
addCommands(app, factory.tracker, browser);
browser.title.label = 'Files';
shell.addToLeftArea(browser, { rank: 100 });
// If the layout is a fresh session without saved data, open file browser.
app.restored.then(layout => {
if (layout.fresh) {
commands.execute(CommandIDs.showBrowser, void 0);
}
});
Promise.all([app.restored, browser.model.restored]).then(() => {
const { model } = browser;
function maybeCreate() {
// Create a launcher if there are no open items.
if (app.shell.isEmpty('main')) {
model.restored.then(() => createLauncher(commands, browser));
}
}
// When layout is modified, create a launcher if there are no open items.
shell.layoutModified.connect(() => { maybeCreate(); });
maybeCreate();
});
}
/**
* Add the main file browser commands to the application's command registry.
*/
function addCommands(app: JupyterLab, tracker: InstanceTracker<FileBrowser>, browser: FileBrowser): void {
const { commands } = app;
commands.addCommand(CommandIDs.del, {
execute: () => {
const widget = tracker.currentWidget;
if (widget) {
return widget.delete();
}
},
iconClass: 'jp-MaterialIcon jp-CloseIcon',
label: 'Delete',
mnemonic: 0
});
commands.addCommand(CommandIDs.copy, {
execute: () => {
const widget = tracker.currentWidget;
if (widget) {
return widget.copy();
}
},
iconClass: 'jp-MaterialIcon jp-CopyIcon',
label: 'Copy',
mnemonic: 0
});
commands.addCommand(CommandIDs.cut, {
execute: () => {
const widget = tracker.currentWidget;
if (widget) {
return widget.cut();
}
},
iconClass: 'jp-MaterialIcon jp-CutIcon',
label: 'Cut'
});
commands.addCommand(CommandIDs.download, {
execute: () => {
const widget = tracker.currentWidget;
if (widget) {
return widget.download();
}
},
iconClass: 'jp-MaterialIcon jp-DownloadIcon',
label: 'Download'
});
commands.addCommand(CommandIDs.duplicate, {
execute: () => {
const widget = tracker.currentWidget;
if (widget) {
return widget.duplicate();
}
},
iconClass: 'jp-MaterialIcon jp-CopyIcon',
label: 'Duplicate'
});
commands.addCommand(CommandIDs.hideBrowser, {
execute: () => {
if (!browser.isHidden) {
app.shell.collapseLeft();
}
}
});
commands.addCommand(CommandIDs.navigate, {
execute: args => {
const path = args.path as string || '';
const services = app.serviceManager;
const open = 'docmanager:open';
const failure = (reason: any) => {
console.warn(`${CommandIDs.navigate} failed to open: ${path}`, reason);
};
return services.ready
.then(() => services.contents.get(path))
.then(value => {
const { model } = browser;
const { restored } = model;
if (value.type === 'directory') {
return restored.then(() => model.cd(`/${path}`));
}
return restored.then(() => model.cd(`/${PathExt.dirname(path)}`))
.then(() => commands.execute(open, { path }));
}).catch(failure);
}
});
commands.addCommand(CommandIDs.open, {
execute: () => {
const widget = tracker.currentWidget;
if (!widget) {
return;
}
return Promise.all(toArray(map(widget.selectedItems(), item => {
if (item.type === 'directory') {
return widget.model.cd(item.path);
}
return commands.execute('docmanager:open', { path: item.path });
})));
},
iconClass: 'jp-MaterialIcon jp-OpenFolderIcon',
label: 'Open',
mnemonic: 0
});
commands.addCommand(CommandIDs.paste, {
execute: () => {
const widget = tracker.currentWidget;
if (widget) {
return widget.paste();
}
},
iconClass: 'jp-MaterialIcon jp-PasteIcon',
label: 'Paste',
mnemonic: 0
});
commands.addCommand(CommandIDs.rename, {
execute: (args) => {
const widget = tracker.currentWidget;
if (widget) {
return widget.rename();
}
},
iconClass: 'jp-MaterialIcon jp-EditIcon',
label: 'Rename',
mnemonic: 0
});
commands.addCommand(CommandIDs.share, {
execute: () => {
const path = encodeURIComponent(browser.selectedItems().next().path);
const tree = PageConfig.getTreeUrl();
Clipboard.copyToSystem(URLExt.join(tree, path));
},
isVisible: () => toArray(browser.selectedItems()).length === 1,
iconClass: 'jp-MaterialIcon jp-LinkIcon',
label: 'Copy Shareable Link'
});
commands.addCommand(CommandIDs.showBrowser, {
execute: () => { app.shell.activateById(browser.id); }
});
commands.addCommand(CommandIDs.shutdown, {
execute: () => {
const widget = tracker.currentWidget;
if (widget) {
return widget.shutdownKernels();
}
},
iconClass: 'jp-MaterialIcon jp-StopIcon',
label: 'Shutdown Kernel'
});
commands.addCommand(CommandIDs.toggleBrowser, {
execute: () => {
if (browser.isHidden) {
return commands.execute(CommandIDs.showBrowser, void 0);
}
return commands.execute(CommandIDs.hideBrowser, void 0);
}
});
commands.addCommand(CommandIDs.createLauncher, {
label: 'New Launcher',
execute: () => createLauncher(commands, browser)
});
}
/**
* Create a context menu for the file browser listing.
*
* #### Notes
* This function generates temporary commands with an incremented name. These
* commands are disposed when the menu itself is disposed.
*/
function createContextMenu(model: Contents.IModel, commands: CommandRegistry, registry: DocumentRegistry): Menu {
const path = model.path;
const menu = new Menu({ commands });
menu.addItem({ command: CommandIDs.open });
if (model.type !== 'directory') {
const factories = registry.preferredWidgetFactories(path).map(f => f.name);
if (path && factories.length > 1) {
const command = 'docmanager:open';
const openWith = new Menu({ commands });
openWith.title.label = 'Open With';
factories.forEach(factory => {
openWith.addItem({ args: { factory, path }, command });
});
menu.addItem({ type: 'submenu', submenu: openWith });
}
}
menu.addItem({ command: CommandIDs.rename });
menu.addItem({ command: CommandIDs.del });
menu.addItem({ command: CommandIDs.cut });
if (model.type !== 'directory') {
menu.addItem({ command: CommandIDs.copy });
}
menu.addItem({ command: CommandIDs.paste });
if (model.type !== 'directory') {
menu.addItem({ command: CommandIDs.duplicate });
menu.addItem({ command: CommandIDs.download });
menu.addItem({ command: CommandIDs.shutdown });
}
menu.addItem({ command: CommandIDs.share });
return menu;
}
/**
* Create a launcher for a given filebrowser widget.
*/
function createLauncher(commands: CommandRegistry, browser: FileBrowser): Promise<Launcher> {
const { model } = browser;
return commands.execute('launcher:create', { cwd: model.path })
.then((launcher: Launcher) => {
model.pathChanged.connect(() => { launcher.cwd = model.path; }, launcher);
return launcher;
});
}