index.ts 7.4 KB

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