This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.ts
79 lines (67 loc) 路 2.08 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
// 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 } from '@jupyterlab/coreutils';
import { find } from '@lumino/algorithm';
/**
* A plugin to open consoles in a new tab
*/
const opener: JupyterFrontEndPlugin<void> = {
id: '@retrolab/console-extension:opener',
requires: [IRouter],
autoStart: true,
activate: (app: JupyterFrontEnd, router: IRouter) => {
const { commands } = app;
const consolePattern = new RegExp('/consoles/(.*)');
const command = 'router:console';
commands.addCommand(command, {
execute: (args: any) => {
const parsed = args as IRouter.ILocation;
const matches = parsed.path.match(consolePattern);
if (!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: '@retrolab/console-extension:redirect',
requires: [IConsoleTracker],
autoStart: true,
activate: (app: JupyterFrontEnd, tracker: IConsoleTracker) => {
const baseUrl = PageConfig.getBaseUrl();
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;
}
window.open(`${baseUrl}retro/consoles/${sessionContext.path}`, '_blank');
// the widget is not needed anymore
console.dispose();
});
}
};
/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [opener, redirect];
export default plugins;