Skip to content

Commit c8e303f

Browse files
committed
add drive browser toolbar
1 parent 634a345 commit c8e303f

File tree

2 files changed

+174
-5
lines changed

2 files changed

+174
-5
lines changed

schema/drives-file-browser.json

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"title": "Jupyter Drives Settings",
3+
"description": "jupyter-drives settings.",
4+
"jupyter.lab.toolbars": {
5+
"DriveBrowser": [
6+
{
7+
"name": "new-launcher",
8+
"command": "launcher:create",
9+
"rank": 1
10+
},
11+
{
12+
"name": "new-directory",
13+
"command": "filebrowser:create-new-directory",
14+
"rank": 10
15+
},
16+
{ "name": "uploader", "rank": 20 },
17+
{ "name": "refresh", "command": "filebrowser:refresh", "rank": 30 },
18+
{
19+
"name": "toggle-file-filter",
20+
"command": "filebrowser:toggle-file-filter",
21+
"rank": 40
22+
},
23+
{
24+
"name": "file-name-searcher",
25+
"rank": 50
26+
}
27+
]
28+
},
29+
"jupyter.lab.setting-icon": "@jupyter/drives:drive-browser",
30+
"jupyter.lab.setting-icon-label": "Drive Browser",
31+
"type": "object",
32+
"jupyter.lab.transform": true,
33+
"properties": {
34+
"toolbar": {
35+
"title": "Drive browser toolbar items",
36+
"description": "Note: To disable a toolbar item,\ncopy it to User Preferences and add the\n\"disabled\" key.",
37+
"items": {
38+
"$ref": "#/definitions/toolbarItem"
39+
},
40+
"type": "array",
41+
"default": []
42+
}
43+
},
44+
"additionalProperties": false,
45+
"definitions": {
46+
"toolbarItem": {
47+
"properties": {
48+
"name": {
49+
"title": "Unique name",
50+
"type": "string"
51+
},
52+
"args": {
53+
"title": "Command arguments",
54+
"type": "object"
55+
},
56+
"command": {
57+
"title": "Command id",
58+
"type": "string",
59+
"default": ""
60+
},
61+
"disabled": {
62+
"title": "Whether the item is ignored or not",
63+
"type": "boolean",
64+
"default": false
65+
},
66+
"icon": {
67+
"title": "Item icon id",
68+
"description": "If defined, it will override the command icon",
69+
"type": "string"
70+
},
71+
"label": {
72+
"title": "Item label",
73+
"description": "If defined, it will override the command label",
74+
"type": "string"
75+
},
76+
"caption": {
77+
"title": "Item caption",
78+
"description": "If defined, it will override the command caption",
79+
"type": "string"
80+
},
81+
"type": {
82+
"title": "Item type",
83+
"type": "string",
84+
"enum": ["command", "spacer"]
85+
},
86+
"rank": {
87+
"title": "Item rank",
88+
"type": "number",
89+
"minimum": 0,
90+
"default": 50
91+
}
92+
},
93+
"required": ["name"],
94+
"additionalProperties": false,
95+
"type": "object"
96+
}
97+
}
98+
}

src/index.ts

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ import {
55
JupyterFrontEnd,
66
JupyterFrontEndPlugin
77
} from '@jupyterlab/application';
8-
import { IFileBrowserFactory, FileBrowser } from '@jupyterlab/filebrowser';
8+
import {
9+
IFileBrowserFactory,
10+
FileBrowser,
11+
Uploader
12+
} from '@jupyterlab/filebrowser';
913
import { ITranslator } from '@jupyterlab/translation';
1014
import { addJupyterLabThemeChangeListener } from '@jupyter/web-components';
11-
import { Dialog, showDialog } from '@jupyterlab/apputils';
15+
import {
16+
createToolbarFactory,
17+
IToolbarWidgetRegistry,
18+
setToolbar,
19+
Dialog,
20+
showDialog
21+
} from '@jupyterlab/apputils';
22+
import { ISettingRegistry } from '@jupyterlab/settingregistry';
23+
import { FilenameSearcher, IScore } from '@jupyterlab/ui-components';
1224
import { CommandRegistry } from '@lumino/commands';
1325
import { Panel } from '@lumino/widgets';
1426

@@ -25,6 +37,16 @@ namespace CommandIDs {
2537
export const toggleBrowser = 'drives:toggle-main';
2638
}
2739

40+
/**
41+
* The file browser factory ID.
42+
*/
43+
const FILE_BROWSER_FACTORY = 'DriveBrowser';
44+
45+
/**
46+
* The class name added to the drive filebrowser filterbox node.
47+
*/
48+
const FILTERBOX_CLASS = 'jp-DriveBrowser-filterBox';
49+
2850
const openDriveDialogPlugin: JupyterFrontEndPlugin<void> = {
2951
id: '@jupyter/drives:widget',
3052
description: 'Open a dialog to select drives to be added in the filebrowser.',
@@ -136,7 +158,12 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {
136158
id: '@jupyter/drives:drives-file-browser',
137159
description: 'The drive file browser factory provider.',
138160
autoStart: true,
139-
requires: [IFileBrowserFactory],
161+
requires: [
162+
IFileBrowserFactory,
163+
IToolbarWidgetRegistry,
164+
ISettingRegistry,
165+
ITranslator
166+
],
140167
optional: [
141168
IRouter,
142169
JupyterFrontEnd.ITreeResolver,
@@ -146,6 +173,9 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {
146173
activate: async (
147174
app: JupyterFrontEnd,
148175
fileBrowserFactory: IFileBrowserFactory,
176+
toolbarRegistry: IToolbarWidgetRegistry,
177+
settingsRegistry: ISettingRegistry,
178+
translator: ITranslator,
149179
router: IRouter | null,
150180
tree: JupyterFrontEnd.ITreeResolver | null,
151181
labShell: ILabShell | null,
@@ -170,15 +200,15 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {
170200
driveName: drive.name
171201
});
172202

173-
// // Set attributes when adding the browser to the UI
203+
// Set attributes when adding the browser to the UI
174204
driveBrowser.node.setAttribute('role', 'region');
175205
driveBrowser.node.setAttribute('aria-label', 'Drive Browser Section');
176206

177207
// instate Drive Browser Panel
178208
const drivePanel = new Panel();
179209
drivePanel.title.icon = driveBrowserIcon;
180210
drivePanel.title.iconClass = 'jp-sideBar-tabIcon';
181-
drivePanel.title.caption = 'Drive FileBrowser';
211+
drivePanel.title.caption = 'Drive File Browser';
182212
drivePanel.id = 'Drive-Browser-Panel';
183213

184214
app.shell.add(drivePanel, 'left', { rank: 102 });
@@ -188,6 +218,47 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {
188218
}
189219

190220
void Private.restoreBrowser(driveBrowser, commands, router, tree, labShell);
221+
222+
toolbarRegistry.addFactory(
223+
FILE_BROWSER_FACTORY,
224+
'uploader',
225+
(fileBrowser: FileBrowser) =>
226+
new Uploader({ model: fileBrowser.model, translator })
227+
);
228+
229+
toolbarRegistry.addFactory(
230+
FILE_BROWSER_FACTORY,
231+
'file-name-searcher',
232+
(fileBrowser: FileBrowser) => {
233+
const searcher = FilenameSearcher({
234+
updateFilter: (
235+
filterFn: (item: string) => Partial<IScore> | null,
236+
query?: string
237+
) => {
238+
fileBrowser.model.setFilter(value => {
239+
return filterFn(value.name.toLowerCase());
240+
});
241+
},
242+
useFuzzyFilter: true,
243+
placeholder: 'Filter files by names',
244+
forceRefresh: true
245+
});
246+
searcher.addClass(FILTERBOX_CLASS);
247+
return searcher;
248+
}
249+
);
250+
251+
// connect the filebrowser toolbar to the settings registry for the plugin
252+
setToolbar(
253+
driveBrowser,
254+
createToolbarFactory(
255+
toolbarRegistry,
256+
settingsRegistry,
257+
FILE_BROWSER_FACTORY,
258+
driveFileBrowser.id,
259+
translator
260+
)
261+
);
191262
}
192263
};
193264

0 commit comments

Comments
 (0)