Skip to content

Commit b37c7bb

Browse files
committed
controlled
1 parent 70d3a02 commit b37c7bb

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

src/components/Editor/Editor.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ interface Props {
2020

2121
export const Editor = (props: Props) => {
2222
const { state, dispatch } = props
23-
const editorDiv = useRef<HTMLDivElement>(null);
23+
const { md } = state.doc
24+
const editorDiv = useRef<HTMLDivElement>(null)
25+
const editorView = useRef<EditorView | null>(null)
2426

2527
useEffect(() => {
2628
const extensions: Extension[] = [
@@ -39,16 +41,25 @@ export const Editor = (props: Props) => {
3941
}),
4042
]
4143

42-
const view = new EditorView({
44+
editorView.current = new EditorView({
4345
parent: editorDiv.current!,
4446
state: EditorState.create({
45-
doc: state.doc.md,
4647
extensions,
4748
}),
4849
})
4950

50-
return () => view.destroy()
51-
}, [editorDiv]);
51+
return () => editorView.current?.destroy()
52+
}, [editorDiv])
53+
54+
useEffect(() => {
55+
if (md !== editorView.current?.state.doc.toString()) {
56+
editorView.current?.dispatch({ changes: {
57+
from: 0,
58+
to: editorView.current.state.doc.length,
59+
insert: md,
60+
}})
61+
}
62+
}, [md])
5263

5364
return <div ref={editorDiv} />
5465
}

0 commit comments

Comments
 (0)