|
|
@@ -1,33 +1,20 @@
|
|
|
-import { FC, useCallback, useRef, useState } from 'react'
|
|
|
+import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
import withErrorBoundary from '../../../../infrastructure/error-boundary'
|
|
|
import { ErrorBoundaryFallback } from '../../../../shared/components/error-boundary-fallback'
|
|
|
import { EditorState, Extension } from '@codemirror/state'
|
|
|
import { EditorView } from '@codemirror/view'
|
|
|
import { lineNumbers } from '../../../../../../modules/source-editor/frontend/js/extensions/line-numbers'
|
|
|
import { indentationMarkers } from '@replit/codemirror-indentation-markers'
|
|
|
-import { highlights, setHighlightsEffect } from '../extensions/highlights'
|
|
|
+import { highlights, setHighlightsEffect } from '../../extensions/highlights'
|
|
|
import useScopeValue from '../../../../shared/hooks/use-scope-value'
|
|
|
import {
|
|
|
FontFamily,
|
|
|
LineHeight,
|
|
|
OverallTheme,
|
|
|
} from '../../../../../../modules/source-editor/frontend/js/extensions/theme'
|
|
|
-import { theme, Options } from '../extensions/theme'
|
|
|
+import { theme, Options } from '../../extensions/theme'
|
|
|
import { indentUnit } from '@codemirror/language'
|
|
|
-
|
|
|
-interface Range {
|
|
|
- from: number
|
|
|
- to: number
|
|
|
-}
|
|
|
-
|
|
|
-type HighlightType = 'addition' | 'deletion'
|
|
|
-
|
|
|
-export interface Highlight {
|
|
|
- label: string
|
|
|
- hue: number
|
|
|
- range: Range
|
|
|
- type: HighlightType
|
|
|
-}
|
|
|
+import { Highlight } from '../../services/types/doc'
|
|
|
|
|
|
function extensions(themeOptions: Options): Extension[] {
|
|
|
return [
|
|
|
@@ -41,10 +28,13 @@ function extensions(themeOptions: Options): Extension[] {
|
|
|
]
|
|
|
}
|
|
|
|
|
|
-const DocumentDiffViewer: FC<{ doc: string; highlights: Highlight[] }> = ({
|
|
|
+function DocumentDiffViewer({
|
|
|
doc,
|
|
|
highlights,
|
|
|
-}) => {
|
|
|
+}: {
|
|
|
+ doc: string
|
|
|
+ highlights: Highlight[]
|
|
|
+}) {
|
|
|
const [fontFamily] = useScopeValue<FontFamily>('settings.fontFamily')
|
|
|
const [fontSize] = useScopeValue<number>('settings.fontSize')
|
|
|
const [lineHeight] = useScopeValue<LineHeight>('settings.lineHeight')
|
|
|
@@ -62,16 +52,13 @@ const DocumentDiffViewer: FC<{ doc: string; highlights: Highlight[] }> = ({
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- const viewRef = useRef<EditorView | null>(null)
|
|
|
- if (viewRef.current === null) {
|
|
|
- viewRef.current = new EditorView({
|
|
|
+ const view = useRef(
|
|
|
+ new EditorView({
|
|
|
state,
|
|
|
})
|
|
|
- }
|
|
|
-
|
|
|
- const view = viewRef.current
|
|
|
+ ).current
|
|
|
|
|
|
- // Append the editor view dom to the container node when mounted
|
|
|
+ // Append the editor view DOM to the container node when mounted
|
|
|
const containerRef = useCallback(
|
|
|
node => {
|
|
|
if (node) {
|
|
|
@@ -81,12 +68,14 @@ const DocumentDiffViewer: FC<{ doc: string; highlights: Highlight[] }> = ({
|
|
|
[view]
|
|
|
)
|
|
|
|
|
|
- view.dispatch({
|
|
|
- changes: { from: 0, to: view.state.doc.length, insert: doc },
|
|
|
- effects: setHighlightsEffect.of(highlights),
|
|
|
- })
|
|
|
+ useEffect(() => {
|
|
|
+ view.dispatch({
|
|
|
+ changes: { from: 0, to: view.state.doc.length, insert: doc },
|
|
|
+ effects: setHighlightsEffect.of(highlights),
|
|
|
+ })
|
|
|
+ }, [doc, highlights, view])
|
|
|
|
|
|
- return <div ref={containerRef} style={{ height: '100%' }} />
|
|
|
+ return <div ref={containerRef} className="document-diff-container" />
|
|
|
}
|
|
|
|
|
|
export default withErrorBoundary(DocumentDiffViewer, ErrorBoundaryFallback)
|