index.ts 7.0 KB

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