widget.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ICellEditorWidget
  5. } from '../../cells/editor';
  6. import {
  7. CodeCellWidget
  8. } from '../../cells/widget';
  9. import {
  10. CodeMirrorCellEditorWidget
  11. } from './editor';
  12. /**
  13. * A code mirror renderer for a code cell widget.
  14. */
  15. export
  16. class CodeMirrorCodeCellWidgetRenderer extends CodeCellWidget.Renderer {
  17. /**
  18. * Construct a code mirror renderer for a code cell widget.
  19. * @param editorConfiguration a code mirror editor configuration
  20. * @param editorInitializer a code cell widget initializer
  21. */
  22. constructor(options: CodeMirrorCodeCellWidgetRenderer.IOptions = {}) {
  23. super();
  24. this._editorConfiguration = (options.editorConfiguration ||
  25. CodeMirrorCodeCellWidgetRenderer.defaultEditorConfiguration);
  26. this._editorInitializer = (options.editorInitializer ||
  27. (editor => { /* no-op */ }));
  28. }
  29. /**
  30. * Construct a code cell widget.
  31. */
  32. createCellEditor(): ICellEditorWidget {
  33. const widget = new CodeMirrorCellEditorWidget(this._editorConfiguration);
  34. this._editorInitializer(widget);
  35. return widget;
  36. }
  37. private _editorConfiguration: CodeMirror.EditorConfiguration = null;
  38. private _editorInitializer: (editor: CodeMirrorCellEditorWidget) => void = null;
  39. }
  40. /**
  41. * A namespace for `CodeMirrorCodeCellWidgetRenderer` statics.
  42. */
  43. export
  44. namespace CodeMirrorCodeCellWidgetRenderer {
  45. /**
  46. * The options used to construct a code mirror code cell widget renderer.
  47. */
  48. export
  49. interface IOptions {
  50. /**
  51. * A code mirror editor configuration.
  52. */
  53. editorConfiguration?: CodeMirror.EditorConfiguration;
  54. /**
  55. * A code cell widget initializer function.
  56. */
  57. editorInitializer?: (editor: CodeMirrorCellEditorWidget) => void;
  58. }
  59. /**
  60. * A default code mirror configuration for a cell editor.
  61. */
  62. export
  63. const defaultEditorConfiguration: CodeMirror.EditorConfiguration = {
  64. indentUnit: 4,
  65. readOnly: false,
  66. theme: 'default',
  67. extraKeys: {
  68. 'Cmd-Right': 'goLineRight',
  69. 'End': 'goLineRight',
  70. 'Cmd-Left': 'goLineLeft',
  71. 'Tab': 'indentMore',
  72. 'Shift-Tab': 'indentLess',
  73. 'Cmd-Alt-[': 'indentAuto',
  74. 'Ctrl-Alt-[': 'indentAuto',
  75. 'Cmd-/': 'toggleComment',
  76. 'Ctrl-/': 'toggleComment',
  77. }
  78. };
  79. /**
  80. * A default code mirror renderer for a code cell widget.
  81. */
  82. export
  83. const defaultRenderer = new CodeMirrorCodeCellWidgetRenderer();
  84. }