factory.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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-/': 'toggleComment',
  27. 'Ctrl-/': 'toggleComment',
  28. },
  29. ...defaults
  30. };
  31. this.documentCodeMirrorConfig = {
  32. ...CodeMirrorEditor.defaultConfig,
  33. extraKeys: {
  34. 'Tab': 'indentMoreOrinsertTab',
  35. 'Shift-Tab': 'indentLess',
  36. 'Shift-Enter': () => { /* no-op */ }
  37. },
  38. lineNumbers: true,
  39. scrollPastEnd: true,
  40. ...defaults
  41. };
  42. }
  43. /**
  44. * Create a new editor for inline code.
  45. */
  46. newInlineEditor = (options: CodeEditor.IOptions) => {
  47. options.host.dataset.type = 'inline';
  48. return new CodeMirrorEditor({
  49. ...options,
  50. config: { ...this.inlineCodeMirrorConfig, ...options.config || {} }
  51. });
  52. }
  53. /**
  54. * Create a new editor for a full document.
  55. */
  56. newDocumentEditor = (options: CodeEditor.IOptions) => {
  57. options.host.dataset.type = 'document';
  58. return new CodeMirrorEditor({
  59. ...options,
  60. config: { ...this.documentCodeMirrorConfig, ...options.config || {} }
  61. });
  62. }
  63. protected inlineCodeMirrorConfig: Partial<CodeMirrorEditor.IConfig>;
  64. protected documentCodeMirrorConfig: Partial<CodeMirrorEditor.IConfig>;
  65. }