index.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. EditorView,
  3. rectangularSelection,
  4. tooltips,
  5. crosshairCursor,
  6. dropCursor,
  7. highlightActiveLineGutter,
  8. } from '@codemirror/view'
  9. import { EditorState, Extension } from '@codemirror/state'
  10. import { foldGutter, indentOnInput, indentUnit } from '@codemirror/language'
  11. import { history } from '@codemirror/commands'
  12. import { language } from './language'
  13. import { lineWrappingIndentation } from './line-wrapping-indentation'
  14. import { theme } from './theme'
  15. import { realtime } from './realtime'
  16. import { cursorPosition } from './cursor-position'
  17. import { scrollPosition } from './scroll-position'
  18. import { annotations } from './annotations'
  19. import { cursorHighlights } from './cursor-highlights'
  20. import { autoComplete } from './auto-complete'
  21. import { editable } from './editable'
  22. import { autoPair } from './auto-pair'
  23. import { phrases } from './phrases'
  24. import { spelling } from './spelling'
  25. import { symbolPalette } from './symbol-palette'
  26. import { trackChanges } from './track-changes'
  27. import { search } from './search'
  28. import { filterCharacters } from './filter-characters'
  29. import { keybindings } from './keybindings'
  30. import { bracketMatching, bracketSelection } from './bracket-matching'
  31. import { verticalOverflow } from './vertical-overflow'
  32. import { thirdPartyExtensions } from './third-party-extensions'
  33. import { lineNumbers } from './line-numbers'
  34. import { highlightActiveLine } from './highlight-active-line'
  35. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  36. import { emptyLineFiller } from './empty-line-filler'
  37. import { goToLinePanel } from './go-to-line'
  38. import { drawSelection } from './draw-selection'
  39. import { visual } from './visual/visual'
  40. import { inlineBackground } from './inline-background'
  41. import { indentationMarkers } from './indentation-markers'
  42. import { codemirrorDevTools } from '../languages/latex/codemirror-dev-tools'
  43. import { keymaps } from './keymaps'
  44. import { shortcuts } from './shortcuts'
  45. import { effectListeners } from './effect-listeners'
  46. import { highlightSpecialChars } from './highlight-special-chars'
  47. import { toolbarPanel } from './toolbar/toolbar-panel'
  48. import { geometryChangeEvent } from './geometry-change-event'
  49. import { docName } from './doc-name'
  50. import { fileTreeItemDrop } from './file-tree-item-drop'
  51. import { mathPreview } from './math-preview'
  52. import { isSplitTestEnabled } from '@/utils/splitTestUtils'
  53. import { ranges } from './ranges'
  54. import { trackDetachedComments } from './track-detached-comments'
  55. import { addComment } from './add-comment'
  56. const moduleExtensions: Array<() => Extension> = importOverleafModules(
  57. 'sourceEditorExtensions'
  58. ).map((item: { import: { extension: Extension } }) => item.import.extension)
  59. export const createExtensions = (options: Record<string, any>): Extension[] => [
  60. lineNumbers(),
  61. highlightSpecialChars(options.visual.visual),
  62. // The built-in extension that manages the history stack,
  63. // configured to increase the maximum delay between adjacent grouped edits
  64. history({ newGroupDelay: 250 }),
  65. // The built-in extension that displays buttons for folding code in a gutter element,
  66. // configured with custom openText and closeText symbols.
  67. foldGutter({
  68. openText: '▾',
  69. closedText: '▸',
  70. }),
  71. drawSelection(),
  72. // A built-in facet that is set to true to allow multiple selections.
  73. // This makes the editor more like a code editor than Google Docs or Microsoft Word,
  74. // which only have single selections.
  75. EditorState.allowMultipleSelections.of(true),
  76. // A built-in extension that enables soft line wrapping.
  77. EditorView.lineWrapping,
  78. // A built-in extension that re-indents input if the language defines an indentOnInput field in its language data.
  79. indentOnInput(),
  80. lineWrappingIndentation(options.visual.visual),
  81. indentationMarkers(options.visual.visual),
  82. bracketMatching(),
  83. bracketSelection(),
  84. // A built-in extension that enables rectangular selections, created by dragging a new selection while holding down Alt.
  85. rectangularSelection(),
  86. // A built-in extension that turns the pointer into a crosshair while Alt is pressed.
  87. crosshairCursor(),
  88. // A built-in extension that shows where dragged content will be dropped.
  89. dropCursor(),
  90. // A built-in extension that is used for configuring tooltip behaviour,
  91. // configured so that the tooltip parent is the document body,
  92. // to avoid cutting off tooltips which overflow the editor.
  93. tooltips({
  94. parent: document.body,
  95. }),
  96. keymaps,
  97. goToLinePanel(),
  98. filterCharacters(),
  99. // NOTE: `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. // NOTE: `keybindings` needs to be before `language` so that Vim/Emacs bindings take
  103. // precedence over language-specific keyboard shortcuts
  104. keybindings(),
  105. docName(options.docName),
  106. // NOTE: `annotations` needs to be before `language`
  107. annotations(),
  108. language(options.docName, options.metadata, options.settings),
  109. indentUnit.of(' '), // 4 spaces
  110. theme(options.theme),
  111. realtime(options.currentDoc, options.handleError),
  112. cursorPosition(options.currentDoc),
  113. scrollPosition(options.currentDoc, options.visual),
  114. cursorHighlights(),
  115. autoPair(options.settings),
  116. editable(),
  117. search(),
  118. phrases(options.phrases),
  119. spelling(options.spelling),
  120. shortcuts,
  121. symbolPalette(),
  122. // NOTE: `emptyLineFiller` needs to be before `trackChanges`,
  123. // so the decorations are added in the correct order.
  124. emptyLineFiller(),
  125. isSplitTestEnabled('review-panel-redesign')
  126. ? ranges(options.currentDoc)
  127. : trackChanges(options.currentDoc, options.changeManager),
  128. trackDetachedComments(options.currentDoc),
  129. visual(options.visual),
  130. mathPreview(options.settings.mathPreview),
  131. addComment(),
  132. toolbarPanel(),
  133. verticalOverflow(),
  134. highlightActiveLine(options.visual.visual),
  135. // The built-in extension that highlights the active line in the gutter.
  136. highlightActiveLineGutter(),
  137. inlineBackground(options.visual.visual),
  138. codemirrorDevTools(),
  139. // Send exceptions to Sentry
  140. EditorView.exceptionSink.of(options.handleException),
  141. // CodeMirror extensions provided by modules
  142. moduleExtensions.map(extension => extension()),
  143. thirdPartyExtensions(),
  144. effectListeners(),
  145. geometryChangeEvent(),
  146. fileTreeItemDrop(),
  147. ]