File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,7 +20,9 @@ interface Props {
2020
2121export 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}
You can’t perform that action at this time.
0 commit comments