document-diff-viewer.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { useCallback, useEffect, useState } from 'react'
  2. import {
  3. EditorSelection,
  4. EditorState,
  5. Extension,
  6. StateEffect,
  7. } from '@codemirror/state'
  8. import { EditorView, lineNumbers } from '@codemirror/view'
  9. import { indentationMarkers } from '@replit/codemirror-indentation-markers'
  10. import { highlights, setHighlightsEffect } from '../../extensions/highlights'
  11. import { useUserSettingsContext } from '@/shared/context/user-settings-context'
  12. import { theme, Options, setOptionsTheme } from '../../extensions/theme'
  13. import { indentUnit } from '@codemirror/language'
  14. import { Highlight } from '../../services/types/doc'
  15. import useIsMounted from '../../../../shared/hooks/use-is-mounted'
  16. import {
  17. highlightLocations,
  18. highlightLocationsField,
  19. scrollToHighlight,
  20. } from '../../extensions/highlight-locations'
  21. import { useTranslation } from 'react-i18next'
  22. import { inlineBackground } from '../../../source-editor/extensions/inline-background'
  23. import OLButton from '@/features/ui/components/ol/ol-button'
  24. function extensions(themeOptions: Options): Extension[] {
  25. return [
  26. EditorView.editable.of(false),
  27. EditorState.readOnly.of(true),
  28. EditorView.contentAttributes.of({ tabindex: '0' }),
  29. lineNumbers(),
  30. EditorView.lineWrapping,
  31. indentUnit.of(' '), // TODO: Vary this by file type
  32. indentationMarkers({ hideFirstIndent: true, highlightActiveBlock: false }),
  33. highlights(),
  34. highlightLocations(),
  35. theme(themeOptions),
  36. inlineBackground(false),
  37. ]
  38. }
  39. function DocumentDiffViewer({
  40. doc,
  41. highlights,
  42. }: {
  43. doc: string
  44. highlights: Highlight[]
  45. }) {
  46. const { userSettings } = useUserSettingsContext()
  47. const { fontFamily, fontSize, lineHeight } = userSettings
  48. const isMounted = useIsMounted()
  49. const { t } = useTranslation()
  50. const [state, setState] = useState(() => {
  51. return EditorState.create({
  52. doc,
  53. extensions: extensions({
  54. fontSize,
  55. fontFamily,
  56. lineHeight,
  57. }),
  58. })
  59. })
  60. const [view] = useState<EditorView>(() => {
  61. return new EditorView({
  62. state,
  63. dispatch: tr => {
  64. view.update([tr])
  65. if (isMounted.current) {
  66. setState(view.state)
  67. }
  68. },
  69. })
  70. })
  71. const highlightLocations = state.field(highlightLocationsField)
  72. // Append the editor view DOM to the container node when mounted
  73. const containerRef = useCallback(
  74. (node: HTMLDivElement) => {
  75. if (node) {
  76. node.appendChild(view.dom)
  77. }
  78. },
  79. [view]
  80. )
  81. const scrollToPrevious = useCallback(() => {
  82. if (highlightLocations.previous) {
  83. scrollToHighlight(view, highlightLocations.previous)
  84. }
  85. }, [highlightLocations.previous, view])
  86. const scrollToNext = useCallback(() => {
  87. if (highlightLocations.next) {
  88. scrollToHighlight(view, highlightLocations.next)
  89. }
  90. }, [highlightLocations.next, view])
  91. const { before, after } = highlightLocations
  92. useEffect(() => {
  93. const effects: StateEffect<unknown>[] = [setHighlightsEffect.of(highlights)]
  94. if (highlights.length > 0) {
  95. const { from, to } = highlights[0].range
  96. effects.push(
  97. EditorView.scrollIntoView(EditorSelection.range(from, to), {
  98. y: 'center',
  99. })
  100. )
  101. }
  102. view.dispatch({
  103. changes: { from: 0, to: view.state.doc.length, insert: doc },
  104. effects,
  105. })
  106. }, [doc, highlights, view])
  107. // Update the document diff viewer theme whenever the font size, font family
  108. // or line height user setting changes
  109. useEffect(() => {
  110. view.dispatch(
  111. setOptionsTheme({
  112. fontSize,
  113. fontFamily,
  114. lineHeight,
  115. })
  116. )
  117. }, [view, fontSize, fontFamily, lineHeight])
  118. return (
  119. <div className="document-diff-container">
  120. <div ref={containerRef} className="cm-viewer-container" />
  121. {before > 0 ? (
  122. <OLButton
  123. variant="secondary"
  124. leadingIcon="arrow_upward"
  125. onClick={scrollToPrevious}
  126. className="previous-highlight-button"
  127. >
  128. {t('n_more_updates_above', { count: before })}
  129. </OLButton>
  130. ) : null}
  131. {after > 0 ? (
  132. <OLButton
  133. variant="secondary"
  134. leadingIcon="arrow_downward"
  135. onClick={scrollToNext}
  136. className="next-highlight-button"
  137. >
  138. {t('n_more_updates_below', { count: after })}
  139. </OLButton>
  140. ) : null}
  141. </div>
  142. )
  143. }
  144. export default DocumentDiffViewer