refac
This commit is contained in:
1 parent
2a0274a0a0
commit
5078d987f8
2 files changed
+35
-27
No files matched your search
@@ -765,7 +765,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (collaboration && documentId && socket && user) {
|
if (collaboration && editable && documentId && socket && user) {
|
||||||
const { SocketIOCollaborationProvider } = await import('./RichTextInput/Collaboration');
|
const { SocketIOCollaborationProvider } = await import('./RichTextInput/Collaboration');
|
||||||
provider = new SocketIOCollaborationProvider(documentId, socket, user, content);
|
provider = new SocketIOCollaborationProvider(documentId, socket, user, content);
|
||||||
}
|
}
|
||||||
@@ -909,7 +909,7 @@
|
|||||||
: []),
|
: []),
|
||||||
...(collaboration && provider ? [provider.getEditorExtension()] : [])
|
...(collaboration && provider ? [provider.getEditorExtension()] : [])
|
||||||
],
|
],
|
||||||
content: collaboration ? undefined : content,
|
content: provider ? undefined : content,
|
||||||
autofocus: messageInput ? true : false,
|
autofocus: messageInput ? true : false,
|
||||||
onTransaction: () => {
|
onTransaction: () => {
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ const generateUserColor = () => {
|
|||||||
export type EditorContentGetter = () => {
|
export type EditorContentGetter = () => {
|
||||||
md: string;
|
md: string;
|
||||||
html: string;
|
html: string;
|
||||||
json: string;
|
json: unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Custom Yjs Socket.IO provider
|
// Custom Yjs Socket.IO provider
|
||||||
@@ -48,7 +48,7 @@ export class SocketIOCollaborationProvider {
|
|||||||
private readonly documentId: string,
|
private readonly documentId: string,
|
||||||
private readonly socket: Socket,
|
private readonly socket: Socket,
|
||||||
private readonly user: SessionUser,
|
private readonly user: SessionUser,
|
||||||
private readonly initialContent: string | null = null
|
private readonly initialContent: unknown = null
|
||||||
) {
|
) {
|
||||||
this.setupEventListeners();
|
this.setupEventListeners();
|
||||||
}
|
}
|
||||||
@@ -82,9 +82,30 @@ export class SocketIOCollaborationProvider {
|
|||||||
public setEditor(editor: Editor, editorContentGetter: EditorContentGetter) {
|
public setEditor(editor: Editor, editorContentGetter: EditorContentGetter) {
|
||||||
this.editor = editor;
|
this.editor = editor;
|
||||||
this.editorContentGetter = editorContentGetter;
|
this.editorContentGetter = editorContentGetter;
|
||||||
|
|
||||||
|
if (this.socket.connected && !this.isConnected) {
|
||||||
|
this.isConnected = true;
|
||||||
|
}
|
||||||
|
if (this.isConnected) {
|
||||||
|
this.joinDocument();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private applyInitialContent() {
|
||||||
|
if (!this.editor || !this.initialContent) return;
|
||||||
|
|
||||||
|
if (typeof this.initialContent === 'string') {
|
||||||
|
this.editor.commands.setContent(this.initialContent);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const doc = prosemirrorJSONToYDoc(this.editor.schema, this.initialContent);
|
||||||
|
Y.applyUpdate(this.doc, Y.encodeStateAsUpdate(doc));
|
||||||
}
|
}
|
||||||
|
|
||||||
private joinDocument() {
|
private joinDocument() {
|
||||||
|
if (!this.editor) return;
|
||||||
|
|
||||||
const userColor = generateUserColor();
|
const userColor = generateUserColor();
|
||||||
this.socket.emit('ydoc:document:join', {
|
this.socket.emit('ydoc:document:join', {
|
||||||
document_id: this.documentId,
|
document_id: this.documentId,
|
||||||
@@ -124,28 +145,16 @@ export class SocketIOCollaborationProvider {
|
|||||||
const state = new Uint8Array(data.state);
|
const state = new Uint8Array(data.state);
|
||||||
|
|
||||||
if (state.length === 2 && state[0] === 0 && state[1] === 0) {
|
if (state.length === 2 && state[0] === 0 && state[1] === 0) {
|
||||||
// Empty state, check if we have content to initialize
|
if (
|
||||||
// check if editor empty as well
|
this.editor &&
|
||||||
// const editor = await getEditorInstance();
|
!this.editor.getText().trim() &&
|
||||||
|
this.doc.getXmlFragment('prosemirror').length === 0
|
||||||
const isEmptyEditor = !this.editor?.getText().trim();
|
) {
|
||||||
if (isEmptyEditor && this.editor) {
|
if (
|
||||||
if (this.initialContent && (data?.sessions ?? ['']).length === 1) {
|
this.initialContent &&
|
||||||
// Check if initialContent is HTML (string) or JSON (object)
|
[...(data.sessions ?? [])].sort()[0] === this.socket.id
|
||||||
if (typeof this.initialContent === 'string') {
|
) {
|
||||||
// HTML content - let the editor parse it, then sync to Yjs
|
this.applyInitialContent();
|
||||||
this.editor.commands.setContent(this.initialContent);
|
|
||||||
// The Yjs plugin will automatically sync the content
|
|
||||||
} else {
|
|
||||||
// JSON content - use the existing approach
|
|
||||||
const editorYdoc = prosemirrorJSONToYDoc(
|
|
||||||
this.editor.schema,
|
|
||||||
this.initialContent
|
|
||||||
);
|
|
||||||
if (editorYdoc) {
|
|
||||||
Y.applyUpdate(this.doc, Y.encodeStateAsUpdate(editorYdoc));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If the editor already has content, we don't need to send an empty state
|
// If the editor already has content, we don't need to send an empty state
|
||||||
@@ -233,7 +242,6 @@ export class SocketIOCollaborationProvider {
|
|||||||
|
|
||||||
if (this.socket.connected) {
|
if (this.socket.connected) {
|
||||||
this.isConnected = true;
|
this.isConnected = true;
|
||||||
this.joinDocument();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user