Skip to content

Commit fa854d0

Browse files
authored
[lexical-website] Documentation Update: Add missing getDocFromMap fn to docs (#8000)
1 parent 80fce01 commit fa854d0

File tree

1 file changed

+16
-1
lines changed
  • packages/lexical-website/docs/collaboration

1 file changed

+16
-1
lines changed

packages/lexical-website/docs/collaboration/react.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ $ HOST=localhost PORT=1234 YPERSISTENCE=./yjs-wss-db npx y-websocket
4040
**Get basic collaborative Lexical setup:**
4141

4242
```tsx
43+
import { useCallback } from 'react';
44+
4345
import {$getRoot, $createParagraphNode, $createTextNode} from 'lexical';
4446
import {LexicalCollaboration} from '@lexical/react/LexicalCollaborationContext';
4547
import {LexicalComposer} from '@lexical/react/LexicalComposer';
@@ -65,6 +67,19 @@ function Editor() {
6567
theme: {},
6668
};
6769

70+
const getDocFromMap = (id: string, yjsDocMap: Map<string, Y.Doc>): Y.Doc => {
71+
let doc = yjsDocMap.get(id);
72+
73+
if (doc === undefined) {
74+
doc = new Y.Doc();
75+
yjsDocMap.set(id, doc);
76+
} else {
77+
doc.load();
78+
}
79+
80+
return doc;
81+
}
82+
6883
const providerFactory = useCallback(
6984
(id: string, yjsDocMap: Map<string, Y.Doc>) => {
7085
const doc = getDocFromMap(id, yjsDocMap);
@@ -88,7 +103,7 @@ function Editor() {
88103
providerFactory={providerFactory}
89104
/>
90105
</LexicalComposer>
91-
<LexicalCollaboration>
106+
</LexicalCollaboration>
92107
);
93108
}
94109
```

0 commit comments

Comments
 (0)