| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import { |
| 5 |
JupyterFrontEnd, |
| 6 |
JupyterFrontEndPlugin, |
| 7 |
} from '@jupyterlab/application'; |
| 8 |
|
| 9 |
import { |
| 10 |
IToolbarWidgetRegistry, |
| 11 |
createToolbarFactory, |
| 12 |
setToolbar, |
| 13 |
} from '@jupyterlab/apputils'; |
| 14 |
|
| 15 |
import { PageConfig } from '@jupyterlab/coreutils'; |
| 16 |
|
| 17 |
import { |
| 18 |
FileBrowser, |
| 19 |
Uploader, |
| 20 |
IDefaultFileBrowser, |
| 21 |
} from '@jupyterlab/filebrowser'; |
| 22 |
|
| 23 |
import { ISettingRegistry } from '@jupyterlab/settingregistry'; |
| 24 |
|
| 25 |
import { IRunningSessionManagers, RunningSessions } from '@jupyterlab/running'; |
| 26 |
|
| 27 |
import { |
| 28 |
IJSONSettingEditorTracker, |
| 29 |
ISettingEditorTracker, |
| 30 |
} from '@jupyterlab/settingeditor'; |
| 31 |
|
| 32 |
import { ITranslator } from '@jupyterlab/translation'; |
| 33 |
|
| 34 |
import { |
| 35 |
caretDownIcon, |
| 36 |
FilenameSearcher, |
| 37 |
folderIcon, |
| 38 |
IScore, |
| 39 |
runningIcon, |
| 40 |
} from '@jupyterlab/ui-components'; |
| 41 |
|
| 42 |
import { Signal } from '@lumino/signaling'; |
| 43 |
|
| 44 |
import { Menu, MenuBar } from '@lumino/widgets'; |
| 45 |
|
| 46 |
import { NotebookTreeWidget, INotebookTree } from '@jupyter-notebook/tree'; |
| 47 |
|
| 48 |
import { FileActionsComponent } from './fileactions'; |
| 49 |
|
| 50 |
/** |
| 51 |
* The file browser factory. |
| 52 |
*/ |
| 53 |
const FILE_BROWSER_FACTORY = 'FileBrowser'; |
| 54 |
|
| 55 |
/** |
| 56 |
* The file browser plugin id. |
| 57 |
*/ |
| 58 |
const FILE_BROWSER_PLUGIN_ID = '@jupyterlab/filebrowser-extension:browser'; |
| 59 |
|
| 60 |
/** |
| 61 |
* The class name added to the filebrowser filterbox node. |
| 62 |
*/ |
| 63 |
const FILTERBOX_CLASS = 'jp-FileBrowser-filterBox'; |
| 64 |
|
| 65 |
/** |
| 66 |
* The namespace for command IDs. |
| 67 |
*/ |
| 68 |
namespace CommandIDs { |
| 69 |
// The command to activate the filebrowser widget in tree view. |
| 70 |
export const activate = 'filebrowser:activate'; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Plugin to add extra commands to the file browser to create |
| 75 |
* new notebooks, files, consoles and terminals |
| 76 |
*/ |
| 77 |
const createNew: JupyterFrontEndPlugin<void> = { |
| 78 |
id: '@jupyter-notebook/tree-extension:new', |
| 79 |
requires: [ITranslator], |
| 80 |
optional: [IToolbarWidgetRegistry], |
| 81 |
autoStart: true, |
| 82 |
activate: ( |
| 83 |
app: JupyterFrontEnd, |
| 84 |
translator: ITranslator, |
| 85 |
toolbarRegistry: IToolbarWidgetRegistry | null |
| 86 |
) => { |
| 87 |
const { commands } = app; |
| 88 |
const trans = translator.load('notebook'); |
| 89 |
|
| 90 |
const overflowOptions = { |
| 91 |
overflowMenuOptions: { isVisible: false }, |
| 92 |
}; |
| 93 |
const menubar = new MenuBar(overflowOptions); |
| 94 |
const newMenu = new Menu({ commands }); |
| 95 |
newMenu.title.label = trans.__('New'); |
| 96 |
newMenu.title.icon = caretDownIcon; |
| 97 |
menubar.addMenu(newMenu); |
| 98 |
|
| 99 |
const newCommands = [ |
| 100 |
'notebook:create-new', |
| 101 |
'terminal:create-new', |
| 102 |
'console:create', |
| 103 |
'filebrowser:create-new-file', |
| 104 |
'filebrowser:create-new-directory', |
| 105 |
]; |
| 106 |
|
| 107 |
newCommands.forEach((command) => { |
| 108 |
newMenu.addItem({ command }); |
| 109 |
}); |
| 110 |
|
| 111 |
if (toolbarRegistry) { |
| 112 |
toolbarRegistry.addFactory( |
| 113 |
FILE_BROWSER_FACTORY, |
| 114 |
'new-dropdown', |
| 115 |
(browser: FileBrowser) => { |
| 116 |
const menubar = new MenuBar(overflowOptions); |
| 117 |
menubar.addMenu(newMenu); |
| 118 |
menubar.addClass('jp-DropdownMenu'); |
| 119 |
return menubar; |
| 120 |
} |
| 121 |
); |
| 122 |
} |
| 123 |
}, |
| 124 |
}; |
| 125 |
|
| 126 |
/** |
| 127 |
* A plugin to add file browser actions to the file browser toolbar. |
| 128 |
*/ |
| 129 |
const fileActions: JupyterFrontEndPlugin<void> = { |
| 130 |
id: '@jupyter-notebook/tree-extension:file-actions', |
| 131 |
autoStart: true, |
| 132 |
requires: [IDefaultFileBrowser, IToolbarWidgetRegistry, ITranslator], |
| 133 |
activate: ( |
| 134 |
app: JupyterFrontEnd, |
| 135 |
browser: IDefaultFileBrowser, |
| 136 |
toolbarRegistry: IToolbarWidgetRegistry, |
| 137 |
translator: ITranslator |
| 138 |
) => { |
| 139 |
// TODO: use upstream signal when available to detect selection changes |
| 140 |
// https://github.com/jupyterlab/jupyterlab/issues/14598 |
| 141 |
const selectionChanged = new Signal<FileBrowser, void>(browser); |
| 142 |
const methods = [ |
| 143 |
'_selectItem', |
| 144 |
'_handleMultiSelect', |
| 145 |
'handleFileSelect', |
| 146 |
] as const; |
| 147 |
methods.forEach((method: (typeof methods)[number]) => { |
| 148 |
const original = browser['listing'][method]; |
| 149 |
browser['listing'][method] = (...args: any[]) => { |
| 150 |
original.call(browser['listing'], ...args); |
| 151 |
selectionChanged.emit(void 0); |
| 152 |
}; |
| 153 |
}); |
| 154 |
|
| 155 |
// Create a toolbar item that adds buttons to the file browser toolbar |
| 156 |
// to perform actions on the files |
| 157 |
toolbarRegistry.addFactory( |
| 158 |
FILE_BROWSER_FACTORY, |
| 159 |
'fileActions', |
| 160 |
(browser: FileBrowser) => { |
| 161 |
const { commands } = app; |
| 162 |
const fileActions = FileActionsComponent.create({ |
| 163 |
commands, |
| 164 |
browser, |
| 165 |
selectionChanged, |
| 166 |
translator, |
| 167 |
}); |
| 168 |
return fileActions; |
| 169 |
} |
| 170 |
); |
| 171 |
}, |
| 172 |
}; |
| 173 |
|
| 174 |
/** |
| 175 |
* Plugin to load the default plugins that are loaded on all the Notebook pages |
| 176 |
* (tree, edit, view, etc.) so they are visible in the settings editor. |
| 177 |
*/ |
| 178 |
const loadPlugins: JupyterFrontEndPlugin<void> = { |
| 179 |
id: '@jupyter-notebook/tree-extension:load-plugins', |
| 180 |
autoStart: true, |
| 181 |
requires: [ISettingRegistry], |
| 182 |
activate(app: JupyterFrontEnd, settingRegistry: ISettingRegistry) { |
| 183 |
const { isDisabled } = PageConfig.Extension; |
| 184 |
const connector = settingRegistry.connector; |
| 185 |
|
| 186 |
const allPluginsOption = PageConfig.getOption('allPlugins'); |
| 187 |
if (!allPluginsOption) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
// build the list of plugins shipped by default on the all the notebook pages |
| 192 |
// this avoid explicitly loading `'all'` plugins such as the ones used |
| 193 |
// in JupyterLab only |
| 194 |
const allPlugins = JSON.parse(allPluginsOption); |
| 195 |
const pluginsSet = new Set<string>(); |
| 196 |
Object.keys(allPlugins).forEach((key: string) => { |
| 197 |
const extensionsAndPlugins: { [key: string]: boolean | [string] } = |
| 198 |
allPlugins[key]; |
| 199 |
Object.keys(extensionsAndPlugins).forEach((plugin) => { |
| 200 |
const value = extensionsAndPlugins[plugin]; |
| 201 |
if (typeof value === 'boolean' && value) { |
| 202 |
pluginsSet.add(plugin); |
| 203 |
} else if (Array.isArray(value)) { |
| 204 |
value.forEach((v: string) => { |
| 205 |
pluginsSet.add(v); |
| 206 |
}); |
| 207 |
} |
| 208 |
}); |
| 209 |
}); |
| 210 |
|
| 211 |
app.restored.then(async () => { |
| 212 |
const plugins = await connector.list('all'); |
| 213 |
plugins.ids.forEach(async (id: string) => { |
| 214 |
const [extension] = id.split(':'); |
| 215 |
// load the plugin if it is built-in the notebook application explicitly |
| 216 |
// either included as an extension or as a plugin directly |
| 217 |
const hasPlugin = pluginsSet.has(extension) || pluginsSet.has(id); |
| 218 |
if (!hasPlugin || isDisabled(id) || id in settingRegistry.plugins) { |
| 219 |
return; |
| 220 |
} |
| 221 |
try { |
| 222 |
await settingRegistry.load(id); |
| 223 |
} catch (error) { |
| 224 |
console.warn(`Settings failed to load for (${id})`, error); |
| 225 |
} |
| 226 |
}); |
| 227 |
}); |
| 228 |
}, |
| 229 |
}; |
| 230 |
|
| 231 |
/** |
| 232 |
* A plugin to add file browser commands for the tree view. |
| 233 |
*/ |
| 234 |
const openFileBrowser: JupyterFrontEndPlugin<void> = { |
| 235 |
id: '@jupyter-notebook/tree-extension:open-file-browser', |
| 236 |
requires: [INotebookTree, IDefaultFileBrowser], |
| 237 |
autoStart: true, |
| 238 |
activate: ( |
| 239 |
app: JupyterFrontEnd, |
| 240 |
notebookTree: INotebookTree, |
| 241 |
browser: IDefaultFileBrowser |
| 242 |
) => { |
| 243 |
const { commands } = app; |
| 244 |
commands.addCommand(CommandIDs.activate, { |
| 245 |
execute: () => { |
| 246 |
notebookTree.currentWidget = browser; |
| 247 |
}, |
| 248 |
}); |
| 249 |
}, |
| 250 |
}; |
| 251 |
|
| 252 |
/** |
| 253 |
* A plugin to add the file browser widget to an INotebookShell |
| 254 |
*/ |
| 255 |
const notebookTreeWidget: JupyterFrontEndPlugin<INotebookTree> = { |
| 256 |
id: '@jupyter-notebook/tree-extension:widget', |
| 257 |
requires: [ |
| 258 |
IDefaultFileBrowser, |
| 259 |
ITranslator, |
| 260 |
ISettingRegistry, |
| 261 |
IToolbarWidgetRegistry, |
| 262 |
], |
| 263 |
optional: [ |
| 264 |
IRunningSessionManagers, |
| 265 |
ISettingEditorTracker, |
| 266 |
IJSONSettingEditorTracker, |
| 267 |
], |
| 268 |
autoStart: true, |
| 269 |
provides: INotebookTree, |
| 270 |
activate: ( |
| 271 |
app: JupyterFrontEnd, |
| 272 |
browser: IDefaultFileBrowser, |
| 273 |
translator: ITranslator, |
| 274 |
settingRegistry: ISettingRegistry, |
| 275 |
toolbarRegistry: IToolbarWidgetRegistry, |
| 276 |
manager: IRunningSessionManagers | null, |
| 277 |
settingEditorTracker: ISettingEditorTracker | null, |
| 278 |
jsonSettingEditorTracker: IJSONSettingEditorTracker | null |
| 279 |
): INotebookTree => { |
| 280 |
const nbTreeWidget = new NotebookTreeWidget(); |
| 281 |
|
| 282 |
const trans = translator.load('notebook'); |
| 283 |
|
| 284 |
browser.title.label = trans.__('Files'); |
| 285 |
browser.node.setAttribute('role', 'region'); |
| 286 |
browser.node.setAttribute('aria-label', trans.__('File Browser Section')); |
| 287 |
browser.title.icon = folderIcon; |
| 288 |
|
| 289 |
nbTreeWidget.addWidget(browser); |
| 290 |
nbTreeWidget.tabBar.addTab(browser.title); |
| 291 |
nbTreeWidget.tabsMovable = false; |
| 292 |
|
| 293 |
toolbarRegistry.addFactory( |
| 294 |
FILE_BROWSER_FACTORY, |
| 295 |
'uploader', |
| 296 |
(browser: FileBrowser) => |
| 297 |
new Uploader({ |
| 298 |
model: browser.model, |
| 299 |
translator, |
| 300 |
label: trans.__('Upload'), |
| 301 |
}) |
| 302 |
); |
| 303 |
|
| 304 |
toolbarRegistry.addFactory( |
| 305 |
FILE_BROWSER_FACTORY, |
| 306 |
'fileNameSearcher', |
| 307 |
(browser: FileBrowser) => { |
| 308 |
const searcher = FilenameSearcher({ |
| 309 |
updateFilter: ( |
| 310 |
filterFn: (item: string) => Partial<IScore> | null, |
| 311 |
query?: string |
| 312 |
) => { |
| 313 |
browser.model.setFilter((value) => { |
| 314 |
return filterFn(value.name.toLowerCase()); |
| 315 |
}); |
| 316 |
}, |
| 317 |
useFuzzyFilter: true, |
| 318 |
placeholder: trans.__('Filter files by name'), |
| 319 |
forceRefresh: true, |
| 320 |
}); |
| 321 |
searcher.addClass(FILTERBOX_CLASS); |
| 322 |
return searcher; |
| 323 |
} |
| 324 |
); |
| 325 |
|
| 326 |
setToolbar( |
| 327 |
browser, |
| 328 |
createToolbarFactory( |
| 329 |
toolbarRegistry, |
| 330 |
settingRegistry, |
| 331 |
FILE_BROWSER_FACTORY, |
| 332 |
notebookTreeWidget.id, |
| 333 |
translator |
| 334 |
) |
| 335 |
); |
| 336 |
|
| 337 |
if (manager) { |
| 338 |
const running = new RunningSessions(manager, translator); |
| 339 |
running.id = 'jp-running-sessions-tree'; |
| 340 |
running.title.label = trans.__('Running'); |
| 341 |
running.title.icon = runningIcon; |
| 342 |
nbTreeWidget.addWidget(running); |
| 343 |
nbTreeWidget.tabBar.addTab(running.title); |
| 344 |
} |
| 345 |
|
| 346 |
const settings = settingRegistry.load(FILE_BROWSER_PLUGIN_ID); |
| 347 |
Promise.all([settings, app.restored]) |
| 348 |
.then(([settings]) => { |
| 349 |
// Set Notebook 7 defaults if there is no user setting override |
| 350 |
[ |
| 351 |
'showFileCheckboxes', |
| 352 |
'showFileSizeColumn', |
| 353 |
'sortNotebooksFirst', |
| 354 |
].forEach((setting) => { |
| 355 |
if (settings.user[setting] === undefined) { |
| 356 |
void settings.set(setting, true); |
| 357 |
} |
| 358 |
}); |
| 359 |
}) |
| 360 |
.catch((reason: Error) => { |
| 361 |
console.error(reason.message); |
| 362 |
}); |
| 363 |
|
| 364 |
app.shell.add(nbTreeWidget, 'main', { rank: 100 }); |
| 365 |
|
| 366 |
// add a separate tab for each setting editor |
| 367 |
[settingEditorTracker, jsonSettingEditorTracker].forEach( |
| 368 |
(editorTracker) => { |
| 369 |
if (editorTracker) { |
| 370 |
editorTracker.widgetAdded.connect((_, editor) => { |
| 371 |
nbTreeWidget.addWidget(editor); |
| 372 |
nbTreeWidget.tabBar.addTab(editor.title); |
| 373 |
nbTreeWidget.currentWidget = editor; |
| 374 |
}); |
| 375 |
} |
| 376 |
} |
| 377 |
); |
| 378 |
|
| 379 |
return nbTreeWidget; |
| 380 |
}, |
| 381 |
}; |
| 382 |
|
| 383 |
/** |
| 384 |
* Export the plugins as default. |
| 385 |
*/ |
| 386 |
const plugins: JupyterFrontEndPlugin<any>[] = [ |
| 387 |
createNew, |
| 388 |
fileActions, |
| 389 |
loadPlugins, |
| 390 |
openFileBrowser, |
| 391 |
notebookTreeWidget, |
| 392 |
]; |
| 393 |
export default plugins; |