factory.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. utils
  5. } from '@jupyterlab/services';
  6. import {
  7. CodeEditor, IEditorFactory
  8. } from '../codeeditor';
  9. import {
  10. CodeMirrorEditor, DEFAULT_CODEMIRROR_THEME
  11. } from './editor';
  12. /**
  13. * CodeMirror editor factory.
  14. */
  15. export
  16. class CodeMirrorEditorFactory implements IEditorFactory {
  17. /**
  18. * Create a new editor for inline code.
  19. */
  20. newInlineEditor(host: HTMLElement, options: CodeEditor.IOptions): CodeEditor.IEditor {
  21. return this.newEditor(host, {
  22. uuid: utils.uuid(),
  23. indentUnit: 4,
  24. model: options.model,
  25. theme: DEFAULT_CODEMIRROR_THEME,
  26. extraKeys: {
  27. 'Cmd-Right': 'goLineRight',
  28. 'End': 'goLineRight',
  29. 'Cmd-Left': 'goLineLeft',
  30. 'Tab': 'indentMore',
  31. 'Shift-Tab': 'indentLess',
  32. 'Cmd-Alt-[': 'indentAuto',
  33. 'Ctrl-Alt-[': 'indentAuto',
  34. 'Cmd-/': 'toggleComment',
  35. 'Ctrl-/': 'toggleComment',
  36. }
  37. }, options);
  38. }
  39. /**
  40. * Create a new editor for a full document.
  41. */
  42. newDocumentEditor(host: HTMLElement, options: CodeEditor.IOptions): CodeEditor.IEditor {
  43. return this.newEditor(host, {
  44. uuid: utils.uuid(),
  45. model: options.model,
  46. extraKeys: {
  47. 'Tab': 'indentMore',
  48. 'Shift-Enter': () => { /* no-op */ }
  49. },
  50. indentUnit: 4,
  51. theme: DEFAULT_CODEMIRROR_THEME,
  52. lineNumbers: true,
  53. lineWrapping: true
  54. }, options);
  55. }
  56. /**
  57. * Creates an editor and applies extra options.
  58. */
  59. protected newEditor(host: HTMLElement, editorOptions: CodeMirrorEditor.IOptions, options: CodeEditor.IOptions): CodeEditor.IEditor {
  60. if (options.readOnly !== undefined) {
  61. editorOptions.readOnly = options.readOnly;
  62. }
  63. if (options.lineNumbers !== undefined) {
  64. editorOptions.lineNumbers = options.lineNumbers;
  65. }
  66. if (options.wordWrap !== undefined) {
  67. editorOptions.lineWrapping = options.wordWrap;
  68. }
  69. const editor = new CodeMirrorEditor(host, editorOptions);
  70. const extra = options.extra;
  71. if (extra) {
  72. for (const option in extra) {
  73. editor.editor.setOption(option, extra[option]);
  74. }
  75. }
  76. return editor;
  77. }
  78. }