@@ -18,14 +18,14 @@ import {
18
18
undo ,
19
19
yCursorPlugin ,
20
20
yCursorPluginKey ,
21
- yDocToProsemirror ,
21
+ yXmlFragmentToProseMirrorRootNode ,
22
22
ySyncPlugin ,
23
23
ySyncPluginKey ,
24
24
yUndoPlugin ,
25
25
yUndoPluginKey ,
26
26
} from 'y-prosemirror'
27
27
import type { Awareness } from 'y-protocols/awareness'
28
- import type { Doc , PermanentUserData } from 'yjs'
28
+ import type { Doc , PermanentUserData , XmlFragment } from 'yjs'
29
29
import { applyUpdate , encodeStateAsUpdate } from 'yjs'
30
30
31
31
/// @internal
@@ -86,7 +86,7 @@ export class CollabService {
86
86
/// @internal
87
87
#options: CollabServiceOptions = { }
88
88
/// @internal
89
- #doc: Doc | null = null
89
+ #xmlFragment: XmlFragment | null = null
90
90
/// @internal
91
91
#awareness: Awareness | null = null
92
92
/// @internal
@@ -107,11 +107,10 @@ export class CollabService {
107
107
108
108
/// @internal
109
109
#createPlugins( ) : Plugin [ ] {
110
- if ( ! this . #doc ) throw missingYjsDoc ( )
110
+ if ( ! this . #xmlFragment ) throw missingYjsDoc ( )
111
111
const { ySyncOpts, yUndoOpts } = this . #options
112
- const type = this . #doc. getXmlFragment ( 'prosemirror' )
113
112
const plugins = [
114
- ySyncPlugin ( type , ySyncOpts ) ,
113
+ ySyncPlugin ( this . #xmlFragment , ySyncOpts ) ,
115
114
yUndoPlugin ( yUndoOpts ) ,
116
115
new Plugin ( {
117
116
key : CollabKeymapPluginKey ,
@@ -156,7 +155,13 @@ export class CollabService {
156
155
157
156
/// Bind the document to the service.
158
157
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
160
165
return this
161
166
}
162
167
@@ -186,20 +191,23 @@ export class CollabService {
186
191
condition ?: ( yDocNode : Node , templateNode : Node ) => boolean
187
192
) {
188
193
if ( ! this . #ctx) throw ctxNotBind ( )
189
- if ( ! this . #doc ) throw missingYjsDoc ( )
194
+ if ( ! this . #xmlFragment ) throw missingYjsDoc ( )
190
195
const conditionFn =
191
196
condition || ( ( yDocNode ) => yDocNode . textContent . length === 0 )
192
197
193
198
const node = this . #valueToNode( template )
194
199
const schema = this . #ctx. get ( schemaCtx )
195
- const yDocNode = yDocToProsemirror ( schema , this . #doc)
200
+ const yDocNode = yXmlFragmentToProseMirrorRootNode (
201
+ this . #xmlFragment,
202
+ schema
203
+ )
196
204
197
205
if ( node && conditionFn ( yDocNode , node ) ) {
198
- const fragment = this . #doc . getXmlFragment ( 'prosemirror' )
206
+ const fragment = this . #xmlFragment
199
207
fragment . delete ( 0 , fragment . length )
200
208
const templateDoc = prosemirrorToYDoc ( node )
201
209
const template = encodeStateAsUpdate ( templateDoc )
202
- applyUpdate ( this . # doc, template )
210
+ if ( fragment . doc ) applyUpdate ( fragment . doc , template )
203
211
templateDoc . destroy ( )
204
212
}
205
213
0 commit comments