index.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {
  2. EditorView,
  3. highlightSpecialChars,
  4. keymap,
  5. rectangularSelection,
  6. tooltips,
  7. crosshairCursor,
  8. dropCursor,
  9. highlightActiveLineGutter,
  10. } from '@codemirror/view'
  11. import { EditorState, Extension } from '@codemirror/state'
  12. import { foldGutter, indentOnInput } from '@codemirror/language'
  13. import { history, historyKeymap, defaultKeymap } from '@codemirror/commands'
  14. import { lintKeymap } from '@codemirror/lint'
  15. import { language } from './language'
  16. import { lineWrappingIndentation } from './line-wrapping-indentation'
  17. import { theme } from './theme'
  18. import { realtime } from './realtime'
  19. import { cursorPosition } from './cursor-position'
  20. import { scrollPosition } from './scroll-position'
  21. import { annotations } from './annotations'
  22. import { cursorHighlights } from './cursor-highlights'
  23. import { autoComplete } from './auto-complete'
  24. import { editable } from './editable'
  25. import { autoPair } from './auto-pair'
  26. import { phrases } from './phrases'
  27. import { spelling } from './spelling'
  28. import { shortcuts } from './shortcuts'
  29. import { symbolPalette } from './symbol-palette'
  30. import { trackChanges } from './track-changes'
  31. import { search } from './search'
  32. import { filterCharacters } from './filter-characters'
  33. import { keybindings } from './keybindings'
  34. import { bracketMatching, bracketSelection } from './bracket-matching'
  35. import { verticalOverflow } from './vertical-overflow'
  36. import { exceptionLogger } from './exception-logger'
  37. import { thirdPartyExtensions } from './third-party-extensions'
  38. import { lineNumbers } from './line-numbers'
  39. import { highlightActiveLine } from './highlight-active-line'
  40. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  41. // import { emptyLineFiller } from './empty-line-filler'
  42. import { goToLinePanel } from './go-to-line'
  43. import { parserWatcher } from './wait-for-parser'
  44. import { drawSelection } from './draw-selection'
  45. import { visual } from './visual/visual'
  46. import { scrollOneLine } from './scroll-one-line'
  47. import { foldingKeymap } from './folding-keymap'
  48. import { inlineBackground } from './inline-background'
  49. import { fontLoad } from './font-load'
  50. import { indentationMarkers } from './indentation-markers'
  51. import { codemirrorDevTools } from '../languages/latex/codemirror-dev-tools'
  52. const ignoredDefaultKeybindings = new Set([
  53. // NOTE: disable "Mod-Enter" as it's used for "Compile"
  54. 'Mod-Enter',
  55. // Disable Alt+Arrow as we have special behaviour on Windows / Linux
  56. 'Alt-ArrowLeft',
  57. 'Alt-ArrowRight',
  58. // This keybinding causes issues on some keyboard layouts where \ is entered
  59. // using AltGr. Windows treats Ctrl-Alt as AltGr, so trying to insert a \
  60. // with Ctrl-Alt would trigger this keybinding, rather than inserting a \
  61. 'Mod-Alt-\\',
  62. ])
  63. const moduleExtensions: Array<() => Extension> = importOverleafModules(
  64. 'sourceEditorExtensions'
  65. ).map((item: { import: { extension: Extension } }) => item.import.extension)
  66. export const createExtensions = (options: Record<string, any>): Extension[] => [
  67. lineNumbers(),
  68. highlightSpecialChars(),
  69. history({ newGroupDelay: 250 }),
  70. foldGutter({
  71. openText: '▾',
  72. closedText: '▸',
  73. }),
  74. drawSelection(),
  75. EditorState.allowMultipleSelections.of(true),
  76. EditorView.lineWrapping,
  77. indentOnInput(),
  78. lineWrappingIndentation(options.visual.visual),
  79. indentationMarkers(options.visual.visual),
  80. bracketMatching(),
  81. bracketSelection(),
  82. rectangularSelection(),
  83. crosshairCursor(),
  84. dropCursor(),
  85. tooltips({
  86. parent: document.body,
  87. }),
  88. keymap.of([
  89. ...defaultKeymap.filter(
  90. // We only filter on keys, so if the keybinding doesn't have a key,
  91. // allow it
  92. item => !item.key || !ignoredDefaultKeybindings.has(item.key)
  93. ),
  94. ...historyKeymap,
  95. ...lintKeymap,
  96. ]),
  97. foldingKeymap(),
  98. goToLinePanel(),
  99. filterCharacters(),
  100. // `autoComplete` needs to be before `keybindings` so that arrow key handling
  101. // in the autocomplete pop-up takes precedence over Vim/Emacs key bindings
  102. autoComplete(options.settings),
  103. // `keybindings` needs to be before `language` so that Vim/Emacs bindings take
  104. // precedence over language-specific keyboard shortcuts
  105. keybindings(),
  106. annotations(), // NOTE: must be before `language`
  107. language(options.currentDoc, options.metadata, options.settings),
  108. theme(options.theme),
  109. realtime(options.currentDoc, options.handleError),
  110. cursorPosition(options.currentDoc),
  111. scrollPosition(options.currentDoc),
  112. cursorHighlights(),
  113. autoPair(options.settings),
  114. editable(),
  115. search(),
  116. phrases(options.phrases),
  117. parserWatcher(),
  118. spelling(options.spelling),
  119. shortcuts(),
  120. symbolPalette(),
  121. // TODO: re-enable this once incompatibility with @codemirror/view is fixed
  122. // emptyLineFiller(), // NOTE: must be before `trackChanges`
  123. trackChanges(options.currentDoc, options.changeManager),
  124. visual(options.currentDoc, options.visual),
  125. verticalOverflow(),
  126. highlightActiveLine(options.visual.visual),
  127. highlightActiveLineGutter(),
  128. scrollOneLine(),
  129. fontLoad(),
  130. inlineBackground(options.visual.visual),
  131. codemirrorDevTools(),
  132. exceptionLogger(),
  133. moduleExtensions.map(extension => extension()),
  134. thirdPartyExtensions(),
  135. ]