plugin.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. JupyterLab, JupyterLabPlugin
  5. } from '../application';
  6. import {
  7. InstanceTracker
  8. } from '../common/instancetracker';
  9. import {
  10. IDocumentRegistry
  11. } from '../docregistry';
  12. import {
  13. IInstanceRestorer
  14. } from '../instancerestorer';
  15. import {
  16. CSVWidget, CSVWidgetFactory
  17. } from './widget';
  18. /**
  19. * The name of the factory that creates CSV widgets.
  20. */
  21. const FACTORY = 'Table';
  22. /**
  23. * The table file handler extension.
  24. */
  25. const plugin: JupyterLabPlugin<void> = {
  26. activate,
  27. id: 'jupyter.extensions.csv-handler',
  28. requires: [IDocumentRegistry, IInstanceRestorer],
  29. autoStart: true
  30. };
  31. /**
  32. * Export the plugin as default.
  33. */
  34. export default plugin;
  35. /**
  36. * Activate the table widget extension.
  37. */
  38. function activate(app: JupyterLab, registry: IDocumentRegistry, restorer: IInstanceRestorer): void {
  39. const factory = new CSVWidgetFactory({
  40. name: FACTORY,
  41. fileExtensions: ['.csv'],
  42. defaultFor: ['.csv']
  43. });
  44. const tracker = new InstanceTracker<CSVWidget>({ namespace: 'csvwidget' });
  45. // Handle state restoration.
  46. restorer.restore(tracker, {
  47. command: 'file-operations:open',
  48. args: widget => ({ path: widget.context.path, factory: FACTORY }),
  49. name: widget => widget.context.path
  50. });
  51. registry.addWidgetFactory(factory);
  52. factory.widgetCreated.connect((sender, widget) => {
  53. // Track the widget.
  54. tracker.add(widget);
  55. // Notify the instance tracker if restore data needs to update.
  56. widget.context.pathChanged.connect(() => { tracker.save(widget); });
  57. });
  58. }