From 968a1383f73a9c702c630ae472dcd57947cbe85b Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Wed, 2 Oct 2024 11:23:08 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(frontend)=20initial=20editor=20conten?= =?UTF-8?q?t=20is=20now=20a=20heading?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When we create a new document, the initial content is now a heading instead of a paragraph. This is to make it easier to set the title of the document. --- .../docs/doc-editor/stores/useDocStore.tsx | 11 +++++++++++ .../src/features/docs/doc-editor/utils.ts | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) 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]); + }); +};