index.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 { search } from './search'
  27. import { filterCharacters } from './filter-characters'
  28. import { keybindings } from './keybindings'
  29. import { bracketMatching, bracketSelection } from './bracket-matching'
  30. import { verticalOverflow } from './vertical-overflow'
  31. import { thirdPartyExtensions } from './third-party-extensions'
  32. import { lineNumbers } from './line-numbers'
  33. import { highlightActiveLine } from './highlight-active-line'
  34. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  35. import { emptyLineFiller } from './empty-line-filler'
  36. import { goToLinePanel } from './go-to-line'
  37. import { drawSelection } from './draw-selection'
  38. import { sourceOnly, visual } from './visual/visual'
  39. import { inlineBackground } from './inline-background'
  40. import { indentationMarkers } from './indentation-markers'
  41. import { codemirrorDevTools } from '../languages/latex/codemirror-dev-tools'
  42. import { keymaps } from './keymaps'
  43. import { shortcuts } from './shortcuts'
  44. import { effectListeners } from './effect-listeners'
  45. import { highlightSpecialChars } from './highlight-special-chars'
  46. import { toolbarPanel } from './toolbar/toolbar-panel'
  47. import { breadcrumbPanel } from './breadcrumbs-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 { ranges } from './ranges'
  53. import { historyOT } from './history-ot'
  54. import { trackDetachedComments } from './track-detached-comments'
  55. import { reviewTooltip } from './review-tooltip'
  56. const moduleExtensions: Array<(options: Record<string, any>) => Extension> =
  57. importOverleafModules('sourceEditorExtensions').map(
  58. (item: { import: { extension: Extension } }) => item.import.extension
  59. )
  60. export const createExtensions = (options: Record<string, any>): Extension[] => [
  61. lineNumbers(),
  62. highlightSpecialChars(options.visual.visual),
  63. // The built-in extension that manages the history stack,
  64. // configured to increase the maximum delay between adjacent grouped edits
  65. history({ newGroupDelay: 250 }),
  66. // The built-in extension that displays buttons for folding code in a gutter element,
  67. // configured with custom openText and closeText symbols.
  68. foldGutter({
  69. openText: '▾',
  70. closedText: '▸',
  71. }),
  72. drawSelection(),
  73. // A built-in facet that is set to true to allow multiple selections.
  74. // This makes the editor more like a code editor than Google Docs or Microsoft Word,
  75. // which only have single selections.
  76. EditorState.allowMultipleSelections.of(true),
  77. // A built-in extension that enables soft line wrapping.
  78. EditorView.lineWrapping,
  79. sourceOnly(
  80. options.visual,
  81. EditorView.contentAttributes.of({ 'aria-label': 'Source Editor editing' })
  82. ),
  83. // A built-in extension that re-indents input if the language defines an indentOnInput field in its language data.
  84. indentOnInput(),
  85. lineWrappingIndentation(options.visual.visual),
  86. indentationMarkers(options.visual.visual),
  87. bracketMatching(),
  88. bracketSelection(),
  89. // A built-in extension that enables rectangular selections, created by dragging a new selection while holding down Alt.
  90. rectangularSelection(),
  91. // A built-in extension that turns the pointer into a crosshair while Alt is pressed.
  92. crosshairCursor(),
  93. // A built-in extension that shows where dragged content will be dropped.
  94. dropCursor(),
  95. // A built-in extension that is used for configuring tooltip behaviour,
  96. // configured so that the tooltip parent is the document body,
  97. // to avoid cutting off tooltips which overflow the editor.
  98. tooltips({
  99. parent: document.body,
  100. tooltipSpace(view) {
  101. const { top, bottom } = view.scrollDOM.getBoundingClientRect()
  102. return {
  103. top,
  104. left: 0,
  105. bottom,
  106. right: window.innerWidth,
  107. }
  108. },
  109. }),
  110. keymaps,
  111. goToLinePanel(),
  112. filterCharacters(),
  113. // NOTE: `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({
  116. enabled: options.settings.autoComplete,
  117. projectFeatures: options.projectFeatures,
  118. referencesSearchMode: options.settings.referencesSearchMode,
  119. }),
  120. // NOTE: `keybindings` needs to be before `language` so that Vim/Emacs bindings take
  121. // precedence over language-specific keyboard shortcuts
  122. keybindings(),
  123. docName(options.docName),
  124. // NOTE: `annotations` needs to be before `language`
  125. annotations(),
  126. language(options.docName, options.metadata, options.settings),
  127. indentUnit.of(' '), // 4 spaces
  128. theme(options.theme),
  129. realtime(options.currentDoc, options.handleError),
  130. cursorPosition(options.currentDoc),
  131. scrollPosition(options.currentDoc, options.visual),
  132. cursorHighlights(),
  133. autoPair(options.settings),
  134. editable(),
  135. search(),
  136. phrases(options.phrases),
  137. spelling(options.spelling),
  138. shortcuts,
  139. symbolPalette(),
  140. // NOTE: `emptyLineFiller` needs to be before `trackChanges`,
  141. // so the decorations are added in the correct order.
  142. emptyLineFiller(),
  143. options.currentDoc.currentDocument.getType() === 'history-ot'
  144. ? historyOT(options.currentDoc.currentDocument)
  145. : ranges(),
  146. trackDetachedComments(options.currentDoc),
  147. visual(options.visual),
  148. mathPreview(options.settings.mathPreview),
  149. reviewTooltip(),
  150. toolbarPanel(),
  151. breadcrumbPanel(),
  152. verticalOverflow(),
  153. highlightActiveLine(options.visual.visual),
  154. // The built-in extension that highlights the active line in the gutter.
  155. highlightActiveLineGutter(),
  156. inlineBackground(options.visual.visual),
  157. codemirrorDevTools(),
  158. // Send exceptions to Sentry
  159. EditorView.exceptionSink.of(options.handleException),
  160. // CodeMirror extensions provided by modules
  161. moduleExtensions.map(extension => extension(options)),
  162. thirdPartyExtensions(),
  163. effectListeners(),
  164. geometryChangeEvent(),
  165. fileTreeItemDrop(),
  166. ]