/
index.ts
202 lines (189 loc) · 5.88 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/**
* @packageDocumentation
* @module running-extension
*/
import {
ILabShell,
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { Dialog, ICommandPalette } from '@jupyterlab/apputils';
import {
IRunningSessionManagers,
IRunningSessionSidebar,
RunningSessionManagers,
RunningSessions,
SearchableSessions
} from '@jupyterlab/running';
import { IRecentsManager } from '@jupyterlab/docmanager';
import { IStateDB } from '@jupyterlab/statedb';
import { ITranslator } from '@jupyterlab/translation';
import {
CommandToolbarButton,
launcherIcon,
runningIcon
} from '@jupyterlab/ui-components';
import { addKernelRunningSessionManager } from './kernels';
import { addOpenTabsSessionManager } from './opentabs';
import { addRecentlyClosedSessionManager } from './recents';
/**
* The command IDs used by the running plugin.
*/
export namespace CommandIDs {
export const kernelNewConsole = 'running:kernel-new-console';
export const kernelNewNotebook = 'running:kernel-new-notebook';
export const kernelOpenSession = 'running:kernel-open-session';
export const kernelShutDown = 'running:kernel-shut-down';
export const showPanel = 'running:show-panel';
export const showModal = 'running:show-modal';
}
/**
* The default running sessions extension.
*/
const plugin: JupyterFrontEndPlugin<IRunningSessionManagers> = {
id: '@jupyterlab/running-extension:plugin',
description: 'Provides the running session managers.',
provides: IRunningSessionManagers,
requires: [ITranslator],
optional: [ILabShell],
autoStart: true,
activate: (
app: JupyterFrontEnd,
translator: ITranslator,
labShell: ILabShell | null
): IRunningSessionManagers => {
const runningSessionManagers = new RunningSessionManagers();
if (labShell) {
addOpenTabsSessionManager(runningSessionManagers, translator, labShell);
}
void addKernelRunningSessionManager(
runningSessionManagers,
translator,
app
);
return runningSessionManagers;
}
};
/**
* The plugin enabling the running sidebar.
*/
const sidebarPlugin: JupyterFrontEndPlugin<IRunningSessionSidebar> = {
id: '@jupyterlab/running-extension:sidebar',
description: 'Provides the running session sidebar.',
provides: IRunningSessionSidebar,
requires: [IRunningSessionManagers, ITranslator],
optional: [ILayoutRestorer, IStateDB],
autoStart: true,
activate: (
app: JupyterFrontEnd,
manager: IRunningSessionManagers,
translator: ITranslator,
restorer: ILayoutRestorer | null,
state: IStateDB | null
): IRunningSessionSidebar => {
const trans = translator.load('jupyterlab');
const running = new RunningSessions(manager, translator, state);
running.id = 'jp-running-sessions';
running.title.caption = trans.__('Running Terminals and Kernels');
running.title.icon = runningIcon;
running.node.setAttribute('role', 'region');
running.node.setAttribute(
'aria-label',
trans.__('Running Sessions section')
);
// Let the application restorer track the running panel for restoration of
// application state (e.g. setting the running panel as the current side bar
// widget).
if (restorer) {
restorer.add(running, 'running-sessions');
}
// Rank has been chosen somewhat arbitrarily to give priority to the running
// sessions widget in the sidebar.
app.shell.add(running, 'left', { rank: 200, type: 'Sessions and Tabs' });
app.commands.addCommand(CommandIDs.showPanel, {
label: trans.__('Sessions and Tabs'),
execute: () => {
app.shell.activateById(running.id);
}
});
return running;
}
};
/**
* An optional adding recently closed tabs.
*/
const recentsPlugin: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/running-extension:recently-closed',
description: 'Adds recently closed documents list.',
requires: [IRunningSessionManagers, IRecentsManager, ITranslator],
autoStart: true,
activate: (
app: JupyterFrontEnd,
manager: IRunningSessionManagers,
recents: IRecentsManager,
translator: ITranslator
): void => {
addRecentlyClosedSessionManager(
manager,
recents,
app.commands,
app.docRegistry,
translator
);
}
};
/**
* An optional plugin allowing to among running items.
*/
const searchPlugin: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/running-extension:search-tabs',
description: 'Adds a widget to search open and closed tabs.',
requires: [IRunningSessionManagers, ITranslator],
optional: [ICommandPalette, IRunningSessionSidebar],
autoStart: true,
activate: (
app: JupyterFrontEnd,
manager: IRunningSessionManagers,
translator: ITranslator,
palette: ICommandPalette | null,
sidebar: IRunningSessionSidebar | null
): void => {
const trans = translator.load('jupyterlab');
app.commands.addCommand(CommandIDs.showModal, {
execute: () => {
const running = new SearchableSessions(manager, translator);
const dialog = new Dialog({
title: trans.__('Tabs and Running Sessions'),
body: running,
buttons: [Dialog.okButton({})],
hasClose: true
});
dialog.addClass('jp-SearchableSessions-modal');
return dialog.launch();
},
label: trans.__('Search Tabs and Running Sessions')
});
if (palette) {
palette.addItem({
command: CommandIDs.showModal,
category: trans.__('Running')
});
}
if (sidebar) {
const button = new CommandToolbarButton({
commands: app.commands,
id: CommandIDs.showModal,
icon: launcherIcon,
label: ''
});
sidebar.toolbar.addItem('open-as-modal', button);
}
}
};
/**
* Export the plugins.
*/
export default [plugin, sidebarPlugin, recentsPlugin, searchPlugin];