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
/
index.ts
209 lines (179 loc) · 5.15 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
207
208
209
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ILabShell,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ICommandPalette, IToolbarWidgetRegistry } from '@jupyterlab/apputils';
import { PageConfig } from '@jupyterlab/coreutils';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { ITranslator } from '@jupyterlab/translation';
import { Menu, MenuBar } from '@lumino/widgets';
import { IRetroShell } from '@retrolab/application';
/**
* The command IDs used by the application plugin.
*/
namespace CommandIDs {
/**
* Launch RetroLab Tree
*/
export const launchRetroTree = 'retrolab:launch-tree';
/**
* Open RetroLab
*/
export const openRetro = 'retrolab:open-retro';
/**
* Open in Classic Notebook
*/
export const openClassic = 'retrolab:open-classic';
/**
* Open in JupyterLab
*/
export const openLab = 'retrolab:open-lab';
}
interface ISwitcherChoice {
command: string;
commandLabel: string;
buttonLabel: string;
urlPrefix: string;
}
/**
* A plugin to add custom toolbar items to the notebook page
*/
const launchButtons: JupyterFrontEndPlugin<void> = {
id: '@retrolab/lab-extension:interface-switcher',
autoStart: true,
requires: [ITranslator],
optional: [
INotebookTracker,
ICommandPalette,
IMainMenu,
IRetroShell,
ILabShell,
IToolbarWidgetRegistry
],
activate: (
app: JupyterFrontEnd,
translator: ITranslator,
notebookTracker: INotebookTracker | null,
palette: ICommandPalette | null,
menu: IMainMenu | null,
retroShell: IRetroShell | null,
labShell: ILabShell | null,
toolbarRegistry: IToolbarWidgetRegistry | null
) => {
if (!notebookTracker) {
// to prevent showing the toolbar button in non-notebook pages
return;
}
const { commands, shell } = app;
const baseUrl = PageConfig.getBaseUrl();
const trans = translator.load('retrolab');
const menubar = new MenuBar();
const switcher = new Menu({ commands });
switcher.title.label = trans.__('Interface');
menubar.addMenu(switcher);
const isEnabled = () => {
return (
notebookTracker.currentWidget !== null &&
notebookTracker.currentWidget === shell.currentWidget
);
};
const addInterface = (option: ISwitcherChoice) => {
const { command, commandLabel, urlPrefix } = option;
commands.addCommand(command, {
label: args => (args.noLabel ? '' : commandLabel),
caption: commandLabel,
execute: () => {
const current = notebookTracker.currentWidget;
if (!current) {
return;
}
window.open(`${urlPrefix}${current.context.path}`);
},
isEnabled
});
if (palette) {
palette.addItem({ command, category: 'Other' });
}
if (menu) {
menu.viewMenu.addGroup([{ command }], 1);
}
switcher.addItem({ command });
};
// always add Classic
addInterface({
command: 'retrolab:open-classic',
commandLabel: trans.__('Open With %1', 'Classic Notebook'),
buttonLabel: 'openClassic',
urlPrefix: `${baseUrl}tree/`
});
if (!retroShell) {
addInterface({
command: 'retrolab:open-retro',
commandLabel: trans.__('Open With %1', 'RetroLab'),
buttonLabel: 'openRetro',
urlPrefix: `${baseUrl}retro/tree/`
});
}
if (!labShell) {
addInterface({
command: 'retrolab:open-lab',
commandLabel: trans.__('Open With %1', 'JupyterLab'),
buttonLabel: 'openLab',
urlPrefix: `${baseUrl}doc/tree/`
});
}
if (toolbarRegistry) {
toolbarRegistry.registerFactory<NotebookPanel>(
'Notebook',
'interfaceSwitcher',
panel => {
const menubar = new MenuBar();
menubar.addMenu(switcher);
menubar.addClass('jp-InterfaceSwitcher');
return menubar;
}
);
}
}
};
/**
* A plugin to add a command to open the RetroLab Tree.
*/
const launchRetroTree: JupyterFrontEndPlugin<void> = {
id: '@retrolab/lab-extension:launch-retrotree',
autoStart: true,
requires: [ITranslator],
optional: [IMainMenu, ICommandPalette],
activate: (
app: JupyterFrontEnd,
translator: ITranslator,
menu: IMainMenu | null,
palette: ICommandPalette | null
): void => {
const { commands } = app;
const trans = translator.load('retrolab');
const category = trans.__('Help');
commands.addCommand(CommandIDs.launchRetroTree, {
label: trans.__('Launch RetroLab File Browser'),
execute: () => {
window.open(PageConfig.getBaseUrl() + 'retro/tree');
}
});
if (menu) {
const helpMenu = menu.helpMenu;
helpMenu.addGroup([{ command: CommandIDs.launchRetroTree }], 1);
}
if (palette) {
palette.addItem({ command: CommandIDs.launchRetroTree, category });
}
}
};
/**
* Export the plugins as default.
*/
const plugins: JupyterFrontEndPlugin<any>[] = [launchRetroTree, launchButtons];
export default plugins;