plugin.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. 'use strict';
  4. import {
  5. NotebookWidget, NotebookModel, NBData, populateNotebookModel
  6. } from 'jupyter-js-notebook';
  7. import {
  8. Container
  9. } from 'phosphor-di';
  10. import {
  11. IContentsModel, IContentsManager
  12. } from 'jupyter-js-services';
  13. import {
  14. IServicesProvider, IFileOpener
  15. } from '../index';
  16. import {
  17. AbstractFileHandler
  18. } from 'jupyter-js-filebrowser';
  19. import {
  20. Widget
  21. } from 'phosphor-widget';
  22. import './plugin.css';
  23. /**
  24. * Register the plugin contributions.
  25. *
  26. * @param container - The di container for type registration.
  27. *
  28. * #### Notes
  29. * This is called automatically when the plugin is loaded.
  30. */
  31. export
  32. function resolve(container: Container) {
  33. return Promise.all([container.resolve(IServicesProvider),
  34. container.resolve(IFileOpener)]).then(([services, opener]) => {
  35. opener.registerDefault(new NotebookFileHandler(services.contentsManager))
  36. }).then(() => {});
  37. }
  38. /**
  39. * An implementation of a file handler.
  40. */
  41. export
  42. class NotebookFileHandler extends AbstractFileHandler {
  43. /**
  44. * Get the list of file extensions supported by the handler.
  45. */
  46. get fileExtensions(): string[] {
  47. return ['.ipynb']
  48. }
  49. /**
  50. * Get file contents given a path.
  51. */
  52. protected getContents(path: string): Promise<IContentsModel> {
  53. return this.manager.get(path, { type: 'notebook' });
  54. }
  55. /**
  56. * Create the widget from an `IContentsModel`.
  57. */
  58. protected createWidget(path: string): Widget {
  59. let model = new NotebookModel();
  60. let widget = new NotebookWidget(model);
  61. widget.title.text = path.split('/').pop();
  62. return widget;
  63. }
  64. protected populateWidget(widget: NotebookWidget, model: IContentsModel): Promise<void> {
  65. let nbdata: NBData = makedata(model);
  66. populateNotebookModel(widget.model, nbdata);
  67. return Promise.resolve();
  68. }
  69. }
  70. function makedata(a: IContentsModel): NBData {
  71. return {
  72. content: a.content,
  73. name: a.name,
  74. path: a.path
  75. }
  76. }