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' ;
45import { useStore } from './store' ;
56import { CodeMirrorMergeProps } from './' ;
67
78export interface InternalRef {
89 container ?: HTMLDivElement | null ;
910 view ?: MergeView ;
11+ original ?: EditorStateConfig ;
12+ modified ?: EditorStateConfig ;
13+ config ?: DirectMergeConfig ;
1014}
1115
12- export const Internal = React . forwardRef ( ( props : CodeMirrorMergeProps , ref ?: React . ForwardedRef < InternalRef > ) => {
16+ export const Internal = React . forwardRef < InternalRef , CodeMirrorMergeProps > ( ( props , ref ) => {
1317 const {
1418 className,
1519 children,
@@ -21,19 +25,48 @@ export const Internal = React.forwardRef((props: CodeMirrorMergeProps, ref?: Rea
2125 renderRevertControl,
2226 ...elmProps
2327 } = props ;
24- const { modified, original, view, dispatch, ...otherStore } = useStore ( ) ;
28+ const { modified, modifiedExtension , original, originalExtension , theme , view, dispatch, ...otherStore } = useStore ( ) ;
2529 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+
2761 useEffect ( ( ) => {
2862 if ( ! view && editor . current && original ?. extensions && modified ?. extensions ) {
29- const opts = { orientation, revertControls, highlightChanges, gutter, collapseUnchanged, renderRevertControl } ;
3063 const viewDefault = new MergeView ( {
3164 a : original ,
3265 b : modified ,
3366 parent : editor . current ,
3467 ...opts ,
3568 } ) ;
36- dispatch && dispatch ( { view : viewDefault , ...opts } ) ;
69+ dispatch && dispatch ( { view : viewDefault , container : editor . current , ...opts } ) ;
3770 }
3871 } , [ editor . current , original , modified , view ] ) ;
3972
0 commit comments