index.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import { CodeEditorWrapper, CodeEditor } from '@jupyterlab/codeeditor';
  6. import { ISignal, Signal } from '@phosphor/signaling';
  7. import { TabPanel } from '@phosphor/widgets';
  8. export class DebuggerEditors extends TabPanel {
  9. constructor(options: DebuggerEditors.IOptions) {
  10. super();
  11. this.tabsMovable = true;
  12. this.model = new DebuggerEditors.IModel();
  13. this.model.editorAdded.connect((sender, data) => {
  14. let editor = new CodeEditorWrapper({
  15. model: new CodeEditor.Model({
  16. value: data.code,
  17. mimeType: data.mimeType
  18. }),
  19. factory: options.editorFactory,
  20. config: {
  21. readOnly: true,
  22. lineNumbers: true
  23. }
  24. });
  25. editor.title.label = data.title;
  26. editor.title.closable = true;
  27. this.addWidget(editor);
  28. });
  29. MOCK_EDITORS.forEach(editor => this.model.addEditor(editor));
  30. this.addClass('jp-DebuggerEditors');
  31. }
  32. /**
  33. * The debugger editors model.
  34. */
  35. model: DebuggerEditors.IModel;
  36. /**
  37. * Dispose the debug editors.
  38. */
  39. dispose(): void {
  40. if (this.isDisposed) {
  41. return;
  42. }
  43. Signal.clearData(this);
  44. }
  45. }
  46. /**
  47. * A namespace for `DebuggerEditors` statics.
  48. */
  49. export namespace DebuggerEditors {
  50. /**
  51. * The options used to create a DebuggerEditors.
  52. */
  53. export interface IOptions {
  54. editorFactory: CodeEditor.Factory;
  55. }
  56. /**
  57. * An interface for read only editors.
  58. */
  59. export interface IEditor {
  60. title: string;
  61. code: string;
  62. mimeType: string;
  63. }
  64. export interface IModel {}
  65. export class IModel implements IModel {
  66. /**
  67. * A signal emitted when a new editor is added.
  68. */
  69. get editorAdded(): ISignal<
  70. DebuggerEditors.IModel,
  71. DebuggerEditors.IEditor
  72. > {
  73. return this._editorAdded;
  74. }
  75. /**
  76. * Get all the editors currently opened.
  77. */
  78. get editors() {
  79. return this._state;
  80. }
  81. /**
  82. * Add a new editor to the editor TabPanel.
  83. * @param editor The read-only editor info to add.
  84. */
  85. addEditor(editor: DebuggerEditors.IEditor) {
  86. this._state.push(editor);
  87. this._editorAdded.emit(editor);
  88. }
  89. private _state: DebuggerEditors.IEditor[] = [];
  90. private _editorAdded = new Signal<this, DebuggerEditors.IEditor>(this);
  91. }
  92. }
  93. const MOCK_EDITORS = [
  94. {
  95. title: 'untitled.py',
  96. mimeType: 'text/x-ipython',
  97. code: 'import math\nprint(math.pi)'
  98. },
  99. {
  100. title: 'test.py',
  101. mimeType: 'text/x-ipython',
  102. code: 'import sys\nprint(sys.version)'
  103. }
  104. ];