Skip to content

Commit 9f90348

Browse files
committedApr 7, 2023
fix(react-codemirror-merge): fix ref issue. (#455)
1 parent f105b58 commit 9f90348

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed
 

‎merge/src/Internal.tsx

+35-30
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { useEffect, useImperativeHandle, useRef } from 'react';
22
import { MergeView } from '@codemirror/merge';
33
import { useStore } from './store';
44

5-
export interface ReactCodeMirrorMergeInternalProps extends React.LiHTMLAttributes<HTMLDivElement> {}
5+
export interface InternalRef {
6+
container?: HTMLDivElement | null;
7+
view?: MergeView;
8+
}
69

7-
export const Internal = React.forwardRef(
8-
(props: ReactCodeMirrorMergeInternalProps, ref?: React.ForwardedRef<HTMLDivElement>) => {
9-
const { className, children } = props;
10-
const { modified, original, view, dispatch } = useStore();
11-
const editor = useRef<HTMLDivElement>(null);
10+
export interface InternalProps extends React.LiHTMLAttributes<HTMLDivElement> {}
1211

13-
useEffect(() => {
14-
if (!view && editor.current && original && modified) {
15-
const viewDefault = new MergeView({
16-
a: original,
17-
b: modified,
18-
parent: editor.current,
19-
});
20-
dispatch && dispatch({ view: viewDefault });
21-
}
22-
}, [editor.current, original, modified, view]);
12+
export const Internal = React.forwardRef((props: InternalProps, ref?: React.ForwardedRef<InternalRef>) => {
13+
const { className, children } = props;
14+
const { modified, original, view, dispatch } = useStore();
15+
const editor = useRef<HTMLDivElement>(null);
16+
useImperativeHandle(ref, () => ({ container: editor.current, view }), [editor, view]);
17+
useEffect(() => {
18+
if (!view && editor.current && original && modified) {
19+
const viewDefault = new MergeView({
20+
a: original,
21+
b: modified,
22+
parent: editor.current,
23+
});
24+
dispatch && dispatch({ view: viewDefault });
25+
}
26+
}, [editor.current, original, modified, view]);
2327

24-
useEffect(() => {
25-
return () => {
26-
view && view.destroy();
27-
};
28-
}, []);
28+
useEffect(() => {
29+
return () => {
30+
view && view.destroy();
31+
};
32+
}, []);
2933

30-
const defaultClassNames = 'cm-merge-theme';
31-
return (
32-
<div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...props}>
33-
{children}
34-
</div>
35-
);
36-
},
37-
);
34+
const defaultClassNames = 'cm-merge-theme';
35+
return (
36+
<div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...props}>
37+
{children}
38+
</div>
39+
);
40+
});
41+
42+
Internal.displayName = 'CodeMirrorMerge.Internal';

‎merge/src/Modified.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ export const Modified = (props: ModifiedProps): JSX.Element | null => {
3838

3939
return null;
4040
};
41+
42+
Modified.displayName = 'CodeMirrorMerge.Modified';

‎merge/src/Original.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ export const Original = (props: OriginalProps): JSX.Element | null => {
3838

3939
return null;
4040
};
41+
42+
Original.displayName = 'CodeMirrorMerge.Original';

‎merge/src/index.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from 'react';
22
import { Original } from './Original';
33
import { Modified } from './Modified';
4-
import { Internal } from './Internal';
4+
import { Internal, InternalRef } from './Internal';
55
import { Provider } from './store';
66

7+
export interface ReactCodeMirrorMergeRef extends InternalRef {}
78
export interface ReactCodeMirrorMergeProps extends React.LiHTMLAttributes<HTMLDivElement> {}
89

9-
const InternalCodeMirror = (props: ReactCodeMirrorMergeProps, ref?: React.ForwardedRef<HTMLDivElement>) => {
10+
const InternalCodeMirror = (props: ReactCodeMirrorMergeProps, ref?: React.ForwardedRef<InternalRef>) => {
1011
return (
1112
<Provider>
1213
<Internal {...props} ref={ref} />
@@ -19,7 +20,7 @@ type CodeMirrorComponent = React.FC<React.PropsWithRef<ReactCodeMirrorMergeProps
1920
Modified: typeof Modified;
2021
};
2122

22-
const ReactCodeMirrorMerge: CodeMirrorComponent = React.forwardRef<HTMLDivElement>(
23+
const ReactCodeMirrorMerge: CodeMirrorComponent = React.forwardRef<InternalRef>(
2324
InternalCodeMirror,
2425
) as unknown as CodeMirrorComponent;
2526

0 commit comments

Comments
 (0)
Please sign in to comment.