index.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 ignoredDefaultMacKeybindings = new Set([
  64. // We replace these with our custom visual-line versions
  65. 'Mod-Backspace',
  66. 'Mod-Delete',
  67. ])
  68. const moduleExtensions: Array<() => Extension> = importOverleafModules(
  69. 'sourceEditorExtensions'
  70. ).map((item: { import: { extension: Extension } }) => item.import.extension)
  71. export const createExtensions = (options: Record<string, any>): Extension[] => [
  72. lineNumbers(),
  73. highlightSpecialChars(),
  74. history({ newGroupDelay: 250 }),
  75. foldGutter({
  76. openText: '▾',
  77. closedText: '▸',
  78. }),
  79. drawSelection(),
  80. EditorState.allowMultipleSelections.of(true),
  81. EditorView.lineWrapping,
  82. indentOnInput(),
  83. lineWrappingIndentation(options.visual.visual),
  84. indentationMarkers(options.visual.visual),
  85. bracketMatching(),
  86. bracketSelection(),
  87. rectangularSelection(),
  88. crosshairCursor(),
  89. dropCursor(),
  90. tooltips({
  91. parent: document.body,
  92. }),
  93. keymap.of([
  94. ...defaultKeymap.filter(
  95. // We only filter on keys, so if the keybinding doesn't have a key,
  96. // allow it
  97. item => {
  98. if (item.key && ignoredDefaultKeybindings.has(item.key)) {
  99. return false
  100. }
  101. if (item.mac && ignoredDefaultMacKeybindings.has(item.mac)) {
  102. return false
  103. }
  104. return true
  105. }
  106. ),
  107. ...historyKeymap,
  108. ...lintKeymap,
  109. ]),
  110. foldingKeymap(),
  111. goToLinePanel(),
  112. filterCharacters(),
  113. // `autoComplete` needs to be before `keybindings` so that arrow key handling
  114. // in the autocomplete pop-up takes precedence over Vim/Emacs key bindings
  115. autoComplete(options.settings),
  116. // `keybindings` needs to be before `language` so that Vim/Emacs bindings take
  117. // precedence over language-specific keyboard shortcuts
  118. keybindings(),
  119. annotations(), // NOTE: must be before `language`
  120. language(options.currentDoc, options.metadata, options.settings),
  121. theme(options.theme),
  122. realtime(options.currentDoc, options.handleError),
  123. cursorPosition(options.currentDoc),
  124. scrollPosition(options.currentDoc),
  125. cursorHighlights(),
  126. autoPair(options.settings),
  127. editable(),
  128. search(),
  129. phrases(options.phrases),
  130. parserWatcher(),
  131. spelling(options.spelling),
  132. shortcuts(),
  133. symbolPalette(),
  134. // TODO: re-enable this once incompatibility with @codemirror/view is fixed
  135. // emptyLineFiller(), // NOTE: must be before `trackChanges`
  136. trackChanges(options.currentDoc, options.changeManager),
  137. visual(options.currentDoc, options.visual),
  138. verticalOverflow(),
  139. highlightActiveLine(options.visual.visual),
  140. highlightActiveLineGutter(),
  141. scrollOneLine(),
  142. fontLoad(),
  143. inlineBackground(options.visual.visual),
  144. codemirrorDevTools(),
  145. exceptionLogger(),
  146. moduleExtensions.map(extension => extension()),
  147. thirdPartyExtensions(),
  148. ]