TypeCellOS / BlockNote

A React Rich Text Editor that's block-based (Notion style) and extensible. Built on top of Prosemirror and Tiptap.
https://www.blocknotejs.org/
Mozilla Public License 2.0
5.89k stars 381 forks source link

[Crashing] Could not convert the snapshot blocks structure to blocks #879 #882

Closed doorkey closed 3 hours ago

doorkey commented 4 days ago

Describe the bug BlockNote could not convert the snapshot structure to blocks, probably due to styles incompatibility. Possible buggy code.

Please check the comments in the code for the exact line that throws error.


import "@blocknote/core/fonts/inter.css";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import { useCreateBlockNote } from "@blocknote/react";
import { useEffect } from "react";

interface EditorProps {
  initialData: {
    markdown: string;
    blocks: any[] | null;
  };
  onChange: ({ blocks, markdown }: { markdown: string; blocks: any[] }) => void;
}

export default function Editor({ initialData, onChange }: EditorProps) {
  const editor = useCreateBlockNote({});

  useEffect(() => {
    async function handleMarkdownChange() {
      if (initialData.blocks === null) {
        const blocks = await editor.tryParseMarkdownToBlocks(
          initialData.markdown
        );
        editor.replaceBlocks(editor.document, blocks);
      } else if (Array.isArray(initialData.blocks)) {
        // this line throws error
        // editor.replaceBlocks(editor.document, initialData.blocks);
      }
    }

    handleMarkdownChange();
  }, []);

  return (
    <div className="w-full max-w-4xl border min-h-[95dvh] rounded-lg py-8 px-2">
      <BlockNoteView
        editor={editor}
        formattingToolbar={false}
        onChange={async () => {
          const blocks = editor.document;
          const markdown = await editor.blocksToMarkdownLossy();

          onChange({
            blocks: JSON.parse(JSON.stringify(blocks)),
            markdown,
          });
        }}
      />
    </div>
  );
}

image image

Misc

YousefED commented 1 day ago

Hi @doorkey, thanks for reporting this!

Could you share the json of the blocks that you're trying to load?

doorkey commented 1 day ago

Hi @doorkey, thanks for reporting this!

Could you share the json of the blocks that you're trying to load?

[
{
"id": "588f083d-c190-477b-8d63-dc5f63019840",
"type": "heading",
"props": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left",
"level": 2
},
"content": [
{
"type": "text",
"text": "This is a test doc"
}
],
"children": []
},
{
"id": "1bb7080a-0975-4c13-8d96-a71ca52a1ec6",
"type": "paragraph",
"props": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left"
},
"content": [
{
"type": "text",
"text": "Nice"
},
{
"type": "text",
"text": " job",
"styles": {
"italic": true
}
}
],
"children": []
},
{
"id": "368781c7-7bab-4734-a629-ff9f70f581c9",
"type": "paragraph",
"props": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left"
},
"content": [],
"children": []
},
{
"id": "cae662cf-3d54-40fa-ad58-31fa8f70a2d2",
"type": "paragraph",
"props": {
"textColor": "default",
"backgroundColor": "default",
"textAlignment": "left"
},
"content": [],
"children": []
}
]
doorkey commented 1 day ago

@YousefED please check above and let me know if you need anything else.

YousefED commented 1 day ago

Thanks. Both these items:

"content": [
            {
                "type": "text",
                "text": "This is a test doc"
            }
        ],

and

"content": [
            {
                "type": "text",
                "text": "Nice"
            },

I'm missing a styles key. Normally, I'd expect this:

"content": [
            {
                "type": "text",
                "text": "Nice",
"styles": {}
            },

When you access editor.document, it should always contain this empty styles object. Do you know where you are losing this information?

doorkey commented 1 day ago

@YousefED thanks, we will change our modal and try it. Appreciate.