Skip to content

Commit 4862bf1

Browse files
zjkmxyautofix-ci[bot]Saul-Mirone
authored
feat: enable multiple files in the same yjs doc (#1551)
* feat: enable multiple files in the same yjs doc * feat: add storybook page for plugin-collab * [autofix.ci] apply automated fixes * chore: resolve lock file conflict * chore: remove unneeded conversion * chore: f --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Mirone <[email protected]>
1 parent 2508ca3 commit 4862bf1

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

Diff for: packages/integrations/vue/tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"extends": "../../../tsconfig.base.json",
33
"compilerOptions": {
4-
"jsx": "react",
4+
"jsx": "preserve",
55
"jsxFactory": "h",
66
"jsxFragmentFactory": "Fragment",
77
"rootDir": "src",
88
"types": ["vue"],
9-
"outDir": "lib"
9+
"outDir": "lib",
10+
"jsxImportSource": "vue"
1011
},
1112
"include": ["src"]
1213
}

Diff for: packages/plugins/plugin-collab/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"@milkdown/core": "workspace:*",
6060
"@milkdown/ctx": "workspace:*",
6161
"@milkdown/prose": "workspace:*",
62-
"y-prosemirror": "^1.2.1",
62+
"y-prosemirror": "^1.2.12",
6363
"y-protocols": "^1.0.5",
6464
"yjs": "^13.5.38"
6565
},

Diff for: packages/plugins/plugin-collab/src/collab-service.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -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'
2727
import type { Awareness } from 'y-protocols/awareness'
28-
import type { Doc, PermanentUserData } from 'yjs'
28+
import type { Doc, PermanentUserData, XmlFragment } from 'yjs'
2929
import { 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

Diff for: packages/plugins/plugin-collab/src/shim.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ declare module 'y-prosemirror' {
5151
export const yCursorPluginKey: PluginKey<any>
5252
export const ySyncPluginKey: PluginKey<any>
5353
export const yUndoPluginKey: PluginKey<any>
54-
export function yDocToProsemirror(schema: Schema, ydoc: Y.Doc): Node
54+
export function yXmlFragmentToProseMirrorRootNode(
55+
yXmlFragment: Y.XmlFragment,
56+
schema: Schema
57+
): Node
5558
export function ySyncPlugin(
5659
yXmlFragment: Y.XmlFragment,
5760
{ colors, colorMapping, permanentUserData, onFirstRender }?: YSyncOpts

Diff for: pnpm-lock.yaml

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)