factory.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. CodeEditor, IEditorFactoryService
  5. } from '@jupyterlab/codeeditor';
  6. import {
  7. CodeMirrorEditor
  8. } from './editor';
  9. /**
  10. * CodeMirror editor factory.
  11. */
  12. export
  13. class CodeMirrorEditorFactory implements IEditorFactoryService {
  14. /**
  15. * Construct an IEditorFactoryService for CodeMirrorEditors.
  16. */
  17. constructor(defaults: Partial<CodeMirrorEditor.IConfig> = {}) {
  18. this.inlineCodeMirrorConfig = {
  19. ...CodeMirrorEditor.defaultConfig,
  20. extraKeys: {
  21. 'Cmd-Right': 'goLineRight',
  22. 'End': 'goLineRight',
  23. 'Cmd-Left': 'goLineLeft',
  24. 'Tab': 'indentMoreOrinsertTab',
  25. 'Shift-Tab': 'indentLess',
  26. 'Cmd-Alt-[': 'indentAuto',
  27. 'Ctrl-Alt-[': 'indentAuto',
  28. 'Cmd-/': 'toggleComment',
  29. 'Ctrl-/': 'toggleComment',
  30. },
  31. ...defaults
  32. };
  33. this.documentCodeMirrorConfig = {
  34. ...CodeMirrorEditor.defaultConfig,
  35. extraKeys: {
  36. 'Tab': 'indentMoreOrinsertTab',
  37. 'Shift-Enter': () => { /* no-op */ }
  38. },
  39. lineNumbers: true,
  40. ...defaults
  41. };
  42. }
  43. /**
  44. * Create a new editor for inline code.
  45. */
  46. newInlineEditor(options: CodeEditor.IOptions): CodeEditor.IEditor {
  47. return new CodeMirrorEditor({
  48. ...options,
  49. config: { ...this.inlineCodeMirrorConfig, ...options.config || {} }
  50. });
  51. }
  52. /**
  53. * Create a new editor for a full document.
  54. */
  55. newDocumentEditor(options: CodeEditor.IOptions): CodeEditor.IEditor {
  56. return new CodeMirrorEditor({
  57. ...options,
  58. config: { ...this.documentCodeMirrorConfig, ...options.config || {} }
  59. });
  60. }
  61. protected inlineCodeMirrorConfig: Partial<CodeMirrorEditor.IConfig>;
  62. protected documentCodeMirrorConfig: Partial<CodeMirrorEditor.IConfig>;
  63. }