11import { AutomergeUrl } from "@automerge/automerge-repo"
2- import { useHandle } from "@automerge/automerge-repo-react-hooks"
3- import { useEffect , useRef , useState } from "react"
2+ import { useDocHandle } from "@automerge/automerge-repo-react-hooks"
3+ import { useEffect , useRef } from "react"
44import { EditorState , Transaction } from "prosemirror-state"
55import { EditorView } from "prosemirror-view"
66import { exampleSetup } from "prosemirror-example-setup"
7- import {
8- syncPlugin ,
9- pmDocFromSpans ,
10- basicSchemaAdapter ,
11- } from "@automerge/prosemirror"
12- import { next as am } from "@automerge/automerge"
7+ import { init , basicSchemaAdapter } from "@automerge/prosemirror"
138import "prosemirror-example-setup/style/style.css"
149import "prosemirror-menu/style/menu.css"
1510import "prosemirror-view/style/prosemirror.css"
1611import "./App.css"
1712
1813function App ( { docUrl } : { docUrl : AutomergeUrl } ) {
1914 const editorRoot = useRef < HTMLDivElement > ( null )
20- const handle = useHandle < { text : string } > ( docUrl )
21- const [ loaded , setLoaded ] = useState ( handle && handle . docSync ( ) != null )
22- useEffect ( ( ) => {
23- if ( handle != null ) {
24- handle . whenReady ( ) . then ( ( ) => {
25- if ( handle . docSync ( ) != null ) {
26- setLoaded ( true )
27- }
28- } )
29- }
30- } , [ handle ] )
15+ const handle = useDocHandle < { text : string } > ( docUrl )
3116
3217 useEffect ( ( ) => {
33- const adapter = basicSchemaAdapter
3418 let view : EditorView
35- if ( editorRoot . current != null && loaded ) {
19+
20+ if ( editorRoot . current != null && handle != null ) {
21+ const { pmDoc, schema, plugin } = init ( handle , [ "text" ] , {
22+ schemaAdapter : basicSchemaAdapter ,
23+ } )
3624 view = new EditorView ( editorRoot . current , {
3725 state : EditorState . create ( {
38- schema : adapter . schema , // It's important that we use the schema from the mirror
39- plugins : [
40- ...exampleSetup ( { schema : adapter . schema } ) ,
41- syncPlugin ( { adapter, handle, path : [ "text" ] } ) ,
42- ] ,
26+ schema, // It's important that we use the schema from the mirror
27+ plugins : [ ...exampleSetup ( { schema } ) , plugin ] ,
4328 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
44- doc : pmDocFromSpans ( adapter , am . spans ( handle . docSync ( ) ! , [ "text" ] ) ) ,
29+ doc : pmDoc ,
4530 } ) ,
4631 dispatchTransaction : ( tx : Transaction ) => {
4732 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -54,7 +39,7 @@ function App({ docUrl }: { docUrl: AutomergeUrl }) {
5439 view . destroy ( )
5540 }
5641 }
57- } , [ editorRoot , loaded , handle ] )
42+ } , [ editorRoot , handle ] )
5843
5944 return < div id = "editor" ref = { editorRoot } > </ div >
6045}
0 commit comments