refac: collaborative editor

This commit is contained in:
Timothy J. Baek
2025-07-15 20:43:49 +04:00
parent 6343f4293a
commit 14ef2e5dea
2 changed files with 21 additions and 6 deletions

View File

@@ -218,10 +218,24 @@
// Empty state, check if we have content to initialize
// check if editor empty as well
const isEmptyEditor = !editor || editor.getText().trim() === '';
if (content && isEmptyEditor && (data?.sessions ?? ['']).length === 1) {
const editorYdoc = prosemirrorJSONToYDoc(editor.schema, content);
if (editorYdoc) {
Y.applyUpdate(this.doc, Y.encodeStateAsUpdate(editorYdoc));
if (content && (data?.sessions ?? ['']).length === 1) {
if (isEmptyEditor) {
const editorYdoc = prosemirrorJSONToYDoc(editor.schema, content);
if (editorYdoc) {
Y.applyUpdate(this.doc, Y.encodeStateAsUpdate(editorYdoc));
}
} else {
// If the editor already has content, we don't need to send an empty state
if (this.doc.getXmlFragment('prosemirror').length > 0) {
this.socket.emit('ydoc:document:update', {
document_id: this.documentId,
user_id: this.user?.id,
socket_id: this.socket.id,
update: Y.encodeStateAsUpdate(this.doc)
});
} else {
console.warn('Yjs document is empty, not sending state.');
}
}
}
} else {