Skip to content

Commit a846f33

Browse files
committed
fix init
1 parent 93a72c6 commit a846f33

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

packages/yjs/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,19 @@ const sidebarEditor = createPlateEditor({
259259
],
260260
});
261261

262-
// Initialize without auto-connecting (parent handles the connection)
263-
await mainEditor.api.yjs.init({ autoConnect: true });
264-
await sidebarEditor.api.yjs.init({ autoConnect: false });
262+
// Initialize with initial values - they'll be applied to the correct sharedTypes
263+
await mainEditor.api.yjs.init({
264+
autoConnect: true,
265+
value: [{ type: 'p', children: [{ text: 'Main editor content' }] }],
266+
});
267+
await sidebarEditor.api.yjs.init({
268+
autoConnect: false,
269+
value: [{ type: 'p', children: [{ text: 'Sidebar content' }] }],
270+
});
265271
```
266272

273+
**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+
267275
### Cursor Support
268276

269277
Collaborative cursors are enabled by default. You can customize their appearance and behavior:

packages/yjs/src/lib/BaseYjsPlugin.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { YjsEditor } from '@slate-yjs/core';
1+
import { slateNodesToInsertDelta, YjsEditor } from '@slate-yjs/core';
22
import {
33
type InitOptions,
44
type Value,
@@ -197,6 +197,7 @@ export const BaseYjsPlugin = createTSlatePlugin<YjsConfig>({
197197
_providers,
198198
awareness,
199199
providers: providerConfigsOrInstances = [],
200+
sharedType: customSharedType,
200201
ydoc,
201202
} = options;
202203

@@ -222,13 +223,23 @@ export const BaseYjsPlugin = createTSlatePlugin<YjsConfig>({
222223
initialNodes = editor.api.create.value();
223224
}
224225

225-
const initialDelta = await slateToDeterministicYjsState(
226-
id ?? editor.id,
227-
initialNodes
228-
);
229-
ydoc.transact(() => {
230-
Y.applyUpdate(ydoc, initialDelta);
231-
});
226+
// Use custom sharedType if provided, otherwise use default 'content'
227+
if (customSharedType) {
228+
// Apply initial value directly to the custom shared type
229+
const delta = slateNodesToInsertDelta(initialNodes);
230+
ydoc.transact(() => {
231+
customSharedType.applyDelta(delta);
232+
});
233+
} else {
234+
// Use deterministic state for default 'content' key
235+
const initialDelta = await slateToDeterministicYjsState(
236+
id ?? editor.id,
237+
initialNodes
238+
);
239+
ydoc.transact(() => {
240+
Y.applyUpdate(ydoc, initialDelta);
241+
});
242+
}
232243
}
233244

234245
// Final providers array that will contain both configured and custom providers

0 commit comments

Comments
 (0)