index.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 { 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. // A built-in extension that re-indents input if the language defines an indentOnInput field in its language data.
  80. indentOnInput(),
  81. lineWrappingIndentation(options.visual.visual),
  82. indentationMarkers(options.visual.visual),
  83. bracketMatching(),
  84. bracketSelection(),
  85. // A built-in extension that enables rectangular selections, created by dragging a new selection while holding down Alt.
  86. rectangularSelection(),
  87. // A built-in extension that turns the pointer into a crosshair while Alt is pressed.
  88. crosshairCursor(),
  89. // A built-in extension that shows where dragged content will be dropped.
  90. dropCursor(),
  91. // A built-in extension that is used for configuring tooltip behaviour,
  92. // configured so that the tooltip parent is the document body,
  93. // to avoid cutting off tooltips which overflow the editor.
  94. tooltips({
  95. parent: document.body,
  96. tooltipSpace(view) {
  97. const { top, bottom } = view.scrollDOM.getBoundingClientRect()
  98. return {
  99. top,
  100. left: 0,
  101. bottom,
  102. right: window.innerWidth,
  103. }
  104. },
  105. }),
  106. keymaps,
  107. goToLinePanel(),
  108. filterCharacters(),
  109. // NOTE: `autoComplete` needs to be before `keybindings` so that arrow key handling
  110. // in the autocomplete pop-up takes precedence over Vim/Emacs key bindings
  111. autoComplete({
  112. enabled: options.settings.autoComplete,
  113. projectFeatures: options.projectFeatures,
  114. referencesSearchMode: options.settings.referencesSearchMode,
  115. }),
  116. // NOTE: `keybindings` needs to be before `language` so that Vim/Emacs bindings take
  117. // precedence over language-specific keyboard shortcuts
  118. keybindings(),
  119. docName(options.docName),
  120. // NOTE: `annotations` needs to be before `language`
  121. annotations(),
  122. language(options.docName, options.metadata, options.settings),
  123. indentUnit.of(' '), // 4 spaces
  124. theme(options.theme),
  125. realtime(options.currentDoc, options.handleError),
  126. cursorPosition(options.currentDoc),
  127. scrollPosition(options.currentDoc, options.visual),
  128. cursorHighlights(),
  129. autoPair(options.settings),
  130. editable(),
  131. search(),
  132. phrases(options.phrases),
  133. spelling(options.spelling),
  134. shortcuts,
  135. symbolPalette(),
  136. // NOTE: `emptyLineFiller` needs to be before `trackChanges`,
  137. // so the decorations are added in the correct order.
  138. emptyLineFiller(),
  139. options.currentDoc.currentDocument.getType() === 'history-ot'
  140. ? historyOT(options.currentDoc.currentDocument)
  141. : ranges(),
  142. trackDetachedComments(options.currentDoc),
  143. visual(options.visual),
  144. mathPreview(options.settings.mathPreview),
  145. reviewTooltip(),
  146. toolbarPanel(),
  147. breadcrumbPanel(),
  148. verticalOverflow(),
  149. highlightActiveLine(options.visual.visual),
  150. // The built-in extension that highlights the active line in the gutter.
  151. highlightActiveLineGutter(),
  152. inlineBackground(options.visual.visual),
  153. codemirrorDevTools(),
  154. // Send exceptions to Sentry
  155. EditorView.exceptionSink.of(options.handleException),
  156. // CodeMirror extensions provided by modules
  157. moduleExtensions.map(extension => extension(options)),
  158. thirdPartyExtensions(),
  159. effectListeners(),
  160. geometryChangeEvent(),
  161. fileTreeItemDrop(),
  162. ]