You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/yjs/README.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -206,6 +206,72 @@ This lets you create robust setups like:
206
206
207
207
By default, content will be shown as soon as at least one provider is synced. If `waitForAllProviders` is set to `true`, content will only appear when all configured providers are in sync.
208
208
209
+
### Using Nested Y.Doc Structures
210
+
211
+
By default, the plugin uses `ydoc.get('content', Y.XmlText)` for storing editor content. However, you can use the `sharedType` option to work with nested structures from a parent Y.Doc:
212
+
213
+
```typescript
214
+
import { YjsPlugin } from'@platejs/yjs';
215
+
import*asYfrom'yjs';
216
+
217
+
// Create a parent document with multiple nested editors
218
+
const parentDoc =newY.Doc();
219
+
220
+
// Create nested structures for different editors
221
+
const editorsMap =parentDoc.getMap('editors');
222
+
const mainEditorContent =newY.XmlText();
223
+
const sidebarEditorContent =newY.XmlText();
224
+
225
+
// Store them in the parent doc
226
+
editorsMap.set('main', mainEditorContent);
227
+
editorsMap.set('sidebar', sidebarEditorContent);
228
+
229
+
// You can also store other data in the parent doc
230
+
const metadata =parentDoc.getMap('metadata');
231
+
metadata.set('title', 'My Document');
232
+
metadata.set('author', 'John Doe');
233
+
metadata.set('createdAt', Date.now());
234
+
235
+
// Create the main editor with the nested shared type
236
+
const mainEditor =createPlateEditor({
237
+
plugins: [
238
+
YjsPlugin.configure({
239
+
ydoc: parentDoc, // Pass the parent doc for provider sync
240
+
sharedType: mainEditorContent, // Use the specific nested content
241
+
providers: [
242
+
{
243
+
type: 'webrtc',
244
+
options: { roomName: 'my-document' },
245
+
},
246
+
],
247
+
}),
248
+
],
249
+
});
250
+
251
+
// Create a sidebar editor with its own nested shared type
252
+
const sidebarEditor =createPlateEditor({
253
+
plugins: [
254
+
YjsPlugin.configure({
255
+
ydoc: parentDoc, // Same parent doc
256
+
sharedType: sidebarEditorContent, // Different nested content
257
+
providers: [], // Don't create new providers, parent is already synced
258
+
}),
259
+
],
260
+
});
261
+
262
+
// Initialize with initial values - they'll be applied to the correct sharedTypes
**Important:** When using a custom `sharedType`, the initial `value` passed to `init()` will be applied directly to that specific `sharedType`, not to the default `ydoc.get('content', Y.XmlText)`. This ensures each editor's initial content goes to the correct location in your nested structure.
274
+
209
275
### Cursor Support
210
276
211
277
Collaborative cursors are enabled by default. You can customize their appearance and behavior:
0 commit comments