Skip to content

Commit 5dd663c

Browse files
fix: Error when custom blocks are in initialContent (#348)
* Fixed custom blocks being rendered before editor init * Gave initial block a non-generated ID to not have to redo snapshots * Updated playwright * update package lock * Small style update to fix tests * Implemented PR feedback --------- Co-authored-by: yousefed <[email protected]>
1 parent ca3c7d6 commit 5dd663c

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

examples/editor/src/index.css

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/editor/src/main.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from "react";
22
import { createRoot } from "react-dom/client";
33
import App from "./App";
4-
import "./index.css";
54

65
window.React = React;
76

packages/core/src/BlockNoteEditor.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,28 +216,36 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
216216
const tiptapOptions: EditorOptions = {
217217
...blockNoteTipTapOptions,
218218
...newOptions._tiptapOptions,
219-
onCreate: () => {
220-
newOptions.onEditorReady?.(this);
221-
this.ready = true;
222-
},
223219
onBeforeCreate(editor) {
224220
if (!initialContent) {
225221
// when using collaboration
226222
return;
227223
}
228-
// we have to set the initial content here, because now we can use the editor schema
229-
// which has been created at this point
230-
const schema = editor.editor.schema;
231-
const ic = initialContent.map((block) => blockToNode(block, schema));
232224

225+
// We always set the initial content to a single paragraph block. This
226+
// allows us to easily replace it with the actual initial content once
227+
// the TipTap editor is initialized.
228+
const schema = editor.editor.schema;
233229
const root = schema.node(
234230
"doc",
235231
undefined,
236-
schema.node("blockGroup", undefined, ic)
232+
schema.node("blockGroup", undefined, [
233+
blockToNode({ id: "initialBlock", type: "paragraph" }, schema),
234+
])
237235
);
238-
// override the initialcontent
239236
editor.editor.options.content = root.toJSON();
240237
},
238+
onCreate: () => {
239+
// We need to wait for the TipTap editor to init before we can set the
240+
// initial content, as the schema may contain custom blocks which need
241+
// it to render.
242+
if (initialContent !== undefined) {
243+
this.replaceBlocks(this.topLevelBlocks, initialContent);
244+
}
245+
246+
newOptions.onEditorReady?.(this);
247+
this.ready = true;
248+
},
241249
onUpdate: () => {
242250
// This seems to be necessary due to a bug in TipTap:
243251
// https://github.com/ueberdosis/tiptap/issues/2583

0 commit comments

Comments
 (0)