1
- import React , { useEffect , useImperativeHandle , useRef } from 'react' ;
2
- import { MergeView } from '@codemirror/merge' ;
1
+ import React , { useEffect , useImperativeHandle , useMemo , useRef , memo } from 'react' ;
2
+ import { MergeView , MergeConfig } from '@codemirror/merge' ;
3
3
import { useStore } from './store' ;
4
+ import { CodeMirrorMergeProps } from './' ;
4
5
5
6
export interface InternalRef {
6
7
container ?: HTMLDivElement | null ;
7
8
view ?: MergeView ;
8
9
}
9
10
10
- export interface InternalProps extends React . LiHTMLAttributes < HTMLDivElement > { }
11
-
12
- export const Internal = React . forwardRef ( ( props : InternalProps , ref ?: React . ForwardedRef < InternalRef > ) => {
13
- const { className, children } = props ;
14
- const { modified, original, view, dispatch } = useStore ( ) ;
11
+ export const Internal = React . forwardRef ( ( props : CodeMirrorMergeProps , ref ?: React . ForwardedRef < InternalRef > ) => {
12
+ const {
13
+ className,
14
+ children,
15
+ orientation,
16
+ revertControls,
17
+ highlightChanges,
18
+ gutter,
19
+ collapseUnchanged,
20
+ renderRevertControl,
21
+ ...elmProps
22
+ } = props ;
23
+ const { modified, original, view, dispatch, ...otherStore } = useStore ( ) ;
15
24
const editor = useRef < HTMLDivElement > ( null ) ;
16
25
useImperativeHandle ( ref , ( ) => ( { container : editor . current , view } ) , [ editor , view ] ) ;
17
26
useEffect ( ( ) => {
18
27
if ( ! view && editor . current && original && modified ) {
28
+ const opts = { orientation, revertControls, highlightChanges, gutter, collapseUnchanged, renderRevertControl } ;
19
29
const viewDefault = new MergeView ( {
20
30
a : original ,
21
31
b : modified ,
22
32
parent : editor . current ,
33
+ ...opts ,
23
34
} ) ;
24
- dispatch && dispatch ( { view : viewDefault } ) ;
35
+ dispatch && dispatch ( { view : viewDefault , ... opts } ) ;
25
36
}
26
37
} , [ editor . current , original , modified , view ] ) ;
27
38
@@ -31,9 +42,51 @@ export const Internal = React.forwardRef((props: InternalProps, ref?: React.Forw
31
42
} ;
32
43
} , [ ] ) ;
33
44
45
+ useEffect ( ( ) => {
46
+ if ( view ) {
47
+ const opts : MergeConfig = { } ;
48
+ if ( otherStore . orientation !== orientation ) {
49
+ opts . orientation = orientation ;
50
+ }
51
+ if ( otherStore . revertControls !== revertControls ) {
52
+ opts . revertControls = revertControls ;
53
+ }
54
+ if ( otherStore . highlightChanges !== highlightChanges ) {
55
+ opts . highlightChanges = highlightChanges ;
56
+ }
57
+ if ( otherStore . gutter !== gutter ) {
58
+ opts . gutter = gutter ;
59
+ }
60
+ if ( otherStore . collapseUnchanged !== collapseUnchanged ) {
61
+ opts . collapseUnchanged = collapseUnchanged ;
62
+ }
63
+ if ( Object . keys ( opts ) . length && dispatch && original && modified && editor . current ) {
64
+ view . destroy ( ) ;
65
+ const viewDefault = new MergeView ( {
66
+ a : original ,
67
+ b : modified ,
68
+ parent : editor . current ,
69
+ ...opts ,
70
+ } ) ;
71
+ dispatch ( { ...opts , renderRevertControl, view : viewDefault } ) ;
72
+ }
73
+ }
74
+ } , [
75
+ view ,
76
+ original ,
77
+ modified ,
78
+ editor ,
79
+ orientation ,
80
+ revertControls ,
81
+ highlightChanges ,
82
+ gutter ,
83
+ collapseUnchanged ,
84
+ renderRevertControl ,
85
+ ] ) ;
86
+
34
87
const defaultClassNames = 'cm-merge-theme' ;
35
88
return (
36
- < div ref = { editor } className = { `${ defaultClassNames } ${ className ? ` ${ className } ` : '' } ` } { ...props } >
89
+ < div ref = { editor } className = { `${ defaultClassNames } ${ className ? ` ${ className } ` : '' } ` } { ...elmProps } >
37
90
{ children }
38
91
</ div >
39
92
) ;
1 commit comments
jaywcjlove commentedon Apr 8, 2023