Skip to content

Commit 2a764ff

Browse files
committed
fix: fix ref issue. (#189)
1 parent f4355cc commit 2a764ff

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

src/commands/fullscreen.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const Fullscreen: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & T
1818
if (!$height.current) {
1919
$height.current = entry.target.clientHeight;
2020
}
21-
if (editorProps.editor && editorProps.editor.view?.dom) {
21+
if (editorProps.editor?.current?.view?.dom) {
2222
if (full) {
23-
editorProps.editor.view.dom.style.height = `${entry.target.clientHeight}px`;
23+
editorProps.editor.current.view.dom.style.height = `${entry.target.clientHeight}px`;
2424
} else {
25-
editorProps.editor.view.dom.removeAttribute('style');
25+
editorProps.editor.current.view.dom.removeAttribute('style');
2626
}
2727
}
2828
}

src/components/ToolBar/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function ToolBar(props: IToolBarProps) {
2929
if (!toolbars || toolbars.length === 0) return null;
3030
function handleClick(execute: ICommand['execute']) {
3131
if (execute && editor && editor) {
32-
execute(editor);
32+
execute(editor.current!);
3333
}
3434
}
3535
return (

src/index.tsx

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ export interface IMarkdownEditor extends ReactCodeMirrorProps {
3737
}
3838

3939
export interface ToolBarProps {
40-
editor?: ReactCodeMirrorRef;
40+
editor?: React.RefObject<ReactCodeMirrorRef>;
4141
preview: React.RefObject<MarkdownPreviewRef>;
4242
container: React.RefObject<HTMLDivElement>;
4343
containerEditor: React.RefObject<HTMLDivElement>;
4444
editorProps: IMarkdownEditor;
4545
}
4646

4747
export interface MarkdownEditorRef {
48-
editor: React.RefObject<ReactCodeMirrorRef> | null;
48+
editor: React.RefObject<ReactCodeMirrorRef>;
4949
preview?: React.RefObject<MarkdownPreviewRef> | null;
5050
}
5151

@@ -73,24 +73,25 @@ function MarkdownEditor(
7373
const previewContainer = useRef<MarkdownPreviewRef>(null);
7474
const container = useRef<HTMLDivElement>(null);
7575
const containerEditor = useRef<HTMLDivElement>(null);
76-
const [editor, setEditor] = useState<ReactCodeMirrorRef>();
7776

78-
useImperativeHandle(
79-
ref,
80-
() => ({
81-
editor: codeMirror,
82-
preview: previewContainer,
83-
}),
84-
[codeMirror, previewContainer],
85-
);
77+
useImperativeHandle(ref, () => ({
78+
editor: codeMirror,
79+
preview: previewContainer,
80+
}));
8681

8782
const toolBarProps: ToolBarProps = {
88-
editor: editor,
83+
editor: codeMirror,
8984
preview: previewContainer,
9085
container: container,
9186
containerEditor: containerEditor,
9287
editorProps: props,
9388
};
89+
const height = typeof codemirrorProps.height === 'number' ? `${codemirrorProps.height}px` : codemirrorProps.height;
90+
const extensionsData: IMarkdownEditor['extensions'] = [
91+
markdown({ base: markdownLanguage, codeLanguages: languages }),
92+
scrollerStyle,
93+
...extensions,
94+
];
9495
return (
9596
<div className={`${prefixCls || ''} wmde-markdown-var ${className || ''}`} ref={container}>
9697
{hideToolbar && (
@@ -105,19 +106,9 @@ function MarkdownEditor(
105106
<CodeMirror
106107
theme={defaultTheme}
107108
{...codemirrorProps}
108-
extensions={[
109-
markdown({ base: markdownLanguage, codeLanguages: languages }),
110-
scrollerStyle,
111-
...extensions,
112-
]}
113-
height={
114-
typeof codemirrorProps.height === 'number' ? `${codemirrorProps.height}px` : codemirrorProps.height
115-
}
116-
ref={($ref) => {
117-
if (!editor && $ref && $ref.editor && $ref.state && $ref.view) {
118-
setEditor({ ...$ref });
119-
}
120-
}}
109+
extensions={extensionsData}
110+
height={height}
111+
ref={codeMirror}
121112
onChange={(value, viewUpdate) => {
122113
setValue(value);
123114
onChange && onChange(value, viewUpdate);

0 commit comments

Comments
 (0)