Comments (3)
@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.
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.
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)
- Getting RangeError: Invalid content for node blockContainer: && Nested block deletion bug HOT 1
- Self XSS vulnerability in editor HOT 1
- Refresh pages content will cause SideMenu false trigger
- Remove blank line under a 'none' content block will remove 'none' block together
- Slash Menu items QoL
- Block nesting functions should take `BlockIdentifier`
- Hyperlink, table, and image menus/toolbars default component exports
- Can I write multiline code block? HOT 2
- TypeError: Cannot read properties of undefined (reading 'dom') HOT 2
- Custom block is not draggable
- Error: mark not found in styleschema link HOT 3
- Customizable default block HOT 1
- Encouraging usage of useMemo in docs for themes object HOT 1
- Empty block is unnecessarily created HOT 14
- Manipulate block after connecting with collaboration service HOT 2
- Cannot read properties of undefined (reading 'dispatch') HOT 1
- Tag example broken
- Handle Text Alignment when parsing HTML HOT 2
- Handle Images/Tables combined with Text when Parsing HTML HOT 1
- List placeholder shows on top of pasted custom inline element HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blocknote.