document-diff-viewer.tsx 4.4 KB

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