123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import {
- IWidgetOpener, FileBrowserWidget
- } from './browser';
- import {
- FileBrowserModel
- } from './model';
- import {
- DocumentManager
- } from '../docmanager';
- import {
- DocumentRegistry
- } from '../docregistry';
- import {
- Application
- } from 'phosphide/lib/core/application';
- import {
- Menu, MenuItem
- } from 'phosphor-menus';
- import {
- Widget
- } from 'phosphor-widget';
- import {
- JupyterServices
- } from '../services/plugin';
- import {
- WidgetTracker
- } from '../widgettracker';
- /**
- * The default file browser extension.
- */
- export
- const fileBrowserExtension = {
- id: 'jupyter.extensions.fileBrowser',
- requires: [JupyterServices, DocumentRegistry],
- activate: activateFileBrowser
- };
- /**
- * The class name for all main area portrait tab icons.
- */
- const PORTRAIT_ICON_CLASS = 'jp-MainAreaPortraitIcon';
- /**
- * The class name for the notebook icon from the default theme.
- */
- const NOTEBOOK_ICON_CLASS = 'jp-ImageNotebook';
- /**
- * The class name for the text editor icon from the default theme.
- */
- const TEXTEDITOR_ICON_CLASS = 'jp-ImageTextEditor';
- /**
- * Activate the file browser.
- */
- function activateFileBrowser(app: Application, provider: JupyterServices, registry: DocumentRegistry): Promise<void> {
- let contents = provider.contentsManager;
- let sessions = provider.sessionManager;
- let id = 0;
- let tracker = new WidgetTracker<Widget>();
- let activeWidget: Widget;
- tracker.activeWidgetChanged.connect((sender, widget) => {
- activeWidget = widget;
- });
- let opener: IWidgetOpener = {
- open: (widget) => {
- if (!widget.id) {
- widget.id = `document-manager-${++id}`;
- }
- if (!widget.isAttached) {
- app.shell.addToMainArea(widget);
- tracker.addWidget(widget);
- }
- }
- };
- let docManager = new DocumentManager({
- registry,
- contentsManager: contents,
- sessionManager: sessions,
- kernelspecs: provider.kernelspecs,
- opener
- });
- let fbModel = new FileBrowserModel({
- contentsManager: contents,
- sessionManager: sessions,
- kernelspecs: provider.kernelspecs
- });
- let fbWidget = new FileBrowserWidget({
- model: fbModel,
- manager: docManager,
- opener
- });
- let menu = createMenu(fbWidget);
- // Add a context menu to the dir listing.
- let node = fbWidget.node.getElementsByClassName('jp-DirListing-content')[0];
- node.addEventListener('contextmenu', (event: MouseEvent) => {
- event.preventDefault();
- let x = event.clientX;
- let y = event.clientY;
- menu.popup(x, y);
- });
- // Add the command for a new items.
- let newTextFileId = 'file-operations:new-text-file';
- app.commands.add([
- {
- id: newTextFileId,
- <<<<<<< HEAD
- handler: () => {
- let icon = `${PORTRAIT_ICON_CLASS} ${TEXTEDITOR_ICON_CLASS}`;
- fbWidget.createNew('file').then(widget => widget.title.icon = icon);
- }
- =======
- handler: () => fbWidget.createNew({ type: 'file' })
- >>>>>>> e8dce4a... Update jupyter-js-services and clean up interfaces
- }
- ]);
- let newNotebookId = 'file-operations:new-notebook';
- app.commands.add([
- {
- id: newNotebookId,
- handler: () => {
- let icon = `${PORTRAIT_ICON_CLASS} ${NOTEBOOK_ICON_CLASS}`;
- fbWidget.createNew({ type: 'notebook' }).then(widget => widget.title.icon = icon);
- }
- }]);
- // Add the command for saving a document.
- let saveDocumentId = 'file-operations:save';
- app.commands.add([
- {
- id: saveDocumentId,
- handler: () => {
- if (activeWidget) {
- let context = docManager.contextForWidget(activeWidget);
- context.save();
- }
- }
- }
- ]);
- app.palette.add([
- {
- command: saveDocumentId,
- category: 'File Operations',
- text: 'Save Document',
- caption: 'Save the current document'
- }
- ]);
- // Add the command for reverting a document.
- let revertDocumentId = 'file-operations:revert';
- app.commands.add([
- {
- id: revertDocumentId,
- handler: () => {
- if (activeWidget) {
- let context = docManager.contextForWidget(activeWidget);
- context.revert();
- }
- }
- }
- ]);
- app.palette.add([
- {
- command: revertDocumentId,
- category: 'File Operations',
- text: 'Revert Document',
- caption: 'Revert the current document'
- }
- ]);
- // Add the command for closing a document.
- let closeDocumentId = 'file-operations:close';
- app.commands.add([
- {
- id: closeDocumentId,
- handler: () => {
- if (activeWidget) {
- activeWidget.close();
- }
- }
- }
- ]);
- app.palette.add([
- {
- command: closeDocumentId,
- category: 'File Operations',
- text: 'Close Document',
- caption: 'Close the current document'
- }
- ]);
- // Add the command for closing all documents.
- let closeAllId = 'file-operations:close-all';
- app.commands.add([
- {
- id: closeAllId,
- handler: () => {
- docManager.closeAll();
- }
- }
- ]);
- app.palette.add([
- {
- command: closeAllId,
- category: 'File Operations',
- text: 'Close All',
- caption: 'Close all open documents'
- }
- ]);
- app.palette.add([
- {
- command: newTextFileId,
- category: 'File Operations',
- text: 'New Text File',
- caption: 'Create a new text file'
- },
- {
- command: newNotebookId,
- category: 'File Operations',
- text: 'New Notebook',
- caption: 'Create a new notebook'
- }
- ]);
- app.commands.add([
- {
- id: 'file-browser:activate',
- handler: showBrowser
- },
- {
- id: 'file-browser:hide',
- handler: hideBrowser
- },
- {
- id: 'file-browser:toggle',
- handler: toggleBrowser
- }
- ]);
- fbWidget.title.text = 'Files';
- fbWidget.id = 'file-browser';
- app.shell.addToLeftArea(fbWidget, { rank: 40 });
- showBrowser();
- return Promise.resolve(void 0);
- function showBrowser(): void {
- app.shell.activateLeft(fbWidget.id);
- }
- function hideBrowser(): void {
- if (!fbWidget.isHidden) {
- app.shell.collapseLeft();
- }
- }
- function toggleBrowser(): void {
- if (fbWidget.isHidden) {
- showBrowser();
- } else {
- hideBrowser();
- }
- }
- }
- /**
- * Create a context menu for the file browser listing.
- */
- function createMenu(fbWidget: FileBrowserWidget): Menu {
- return new Menu([
- new MenuItem({
- text: '&Open',
- icon: 'fa fa-folder-open-o',
- shortcut: 'Ctrl+O',
- handler: () => { fbWidget.open(); }
- }),
- new MenuItem({
- text: '&Rename',
- icon: 'fa fa-edit',
- shortcut: 'Ctrl+R',
- handler: () => { fbWidget.rename(); }
- }),
- new MenuItem({
- text: '&Delete',
- icon: 'fa fa-remove',
- shortcut: 'Ctrl+D',
- handler: () => { fbWidget.delete(); }
- }),
- new MenuItem({
- text: 'Duplicate',
- icon: 'fa fa-copy',
- handler: () => { fbWidget.duplicate(); }
- }),
- new MenuItem({
- text: 'Cut',
- icon: 'fa fa-cut',
- shortcut: 'Ctrl+X',
- handler: () => { fbWidget.cut(); }
- }),
- new MenuItem({
- text: '&Copy',
- icon: 'fa fa-copy',
- shortcut: 'Ctrl+C',
- handler: () => { fbWidget.copy(); }
- }),
- new MenuItem({
- text: '&Paste',
- icon: 'fa fa-paste',
- shortcut: 'Ctrl+V',
- handler: () => { fbWidget.paste(); }
- }),
- new MenuItem({
- text: 'Download',
- icon: 'fa fa-download',
- handler: () => { fbWidget.download(); }
- }),
- new MenuItem({
- text: 'Shutdown Kernel',
- icon: 'fa fa-stop-circle-o',
- handler: () => { fbWidget.shutdownKernels(); }
- })
- ]);
- }
|