plugin.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. 'use strict';
  4. import {
  5. FileBrowser
  6. } from 'jupyter-js-filebrowser';
  7. import {
  8. IAppShell, ICommandPalette, ICommandRegistry
  9. } from 'phosphide';
  10. import {
  11. ICommand, DelegateCommand
  12. } from 'phosphor-command';
  13. import {
  14. Container, Token
  15. } from 'phosphor-di';
  16. import {
  17. ITerminalProvider, IFileBrowserProvider
  18. } from '../lib';
  19. /**
  20. * Register the plugin contributions.
  21. *
  22. * @param container - The di container for type registration.
  23. *
  24. * #### Notes
  25. * This is called automatically when the plugin is loaded.
  26. */
  27. export
  28. function resolve(container: Container): void {
  29. container.resolve(DefaultHandler).then(handler => { handler.run(); });
  30. }
  31. /**
  32. * The default plugin for the example.
  33. */
  34. class DefaultHandler {
  35. /**
  36. * The dependencies required by the default plugin.
  37. */
  38. static requires: Token<any>[] = [IAppShell, ITerminalProvider, ICommandPalette, ICommandRegistry, IFileBrowserProvider];
  39. /**
  40. * Create a default plugin instance..
  41. */
  42. static create(shell: IAppShell, term: ITerminalProvider, palette: ICommandPalette, registry: ICommandRegistry, browser: IFileBrowserProvider): DefaultHandler {
  43. return new DefaultHandler(shell, term, palette, registry, browser);
  44. }
  45. /**
  46. * Construct a new default plugin.
  47. */
  48. constructor(shell: IAppShell, term: ITerminalProvider, palette: ICommandPalette, registry: ICommandRegistry, browser: IFileBrowserProvider) {
  49. this._shell = shell;
  50. this._term = term;
  51. this._palette = palette;
  52. this._registry = registry;
  53. this._browser = browser.fileBrowser;
  54. }
  55. /**
  56. * Create a terminal and add it to the main shell area.
  57. */
  58. run() {
  59. let commandItem = {
  60. id: 'jupyter-plugins:new-terminal',
  61. command: new DelegateCommand(() => {
  62. let term = this._term.createTerminal();
  63. this._shell.addToMainArea(term);
  64. })
  65. }
  66. this._registry.add([commandItem]);
  67. let paletteItem = {
  68. id: 'jupyter-plugins:new-terminal',
  69. title: 'New Terminal',
  70. caption: ''
  71. }
  72. let section = {
  73. text: 'Open...',
  74. items: [paletteItem]
  75. }
  76. this._palette.add([section]);
  77. this._shell.addToLeftArea(this._browser, { rank: 10 });
  78. }
  79. private _term: ITerminalProvider = null;
  80. private _shell: IAppShell = null;
  81. private _palette: ICommandPalette = null;
  82. private _registry: ICommandRegistry = null;
  83. private _browser: FileBrowser;
  84. }