diff --git a/src/frontend/apps/impress/src/features/docs/doc-editor/stores/useDocStore.tsx b/src/frontend/apps/impress/src/features/docs/doc-editor/stores/useDocStore.tsx index ceffcf5f..ee575084 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-editor/stores/useDocStore.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-editor/stores/useDocStore.tsx @@ -6,6 +6,8 @@ import { create } from 'zustand'; import { providerUrl } from '@/core'; import { Base64 } from '@/features/docs/doc-management'; +import { blocksToYDoc } from '../utils'; + interface DocStore { provider: HocuspocusProvider; editor?: BlockNoteEditor; @@ -28,6 +30,15 @@ export const useDocStore = create((set, get) => ({ if (initialDoc) { Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64')); + } else { + const initialDocContent = [ + { + type: 'heading', + content: '', + }, + ]; + + blocksToYDoc(initialDocContent, doc); } const provider = new HocuspocusProvider({ diff --git a/src/frontend/apps/impress/src/features/docs/doc-editor/utils.ts b/src/frontend/apps/impress/src/features/docs/doc-editor/utils.ts index aa0d43ba..2c9495ec 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-editor/utils.ts +++ b/src/frontend/apps/impress/src/features/docs/doc-editor/utils.ts @@ -1,3 +1,5 @@ +import * as Y from 'yjs'; + export const randomColor = () => { const randomInt = (min: number, max: number) => { return Math.floor(Math.random() * (max - min + 1)) + min; @@ -26,3 +28,20 @@ function hslToHex(h: number, s: number, l: number) { export const toBase64 = ( str: WithImplicitCoercion, ) => Buffer.from(str).toString('base64'); + +type BasicBlock = { + type: string; + content: string; +}; +export const blocksToYDoc = (blocks: BasicBlock[], doc: Y.Doc) => { + const xmlFragment = doc.getXmlFragment('document-store'); + + blocks.forEach((block) => { + const xmlElement = new Y.XmlElement(block.type); + if (block.content) { + xmlElement.insert(0, [new Y.XmlText(block.content)]); + } + + xmlFragment.push([xmlElement]); + }); +};