@jupyterlab/ui-components 4.0.0-alpha.34 → 4.0.0-alpha.36
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/components/collapser.js +26 -10
- package/lib/components/collapser.js.map +1 -1
- package/lib/components/form.js +1 -1
- package/lib/components/form.js.map +1 -1
- package/lib/components/menu.d.ts +0 -7
- package/lib/components/menu.js +0 -9
- package/lib/components/menu.js.map +1 -1
- package/lib/components/vdom.d.ts +1 -1
- package/lib/components/windowedlist.d.ts +1 -1
- package/lib/components/windowedlist.js +3 -2
- package/lib/components/windowedlist.js.map +1 -1
- package/lib/hoverbox.d.ts +1 -1
- package/lib/icon/iconimports.d.ts +2 -0
- package/lib/icon/iconimports.js +4 -0
- package/lib/icon/iconimports.js.map +1 -1
- package/lib/icon/labicon.js +49 -48
- package/lib/icon/labicon.js.map +1 -1
- package/lib/icon/widgets/tabbarsvg.js +2 -1
- package/lib/icon/widgets/tabbarsvg.js.map +1 -1
- package/lib/style/icon.d.ts +0 -8
- package/lib/style/icon.js +1 -9
- package/lib/style/icon.js.map +1 -1
- package/package.json +20 -19
- package/src/FormRendererRegistry.tsx +60 -0
- package/src/components/accordiontoolbar.ts +252 -0
- package/src/components/button.tsx +45 -0
- package/src/components/collapser.ts +201 -0
- package/src/components/form.tsx +678 -0
- package/src/components/htmlselect.tsx +99 -0
- package/src/components/iframe.ts +153 -0
- package/src/components/index.ts +20 -0
- package/src/components/inputgroup.tsx +51 -0
- package/src/components/interface.ts +9 -0
- package/src/components/menu.ts +432 -0
- package/src/components/panelwithtoolbar.ts +41 -0
- package/src/components/search.tsx +216 -0
- package/src/components/sidepanel.ts +168 -0
- package/src/components/spinner.ts +31 -0
- package/src/components/styling.ts +114 -0
- package/src/components/switch.ts +103 -0
- package/src/components/toolbar.tsx +1119 -0
- package/src/components/vdom.ts +324 -0
- package/src/components/windowedlist.ts +1422 -0
- package/src/hoverbox.ts +401 -0
- package/src/icon/iconimports.ts +210 -0
- package/src/icon/index.ts +6 -0
- package/src/icon/labicon.tsx +955 -0
- package/src/icon/widgets/commandpalettesvg.ts +78 -0
- package/src/icon/widgets/index.ts +6 -0
- package/src/icon/widgets/menusvg.ts +218 -0
- package/src/icon/widgets/tabbarsvg.ts +122 -0
- package/src/index.ts +13 -0
- package/src/style/icon.ts +554 -0
- package/src/style/index.ts +4 -0
- package/src/svg.d.ts +58 -0
- package/src/tokens.ts +68 -0
- package/src/utils.ts +90 -0
- package/style/collapse.css +20 -6
- package/style/deprecated.css +10 -0
- package/style/icons/search/filter-dot.svg +8 -0
- package/style/icons/search/filter.svg +5 -0
- package/style/icons.css +4 -0
- package/style/rjsfTemplates.css +18 -13
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
/* global WeakRef */
|
|
5
|
+
|
|
6
|
+
import { ArrayExt } from '@lumino/algorithm';
|
|
7
|
+
import { CommandRegistry } from '@lumino/commands';
|
|
8
|
+
import { ReadonlyJSONObject } from '@lumino/coreutils';
|
|
9
|
+
import { DisposableDelegate, IDisposable } from '@lumino/disposable';
|
|
10
|
+
import { Signal } from '@lumino/signaling';
|
|
11
|
+
import { VirtualElement } from '@lumino/virtualdom';
|
|
12
|
+
import { Menu } from '@lumino/widgets';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Interface for disposable item menu
|
|
16
|
+
*/
|
|
17
|
+
export interface IDisposableMenuItem extends IDisposable, Menu.IItem {}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A common interface for extensible JupyterLab application menus.
|
|
21
|
+
*
|
|
22
|
+
* Plugins are still free to define their own menus in any way
|
|
23
|
+
* they like. However, JupyterLab defines a few top-level
|
|
24
|
+
* application menus that may be extended by plugins as well,
|
|
25
|
+
* such as "Edit" and "View"
|
|
26
|
+
*/
|
|
27
|
+
export interface IRankedMenu extends IDisposable {
|
|
28
|
+
/**
|
|
29
|
+
* Add a group of menu items specific to a particular
|
|
30
|
+
* plugin.
|
|
31
|
+
*
|
|
32
|
+
* The rank can be set for all items in the group using the
|
|
33
|
+
* function argument or per item.
|
|
34
|
+
*
|
|
35
|
+
* @param items - the list of menu items to add.
|
|
36
|
+
* @param rank - the default rank in the menu in which to insert the group.
|
|
37
|
+
* @returns Disposable of the group
|
|
38
|
+
*/
|
|
39
|
+
addGroup(items: Menu.IItemOptions[], rank?: number): IDisposable;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Add a menu item to the end of the menu.
|
|
43
|
+
*
|
|
44
|
+
* @param options - The options for creating the menu item.
|
|
45
|
+
*
|
|
46
|
+
* @returns The disposable menu item added to the menu.
|
|
47
|
+
*/
|
|
48
|
+
addItem(options: IRankedMenu.IItemOptions): IDisposable;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A read-only array of the menu items in the menu.
|
|
52
|
+
*/
|
|
53
|
+
readonly items: ReadonlyArray<Menu.IItem>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Menu rank
|
|
57
|
+
*/
|
|
58
|
+
readonly rank?: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Namespace for JupyterLabMenu interfaces
|
|
63
|
+
*/
|
|
64
|
+
export namespace IRankedMenu {
|
|
65
|
+
/**
|
|
66
|
+
* Default menu item rank
|
|
67
|
+
*/
|
|
68
|
+
export const DEFAULT_RANK = 100;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* An options object for creating a menu item.
|
|
72
|
+
*/
|
|
73
|
+
export interface IItemOptions extends Menu.IItemOptions {
|
|
74
|
+
/**
|
|
75
|
+
* Menu item rank
|
|
76
|
+
*/
|
|
77
|
+
rank?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* An options object for creating a JupyterLab menu.
|
|
82
|
+
*/
|
|
83
|
+
export interface IOptions extends Menu.IOptions {
|
|
84
|
+
/**
|
|
85
|
+
* Whether to include separators between the
|
|
86
|
+
* groups that are added to the menu.
|
|
87
|
+
*
|
|
88
|
+
* Default: true
|
|
89
|
+
*/
|
|
90
|
+
includeSeparators?: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Menu rank
|
|
93
|
+
*/
|
|
94
|
+
rank?: number;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* An extensible menu for JupyterLab application menus.
|
|
100
|
+
*/
|
|
101
|
+
export class RankedMenu extends Menu implements IRankedMenu {
|
|
102
|
+
/**
|
|
103
|
+
* Construct a new menu.
|
|
104
|
+
*
|
|
105
|
+
* @param options - Options for the lumino menu.
|
|
106
|
+
*/
|
|
107
|
+
constructor(options: IRankedMenu.IOptions) {
|
|
108
|
+
super(options);
|
|
109
|
+
this._rank = options.rank;
|
|
110
|
+
this._includeSeparators = options.includeSeparators ?? true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Menu rank.
|
|
115
|
+
*/
|
|
116
|
+
get rank(): number | undefined {
|
|
117
|
+
return this._rank;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Add a group of menu items specific to a particular
|
|
122
|
+
* plugin.
|
|
123
|
+
*
|
|
124
|
+
* The rank can be set for all items in the group using the
|
|
125
|
+
* function argument or per item.
|
|
126
|
+
*
|
|
127
|
+
* @param items - the list of menu items to add.
|
|
128
|
+
* @param rank - the default rank in the menu in which to insert the group.
|
|
129
|
+
* @returns Disposable of the group
|
|
130
|
+
*/
|
|
131
|
+
addGroup(items: IRankedMenu.IItemOptions[], rank?: number): IDisposable {
|
|
132
|
+
if (items.length === 0) {
|
|
133
|
+
return new DisposableDelegate(() => void 0);
|
|
134
|
+
}
|
|
135
|
+
const defaultRank = rank ?? IRankedMenu.DEFAULT_RANK;
|
|
136
|
+
|
|
137
|
+
const sortedItems = items
|
|
138
|
+
.map(item => {
|
|
139
|
+
return { ...item, rank: item.rank ?? defaultRank };
|
|
140
|
+
})
|
|
141
|
+
.sort((a, b) => a.rank - b.rank);
|
|
142
|
+
|
|
143
|
+
// Insert the plugin group into the menu.
|
|
144
|
+
let insertIndex = this._ranks.findIndex(rank => sortedItems[0].rank < rank);
|
|
145
|
+
if (insertIndex < 0) {
|
|
146
|
+
insertIndex = this._ranks.length; // Insert at the end of the menu
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Keep an array of the menu items that have been created.
|
|
150
|
+
const added: IDisposableMenuItem[] = [];
|
|
151
|
+
|
|
152
|
+
// Insert a separator before the group.
|
|
153
|
+
// Lumino takes care of superfluous leading,
|
|
154
|
+
// trailing, and duplicate separators.
|
|
155
|
+
if (this._includeSeparators) {
|
|
156
|
+
added.push(
|
|
157
|
+
this.insertItem(insertIndex++, { type: 'separator', rank: defaultRank })
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
// Insert the group.
|
|
161
|
+
added.push(
|
|
162
|
+
...sortedItems.map(item => {
|
|
163
|
+
return this.insertItem(insertIndex++, item);
|
|
164
|
+
})
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
// Insert a separator after the group.
|
|
168
|
+
if (this._includeSeparators) {
|
|
169
|
+
added.push(
|
|
170
|
+
this.insertItem(insertIndex++, { type: 'separator', rank: defaultRank })
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return new DisposableDelegate(() => {
|
|
175
|
+
added.forEach(i => i.dispose());
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Add a menu item to the end of the menu.
|
|
181
|
+
*
|
|
182
|
+
* @param options - The options for creating the menu item.
|
|
183
|
+
*
|
|
184
|
+
* @returns The menu item added to the menu.
|
|
185
|
+
*/
|
|
186
|
+
addItem(options: IRankedMenu.IItemOptions): IDisposableMenuItem {
|
|
187
|
+
let insertIndex = -1;
|
|
188
|
+
|
|
189
|
+
if (options.rank) {
|
|
190
|
+
insertIndex = this._ranks.findIndex(rank => options.rank! < rank);
|
|
191
|
+
}
|
|
192
|
+
if (insertIndex < 0) {
|
|
193
|
+
insertIndex = this._ranks.length; // Insert at the end of the menu
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return this.insertItem(insertIndex, options);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Remove all menu items from the menu.
|
|
201
|
+
*/
|
|
202
|
+
clearItems(): void {
|
|
203
|
+
this._ranks.length = 0;
|
|
204
|
+
super.clearItems();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Dispose of the resources held by the menu.
|
|
209
|
+
*/
|
|
210
|
+
dispose(): void {
|
|
211
|
+
this._ranks.length = 0;
|
|
212
|
+
super.dispose();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Get the rank of the item at index.
|
|
217
|
+
*
|
|
218
|
+
* @param index Item index.
|
|
219
|
+
* @returns Rank of the item.
|
|
220
|
+
*/
|
|
221
|
+
getRankAt(index: number): number {
|
|
222
|
+
return this._ranks[index];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Insert a menu item into the menu at the specified index.
|
|
227
|
+
*
|
|
228
|
+
* @param index - The index at which to insert the item.
|
|
229
|
+
*
|
|
230
|
+
* @param options - The options for creating the menu item.
|
|
231
|
+
*
|
|
232
|
+
* @returns The menu item added to the menu.
|
|
233
|
+
*
|
|
234
|
+
* #### Notes
|
|
235
|
+
* The index will be clamped to the bounds of the items.
|
|
236
|
+
*/
|
|
237
|
+
insertItem(
|
|
238
|
+
index: number,
|
|
239
|
+
options: IRankedMenu.IItemOptions
|
|
240
|
+
): IDisposableMenuItem {
|
|
241
|
+
const clampedIndex = Math.max(0, Math.min(index, this._ranks.length));
|
|
242
|
+
ArrayExt.insert(
|
|
243
|
+
this._ranks,
|
|
244
|
+
clampedIndex,
|
|
245
|
+
options.rank ??
|
|
246
|
+
Math.max(
|
|
247
|
+
IRankedMenu.DEFAULT_RANK,
|
|
248
|
+
this._ranks[this._ranks.length - 1] ?? IRankedMenu.DEFAULT_RANK
|
|
249
|
+
)
|
|
250
|
+
);
|
|
251
|
+
const item = super.insertItem(clampedIndex, options);
|
|
252
|
+
return new DisposableMenuItem(item, this);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Remove the item at a given index from the menu.
|
|
257
|
+
*
|
|
258
|
+
* @param index - The index of the item to remove.
|
|
259
|
+
*
|
|
260
|
+
* #### Notes
|
|
261
|
+
* This is a no-op if the index is out of range.
|
|
262
|
+
*/
|
|
263
|
+
removeItemAt(index: number): void {
|
|
264
|
+
ArrayExt.removeAt(this._ranks, index);
|
|
265
|
+
super.removeItemAt(index);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private _includeSeparators: boolean;
|
|
269
|
+
private _rank: number | undefined;
|
|
270
|
+
private _ranks: number[] = [];
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Disposable Menu Item
|
|
275
|
+
*/
|
|
276
|
+
class DisposableMenuItem implements IDisposableMenuItem {
|
|
277
|
+
/**
|
|
278
|
+
* Create a disposable menu item from an item and the menu it belongs to
|
|
279
|
+
*
|
|
280
|
+
* @param item Menu item
|
|
281
|
+
* @param menu Menu
|
|
282
|
+
*/
|
|
283
|
+
constructor(item: Menu.IItem, menu: Menu) {
|
|
284
|
+
this._item = new WeakRef(item);
|
|
285
|
+
this._menu = menu;
|
|
286
|
+
|
|
287
|
+
// dispose this item if the parent menu is disposed
|
|
288
|
+
const dispose = (menu: Menu): void => {
|
|
289
|
+
menu.disposed.disconnect(dispose, this);
|
|
290
|
+
this.dispose();
|
|
291
|
+
};
|
|
292
|
+
this._menu.disposed.connect(dispose, this);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Whether the menu item is disposed or not.
|
|
297
|
+
*/
|
|
298
|
+
get isDisposed(): boolean {
|
|
299
|
+
return this._isDisposed;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* The type of the menu item.
|
|
304
|
+
*/
|
|
305
|
+
get type(): Menu.ItemType {
|
|
306
|
+
return this._item.deref()!.type;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* The command to execute when the item is triggered.
|
|
311
|
+
*/
|
|
312
|
+
get command(): string {
|
|
313
|
+
return this._item.deref()!.command;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The arguments for the command.
|
|
318
|
+
*/
|
|
319
|
+
get args(): ReadonlyJSONObject {
|
|
320
|
+
return this._item.deref()!.args;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The submenu for a `'submenu'` type item.
|
|
325
|
+
*/
|
|
326
|
+
get submenu(): Menu | null {
|
|
327
|
+
return this._item.deref()!.submenu;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The display label for the menu item.
|
|
332
|
+
*/
|
|
333
|
+
get label(): string {
|
|
334
|
+
return this._item.deref()!.label;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The mnemonic index for the menu item.
|
|
339
|
+
*/
|
|
340
|
+
get mnemonic(): number {
|
|
341
|
+
return this._item.deref()!.mnemonic;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* The icon renderer for the menu item.
|
|
346
|
+
*/
|
|
347
|
+
get icon(): VirtualElement.IRenderer | undefined {
|
|
348
|
+
return this._item.deref()!.icon;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* The icon class for the menu item.
|
|
353
|
+
*/
|
|
354
|
+
get iconClass(): string {
|
|
355
|
+
return this._item.deref()!.iconClass;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* The icon label for the menu item.
|
|
360
|
+
*/
|
|
361
|
+
get iconLabel(): string {
|
|
362
|
+
return this._item.deref()!.iconLabel;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* The display caption for the menu item.
|
|
367
|
+
*/
|
|
368
|
+
get caption(): string {
|
|
369
|
+
return this._item.deref()!.caption;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* The extra class name for the menu item.
|
|
374
|
+
*/
|
|
375
|
+
get className(): string {
|
|
376
|
+
return this._item.deref()!.className;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* The dataset for the menu item.
|
|
381
|
+
*/
|
|
382
|
+
get dataset(): CommandRegistry.Dataset {
|
|
383
|
+
return this._item.deref()!.dataset;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Whether the menu item is enabled.
|
|
388
|
+
*/
|
|
389
|
+
get isEnabled(): boolean {
|
|
390
|
+
return this._item.deref()!.isEnabled;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Whether the menu item is toggled.
|
|
395
|
+
*/
|
|
396
|
+
get isToggled(): boolean {
|
|
397
|
+
return this._item.deref()!.isToggled;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Whether the menu item is visible.
|
|
402
|
+
*/
|
|
403
|
+
get isVisible(): boolean {
|
|
404
|
+
return this._item.deref()!.isVisible;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* The key binding for the menu item.
|
|
409
|
+
*/
|
|
410
|
+
get keyBinding(): CommandRegistry.IKeyBinding | null {
|
|
411
|
+
return this._item.deref()!.keyBinding;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Dispose the menu item by removing it from its menu.
|
|
416
|
+
*/
|
|
417
|
+
dispose(): void {
|
|
418
|
+
if (this._isDisposed) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
this._isDisposed = true;
|
|
422
|
+
const item = this._item.deref();
|
|
423
|
+
if (item && !this._menu.isDisposed) {
|
|
424
|
+
this._menu.removeItem(item);
|
|
425
|
+
}
|
|
426
|
+
Signal.clearData(this);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
private _isDisposed: boolean;
|
|
430
|
+
private _item: WeakRef<Menu.IItem>;
|
|
431
|
+
private _menu: Menu;
|
|
432
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Jupyter Development Team.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Panel } from '@lumino/widgets';
|
|
7
|
+
import { Toolbar } from './toolbar';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A base class for panel widget with toolbar.
|
|
11
|
+
*/
|
|
12
|
+
export class PanelWithToolbar extends Panel implements Toolbar.IWidgetToolbar {
|
|
13
|
+
constructor(options: PanelWithToolbar.IOptions = {}) {
|
|
14
|
+
super(options);
|
|
15
|
+
this._toolbar = new Toolbar();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Widget toolbar
|
|
20
|
+
*/
|
|
21
|
+
get toolbar(): Toolbar {
|
|
22
|
+
return this._toolbar;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected _toolbar: Toolbar;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Namespace for panel with toolbar
|
|
30
|
+
*/
|
|
31
|
+
export namespace PanelWithToolbar {
|
|
32
|
+
/**
|
|
33
|
+
* An options object for creating a panel with toolbar widget.
|
|
34
|
+
*/
|
|
35
|
+
export interface IOptions extends Panel.IOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Custom toolbar
|
|
38
|
+
*/
|
|
39
|
+
toolbar?: Toolbar;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { InputGroup } from './inputgroup';
|
|
5
|
+
import { ReactWidget } from './vdom';
|
|
6
|
+
import { StringExt } from '@lumino/algorithm';
|
|
7
|
+
import React, { useEffect, useState } from 'react';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The class name added to the filebrowser crumbs node.
|
|
11
|
+
*/
|
|
12
|
+
export interface IFilterBoxProps {
|
|
13
|
+
/**
|
|
14
|
+
* Whether to use case-sensitive search
|
|
15
|
+
*/
|
|
16
|
+
caseSensitive?: boolean;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Whether the search box is disabled or not.
|
|
20
|
+
*/
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether to force a refresh.
|
|
25
|
+
*/
|
|
26
|
+
forceRefresh?: boolean;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An optional initial search value.
|
|
30
|
+
*/
|
|
31
|
+
initialQuery?: string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Pass a ref to the input element
|
|
35
|
+
*/
|
|
36
|
+
inputRef?: React.RefObject<HTMLInputElement>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Optional placeholder for the search box.
|
|
40
|
+
*/
|
|
41
|
+
placeholder?: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A function to callback when filter is updated.
|
|
45
|
+
*/
|
|
46
|
+
updateFilter: (
|
|
47
|
+
filterFn: (item: string) => Partial<IScore> | null,
|
|
48
|
+
query?: string
|
|
49
|
+
) => void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether to use the fuzzy filter.
|
|
53
|
+
*/
|
|
54
|
+
useFuzzyFilter: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A text match score with associated content item.
|
|
59
|
+
*/
|
|
60
|
+
export interface IScore {
|
|
61
|
+
/**
|
|
62
|
+
* The numerical score for the text match.
|
|
63
|
+
*/
|
|
64
|
+
score: number;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The indices of the text matches.
|
|
68
|
+
*/
|
|
69
|
+
indices: number[] | null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Perform a fuzzy search on a single item.
|
|
74
|
+
*/
|
|
75
|
+
export function fuzzySearch(source: string, query: string): IScore | null {
|
|
76
|
+
// Set up the match score and indices array.
|
|
77
|
+
let score = Infinity;
|
|
78
|
+
let indices: number[] | null = null;
|
|
79
|
+
|
|
80
|
+
// The regex for search word boundaries
|
|
81
|
+
const rgx = /\b\w/g;
|
|
82
|
+
|
|
83
|
+
let continueSearch = true;
|
|
84
|
+
|
|
85
|
+
// Search the source by word boundary.
|
|
86
|
+
while (continueSearch) {
|
|
87
|
+
// Find the next word boundary in the source.
|
|
88
|
+
let rgxMatch = rgx.exec(source);
|
|
89
|
+
|
|
90
|
+
// Break if there is no more source context.
|
|
91
|
+
if (!rgxMatch) {
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Run the string match on the relevant substring.
|
|
96
|
+
let match = StringExt.matchSumOfDeltas(source, query, rgxMatch.index);
|
|
97
|
+
|
|
98
|
+
// Break if there is no match.
|
|
99
|
+
if (!match) {
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Update the match if the score is better.
|
|
104
|
+
if (match && match.score <= score) {
|
|
105
|
+
score = match.score;
|
|
106
|
+
indices = match.indices;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Bail if there was no match.
|
|
111
|
+
if (!indices || score === Infinity) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Handle a split match.
|
|
116
|
+
return {
|
|
117
|
+
score,
|
|
118
|
+
indices
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export const updateFilterFunction = (
|
|
123
|
+
value: string,
|
|
124
|
+
useFuzzyFilter: boolean,
|
|
125
|
+
caseSensitive?: boolean
|
|
126
|
+
) => {
|
|
127
|
+
return (item: string): Partial<IScore> | null => {
|
|
128
|
+
if (useFuzzyFilter) {
|
|
129
|
+
// Run the fuzzy search for the item and query.
|
|
130
|
+
const query = value.toLowerCase();
|
|
131
|
+
// Ignore the item if it is not a match.
|
|
132
|
+
return fuzzySearch(item, query);
|
|
133
|
+
}
|
|
134
|
+
if (!caseSensitive) {
|
|
135
|
+
item = item.toLocaleLowerCase();
|
|
136
|
+
value = value.toLocaleLowerCase();
|
|
137
|
+
}
|
|
138
|
+
const i = item.indexOf(value);
|
|
139
|
+
if (i === -1) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
indices: [...Array(item.length).keys()].map(x => x + 1)
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export const FilterBox = (props: IFilterBoxProps): JSX.Element => {
|
|
149
|
+
const [filter, setFilter] = useState(props.initialQuery ?? '');
|
|
150
|
+
|
|
151
|
+
if (props.forceRefresh) {
|
|
152
|
+
useEffect(() => {
|
|
153
|
+
props.updateFilter((item: string) => {
|
|
154
|
+
return {};
|
|
155
|
+
});
|
|
156
|
+
}, []);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
useEffect(() => {
|
|
160
|
+
// If there is an initial search value, pass the parent the initial filter function for that value.
|
|
161
|
+
if (props.initialQuery !== undefined) {
|
|
162
|
+
props.updateFilter(
|
|
163
|
+
updateFilterFunction(
|
|
164
|
+
props.initialQuery,
|
|
165
|
+
props.useFuzzyFilter,
|
|
166
|
+
props.caseSensitive
|
|
167
|
+
),
|
|
168
|
+
props.initialQuery
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}, []);
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Handler for search input changes.
|
|
175
|
+
*/
|
|
176
|
+
const handleChange = (e: React.FormEvent<HTMLElement>) => {
|
|
177
|
+
const target = e.target as HTMLInputElement;
|
|
178
|
+
setFilter(target.value);
|
|
179
|
+
props.updateFilter(
|
|
180
|
+
updateFilterFunction(
|
|
181
|
+
target.value,
|
|
182
|
+
props.useFuzzyFilter,
|
|
183
|
+
props.caseSensitive
|
|
184
|
+
),
|
|
185
|
+
target.value
|
|
186
|
+
);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<InputGroup
|
|
191
|
+
className="jp-FilterBox"
|
|
192
|
+
inputRef={props.inputRef}
|
|
193
|
+
type="text"
|
|
194
|
+
disabled={props.disabled}
|
|
195
|
+
rightIcon="ui-components:search"
|
|
196
|
+
placeholder={props.placeholder}
|
|
197
|
+
onChange={handleChange}
|
|
198
|
+
value={filter}
|
|
199
|
+
/>
|
|
200
|
+
);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* A widget which hosts a input textbox to filter on file names.
|
|
205
|
+
*/
|
|
206
|
+
export const FilenameSearcher = (props: IFilterBoxProps): ReactWidget => {
|
|
207
|
+
return ReactWidget.create(
|
|
208
|
+
<FilterBox
|
|
209
|
+
updateFilter={props.updateFilter}
|
|
210
|
+
useFuzzyFilter={props.useFuzzyFilter}
|
|
211
|
+
placeholder={props.placeholder}
|
|
212
|
+
forceRefresh={props.forceRefresh}
|
|
213
|
+
caseSensitive={props.caseSensitive}
|
|
214
|
+
/>
|
|
215
|
+
);
|
|
216
|
+
};
|