-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
index.ts
206 lines (178 loc) · 5.49 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
JupyterLab, JupyterLabPlugin, ILayoutRestorer, LayoutRestorer
} from '@jupyterlab/application';
import {
Dialog, ICommandPalette, showDialog
} from '@jupyterlab/apputils';
import {
IStateDB
} from '@jupyterlab/coreutils';
import {
h
} from '@phosphor/virtualdom';
/**
* The command IDs used by the application plugin.
*/
namespace CommandIDs {
export
const activateNextTab: string = 'application:activate-next-tab';
export
const activatePreviousTab: string = 'application:activate-previous-tab';
export
const closeAll: string = 'application:close-all';
export
const setMode: string = 'application:set-mode';
export
const toggleMode: string = 'application:toggle-mode';
}
/**
* The main extension.
*/
const main: JupyterLabPlugin<void> = {
id: '@jupyterlab/application-extension:main',
requires: [ICommandPalette],
activate: (app: JupyterLab, palette: ICommandPalette) => {
addCommands(app, palette);
// If the currently active widget changes,
// trigger a refresh of the commands.
app.shell.currentChanged.connect(() => {
app.commands.notifyCommandChanged(CommandIDs.closeAll);
});
let builder = app.serviceManager.builder;
let doBuild = () => {
return builder.build().then(() => {
return showDialog({
title: 'Build Complete',
body: 'Build successfully completed, reload page?',
buttons: [Dialog.cancelButton(),
Dialog.warnButton({ label: 'RELOAD' })]
});
}).then(result => {
if (result.button.accept) {
location.reload();
}
}).catch(err => {
showDialog({
title: 'Build Failed',
body: h.pre(err.message)
});
});
};
if (builder.isAvailable && builder.shouldCheck) {
builder.getStatus().then(response => {
if (response.status === 'building') {
return doBuild();
}
if (response.status !== 'needed') {
return;
}
let body = h.div(
h.p(
'JupyterLab build is suggested:',
h.br(),
h.pre(response.message)
)
);
showDialog({
title: 'Build Recommended',
body,
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'BUILD' })]
}).then(result => {
if (result.button.accept) {
return doBuild();
}
});
});
}
const message = 'Are you sure you want to exit JupyterLab?\n' +
'Any unsaved changes will be lost.';
// The spec for the `beforeunload` event is implemented differently by
// the different browser vendors. Consequently, the `event.returnValue`
// attribute needs to set in addition to a return value being returned.
// For more information, see:
// https://developer.mozilla.org/en/docs/Web/Events/beforeunload
window.addEventListener('beforeunload', event => {
if (app.isDirty) {
return (event as any).returnValue = message;
}
});
},
autoStart: true
};
/**
* The default layout restorer provider.
*/
const layout: JupyterLabPlugin<ILayoutRestorer> = {
id: '@jupyterlab/application-extension:layout',
requires: [IStateDB],
activate: (app: JupyterLab, state: IStateDB) => {
const first = app.started;
const registry = app.commands;
let restorer = new LayoutRestorer({ first, registry, state });
restorer.fetch().then(saved => {
app.shell.restoreLayout(saved);
app.shell.layoutModified.connect(() => {
restorer.save(app.shell.saveLayout());
});
});
return restorer;
},
autoStart: true,
provides: ILayoutRestorer
};
/**
* Add the main application commands.
*/
function addCommands(app: JupyterLab, palette: ICommandPalette): void {
const category = 'Main Area';
let command = CommandIDs.activateNextTab;
app.commands.addCommand(command, {
label: 'Activate Next Tab',
execute: () => { app.shell.activateNextTab(); }
});
palette.addItem({ command, category });
command = CommandIDs.activatePreviousTab;
app.commands.addCommand(command, {
label: 'Activate Previous Tab',
execute: () => { app.shell.activatePreviousTab(); }
});
palette.addItem({ command, category });
command = CommandIDs.closeAll;
app.commands.addCommand(command, {
label: 'Close All Widgets',
execute: () => { app.shell.closeAll(); }
});
palette.addItem({ command, category });
command = CommandIDs.setMode;
app.commands.addCommand(command, {
isVisible: args => {
const mode = args['mode'] as string;
return mode === 'single-document' || mode === 'multiple-document';
},
execute: args => {
const mode = args['mode'] as string;
if (mode === 'single-document' || mode === 'multiple-document') {
app.shell.mode = mode;
return;
}
throw new Error(`Unsupported application shell mode: ${mode}`);
}
});
command = CommandIDs.toggleMode;
app.commands.addCommand(command, {
label: 'Toggle Single-Document Mode',
execute: () => {
const args = app.shell.mode === 'multiple-document' ?
{ mode: 'single-document' } : { mode: 'multiple-document' };
return app.commands.execute(CommandIDs.setMode, args);
}
});
palette.addItem({ command, category });
}
/**
* Export the plugins as default.
*/
const plugins: JupyterLabPlugin<any>[] = [main, layout];
export default plugins;