BlockNote icon indicating copy to clipboard operation
BlockNote copied to clipboard

Set HTML as initialContent

Open stepmixx opened this issue 2 years ago • 3 comments

The documentation is not clear how to set HTML as initialContent. using this in an useEffect doesn't work for initial state:

const getInitialBlocks = async () => {
    const blocks = await  editor.HTMLToBlocks(
      html
    );
    editor.replaceBlocks(editor.topLevelBlocks, blocks);
  }

And the initialContent doesn't let me use an async await function to convert directly the html there.

The solution that i found best is to allow the initialContent prop to receive asynchronous functions. So i can do something like this:

const editor = useBlockNote({
    initialContent: getInitialBlocks()
});

const getInitialBlocks = async () => {
    const blocks = await  editor.HTMLToBlocks(
      html
    );
    return blocks
  }

stepmixx avatar Dec 01 '23 15:12 stepmixx

Yep good suggestion, will look into implementing this. Right now I think your best bet is to just call getInitialBlocks in the onEditorReady option (see https://www.blocknotejs.org/docs/editor). But ofc this isn't quite the same since the editor will still render before the initial content is created. You might also want the use the editor.isEditable getter/setter to make the editor read-only until the initial content is loaded.

matthewlipski avatar Dec 05 '23 13:12 matthewlipski

Hi, Any sollution for this

syedslegend786 avatar Apr 05 '24 05:04 syedslegend786

The best way currently to do this is to have a setup similar to instantiate the editor yourself with empty "initialContent" and then replace the content with blocks converted from HTML

pseudocode:

const editor = useMemo(() => {
    
    const editor = BlockNoteEditor.create();
    editor.replaceBlocks(blocksfromHtml(initialHtml));
    return editor
  }, [initialContent]);

It's a bit of a workaround. We can make a more robust API that would make this nicer.

(consider adding a bounty for this if it's blocking you)

YousefED avatar Apr 08 '24 08:04 YousefED

closed by https://github.com/TypeCellOS/BlockNote/pull/909

YousefED avatar Jul 16 '24 10:07 YousefED