123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- 'use strict';
- import {
- FileBrowserWidget, FileHandler
- } from 'jupyter-js-filebrowser';
- import {
- IAppShell, ICommandPalette, ICommandRegistry
- } from 'phosphide';
- import {
- DelegateCommand
- } from 'phosphor-command';
- import {
- Container, Token
- } from 'phosphor-di';
- import {
- Property
- } from 'phosphor-properties';
- import {
- Widget
- } from 'phosphor-widget';
- import {
- IFileBrowserProvider
- } from '../index';
- import {
- IFileOpener, IFileHandler
- } from './index';
- /**
- * Register the plugin contributions.
- */
- export
- function resolve(container: Container): Promise<void> {
- return container.resolve(FileOpenerProvider).then(provider => provider.run());
- }
- export
- function register(container: Container): void {
- container.register(IFileOpener, FileOpener);
- }
- class FileOpenerProvider {
- /**
- * The dependencies required by the file opener.
- */
- static requires: Token<any>[] = [IAppShell, IFileOpener, IFileBrowserProvider, ICommandPalette, ICommandRegistry];
- static create(appShell: IAppShell, opener: IFileOpener, browserProvider: IFileBrowserProvider, palette: ICommandPalette, registry: ICommandRegistry): FileOpenerProvider {
- return new FileOpenerProvider(appShell, opener, browserProvider, palette, registry);
- }
- /**
- * Construct a new file opener.
- */
- constructor(appShell: IAppShell, opener: IFileOpener, browserProvider: IFileBrowserProvider, palette: ICommandPalette, registry: ICommandRegistry) {
- this._browser = browserProvider.fileBrowser;
- this._registry = registry;
- this._palette = palette;
- this._appShell = appShell;
- this._opener = opener;
- }
- run() {
- let newFileCommandItem = {
- id: 'jupyter-plugins:new-text-file',
- command: new DelegateCommand(() => {
- this._browser.newUntitled('file', '.txt').then(
- contents => this._opener.open(contents.path)
- );
- })
- }
- let newNotebookCommandItem = {
- id: 'jupyter-plugins:new-notebook',
- command: new DelegateCommand(() => {
- this._browser.newUntitled('notebook').then(
- contents => this._opener.open(contents.path)
- );
- })
- }
- this._registry.add([newFileCommandItem, newNotebookCommandItem]);
- let paletteItems = [{
- id: 'jupyter-plugins:new-text-file',
- title: 'Text File',
- caption: ''
- }, {
- id: 'jupyter-plugins:new-notebook',
- title: 'Notebook',
- caption: ''
- }];
- let section = {
- text: 'New...',
- items: paletteItems
- }
- this._palette.add([section]);
- }
- private _appShell: IAppShell = null;
- private _defaultHandler: IFileHandler = null;
- private _browser: FileBrowserWidget = null;
- private _registry: ICommandRegistry = null;
- private _palette: ICommandPalette = null;
- private _opener: IFileOpener = null;
- }
- /**
- * An implementation on an IFileOpener.
- */
- class FileOpener implements IFileOpener {
- /**
- * The dependencies required by the file opener.
- */
- static requires: Token<any>[] = [IAppShell, IFileBrowserProvider];
- /**
- * Create a new file opener instance.
- */
- static create(appShell: IAppShell, browserProvider: IFileBrowserProvider): IFileOpener {
- return new FileOpener(appShell, browserProvider);
- }
- /**
- * Construct a new file opener.
- */
- constructor(appShell: IAppShell, browserProvider: IFileBrowserProvider) {
- this._appShell = appShell;
- browserProvider.fileBrowser.openRequested.connect(this._openRequested,
- this);
- }
- /**
- * Register a file handler.
- */
- register(handler: IFileHandler): void {
- this._handlers.push(handler);
- }
- /**
- * Register a default file handler.
- */
- registerDefault(handler: IFileHandler): void {
- if (this._defaultHandler !== null) {
- throw Error('Default handler already registered');
- }
- this._handlers.push(handler);
- this._defaultHandler = handler;
- }
- /**
- * Open a file and add it to the application shell.
- */
- open(path: string): Widget {
- if (this._handlers.length === 0) {
- return;
- }
- let ext = '.' + path.split('.').pop();
- let handlers: IFileHandler[] = [];
- // Look for matching file extensions.
- for (let h of this._handlers) {
- if (h.fileExtensions.indexOf(ext) !== -1) handlers.push(h);
- }
- // If there was only one match, use it.
- if (handlers.length === 1) {
- return this._open(handlers[0], path);
- // If there were no matches, look for default handler(s).
- } else if (handlers.length === 0 && this._defaultHandler !== null) {
- return this._open(this._defaultHandler, path);
- }
- // If there we no matches, do nothing.
- if (handlers.length == 0) {
- throw new Error(`Could not open file '${path}'`);
- // If there was one handler, use it.
- } else if (handlers.length === 1) {
- return this._open(handlers[0], path);
- } else {
- // There are more than one possible handlers.
- // TODO: Ask the user to choose one.
- return this._open(handlers[0], path);
- }
- }
- /**
- * Handle an `openRequested` signal by invoking the appropriate handler.
- */
- private _openRequested(browser: FileBrowserWidget, path: string): void {
- this.open(path);
- }
- /**
- * Open a file and add it to the application shell.
- */
- private _open(handler: IFileHandler, path: string): Widget {
- var widget = handler.open(path);
- this._appShell.addToMainArea(widget);
- return widget;
- }
- private _handlers: IFileHandler[] = [];
- private _appShell: IAppShell = null;
- private _defaultHandler: IFileHandler = null;
- }
|