|
1 | 1 | import { |
| 2 | + ILabShell, |
| 3 | + ILayoutRestorer, |
| 4 | + IRouter, |
2 | 5 | JupyterFrontEnd, |
3 | 6 | JupyterFrontEndPlugin |
4 | 7 | } from '@jupyterlab/application'; |
5 | | - |
6 | | -import { IFileBrowserFactory } from '@jupyterlab/filebrowser'; |
| 8 | +import { IFileBrowserFactory, FileBrowser } from '@jupyterlab/filebrowser'; |
7 | 9 | import { ITranslator } from '@jupyterlab/translation'; |
8 | 10 | import { addJupyterLabThemeChangeListener } from '@jupyter/web-components'; |
9 | 11 | import { Dialog, showDialog } from '@jupyterlab/apputils'; |
| 12 | +import { CommandRegistry } from '@lumino/commands'; |
| 13 | +import { Panel } from '@lumino/widgets'; |
| 14 | + |
10 | 15 | import { DriveListModel, DriveListView, IDrive } from './drivelistmanager'; |
11 | | -import { DriveIcon } from './icons'; |
| 16 | +import { DriveIcon, driveBrowserIcon } from './icons'; |
12 | 17 |
|
| 18 | +/** |
| 19 | + * The command IDs used by the driveBrowser plugin. |
| 20 | + */ |
13 | 21 | namespace CommandIDs { |
14 | 22 | export const openDrivesDialog = 'drives:open-drives-dialog'; |
| 23 | + export const openPath = 'filebrowser:open-path'; |
| 24 | + export const toggleBrowser = 'filebrowser:toggle-main'; |
15 | 25 | } |
16 | 26 |
|
17 | 27 | /** |
@@ -129,5 +139,126 @@ const openDriveDialogPlugin: JupyterFrontEndPlugin<void> = { |
129 | 139 | }); |
130 | 140 | } |
131 | 141 | }; |
132 | | -const plugins: JupyterFrontEndPlugin<any>[] = [plugin, openDriveDialogPlugin]; |
| 142 | + |
| 143 | +/** |
| 144 | + * The drive file browser factory provider. |
| 145 | + */ |
| 146 | +const driveFileBrowser: JupyterFrontEndPlugin<void> = { |
| 147 | + id: 'jupyter-drives:drive-file-browser', |
| 148 | + description: 'The drive file browser factory provider.', |
| 149 | + autoStart: true, |
| 150 | + requires: [IFileBrowserFactory], |
| 151 | + optional: [ |
| 152 | + IRouter, |
| 153 | + JupyterFrontEnd.ITreeResolver, |
| 154 | + ILabShell, |
| 155 | + ILayoutRestorer |
| 156 | + ], |
| 157 | + activate: async ( |
| 158 | + app: JupyterFrontEnd, |
| 159 | + fileBrowserFactory: IFileBrowserFactory, |
| 160 | + router: IRouter | null, |
| 161 | + tree: JupyterFrontEnd.ITreeResolver | null, |
| 162 | + labShell: ILabShell | null, |
| 163 | + restorer: ILayoutRestorer | null |
| 164 | + ): Promise<void> => { |
| 165 | + const { commands } = app; |
| 166 | + |
| 167 | + // create S3 drive |
| 168 | + // const S3Drive = new Drive({ |
| 169 | + // name: auth.bucket, |
| 170 | + // root: auth.root, |
| 171 | + // config: auth.config |
| 172 | + // }); |
| 173 | + |
| 174 | + // app.serviceManager.contents.addDrive(S3Drive); |
| 175 | + |
| 176 | + // Manually restore and load the drive file browser. |
| 177 | + const driveBrowser = fileBrowserFactory.createFileBrowser('drivebrowser', { |
| 178 | + auto: false, |
| 179 | + restore: false |
| 180 | + // driveName: S3Drive.name |
| 181 | + }); |
| 182 | + |
| 183 | + // // Set attributes when adding the browser to the UI |
| 184 | + driveBrowser.node.setAttribute('role', 'region'); |
| 185 | + driveBrowser.node.setAttribute('aria-label', 'Drive Browser Section'); |
| 186 | + |
| 187 | + // instate Drive Browser Panel |
| 188 | + const drivePanel = new Panel(); |
| 189 | + drivePanel.title.icon = driveBrowserIcon; |
| 190 | + drivePanel.title.iconClass = 'jp-sidebar-tabIcon'; |
| 191 | + drivePanel.title.caption = 'Drive FileBrowser'; |
| 192 | + drivePanel.id = 'Drive-Browser-Panel'; |
| 193 | + |
| 194 | + app.shell.add(drivePanel, 'left', { rank: 102 }); |
| 195 | + // drivePanel.addWidget(driveBrowser); |
| 196 | + if (restorer) { |
| 197 | + restorer.add(drivePanel, 'drive-sidepanel'); |
| 198 | + } |
| 199 | + |
| 200 | + void Private.restoreBrowser(driveBrowser, commands, router, tree, labShell); |
| 201 | + } |
| 202 | +}; |
| 203 | + |
| 204 | +const plugins: JupyterFrontEndPlugin<any>[] = [ |
| 205 | + plugin, |
| 206 | + driveFileBrowser, |
| 207 | + openDriveDialogPlugin |
| 208 | +]; |
133 | 209 | export default plugins; |
| 210 | + |
| 211 | +namespace Private { |
| 212 | + /** |
| 213 | + * Restores file browser state and overrides state if tree resolver resolves. |
| 214 | + */ |
| 215 | + export async function restoreBrowser( |
| 216 | + browser: FileBrowser, |
| 217 | + commands: CommandRegistry, |
| 218 | + router: IRouter | null, |
| 219 | + tree: JupyterFrontEnd.ITreeResolver | null, |
| 220 | + labShell: ILabShell | null |
| 221 | + ): Promise<void> { |
| 222 | + const restoring = 'jp-mod-restoring'; |
| 223 | + |
| 224 | + browser.addClass(restoring); |
| 225 | + |
| 226 | + if (!router) { |
| 227 | + await browser.model.restore(browser.id); |
| 228 | + await browser.model.refresh(); |
| 229 | + browser.removeClass(restoring); |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + const listener = async () => { |
| 234 | + router.routed.disconnect(listener); |
| 235 | + |
| 236 | + const paths = await tree?.paths; |
| 237 | + if (paths?.file || paths?.browser) { |
| 238 | + // Restore the model without populating it. |
| 239 | + await browser.model.restore(browser.id, false); |
| 240 | + if (paths.file) { |
| 241 | + await commands.execute(CommandIDs.openPath, { |
| 242 | + path: paths.file, |
| 243 | + dontShowBrowser: true |
| 244 | + }); |
| 245 | + } |
| 246 | + if (paths.browser) { |
| 247 | + await commands.execute(CommandIDs.openPath, { |
| 248 | + path: paths.browser, |
| 249 | + dontShowBrowser: true |
| 250 | + }); |
| 251 | + } |
| 252 | + } else { |
| 253 | + await browser.model.restore(browser.id); |
| 254 | + await browser.model.refresh(); |
| 255 | + } |
| 256 | + browser.removeClass(restoring); |
| 257 | + |
| 258 | + if (labShell?.isEmpty('main')) { |
| 259 | + void commands.execute('launcher:create'); |
| 260 | + } |
| 261 | + }; |
| 262 | + router.routed.connect(listener); |
| 263 | + } |
| 264 | +} |
0 commit comments