瀏覽代碼

refac: collaborative editor

Timothy J. Baek 2 月之前
父節點
當前提交
14ef2e5dea
共有 2 個文件被更改,包括 21 次插入6 次删除
  1. 3 2
      backend/open_webui/socket/main.py
  2. 18 4
      src/lib/components/common/RichTextInput.svelte

+ 3 - 2
backend/open_webui/socket/main.py

@@ -450,7 +450,7 @@ async def yjs_document_state(sid, data):
         room = f"doc_{document_id}"
 
         active_session_ids = get_session_ids_from_room(room)
-        print(active_session_ids)
+
         if sid not in active_session_ids:
             log.warning(f"Session {sid} not in room {room}. Cannot send state.")
             return
@@ -520,7 +520,8 @@ async def yjs_document_update(sid, data):
                 document_id, data.get("data", {}), SESSION_POOL.get(sid)
             )
 
-        await create_task(REDIS, debounced_save(), document_id)
+        if data.get("data"):
+            await create_task(REDIS, debounced_save(), document_id)
 
     except Exception as e:
         log.error(f"Error in yjs_document_update: {e}")

+ 18 - 4
src/lib/components/common/RichTextInput.svelte

@@ -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 {