GithubHelp home page GithubHelp logo

Comments (3)

i-am-chitti avatar i-am-chitti commented on June 12, 2024

@davidliudev, the useEffect runs even before the blocknote editor is ready. Have a look at this snippet -

const editor = useBlockNote({
    domAttributes: {
      editor: {
        class: "editor",
        "data-test": "editor",
      },
    },
    uploadFile: uploadToTmpFilesDotOrg_DEV_ONLY,
    onEditorReady: () => console.log("Editor is ready now"),
  });

  useEffect(() => {
    console.log("From useEffect");
    editor.insertBlocks(
      [{ content: "Hello!" }],
      editor.getTextCursorPosition().block,
      "after"
    );
  }, [editor]);

We get this output in console -

From useEffect
Editor is ready now

As BlockNote editor takes little time to initialize and after it's ready the onEditorReady callback is triggerred. But useEffect is synchronous due to which it's triggerred immediately. Since, editor isn't ready, the block insertion doesn't work and replaced by what's used to during initialization.

To avoid this, you can use the onEditorReady callback to insert the block. The code is -

const editor = useBlockNote({
    domAttributes: {
      editor: {
        class: "editor",
        "data-test": "editor",
      },
    },
    uploadFile: uploadToTmpFilesDotOrg_DEV_ONLY,
    onEditorReady: (edtior) => {
      editor.insertBlocks(
        [{ content: "Hello!" }],
        editor.getTextCursorPosition().block,
        "after"
      );
    },
  });

Also, note that the onEditorReady accepts an async function as well. For preprocessing, you can leverage it and pass the resolution further.

from blocknote.

davidliudev avatar davidliudev commented on June 12, 2024

Umm...I see. This behavior is different from other editors.
The reason I am doing this is that I have a file name string passed in as a prop and I need to load content from disk as the prop changes.

That's why I am putting it in useEffect...

But if the editor is not ready onMount then how to achieve this goal?
Like setting a isEditorReadyRef and then only update when the flag is true?
This may miss a few updates IMO...

from blocknote.

matthewlipski avatar matthewlipski commented on June 12, 2024

You shouldn't need to add an isEditorReadyRef - you can just check if the editor is ready using editor.ready. But yes I would use a useEffect hook with the file name prop in the dependency array, and inside the useEffect call editor.insertBlocks if editor.ready is true.

from blocknote.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.