-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin.tsx
258 lines (231 loc) · 7.02 KB
/
plugin.tsx
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application';
import {
ICommandPalette,
IToolbarWidgetRegistry,
InputDialog,
ReactWidget
} from '@jupyterlab/apputils';
import { IMainMenu, MainMenu } from '@jupyterlab/mainmenu';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ConfigSection } from '@jupyterlab/services';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { Widget } from '@lumino/widgets';
import React from 'react';
import { TourContainer } from './components';
import { CommandIDs, NOTEBOOK_ID, WELCOME_ID } from './constants';
import { addTours } from './defaults';
import { tourIcon } from './icons';
import { TourButton } from './notebookButton';
import { NotebookTourManager } from './notebookTourManager';
import {
DEFAULTS_PLUGIN_ID,
INotebookTourManager,
ITourHandler,
ITourManager,
ITourState,
ITourTracker,
IUserTourManager,
NOTEBOOK_PLUGIN_ID,
NS,
PLUGIN_ID,
USER_PLUGIN_ID
} from './tokens';
import { TourHandler } from './tour';
import { TourManager } from './tourManager';
import { UserTourManager } from './userTourManager';
import { PromiseDelegate } from '@lumino/coreutils';
/**
* Initialization data for the jupyterlab-tour extension.
*/
const corePlugin: JupyterFrontEndPlugin<ITourManager> = {
id: PLUGIN_ID,
autoStart: true,
activate,
optional: [ICommandPalette, IMainMenu, ITranslator],
provides: ITourManager
};
function activate(
app: JupyterFrontEnd,
palette: ICommandPalette | null,
menu: MainMenu | null,
translator: ITranslator | null
): ITourManager {
const CONFIG_SECTION_NAME = corePlugin.id.replace(/[^\w]/g, '');
const { commands } = app;
translator = translator ?? nullTranslator;
const restoreState = new PromiseDelegate<ITourState[]>();
const configReady = ConfigSection.create({
name: CONFIG_SECTION_NAME
}).catch(error => {
console.error('Failed to fetch state for jupyterlab-tour.', error);
});
const tracker: ITourTracker = Object.freeze({
restored: restoreState.promise,
save: async (state: ITourState[]) => {
await restoreState.promise;
(await configReady)?.update({ state } as any);
}
});
// Create tour manager
const manager = new TourManager({ helpMenu: menu?.helpMenu, tracker, translator });
void Promise.all([
app.restored,
// Use config instead of state to store independently of the workspace
// if a tour has been displayed or not.
configReady
])
.then(async ([_, config]) => {
restoreState.resolve((config?.data['state'] ?? []) as any);
})
.catch(error => {
console.error('Failed to load tour states.', error);
restoreState.reject(error);
});
const trans = manager.translator;
commands.addCommand(CommandIDs.launch, {
label: args => {
if (args['id']) {
const tour = manager.tours.get(args['id'] as string) as TourHandler;
return trans.__(tour.label);
} else {
return trans.__('Launch a Tour');
}
},
icon: args =>
(manager.tours.get(args['id'] as string) as TourHandler)?.icon || tourIcon,
usage: trans.__(
'Launch a tour.\nIf no id provided, prompt the user.\nArguments {id: Tour ID}'
),
isEnabled: () => !manager.activeTour,
execute: async args => {
let id = args['id'] as string;
const force = args['force'] === undefined ? true : (args['force'] as boolean);
if (!id) {
const answer = await InputDialog.getItem({
items: Array.from(manager.tours.keys()),
title: trans.__('Choose a tour')
});
if (answer.button.accept && answer.value) {
id = answer.value;
} else {
return;
}
}
await manager.ready;
manager.launch([id], force);
}
});
commands.addCommand(CommandIDs.addTour, {
label: trans.__('Add a tour'),
usage: trans.__(
'Add a tour and returns it.\nArguments {tour: ITour}\nReturns `null` if a failure occurs.'
),
execute: (args): ITourHandler | null => {
return manager.addTour(args.tour as any);
}
});
if (palette) {
palette.addItem({
category: trans.__('Help'),
command: CommandIDs.launch
});
}
const tourContainer = ReactWidget.create(
<TourContainer tourLaunched={manager.tourLaunched} />
);
Widget.attach(tourContainer, document.body);
return manager;
}
/**
* Optional plugin for user-defined tours stored in the settings registry
*/
const userPlugin: JupyterFrontEndPlugin<IUserTourManager> = {
id: USER_PLUGIN_ID,
autoStart: true,
activate: activateUser,
requires: [ISettingRegistry, ITourManager],
optional: [ITranslator],
provides: IUserTourManager
};
function activateUser(
app: JupyterFrontEnd,
settings: ISettingRegistry,
tourManager: ITourManager
): IUserTourManager {
const manager = new UserTourManager({
tourManager,
getSettings: (): Promise<ISettingRegistry.ISettings> =>
settings.load(USER_PLUGIN_ID)
});
return manager;
}
/**
* Optional plugin for notebook-defined tours stored in metadata
*/
const notebookPlugin: JupyterFrontEndPlugin<INotebookTourManager> = {
id: NOTEBOOK_PLUGIN_ID,
autoStart: true,
activate: activateNotebook,
requires: [INotebookTracker, ITourManager],
optional: [IToolbarWidgetRegistry],
provides: INotebookTourManager
};
function activateNotebook(
app: JupyterFrontEnd,
nbTracker: INotebookTracker,
tourManager: ITourManager,
toolbarRegistry: IToolbarWidgetRegistry | null
): INotebookTourManager {
const manager = new NotebookTourManager({
tourManager
});
nbTracker.widgetAdded.connect((nbTracker, panel) =>
manager.addNotebook(panel.content)
);
toolbarRegistry?.addFactory(
'Notebook',
NS,
(panel: NotebookPanel) => new TourButton(panel.content, manager)
);
return manager;
}
/**
* Optional plugin for the curated default tours and default toast behavior
*/
const defaultsPlugin: JupyterFrontEndPlugin<void> = {
id: DEFAULTS_PLUGIN_ID,
autoStart: true,
activate: activateDefaults,
requires: [ITourManager],
optional: [INotebookTracker]
};
function activateDefaults(
app: JupyterFrontEnd,
tourManager: ITourManager,
nbTracker?: INotebookTracker
): void {
addTours(tourManager, app, nbTracker);
if (
nbTracker &&
(app.name !== 'Jupyter Notebook' ||
window.location.pathname.match(/\/notebooks\/.+$/))
) {
nbTracker.widgetAdded.connect(() => {
if (tourManager.tours.has(NOTEBOOK_ID)) {
tourManager.launch([NOTEBOOK_ID], false);
}
});
}
Promise.all([app.restored, tourManager.ready]).then(() => {
if (
tourManager.tours.has(WELCOME_ID) &&
(app.name !== 'Jupyter Notebook' ||
window.location.pathname.match(/\/tree(\/.+)?$/))
) {
// Wait 3s before launching the first tour - to be sure element are loaded
setTimeout(() => tourManager.launch([WELCOME_ID], false), 3000);
}
});
}
export default [corePlugin, userPlugin, notebookPlugin, defaultsPlugin];