Skip to content
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

i.view is undefined with editor.replaceBlocks or editor.removeBlocks #810

Open
ikazaksoftaria opened this issue Jun 5, 2024 · 3 comments
Assignees
Labels
architecture bug Something isn't working

Comments

@ikazaksoftaria
Copy link

ikazaksoftaria commented Jun 5, 2024

Hello! I want to update content in BlockNoteView component. And when I try to replace blocks with Editor API I get this error:

TypeError: i.view is undefined log screenshot

Code

export const Content = ({ contentJson }: { contentJson: string }) => {
  const editor = useCreateBlockNote({ initialContent: JSON.parse(contentJson) });

  useEffect(() => {
    try {
      editor.replaceBlocks(editor.document, JSON.parse(contentJson));
    } catch (e) {
      editor.removeBlocks(editor.document);
    }
  }, [editor, contentJson]);

  return <BlockNoteView editor={editor} editable={false} />;
};

What is the current working alternative to dynamically update content in BlockNoteView component?

Versions
@blocknote/shadcn: "0.13.5"
Node: v18.19.1
System: Manjaro Linux

@ikazaksoftaria ikazaksoftaria added the bug Something isn't working label Jun 5, 2024
@ikazaksoftaria ikazaksoftaria changed the title The alternative of onEditorContentChange i.view is undefined or alternative of onEditorContentChange Jun 5, 2024
@ikazaksoftaria ikazaksoftaria changed the title i.view is undefined or alternative of onEditorContentChange i.view is undefined with editor.replaceBlocks or editor.removeBlocks Jun 5, 2024
@ikazaksoftaria
Copy link
Author

ikazaksoftaria commented Jun 5, 2024

My solution

export const Content = ({ contentJson }: { contentJson: string }) => {
  const [editor, setEditor] = useState<BlockNoteEditor>(BlockNoteEditor.create({ initialContent: JSON.parse(contentJson) }));

  useEffect(() => {
    setEditor(BlockNoteEditor.create({ initialContent: JSON.parse(contentJson) }));
  }, [news.content]);

  return <BlockNoteView editor={editor} editable={false} />
};

@YousefED YousefED self-assigned this Jun 6, 2024
@YousefED
Copy link
Collaborator

YousefED commented Jun 6, 2024

This is probably an edge case where immediately calling replaceBlocks before the editor has fully initialized throws an error. We'll look into this

@ikazaksoftaria
Copy link
Author

ikazaksoftaria commented Jun 6, 2024

This is probably an edge case where immediately calling replaceBlocks before the editor has fully initialized throws an error. We'll look into this

Yes! It does work with await new Promise((resolve) => setTimeout(resolve, 10));

export const Content = ({ contentJson }: { contentJson: string }) => {
  const editor = useCreateBlockNote({ initialContent: JSON.parse(contentJson) });

  useEffect(() => {
    new Promise((resolve) => setTimeout(resolve, 10)).then(() => {
      try {
        editor.replaceBlocks(editor.document, JSON.parse(contentJson));
      } catch (e) {
        editor.removeBlocks(editor.document);
      }
    });
  }, [editor, contentJson]);

  return <BlockNoteView editor={editor} editable={false} />;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
architecture bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants