Skip to content

Commit fc36ed0

Browse files
committed
🐛(frontend) fix initial content with collaboration
The way the initial content was created was causing issues with the collaboration server. As soon a user started typing, the problem was gone. This commit fixes that by letting Blocknote managing the initial content, then we update the Blocknote initial content with our initial content.
1 parent ed90769 commit fc36ed0

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to
2424

2525
## Fixed
2626

27+
- 🐛(frontend) fix initial content with collaboration #484
2728
- 🐛(frontend) Fix hidden menu on Firefox #468
2829
- 🐛(backend) fix sanitize problem IA #490
2930

src/frontend/apps/impress/src/features/docs/doc-editor/components/BlockNoteEditor.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ export const BlockNoteEditor = ({ doc, provider }: BlockNoteEditorProps) => {
128128
);
129129
useHeadings(editor);
130130

131+
/**
132+
* With the collaboration it gets complicated to create the initial block
133+
* better to let Blocknote manage, then we update the block with the content.
134+
*/
135+
useEffect(() => {
136+
if (doc.content) {
137+
return;
138+
}
139+
140+
setTimeout(() => {
141+
editor.updateBlock(editor.document[0], {
142+
type: 'heading',
143+
content: '',
144+
});
145+
}, 100);
146+
}, [editor, doc.content]);
147+
131148
useEffect(() => {
132149
setEditor(editor);
133150

src/frontend/apps/impress/src/features/docs/doc-management/stores/useDocStore.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HocuspocusProvider } from '@hocuspocus/provider';
22
import * as Y from 'yjs';
33
import { create } from 'zustand';
44

5-
import { Base64, Doc, blocksToYDoc } from '@/features/docs/doc-management';
5+
import { Base64, Doc } from '@/features/docs/doc-management';
66

77
export interface UseDocStore {
88
currentDoc?: Doc;
@@ -28,15 +28,6 @@ export const useDocStore = create<UseDocStore>((set, get) => ({
2828

2929
if (initialDoc) {
3030
Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64'));
31-
} else {
32-
const initialDocContent = [
33-
{
34-
type: 'heading',
35-
content: '',
36-
},
37-
];
38-
39-
blocksToYDoc(initialDocContent, doc);
4031
}
4132

4233
const provider = new HocuspocusProvider({

src/frontend/apps/impress/src/features/docs/doc-management/utils.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,6 @@ export const currentDocRole = (abilities: Doc['abilities']): Role => {
1212
: Role.READER;
1313
};
1414

15-
type BasicBlock = {
16-
type: string;
17-
content: string;
18-
};
19-
export const blocksToYDoc = (blocks: BasicBlock[], doc: Y.Doc) => {
20-
const xmlFragment = doc.getXmlFragment('document-store');
21-
22-
blocks.forEach((block) => {
23-
const xmlElement = new Y.XmlElement(block.type);
24-
if (block.content) {
25-
xmlElement.insert(0, [new Y.XmlText(block.content)]);
26-
}
27-
28-
xmlFragment.push([xmlElement]);
29-
});
30-
};
31-
3215
export const base64ToYDoc = (base64: string) => {
3316
const uint8Array = Buffer.from(base64, 'base64');
3417
const ydoc = new Y.Doc();

0 commit comments

Comments
 (0)