@jupyterlab/apputils 4.0.0-alpha.2 → 4.0.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/clipboard.d.ts +1 -1
- package/lib/dialog.d.ts +73 -4
- package/lib/dialog.js +143 -30
- package/lib/dialog.js.map +1 -1
- package/lib/domutils.js.map +1 -1
- package/lib/index.d.ts +6 -5
- package/lib/index.js +6 -5
- package/lib/index.js.map +1 -1
- package/lib/inputdialog.d.ts +35 -3
- package/lib/inputdialog.js +122 -12
- package/lib/inputdialog.js.map +1 -1
- package/lib/kernelstatuses.d.ts +74 -2
- package/lib/kernelstatuses.js +142 -2
- package/lib/kernelstatuses.js.map +1 -1
- package/lib/mainareawidget.d.ts +9 -0
- package/lib/mainareawidget.js +29 -4
- package/lib/mainareawidget.js.map +1 -1
- package/lib/menufactory.js +14 -15
- package/lib/menufactory.js.map +1 -1
- package/lib/notification.d.ts +336 -0
- package/lib/notification.js +310 -0
- package/lib/notification.js.map +1 -0
- package/lib/runningSessions.d.ts +72 -0
- package/lib/runningSessions.js +119 -0
- package/lib/runningSessions.js.map +1 -0
- package/lib/sanitizer.d.ts +6 -4
- package/lib/sanitizer.js +32 -12
- package/lib/sanitizer.js.map +1 -1
- package/lib/semanticCommand.js +11 -2
- package/lib/semanticCommand.js.map +1 -1
- package/lib/sessioncontext.d.ts +2 -3
- package/lib/sessioncontext.js +46 -27
- package/lib/sessioncontext.js.map +1 -1
- package/lib/testutils.d.ts +5 -0
- package/lib/testutils.js +46 -0
- package/lib/testutils.js.map +1 -0
- package/lib/thememanager.js +13 -4
- package/lib/thememanager.js.map +1 -1
- package/lib/tokens.d.ts +26 -9
- package/lib/tokens.js +1 -6
- package/lib/tokens.js.map +1 -1
- package/lib/toolbar/factory.d.ts +12 -2
- package/lib/toolbar/factory.js +237 -145
- package/lib/toolbar/factory.js.map +1 -1
- package/lib/toolbar/index.js +4 -0
- package/lib/toolbar/index.js.map +1 -1
- package/lib/toolbar/registry.d.ts +11 -0
- package/lib/toolbar/registry.js +20 -3
- package/lib/toolbar/registry.js.map +1 -1
- package/lib/toolbar/widget.d.ts +3 -0
- package/lib/toolbar/widget.js +3 -0
- package/lib/toolbar/widget.js.map +1 -1
- package/lib/widgettracker.d.ts +13 -1
- package/lib/widgettracker.js +29 -2
- package/lib/widgettracker.js.map +1 -1
- package/package.json +30 -30
- package/src/clipboard.ts +107 -0
- package/src/commandlinker.ts +209 -0
- package/src/commandpalette.ts +175 -0
- package/src/dialog.tsx +1133 -0
- package/src/domutils.ts +53 -0
- package/src/index.ts +78 -0
- package/src/inputdialog.ts +551 -0
- package/src/kernelstatuses.tsx +279 -0
- package/src/mainareawidget.ts +362 -0
- package/src/menufactory.ts +201 -0
- package/src/notification.ts +559 -0
- package/src/printing.ts +180 -0
- package/src/runningSessions.tsx +233 -0
- package/src/sanitizer.ts +968 -0
- package/src/semanticCommand.ts +77 -0
- package/src/sessioncontext.tsx +1712 -0
- package/src/testutils.ts +49 -0
- package/src/thememanager.ts +519 -0
- package/src/tokens.ts +372 -0
- package/src/toolbar/factory.ts +422 -0
- package/src/toolbar/index.ts +8 -0
- package/src/toolbar/registry.ts +158 -0
- package/src/toolbar/widget.tsx +243 -0
- package/src/widgettracker.ts +391 -0
- package/src/windowresolver.ts +218 -0
- package/style/base.css +0 -1
- package/style/commandpalette.css +7 -10
- package/style/dialog.css +23 -7
- package/style/index.css +1 -0
- package/style/index.js +1 -0
- package/style/inputdialog.css +5 -0
- package/style/materialcolors.css +8 -21
- package/lib/hoverbox.d.ts +0 -82
- package/lib/hoverbox.js +0 -100
- package/lib/hoverbox.js.map +0 -1
- package/style/hoverbox.css +0 -13
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Jupyter Development Team.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { IObservableList, ObservableList } from '@jupyterlab/observables';
|
|
7
|
+
import { ISettingRegistry, SettingRegistry } from '@jupyterlab/settingregistry';
|
|
8
|
+
import { ITranslator, TranslationBundle } from '@jupyterlab/translation';
|
|
9
|
+
import { Toolbar } from '@jupyterlab/ui-components';
|
|
10
|
+
import { findIndex } from '@lumino/algorithm';
|
|
11
|
+
import { JSONExt, PartialJSONObject } from '@lumino/coreutils';
|
|
12
|
+
import { Widget } from '@lumino/widgets';
|
|
13
|
+
import { Dialog, showDialog } from '../dialog';
|
|
14
|
+
import { IToolbarWidgetRegistry, ToolbarRegistry } from '../tokens';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Default toolbar item rank
|
|
18
|
+
*
|
|
19
|
+
* #### Notes
|
|
20
|
+
* This will place item just before the white spacer item in the notebook toolbar.
|
|
21
|
+
*/
|
|
22
|
+
const DEFAULT_TOOLBAR_ITEM_RANK = 50;
|
|
23
|
+
|
|
24
|
+
const TOOLBAR_KEY = 'jupyter.lab.toolbars';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Display warning when the toolbar definition have been modified.
|
|
28
|
+
*
|
|
29
|
+
* @param trans Translation bundle
|
|
30
|
+
*/
|
|
31
|
+
async function displayInformation(trans: TranslationBundle): Promise<void> {
|
|
32
|
+
const result = await showDialog({
|
|
33
|
+
title: trans.__('Information'),
|
|
34
|
+
body: trans.__(
|
|
35
|
+
'Toolbar customization has changed. You will need to reload JupyterLab to see the changes.'
|
|
36
|
+
),
|
|
37
|
+
buttons: [
|
|
38
|
+
Dialog.cancelButton(),
|
|
39
|
+
Dialog.okButton({ label: trans.__('Reload') })
|
|
40
|
+
]
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (result.button.accept) {
|
|
44
|
+
location.reload();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Set the toolbar definition by accumulating all settings definition.
|
|
50
|
+
*
|
|
51
|
+
* The list will be populated only with the enabled items.
|
|
52
|
+
*
|
|
53
|
+
* @param toolbarItems Observable list to populate
|
|
54
|
+
* @param registry Application settings registry
|
|
55
|
+
* @param factoryName Widget factory name that needs a toolbar
|
|
56
|
+
* @param pluginId Settings plugin id
|
|
57
|
+
* @param translator Translator object
|
|
58
|
+
* @param propertyId Property holding the toolbar definition in the settings; default 'toolbar'
|
|
59
|
+
* @returns List of toolbar items
|
|
60
|
+
*/
|
|
61
|
+
async function setToolbarItems(
|
|
62
|
+
toolbarItems: IObservableList<ISettingRegistry.IToolbarItem>,
|
|
63
|
+
registry: ISettingRegistry,
|
|
64
|
+
factoryName: string,
|
|
65
|
+
pluginId: string,
|
|
66
|
+
translator: ITranslator,
|
|
67
|
+
propertyId: string = 'toolbar'
|
|
68
|
+
): Promise<void> {
|
|
69
|
+
const trans = translator.load('jupyterlab');
|
|
70
|
+
|
|
71
|
+
let canonical: ISettingRegistry.ISchema | null = null;
|
|
72
|
+
let loaded: { [name: string]: ISettingRegistry.IToolbarItem[] } = {};
|
|
73
|
+
let listenPlugin = true;
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
/**
|
|
77
|
+
* Populate the plugin's schema defaults.
|
|
78
|
+
*
|
|
79
|
+
* We keep track of disabled entries in case the plugin is loaded
|
|
80
|
+
* after the toolbar initialization.
|
|
81
|
+
*/
|
|
82
|
+
function populate(schema: ISettingRegistry.ISchema) {
|
|
83
|
+
loaded = {};
|
|
84
|
+
|
|
85
|
+
const pluginDefaults = Object.keys(registry.plugins)
|
|
86
|
+
// Filter out the current plugin (will be listed when reloading)
|
|
87
|
+
// because we control its addition after the mapping step
|
|
88
|
+
.filter(plugin => plugin !== pluginId)
|
|
89
|
+
.map(plugin => {
|
|
90
|
+
const items =
|
|
91
|
+
(registry.plugins[plugin]!.schema[TOOLBAR_KEY] ?? {})[
|
|
92
|
+
factoryName
|
|
93
|
+
] ?? [];
|
|
94
|
+
loaded[plugin] = items;
|
|
95
|
+
return items;
|
|
96
|
+
})
|
|
97
|
+
.concat([(schema[TOOLBAR_KEY] ?? {})[factoryName] ?? []])
|
|
98
|
+
.reduceRight(
|
|
99
|
+
(acc, val) => SettingRegistry.reconcileToolbarItems(acc, val, true),
|
|
100
|
+
[]
|
|
101
|
+
)!;
|
|
102
|
+
|
|
103
|
+
// Apply default value as last step to take into account overrides.json
|
|
104
|
+
// The standard toolbars default is [] as the plugin must use
|
|
105
|
+
// `jupyter.lab.toolbars.<factory>` to define its default value.
|
|
106
|
+
schema.properties![propertyId].default =
|
|
107
|
+
SettingRegistry.reconcileToolbarItems(
|
|
108
|
+
pluginDefaults,
|
|
109
|
+
schema.properties![propertyId].default as any[],
|
|
110
|
+
true
|
|
111
|
+
)!.sort(
|
|
112
|
+
(a, b) =>
|
|
113
|
+
(a.rank ?? DEFAULT_TOOLBAR_ITEM_RANK) -
|
|
114
|
+
(b.rank ?? DEFAULT_TOOLBAR_ITEM_RANK)
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Transform the plugin object to return different schema than the default.
|
|
119
|
+
registry.transform(pluginId, {
|
|
120
|
+
compose: plugin => {
|
|
121
|
+
// Only override the canonical schema the first time.
|
|
122
|
+
if (!canonical) {
|
|
123
|
+
canonical = JSONExt.deepCopy(plugin.schema);
|
|
124
|
+
populate(canonical);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const defaults =
|
|
128
|
+
((canonical.properties ?? {})[propertyId] ?? {}).default ?? [];
|
|
129
|
+
// Initialize the settings
|
|
130
|
+
const user: PartialJSONObject = plugin.data.user;
|
|
131
|
+
const composite: PartialJSONObject = plugin.data.composite;
|
|
132
|
+
// Overrides the value with using the aggregated default for the toolbar property
|
|
133
|
+
user[propertyId] =
|
|
134
|
+
(plugin.data.user[propertyId] as ISettingRegistry.IToolbarItem[]) ??
|
|
135
|
+
[];
|
|
136
|
+
composite[propertyId] = (
|
|
137
|
+
SettingRegistry.reconcileToolbarItems(
|
|
138
|
+
defaults as ISettingRegistry.IToolbarItem[],
|
|
139
|
+
user[propertyId] as ISettingRegistry.IToolbarItem[],
|
|
140
|
+
false
|
|
141
|
+
) ?? []
|
|
142
|
+
).sort(
|
|
143
|
+
(a, b) =>
|
|
144
|
+
(a.rank ?? DEFAULT_TOOLBAR_ITEM_RANK) -
|
|
145
|
+
(b.rank ?? DEFAULT_TOOLBAR_ITEM_RANK)
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
plugin.data = { composite, user };
|
|
149
|
+
|
|
150
|
+
return plugin;
|
|
151
|
+
},
|
|
152
|
+
fetch: plugin => {
|
|
153
|
+
// Only override the canonical schema the first time.
|
|
154
|
+
if (!canonical) {
|
|
155
|
+
canonical = JSONExt.deepCopy(plugin.schema);
|
|
156
|
+
populate(canonical);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
data: plugin.data,
|
|
161
|
+
id: plugin.id,
|
|
162
|
+
raw: plugin.raw,
|
|
163
|
+
schema: canonical,
|
|
164
|
+
version: plugin.version
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (error.name === 'TransformError') {
|
|
170
|
+
// Assume the existing transformer is the toolbar builder transformer
|
|
171
|
+
// from another factory set up.
|
|
172
|
+
listenPlugin = false;
|
|
173
|
+
} else {
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Repopulate the canonical variable after the setting registry has
|
|
179
|
+
// preloaded all initial plugins.
|
|
180
|
+
const settings = await registry.load(pluginId);
|
|
181
|
+
|
|
182
|
+
// React to customization by the user
|
|
183
|
+
settings.changed.connect(() => {
|
|
184
|
+
const newItems: ISettingRegistry.IToolbarItem[] =
|
|
185
|
+
(settings.composite[propertyId] as any) ?? [];
|
|
186
|
+
|
|
187
|
+
transferSettings(newItems);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const transferSettings = (newItems: ISettingRegistry.IToolbarItem[]) => {
|
|
191
|
+
// This is not optimal but safer because a toolbar item with the same
|
|
192
|
+
// name cannot be inserted (it will be a no-op). But that could happen
|
|
193
|
+
// if the settings are changing the items order.
|
|
194
|
+
toolbarItems.clear();
|
|
195
|
+
toolbarItems.pushAll(newItems.filter(item => !item.disabled));
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// Initialize the toolbar
|
|
199
|
+
transferSettings((settings.composite[propertyId] as any) ?? []);
|
|
200
|
+
|
|
201
|
+
// React to plugin changes if no other transformer exists, otherwise bail.
|
|
202
|
+
if (!listenPlugin) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
registry.pluginChanged.connect(async (sender, plugin) => {
|
|
206
|
+
// Since the plugin storing the toolbar definition is transformed above,
|
|
207
|
+
// if it has changed, it means that a request to reload was triggered.
|
|
208
|
+
// Hence the toolbar definitions from the other plugins have been
|
|
209
|
+
// automatically reset during the transform step.
|
|
210
|
+
if (plugin === pluginId) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
// If a plugin changed its toolbar items
|
|
214
|
+
const oldItems = loaded[plugin] ?? [];
|
|
215
|
+
const newItems =
|
|
216
|
+
(registry.plugins[plugin]!.schema[TOOLBAR_KEY] ?? {})[factoryName] ?? [];
|
|
217
|
+
if (!JSONExt.deepEqual(oldItems, newItems)) {
|
|
218
|
+
if (loaded[plugin]) {
|
|
219
|
+
// The plugin has changed, request the user to reload the UI
|
|
220
|
+
await displayInformation(trans);
|
|
221
|
+
} else {
|
|
222
|
+
if (newItems.length > 0) {
|
|
223
|
+
canonical = null;
|
|
224
|
+
// This will trigger a settings.changed signal that will update the items
|
|
225
|
+
await registry.reload(pluginId);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Create the toolbar factory for a given container widget based
|
|
234
|
+
* on a data description stored in settings
|
|
235
|
+
*
|
|
236
|
+
* @param toolbarRegistry Toolbar widgets registry
|
|
237
|
+
* @param settingsRegistry Settings registry
|
|
238
|
+
* @param factoryName Toolbar container factory name
|
|
239
|
+
* @param pluginId Settings plugin id
|
|
240
|
+
* @param translator Translator
|
|
241
|
+
* @param propertyId Toolbar definition key in the settings plugin
|
|
242
|
+
* @returns List of toolbar widgets factory
|
|
243
|
+
*/
|
|
244
|
+
export function createToolbarFactory(
|
|
245
|
+
toolbarRegistry: IToolbarWidgetRegistry,
|
|
246
|
+
settingsRegistry: ISettingRegistry,
|
|
247
|
+
factoryName: string,
|
|
248
|
+
pluginId: string,
|
|
249
|
+
translator: ITranslator,
|
|
250
|
+
propertyId: string = 'toolbar'
|
|
251
|
+
): (widget: Widget) => IObservableList<ToolbarRegistry.IToolbarItem> {
|
|
252
|
+
const items = new ObservableList<ISettingRegistry.IToolbarItem>({
|
|
253
|
+
itemCmp: (a, b) => JSONExt.deepEqual(a as any, b as any)
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// Get toolbar definition from the settings
|
|
257
|
+
setToolbarItems(
|
|
258
|
+
items,
|
|
259
|
+
settingsRegistry,
|
|
260
|
+
factoryName,
|
|
261
|
+
pluginId,
|
|
262
|
+
translator,
|
|
263
|
+
propertyId
|
|
264
|
+
).catch(reason => {
|
|
265
|
+
console.error(
|
|
266
|
+
`Failed to load toolbar items for factory ${factoryName} from ${pluginId}`,
|
|
267
|
+
reason
|
|
268
|
+
);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
return (widget: Widget) => {
|
|
272
|
+
const updateToolbar = (
|
|
273
|
+
list: IObservableList<ToolbarRegistry.IWidget>,
|
|
274
|
+
change: IObservableList.IChangedArgs<ToolbarRegistry.IWidget>
|
|
275
|
+
) => {
|
|
276
|
+
switch (change.type) {
|
|
277
|
+
case 'move':
|
|
278
|
+
toolbar.move(change.oldIndex, change.newIndex);
|
|
279
|
+
break;
|
|
280
|
+
case 'add':
|
|
281
|
+
change.newValues.forEach(item =>
|
|
282
|
+
toolbar.push({
|
|
283
|
+
name: item.name,
|
|
284
|
+
widget: toolbarRegistry.createWidget(factoryName, widget, item)
|
|
285
|
+
})
|
|
286
|
+
);
|
|
287
|
+
break;
|
|
288
|
+
case 'remove':
|
|
289
|
+
change.oldValues.forEach(() => toolbar.remove(change.oldIndex));
|
|
290
|
+
break;
|
|
291
|
+
case 'set':
|
|
292
|
+
change.newValues.forEach(item =>
|
|
293
|
+
toolbar.set(change.newIndex, {
|
|
294
|
+
name: item.name,
|
|
295
|
+
widget: toolbarRegistry.createWidget(factoryName, widget, item)
|
|
296
|
+
})
|
|
297
|
+
);
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const toolbar = new ObservableList<ToolbarRegistry.IToolbarItem>({
|
|
303
|
+
values: Array.from(items).map(item => {
|
|
304
|
+
return {
|
|
305
|
+
name: item.name,
|
|
306
|
+
widget: toolbarRegistry.createWidget(factoryName, widget, item)
|
|
307
|
+
};
|
|
308
|
+
})
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
items.changed.connect(updateToolbar);
|
|
312
|
+
widget.disposed.connect(() => {
|
|
313
|
+
items.changed.disconnect(updateToolbar);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
return toolbar;
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Set the toolbar items of a widget from a factory
|
|
322
|
+
*
|
|
323
|
+
* @param widget Widget with the toolbar to set
|
|
324
|
+
* @param factory Toolbar items factory
|
|
325
|
+
* @param toolbar Separated toolbar if widget is a raw widget
|
|
326
|
+
*/
|
|
327
|
+
export function setToolbar(
|
|
328
|
+
widget: Toolbar.IWidgetToolbar | Widget,
|
|
329
|
+
factory: (
|
|
330
|
+
widget: Widget
|
|
331
|
+
) =>
|
|
332
|
+
| IObservableList<ToolbarRegistry.IToolbarItem>
|
|
333
|
+
| ToolbarRegistry.IToolbarItem[],
|
|
334
|
+
toolbar?: Toolbar
|
|
335
|
+
): void {
|
|
336
|
+
// @ts-expect-error Widget has no toolbar
|
|
337
|
+
if (!widget.toolbar && !toolbar) {
|
|
338
|
+
console.log(
|
|
339
|
+
`Widget ${widget.id} has no 'toolbar' and no explicit toolbar was provided.`
|
|
340
|
+
);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// @ts-expect-error Widget has no toolbar
|
|
345
|
+
const toolbar_ = (widget.toolbar as Toolbar) ?? toolbar;
|
|
346
|
+
|
|
347
|
+
const items = factory(widget);
|
|
348
|
+
|
|
349
|
+
if (Array.isArray(items)) {
|
|
350
|
+
items.forEach(({ name, widget: item }) => {
|
|
351
|
+
toolbar_.addItem(name, item);
|
|
352
|
+
});
|
|
353
|
+
} else {
|
|
354
|
+
const updateToolbar = (
|
|
355
|
+
list: IObservableList<ToolbarRegistry.IToolbarItem>,
|
|
356
|
+
changes: IObservableList.IChangedArgs<ToolbarRegistry.IToolbarItem>
|
|
357
|
+
) => {
|
|
358
|
+
switch (changes.type) {
|
|
359
|
+
case 'add':
|
|
360
|
+
changes.newValues.forEach((item, index) => {
|
|
361
|
+
toolbar_.insertItem(
|
|
362
|
+
changes.newIndex + index,
|
|
363
|
+
item.name,
|
|
364
|
+
item.widget
|
|
365
|
+
);
|
|
366
|
+
});
|
|
367
|
+
break;
|
|
368
|
+
case 'move':
|
|
369
|
+
changes.oldValues.forEach(item => {
|
|
370
|
+
item.widget.parent = null;
|
|
371
|
+
});
|
|
372
|
+
changes.newValues.forEach((item, index) => {
|
|
373
|
+
toolbar_.insertItem(
|
|
374
|
+
changes.newIndex + index,
|
|
375
|
+
item.name,
|
|
376
|
+
item.widget
|
|
377
|
+
);
|
|
378
|
+
});
|
|
379
|
+
break;
|
|
380
|
+
case 'remove':
|
|
381
|
+
changes.oldValues.forEach(item => {
|
|
382
|
+
item.widget.parent = null;
|
|
383
|
+
});
|
|
384
|
+
break;
|
|
385
|
+
case 'set':
|
|
386
|
+
changes.oldValues.forEach(item => {
|
|
387
|
+
item.widget.parent = null;
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
changes.newValues.forEach((item, index) => {
|
|
391
|
+
const existingIndex = findIndex(
|
|
392
|
+
toolbar_.names(),
|
|
393
|
+
name => item.name === name
|
|
394
|
+
);
|
|
395
|
+
if (existingIndex >= 0) {
|
|
396
|
+
Array.from(toolbar_.children())[existingIndex].parent = null;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
toolbar_.insertItem(
|
|
400
|
+
changes.newIndex + index,
|
|
401
|
+
item.name,
|
|
402
|
+
item.widget
|
|
403
|
+
);
|
|
404
|
+
});
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
updateToolbar(items, {
|
|
410
|
+
newIndex: 0,
|
|
411
|
+
newValues: Array.from(items),
|
|
412
|
+
oldIndex: 0,
|
|
413
|
+
oldValues: [],
|
|
414
|
+
type: 'add'
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
items.changed.connect(updateToolbar);
|
|
418
|
+
widget.disposed.connect(() => {
|
|
419
|
+
items.changed.disconnect(updateToolbar);
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Jupyter Development Team.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
CommandToolbarButton,
|
|
8
|
+
LabIcon,
|
|
9
|
+
Toolbar
|
|
10
|
+
} from '@jupyterlab/ui-components';
|
|
11
|
+
import { CommandRegistry } from '@lumino/commands';
|
|
12
|
+
import { Widget } from '@lumino/widgets';
|
|
13
|
+
import { IToolbarWidgetRegistry, ToolbarRegistry } from '../tokens';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Concrete implementation of IToolbarWidgetRegistry interface
|
|
17
|
+
*/
|
|
18
|
+
export class ToolbarWidgetRegistry implements IToolbarWidgetRegistry {
|
|
19
|
+
constructor(options: ToolbarRegistry.IOptions) {
|
|
20
|
+
this._defaultFactory = options.defaultFactory;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Default toolbar item factory
|
|
25
|
+
*/
|
|
26
|
+
get defaultFactory(): (
|
|
27
|
+
widgetFactory: string,
|
|
28
|
+
widget: Widget,
|
|
29
|
+
toolbarItem: ToolbarRegistry.IWidget
|
|
30
|
+
) => Widget {
|
|
31
|
+
return this._defaultFactory;
|
|
32
|
+
}
|
|
33
|
+
set defaultFactory(
|
|
34
|
+
factory: (
|
|
35
|
+
widgetFactory: string,
|
|
36
|
+
widget: Widget,
|
|
37
|
+
toolbarItem: ToolbarRegistry.IWidget
|
|
38
|
+
) => Widget
|
|
39
|
+
) {
|
|
40
|
+
this._defaultFactory = factory;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a toolbar item widget
|
|
45
|
+
*
|
|
46
|
+
* @param widgetFactory The widget factory name that creates the toolbar
|
|
47
|
+
* @param widget The newly widget containing the toolbar
|
|
48
|
+
* @param toolbarItem The toolbar item definition
|
|
49
|
+
* @returns The widget to be inserted in the toolbar.
|
|
50
|
+
*/
|
|
51
|
+
createWidget(
|
|
52
|
+
widgetFactory: string,
|
|
53
|
+
widget: Widget,
|
|
54
|
+
toolbarItem: ToolbarRegistry.IWidget
|
|
55
|
+
): Widget {
|
|
56
|
+
const factory = this._widgets.get(widgetFactory)?.get(toolbarItem.name);
|
|
57
|
+
return factory
|
|
58
|
+
? factory(widget)
|
|
59
|
+
: this._defaultFactory(widgetFactory, widget, toolbarItem);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Add a new toolbar item factory
|
|
64
|
+
*
|
|
65
|
+
* @param widgetFactory The widget factory name that creates the toolbar
|
|
66
|
+
* @param toolbarItemName The unique toolbar item
|
|
67
|
+
* @param factory The factory function that receives the widget containing the toolbar and returns the toolbar widget.
|
|
68
|
+
* @returns The previously defined factory
|
|
69
|
+
*/
|
|
70
|
+
addFactory<T extends Widget = Widget>(
|
|
71
|
+
widgetFactory: string,
|
|
72
|
+
toolbarItemName: string,
|
|
73
|
+
factory: (main: T) => Widget
|
|
74
|
+
): ((main: T) => Widget) | undefined {
|
|
75
|
+
let namespace = this._widgets.get(widgetFactory);
|
|
76
|
+
const oldFactory = namespace?.get(toolbarItemName);
|
|
77
|
+
if (!namespace) {
|
|
78
|
+
namespace = new Map<string, (main: Widget) => Widget>();
|
|
79
|
+
this._widgets.set(widgetFactory, namespace);
|
|
80
|
+
}
|
|
81
|
+
namespace.set(toolbarItemName, factory);
|
|
82
|
+
return oldFactory;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Register a new toolbar item factory
|
|
87
|
+
*
|
|
88
|
+
* @param widgetFactory The widget factory name that creates the toolbar
|
|
89
|
+
* @param toolbarItemName The unique toolbar item
|
|
90
|
+
* @param factory The factory function that receives the widget containing the toolbar and returns the toolbar widget.
|
|
91
|
+
* @returns The previously defined factory
|
|
92
|
+
*
|
|
93
|
+
* @deprecated since v4 use `addFactory` instead
|
|
94
|
+
*/
|
|
95
|
+
registerFactory<T extends Widget = Widget>(
|
|
96
|
+
widgetFactory: string,
|
|
97
|
+
toolbarItemName: string,
|
|
98
|
+
factory: (main: T) => Widget
|
|
99
|
+
): ((main: T) => Widget) | undefined {
|
|
100
|
+
return this.addFactory(widgetFactory, toolbarItemName, factory);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
protected _defaultFactory: (
|
|
104
|
+
widgetFactory: string,
|
|
105
|
+
widget: Widget,
|
|
106
|
+
toolbarItem: ToolbarRegistry.IWidget
|
|
107
|
+
) => Widget;
|
|
108
|
+
protected _widgets: Map<string, Map<string, (main: Widget) => Widget>> =
|
|
109
|
+
new Map<string, Map<string, (main: Widget) => Widget>>();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Create the default toolbar item widget factory
|
|
114
|
+
*
|
|
115
|
+
* @param commands Application commands registry
|
|
116
|
+
* @returns Default factory
|
|
117
|
+
*/
|
|
118
|
+
export function createDefaultFactory(
|
|
119
|
+
commands: CommandRegistry
|
|
120
|
+
): (
|
|
121
|
+
widgetFactory: string,
|
|
122
|
+
widget: Widget,
|
|
123
|
+
toolbarItem: ToolbarRegistry.IWidget
|
|
124
|
+
) => Widget {
|
|
125
|
+
return (
|
|
126
|
+
widgetFactory: string,
|
|
127
|
+
widget: Widget,
|
|
128
|
+
toolbarItem: ToolbarRegistry.IWidget
|
|
129
|
+
) => {
|
|
130
|
+
switch (toolbarItem.type ?? 'command') {
|
|
131
|
+
case 'command': {
|
|
132
|
+
const {
|
|
133
|
+
command: tId,
|
|
134
|
+
args: tArgs,
|
|
135
|
+
label: tLabel,
|
|
136
|
+
icon: tIcon
|
|
137
|
+
} = toolbarItem;
|
|
138
|
+
const id = tId ?? '';
|
|
139
|
+
const args = { toolbar: true, ...tArgs };
|
|
140
|
+
const icon = tIcon ? LabIcon.resolve({ icon: tIcon }) : undefined;
|
|
141
|
+
// If there is an icon, undefined label will results in no label
|
|
142
|
+
// otherwise the label will be set using the setting or the command label
|
|
143
|
+
const label = icon ?? commands.icon(id, args) ? tLabel ?? '' : tLabel;
|
|
144
|
+
return new CommandToolbarButton({
|
|
145
|
+
commands,
|
|
146
|
+
id,
|
|
147
|
+
args,
|
|
148
|
+
icon,
|
|
149
|
+
label
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
case 'spacer':
|
|
153
|
+
return Toolbar.createSpacerItem();
|
|
154
|
+
default:
|
|
155
|
+
return new Widget();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|