Skip to content

Improve error handling for initialContent in BlockNoteEditor #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions packages/core/src/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,17 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {

this.schema = newOptions.blockSchema;

const initialContent =

const defaultInitialContent = [
{
type: "paragraph",
id: UniqueID.options.generateID(),
},
];

let initialContent =
newOptions.initialContent ||
(options.collaboration
? undefined
: [
{
type: "paragraph",
id: UniqueID.options.generateID(),
},
]);
(options.collaboration ? undefined : defaultInitialContent);

const tiptapOptions: EditorOptions = {
...blockNoteTipTapOptions,
Expand All @@ -225,8 +226,43 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
// when using collaboration
return;
}
// we have to set the initial content here, because now we can use the editor schema
// which has been created at this point
if (typeof initialContent === "string") {
console.error(
"Invalid initialContent: Expected an array of PartialBlock, but received a string.",
"If this string is a JSON of the initial content, please ensure to parse it before passing it as initialContent.",
"If a string was passed intentionally, please convert it to the appropriate format."
);
initialContent = defaultInitialContent;
} else if (!Array.isArray(initialContent)) {
// Check if the processedInitialContent is an array of PartialBlock
console.error(
"Invalid initialContent: Expected an array of PartialBlock, received",
initialContent
);
initialContent = defaultInitialContent;
} else {
for (const block of initialContent) {
if (typeof block !== "object" || block === null) {
console.error(
"Invalid block in initialContent: Expected an object of type PartialBlock, received",
block
);
initialContent = defaultInitialContent;
break;
} else if (
!block.hasOwnProperty("type") ||
!block.hasOwnProperty("id")
) {
console.error(
"Invalid block in initialContent: Block is missing required properties 'type' and 'id'",
block
);
initialContent = defaultInitialContent;
break;
}
}
}

const schema = editor.editor.schema;
const ic = initialContent.map((block) => blockToNode(block, schema));

Expand All @@ -238,6 +274,7 @@ export class BlockNoteEditor<BSchema extends BlockSchema = DefaultBlockSchema> {
// override the initialcontent
editor.editor.options.content = root.toJSON();
},

onUpdate: () => {
// This seems to be necessary due to a bug in TipTap:
// https://github.com/ueberdosis/tiptap/issues/2583
Expand Down