plugin.ts 1.6 KB

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