1
- import React , { useEffect , useImperativeHandle , useMemo , useRef , memo } from 'react' ;
2
- import { EditorStateConfig , Extension , StateEffect , Annotation } from '@codemirror/state' ;
3
- import { MergeView , MergeConfig } from '@codemirror/merge' ;
1
+ import React , { useEffect , useImperativeHandle , useRef } from 'react' ;
2
+ import { EditorStateConfig } from '@codemirror/state' ;
3
+ import { getDefaultExtensions } from '@uiw/react-codemirror' ;
4
+ import { MergeView , MergeConfig , DirectMergeConfig } from '@codemirror/merge' ;
4
5
import { useStore } from './store' ;
5
6
import { CodeMirrorMergeProps } from './' ;
6
7
7
8
export interface InternalRef {
8
9
container ?: HTMLDivElement | null ;
9
10
view ?: MergeView ;
11
+ original ?: EditorStateConfig ;
12
+ modified ?: EditorStateConfig ;
13
+ config ?: DirectMergeConfig ;
10
14
}
11
15
12
- export const Internal = React . forwardRef ( ( props : CodeMirrorMergeProps , ref ?: React . ForwardedRef < InternalRef > ) => {
16
+ export const Internal = React . forwardRef < InternalRef , CodeMirrorMergeProps > ( ( props , ref ) => {
13
17
const {
14
18
className,
15
19
children,
@@ -21,19 +25,48 @@ export const Internal = React.forwardRef((props: CodeMirrorMergeProps, ref?: Rea
21
25
renderRevertControl,
22
26
...elmProps
23
27
} = props ;
24
- const { modified, original, view, dispatch, ...otherStore } = useStore ( ) ;
28
+ const { modified, modifiedExtension , original, originalExtension , theme , view, dispatch, ...otherStore } = useStore ( ) ;
25
29
const editor = useRef < HTMLDivElement > ( null ) ;
26
- useImperativeHandle ( ref , ( ) => ( { container : editor . current , view } ) , [ editor , view ] ) ;
30
+ const opts = { orientation, revertControls, highlightChanges, gutter, collapseUnchanged, renderRevertControl } ;
31
+
32
+ useImperativeHandle (
33
+ ref ,
34
+ ( ) => ( {
35
+ container : editor . current ,
36
+ view,
37
+ modified,
38
+ original,
39
+ config : {
40
+ a : original ! ,
41
+ b : modified ! ,
42
+ parent : editor . current ! ,
43
+ ...opts ,
44
+ } ,
45
+ } ) ,
46
+ [ editor , view , modified , original , opts ] ,
47
+ ) ;
48
+
49
+ useEffect ( ( ) => {
50
+ if ( view && original && modified && theme && editor . current && dispatch ) {
51
+ editor . current . innerHTML = '' ;
52
+ new MergeView ( {
53
+ a : { ...original , extensions : [ ...( originalExtension || [ ] ) , ...getDefaultExtensions ( { theme : theme } ) ] } ,
54
+ b : { ...modified , extensions : [ ...( modifiedExtension || [ ] ) , ...getDefaultExtensions ( { theme : theme } ) ] } ,
55
+ parent : editor . current ,
56
+ ...opts ,
57
+ } ) ;
58
+ }
59
+ } , [ theme , editor . current , original , modified , originalExtension , modifiedExtension ] ) ;
60
+
27
61
useEffect ( ( ) => {
28
62
if ( ! view && editor . current && original ?. extensions && modified ?. extensions ) {
29
- const opts = { orientation, revertControls, highlightChanges, gutter, collapseUnchanged, renderRevertControl } ;
30
63
const viewDefault = new MergeView ( {
31
64
a : original ,
32
65
b : modified ,
33
66
parent : editor . current ,
34
67
...opts ,
35
68
} ) ;
36
- dispatch && dispatch ( { view : viewDefault , ...opts } ) ;
69
+ dispatch && dispatch ( { view : viewDefault , container : editor . current , ...opts } ) ;
37
70
}
38
71
} , [ editor . current , original , modified , view ] ) ;
39
72
0 commit comments