@@ -18,14 +18,14 @@ import {
1818 undo ,
1919 yCursorPlugin ,
2020 yCursorPluginKey ,
21- yDocToProsemirror ,
21+ yXmlFragmentToProseMirrorRootNode ,
2222 ySyncPlugin ,
2323 ySyncPluginKey ,
2424 yUndoPlugin ,
2525 yUndoPluginKey ,
2626} from 'y-prosemirror'
2727import type { Awareness } from 'y-protocols/awareness'
28- import type { Doc , PermanentUserData } from 'yjs'
28+ import type { Doc , PermanentUserData , XmlFragment } from 'yjs'
2929import { applyUpdate , encodeStateAsUpdate } from 'yjs'
3030
3131/// @internal
@@ -86,7 +86,7 @@ export class CollabService {
8686 /// @internal
8787 #options: CollabServiceOptions = { }
8888 /// @internal
89- #doc: Doc | null = null
89+ #xmlFragment: XmlFragment | null = null
9090 /// @internal
9191 #awareness: Awareness | null = null
9292 /// @internal
@@ -107,11 +107,10 @@ export class CollabService {
107107
108108 /// @internal
109109 #createPlugins( ) : Plugin [ ] {
110- if ( ! this . #doc ) throw missingYjsDoc ( )
110+ if ( ! this . #xmlFragment ) throw missingYjsDoc ( )
111111 const { ySyncOpts, yUndoOpts } = this . #options
112- const type = this . #doc. getXmlFragment ( 'prosemirror' )
113112 const plugins = [
114- ySyncPlugin ( type , ySyncOpts ) ,
113+ ySyncPlugin ( this . #xmlFragment , ySyncOpts ) ,
115114 yUndoPlugin ( yUndoOpts ) ,
116115 new Plugin ( {
117116 key : CollabKeymapPluginKey ,
@@ -156,7 +155,13 @@ export class CollabService {
156155
157156 /// Bind the document to the service.
158157 bindDoc ( doc : Doc ) {
159- this . #doc = doc
158+ this . #xmlFragment = doc . getXmlFragment ( 'prosemirror' )
159+ return this
160+ }
161+
162+ /// Bind the Yjs XmlFragment to the service.
163+ bindXmlFragment ( xmlFragment : XmlFragment ) {
164+ this . #xmlFragment = xmlFragment
160165 return this
161166 }
162167
@@ -186,20 +191,23 @@ export class CollabService {
186191 condition ?: ( yDocNode : Node , templateNode : Node ) => boolean
187192 ) {
188193 if ( ! this . #ctx) throw ctxNotBind ( )
189- if ( ! this . #doc ) throw missingYjsDoc ( )
194+ if ( ! this . #xmlFragment ) throw missingYjsDoc ( )
190195 const conditionFn =
191196 condition || ( ( yDocNode ) => yDocNode . textContent . length === 0 )
192197
193198 const node = this . #valueToNode( template )
194199 const schema = this . #ctx. get ( schemaCtx )
195- const yDocNode = yDocToProsemirror ( schema , this . #doc)
200+ const yDocNode = yXmlFragmentToProseMirrorRootNode (
201+ this . #xmlFragment,
202+ schema
203+ )
196204
197205 if ( node && conditionFn ( yDocNode , node ) ) {
198- const fragment = this . #doc . getXmlFragment ( 'prosemirror' )
206+ const fragment = this . #xmlFragment
199207 fragment . delete ( 0 , fragment . length )
200208 const templateDoc = prosemirrorToYDoc ( node )
201209 const template = encodeStateAsUpdate ( templateDoc )
202- applyUpdate ( this . # doc, template )
210+ if ( fragment . doc ) applyUpdate ( fragment . doc , template )
203211 templateDoc . destroy ( )
204212 }
205213
0 commit comments