-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Copy pathindex.ts
103 lines (88 loc) · 2.71 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
IRouter,
JupyterFrontEnd,
JupyterFrontEndPlugin,
} from '@jupyterlab/application';
import { IConsoleTracker } from '@jupyterlab/console';
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
import {
INotebookPathOpener,
defaultNotebookPathOpener,
} from '@jupyter-notebook/application';
import { find } from '@lumino/algorithm';
/**
* A plugin to open consoles in a new tab
*/
const opener: JupyterFrontEndPlugin<void> = {
id: '@jupyter-notebook/console-extension:opener',
requires: [IRouter],
autoStart: true,
description: 'A plugin to open consoles in a new tab',
activate: (app: JupyterFrontEnd, router: IRouter) => {
const { commands } = app;
const consolePattern = new RegExp('/consoles/(.*)');
const ignoreTreePattern = new RegExp('/(tree|notebooks|edit)/(.*)');
const command = 'router:console';
commands.addCommand(command, {
execute: (args: any) => {
const parsed = args as IRouter.ILocation;
const matches = parsed.path.match(consolePattern);
const isTreeMatch = parsed.path.match(ignoreTreePattern);
if (isTreeMatch || !matches) {
return;
}
const [, match] = matches;
if (!match) {
return;
}
const path = decodeURIComponent(match);
commands.execute('console:create', { path });
},
});
router.register({ command, pattern: consolePattern });
},
};
/**
* Open consoles in a new tab.
*/
const redirect: JupyterFrontEndPlugin<void> = {
id: '@jupyter-notebook/console-extension:redirect',
requires: [IConsoleTracker],
optional: [INotebookPathOpener],
autoStart: true,
description: 'Open consoles in a new tab',
activate: (
app: JupyterFrontEnd,
tracker: IConsoleTracker,
notebookPathOpener: INotebookPathOpener | null
) => {
const baseUrl = PageConfig.getBaseUrl();
const opener = notebookPathOpener ?? defaultNotebookPathOpener;
tracker.widgetAdded.connect(async (send, console) => {
const { sessionContext } = console;
await sessionContext.ready;
const widget = find(
app.shell.widgets('main'),
(w) => w.id === console.id
);
if (widget) {
// bail if the console is already added to the main area
return;
}
opener.open({
prefix: URLExt.join(baseUrl, 'consoles'),
path: sessionContext.path,
target: '_blank',
});
// the widget is not needed anymore
console.dispose();
});
},
};
/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [opener, redirect];
export default plugins;