document-diff-viewer.tsx 3.6 KB

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