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 46
/
Copy pathindex.ts
77 lines (65 loc) · 2.03 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
IRouter,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { PageConfig } from '@jupyterlab/coreutils';
import { ITerminalTracker } from '@jupyterlab/terminal';
import { find } from '@lumino/algorithm';
/**
* A plugin to open terminals in a new tab
*/
const opener: JupyterFrontEndPlugin<void> = {
id: '@retrolab/terminal-extension:opener',
requires: [IRouter],
autoStart: true,
activate: (app: JupyterFrontEnd, router: IRouter) => {
const { commands } = app;
const terminalPattern = new RegExp('/terminals/(.*)');
const command = 'router:terminal';
commands.addCommand(command, {
execute: (args: any) => {
const parsed = args as IRouter.ILocation;
const matches = parsed.path.match(terminalPattern);
if (!matches) {
return;
}
const [, name] = matches;
if (!name) {
return;
}
commands.execute('terminal:open', { name });
}
});
router.register({ command, pattern: terminalPattern });
}
};
/**
* Open terminals in a new tab.
*/
const redirect: JupyterFrontEndPlugin<void> = {
id: '@retrolab/terminal-extension:redirect',
requires: [ITerminalTracker],
autoStart: true,
activate: (app: JupyterFrontEnd, tracker: ITerminalTracker) => {
const baseUrl = PageConfig.getBaseUrl();
tracker.widgetAdded.connect((send, terminal) => {
const widget = find(app.shell.widgets('main'), w => w.id === terminal.id);
if (widget) {
// bail if the terminal is already added to the main area
return;
}
const name = terminal.content.session.name;
window.open(`${baseUrl}retro/terminals/${name}`, '_blank');
// dispose the widget since it is not used on this page
terminal.dispose();
});
}
};
/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [opener, redirect];
export default plugins;