factory.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import * as CodeMirror
  4. from 'codemirror';
  5. import {
  6. CodeEditor, IEditorFactoryService
  7. } from '../codeeditor';
  8. import {
  9. CodeMirrorEditor
  10. } from './editor';
  11. /**
  12. * CodeMirror editor factory.
  13. */
  14. export
  15. class CodeMirrorEditorFactory implements IEditorFactoryService {
  16. /**
  17. * Construct an IEditorFactoryService for CodeMirrorEditors.
  18. */
  19. constructor(codeMirrorOptions?: CodeMirror.EditorConfiguration) {
  20. this.inlineCodeMirrorOptions = {
  21. extraKeys: {
  22. 'Cmd-Right': 'goLineRight',
  23. 'End': 'goLineRight',
  24. 'Cmd-Left': 'goLineLeft',
  25. 'Tab': 'indentMore',
  26. 'Shift-Tab': 'indentLess',
  27. 'Cmd-Alt-[': 'indentAuto',
  28. 'Ctrl-Alt-[': 'indentAuto',
  29. 'Cmd-/': 'toggleComment',
  30. 'Ctrl-/': 'toggleComment',
  31. }
  32. };
  33. this.documentCodeMirrorOptions = {
  34. extraKeys: {
  35. 'Tab': 'indentMore',
  36. 'Shift-Enter': () => { /* no-op */ }
  37. },
  38. lineNumbers: true,
  39. lineWrapping: true
  40. };
  41. if (codeMirrorOptions !== undefined) {
  42. // Note: If codeMirrorOptions include `extraKeys`,
  43. // existing option will be overwritten.
  44. Private.assign(this.inlineCodeMirrorOptions, codeMirrorOptions);
  45. Private.assign(this.documentCodeMirrorOptions, codeMirrorOptions);
  46. }
  47. }
  48. /**
  49. * Create a new editor for inline code.
  50. */
  51. newInlineEditor(options: CodeEditor.IOptions): CodeEditor.IEditor {
  52. return new CodeMirrorEditor(options, this.inlineCodeMirrorOptions);
  53. }
  54. /**
  55. * Create a new editor for a full document.
  56. */
  57. newDocumentEditor(options: CodeEditor.IOptions): CodeEditor.IEditor {
  58. return new CodeMirrorEditor(options, this.documentCodeMirrorOptions);
  59. }
  60. protected inlineCodeMirrorOptions: CodeMirror.EditorConfiguration;
  61. protected documentCodeMirrorOptions: CodeMirror.EditorConfiguration;
  62. }
  63. namespace Private {
  64. // Replace with Object.assign when available.
  65. export
  66. function assign<T>(target: T, ...configs: any[]): T {
  67. for (const source of configs) {
  68. if (source) {
  69. Object.keys(source).forEach(key => {
  70. (target as any)[key] = (source as any)[key];
  71. });
  72. }
  73. }
  74. return target;
  75. }
  76. }