factory.ts 2.0 KB

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