Skip to content

Commit

Permalink
fix(react-codemirror-merge): fix ref issue. (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 7, 2023
1 parent f105b58 commit 9f90348
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
65 changes: 35 additions & 30 deletions merge/src/Internal.tsx
@@ -1,37 +1,42 @@
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useImperativeHandle, useRef } from 'react';
import { MergeView } from '@codemirror/merge';
import { useStore } from './store';

export interface ReactCodeMirrorMergeInternalProps extends React.LiHTMLAttributes<HTMLDivElement> {}
export interface InternalRef {
container?: HTMLDivElement | null;
view?: MergeView;
}

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

useEffect(() => {
if (!view && editor.current && original && modified) {
const viewDefault = new MergeView({
a: original,
b: modified,
parent: editor.current,
});
dispatch && dispatch({ view: viewDefault });
}
}, [editor.current, original, modified, view]);
export const Internal = React.forwardRef((props: InternalProps, ref?: React.ForwardedRef<InternalRef>) => {
const { className, children } = props;
const { modified, original, view, dispatch } = useStore();
const editor = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => ({ container: editor.current, view }), [editor, view]);
useEffect(() => {
if (!view && editor.current && original && modified) {
const viewDefault = new MergeView({
a: original,
b: modified,
parent: editor.current,
});
dispatch && dispatch({ view: viewDefault });
}
}, [editor.current, original, modified, view]);

useEffect(() => {
return () => {
view && view.destroy();
};
}, []);
useEffect(() => {
return () => {
view && view.destroy();
};
}, []);

const defaultClassNames = 'cm-merge-theme';
return (
<div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...props}>
{children}
</div>
);
},
);
const defaultClassNames = 'cm-merge-theme';
return (
<div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...props}>
{children}
</div>
);
});

Internal.displayName = 'CodeMirrorMerge.Internal';
2 changes: 2 additions & 0 deletions merge/src/Modified.tsx
Expand Up @@ -38,3 +38,5 @@ export const Modified = (props: ModifiedProps): JSX.Element | null => {

return null;
};

Modified.displayName = 'CodeMirrorMerge.Modified';
2 changes: 2 additions & 0 deletions merge/src/Original.tsx
Expand Up @@ -38,3 +38,5 @@ export const Original = (props: OriginalProps): JSX.Element | null => {

return null;
};

Original.displayName = 'CodeMirrorMerge.Original';
7 changes: 4 additions & 3 deletions merge/src/index.tsx
@@ -1,12 +1,13 @@
import React from 'react';
import { Original } from './Original';
import { Modified } from './Modified';
import { Internal } from './Internal';
import { Internal, InternalRef } from './Internal';
import { Provider } from './store';

export interface ReactCodeMirrorMergeRef extends InternalRef {}
export interface ReactCodeMirrorMergeProps extends React.LiHTMLAttributes<HTMLDivElement> {}

const InternalCodeMirror = (props: ReactCodeMirrorMergeProps, ref?: React.ForwardedRef<HTMLDivElement>) => {
const InternalCodeMirror = (props: ReactCodeMirrorMergeProps, ref?: React.ForwardedRef<InternalRef>) => {
return (
<Provider>
<Internal {...props} ref={ref} />
Expand All @@ -19,7 +20,7 @@ type CodeMirrorComponent = React.FC<React.PropsWithRef<ReactCodeMirrorMergeProps
Modified: typeof Modified;
};

const ReactCodeMirrorMerge: CodeMirrorComponent = React.forwardRef<HTMLDivElement>(
const ReactCodeMirrorMerge: CodeMirrorComponent = React.forwardRef<InternalRef>(
InternalCodeMirror,
) as unknown as CodeMirrorComponent;

Expand Down

0 comments on commit 9f90348

Please sign in to comment.