Subdocuments
Embedding Yjs documents into Yjs documents
// Client One
const rootDoc = new Y.Doc()
const folder = rootDoc.getMap()
const subDoc = new Y.Doc()
subDoc.getText().insert(0, 'some initial content')
folder.set('my-document.txt', subDoc)// Client Two
const subDoc = rootDoc.getMap().get('my-document.txt')
const subDocText = subDoc.getText()
subDocText.toString() // => "" - content is empty
// Data needs to be loaded first..
subDoc.load()
// Then the providers will fetch data from the database / network
// and eventually fill the content
subDoc.on('synced', () => {
subDocText.toString() // => "some initial content"
})
// It is hard to determine when data was actually synced.
// The synced event is still helpful to show the user that this user synced
// with other users.
// It is safer to observe the data types and don't listen
// to an explicit sync event.
subDocText.observe(() => {
// data changed..
})Last updated