index.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. const ignoredDefaultKeybindings = new Set([
  52. // NOTE: disable "Mod-Enter" as it's used for "Compile"
  53. 'Mod-Enter',
  54. // Disable Alt+Arrow as we have special behaviour on Windows / Linux
  55. 'Alt-ArrowLeft',
  56. 'Alt-ArrowRight',
  57. // This keybinding causes issues on some keyboard layouts where \ is entered
  58. // using AltGr. Windows treats Ctrl-Alt as AltGr, so trying to insert a \
  59. // with Ctrl-Alt would trigger this keybinding, rather than inserting a \
  60. 'Mod-Alt-\\',
  61. ])
  62. const moduleExtensions: Array<() => Extension> = importOverleafModules(
  63. 'sourceEditorExtensions'
  64. ).map((item: { import: { extension: Extension } }) => item.import.extension)
  65. export const createExtensions = (options: Record<string, any>): Extension[] => [
  66. lineNumbers(),
  67. highlightSpecialChars(),
  68. history({ newGroupDelay: 250 }),
  69. foldGutter({
  70. openText: '▾',
  71. closedText: '▸',
  72. }),
  73. drawSelection(),
  74. EditorState.allowMultipleSelections.of(true),
  75. EditorView.lineWrapping,
  76. indentOnInput(),
  77. lineWrappingIndentation(options.visual.visual),
  78. indentationMarkers(options.visual.visual),
  79. bracketMatching(),
  80. bracketSelection(),
  81. rectangularSelection(),
  82. crosshairCursor(),
  83. dropCursor(),
  84. tooltips({
  85. parent: document.body,
  86. }),
  87. keymap.of([
  88. ...defaultKeymap.filter(
  89. // We only filter on keys, so if the keybinding doesn't have a key,
  90. // allow it
  91. item => !item.key || !ignoredDefaultKeybindings.has(item.key)
  92. ),
  93. ...historyKeymap,
  94. ...lintKeymap,
  95. ]),
  96. foldingKeymap(),
  97. goToLinePanel(),
  98. filterCharacters(),
  99. // `autoComplete` needs to be before `keybindings` so that arrow key handling
  100. // in the autocomplete pop-up takes precedence over Vim/Emacs key bindings
  101. autoComplete(options.settings),
  102. // `keybindings` needs to be before `language` so that Vim/Emacs bindings take
  103. // precedence over language-specific keyboard shortcuts
  104. keybindings(),
  105. annotations(), // NOTE: must be before `language`
  106. language(options.currentDoc, options.metadata, options.settings),
  107. theme(options.theme),
  108. realtime(options.currentDoc, options.handleError),
  109. cursorPosition(options.currentDoc),
  110. scrollPosition(options.currentDoc),
  111. cursorHighlights(),
  112. autoPair(options.settings),
  113. editable(),
  114. search(),
  115. phrases(options.phrases),
  116. parserWatcher(),
  117. spelling(options.spelling),
  118. shortcuts(),
  119. symbolPalette(),
  120. emptyLineFiller(), // NOTE: must be before `trackChanges`
  121. trackChanges(options.currentDoc, options.changeManager),
  122. visual(options.currentDoc, options.visual),
  123. verticalOverflow(),
  124. highlightActiveLine(options.visual.visual),
  125. highlightActiveLineGutter(),
  126. scrollOneLine(),
  127. fontLoad(),
  128. inlineBackground(options.visual.visual),
  129. exceptionLogger(),
  130. moduleExtensions.map(extension => extension()),
  131. thirdPartyExtensions(),
  132. ]