notebook.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { INotebookTracker, NotebookTracker } from '@jupyterlab/notebook';
  4. import { CodeCell } from '@jupyterlab/cells';
  5. import { IDisposable } from '@phosphor/disposable';
  6. import { Signal } from '@phosphor/signaling';
  7. import { Breakpoints } from '../breakpoints';
  8. import { Debugger } from '../debugger';
  9. import { IDebugger } from '../tokens';
  10. import { CellManager } from './cell';
  11. export class NotebookHandler implements IDisposable {
  12. constructor(options: NotebookHandler.IOptions) {
  13. this.debuggerModel = options.debuggerService.model;
  14. this.debuggerService = options.debuggerService;
  15. this.notebookTracker = options.tracker;
  16. this.id = options.id;
  17. this.breakpoints = this.debuggerModel.breakpointsModel;
  18. this.cellManager = new CellManager({
  19. breakpointsModel: this.breakpoints,
  20. debuggerModel: this.debuggerModel,
  21. debuggerService: this.debuggerService,
  22. type: 'notebook',
  23. activeCell: this.notebookTracker.activeCell as CodeCell
  24. });
  25. this.notebookTracker.activeCellChanged.connect(
  26. this.onActiveCellChanged,
  27. this
  28. );
  29. }
  30. isDisposed: boolean;
  31. dispose(): void {
  32. if (this.isDisposed) {
  33. return;
  34. }
  35. this.isDisposed = true;
  36. this.cellManager.dispose();
  37. Signal.clearData(this);
  38. }
  39. protected onActiveCellChanged(
  40. notebookTracker: NotebookTracker,
  41. codeCell: CodeCell
  42. ) {
  43. if (notebookTracker.currentWidget.id === this.id) {
  44. requestAnimationFrame(() => {
  45. this.cellManager.activeCell = codeCell;
  46. });
  47. }
  48. }
  49. private notebookTracker: INotebookTracker;
  50. private debuggerModel: Debugger.Model;
  51. private debuggerService: IDebugger;
  52. private breakpoints: Breakpoints.Model;
  53. private cellManager: CellManager;
  54. private id: string;
  55. }
  56. export namespace NotebookHandler {
  57. export interface IOptions {
  58. debuggerService: IDebugger;
  59. tracker: INotebookTracker;
  60. id?: string;
  61. }
  62. }