From 7aeea18202b2dea9cc6a6204fd500f39a5ff07bb Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Fri, 17 May 2024 23:06:29 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=83=EF=B8=8F(frontend)=20add=20initial?= =?UTF-8?q?=20pad=20to=20webrtcserver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is the webrtc server that will be responsible for managing the data of the BlockNote Editor. So to restore the data of a saved pads, we have to add the initial pad to the room of the webrtc server, it will then update the data of BlockNote Editor. The webrtc server accept Y.Doc, so we convert our base64 data from Minio to make it compatible with the server. By doing so, we avoid the problem of data lost when multiple users are connecting and one user already updated the data of the BlockNote Editor before saving it. --- .../pads/pad/components/BlockNoteEditor.tsx | 2 +- .../src/features/pads/pad/stores/usePadStore.tsx | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/frontend/apps/impress/src/features/pads/pad/components/BlockNoteEditor.tsx b/src/frontend/apps/impress/src/features/pads/pad/components/BlockNoteEditor.tsx index 9b29d88c..576dfc4f 100644 --- a/src/frontend/apps/impress/src/features/pads/pad/components/BlockNoteEditor.tsx +++ b/src/frontend/apps/impress/src/features/pads/pad/components/BlockNoteEditor.tsx @@ -23,7 +23,7 @@ export const BlockNoteEditor = ({ pad }: BlockNoteEditorProps) => { const provider = padsStore?.[pad.id]?.provider; if (!provider) { - createProvider(pad.id); + createProvider(pad.id, pad.content); return null; } diff --git a/src/frontend/apps/impress/src/features/pads/pad/stores/usePadStore.tsx b/src/frontend/apps/impress/src/features/pads/pad/stores/usePadStore.tsx index f7839e1b..e2ca6b79 100644 --- a/src/frontend/apps/impress/src/features/pads/pad/stores/usePadStore.tsx +++ b/src/frontend/apps/impress/src/features/pads/pad/stores/usePadStore.tsx @@ -5,7 +5,7 @@ import { create } from 'zustand'; import { signalingUrl } from '@/core'; -import { Pad } from '../types'; +import { Base64, Pad } from '../types'; export interface PadStore { padsStore: { @@ -14,7 +14,7 @@ export interface PadStore { editor?: BlockNoteEditor; }; }; - createProvider: (padId: Pad['id']) => WebrtcProvider; + createProvider: (padId: Pad['id'], initialDoc: Base64) => WebrtcProvider; setEditor: (padId: Pad['id'], editor: BlockNoteEditor) => void; } @@ -24,8 +24,14 @@ const initialState = { export const usePadStore = create((set) => ({ padsStore: initialState.padsStore, - createProvider: (padId: string) => { - const provider = new WebrtcProvider(padId, new Y.Doc(), { + createProvider: (padId: string, initialDoc: Base64) => { + const doc = new Y.Doc(); + + if (initialDoc) { + Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64')); + } + + const provider = new WebrtcProvider(padId, doc, { signaling: [signalingUrl()], });