You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a few custom blocks that I don't want the formatting toolbar to show up on. So, in order to avoid issue #360, I've set up a CustomFormattingToolbar.js component following the docs on the website.
I check whether I want the toolbar to show up on not using this condition: My custom blocks return the editor.getSelection() as undefined + if the current block is an active block. I use a local state variable called showToolbar that I update when selection changes, and upon console logging, it does calculate the correct value.
But, the problem is, the nest/unnest buttons are always disabled. I added a few of debugger statments and it seems like the canNestBlock variable here is somehow always undefined when something is selected in my version.
Here's code to reproduce my issue. You can run this in any of the playgrounds on the blocknote website.
import{useState}from"react";import{BlockNoteEditor}from"@blocknote/core";import{BlockNoteView,FormattingToolbarPositioner,HyperlinkToolbarPositioner,SideMenuPositioner,SlashMenuPositioner,ToggledStyleButton,Toolbar,ToolbarButton,useBlockNote,useEditorContentChange,useEditorSelectionChange,BlockTypeDropdown,TextAlignButton,ColorStyleButton,NestBlockButton,UnnestBlockButton,CreateLinkButton}from"@blocknote/react";constcustomBlocks=["custom-1","custom-2"];import"@blocknote/core/style.css";exportconstCustomFormattingToolbar=({ editor })=>{const[showToolBar,setShowToolBar]=useState(true);useEditorSelectionChange(editor,()=>{setShowToolBar(customBlocks.indexOf(editor.getTextCursorPosition()?.block?.type)===-1&&editor.getSelection()!==undefined);});console.log(showToolBar);returnshowToolBar ? (<Toolbar><BlockTypeDropdowneditor={editor}/><ToggledStyleButtoneditor={editor}toggledStyle={"bold"}/><ToggledStyleButtoneditor={editor}toggledStyle={"italic"}/><ToggledStyleButtoneditor={editor}toggledStyle={"underline"}/><ToggledStyleButtoneditor={editor}toggledStyle={"strike"}/><TextAlignButtoneditor={editor}textAlignment={"left"}/><TextAlignButtoneditor={editor}textAlignment={"center"}/><TextAlignButtoneditor={editor}textAlignment={"right"}/><ColorStyleButtoneditor={editor}/><NestBlockButtoneditor={editor}/><UnnestBlockButtoneditor={editor}/><CreateLinkButtoneditor={editor}/></Toolbar>) : null;};exportdefaultfunctionApp(){// Creates a new editor instance.consteditor: BlockNoteEditor=useBlockNote();// Renders the editor instance.return(<BlockNoteVieweditor={editor}theme={"light"}><FormattingToolbarPositionereditor={editor}formattingToolbar={CustomFormattingToolbar}/><HyperlinkToolbarPositionereditor={editor}/><SlashMenuPositionereditor={editor}/><SideMenuPositionereditor={editor}/></BlockNoteView>);}
Another weird thing is that if I just replace showToolBar with true in the return statement, it works fine ha. Like so:
man-shar
changed the title
Strange issue with the nested block option in FormattingToolbar
Strange issue with the NestBlock button in FormattingToolbar
Oct 6, 2023
Seems to be fixed now! Here's the updated code for 0.13.4:
import{useState}from"react";import"@blocknote/core/fonts/inter.css";import{useEditorSelectionChange,TextAlignButton,ColorStyleButton,NestBlockButton,UnnestBlockButton,CreateLinkButton,useBlockNoteEditor,BlockTypeSelect,FormattingToolbar,BasicTextStyleButton,useCreateBlockNote,FormattingToolbarController,}from"@blocknote/react";import{BlockNoteView}from"@blocknote/mantine";import"@blocknote/mantine/style.css";constcustomBlocks=["custom-1","custom-2"];exportconstCustomFormattingToolbar=()=>{consteditor=useBlockNoteEditor();const[showToolBar,setShowToolBar]=useState(true);useEditorSelectionChange(()=>{setShowToolBar(customBlocks.indexOf(editor.getTextCursorPosition()?.block?.type)===-1&&editor.getSelection()!==undefined);},editor);console.log(showToolBar);returnshowToolBar ? (<FormattingToolbar><BlockTypeSelect/><BasicTextStyleButtonbasicTextStyle={"bold"}/><BasicTextStyleButtonbasicTextStyle={"italic"}/><BasicTextStyleButtonbasicTextStyle={"underline"}/><BasicTextStyleButtonbasicTextStyle={"strike"}/><TextAlignButtontextAlignment={"left"}/><TextAlignButtontextAlignment={"center"}/><TextAlignButtontextAlignment={"right"}/><ColorStyleButton/><NestBlockButton/><UnnestBlockButton/><CreateLinkButton/></FormattingToolbar>) : null;};exportdefaultfunctionApp(){// Creates a new editor instance.consteditor=useCreateBlockNote();// Renders the editor instance.return(<BlockNoteVieweditor={editor}theme={"light"}formattingToolbar={false}><FormattingToolbarControllerformattingToolbar={CustomFormattingToolbar}/></BlockNoteView>);}
Uh oh!
There was an error while loading. Please reload this page.
The bug
I have a few custom blocks that I don't want the formatting toolbar to show up on. So, in order to avoid issue #360, I've set up a
CustomFormattingToolbar.js
component following the docs on the website.I check whether I want the toolbar to show up on not using this condition: My custom blocks return the
editor.getSelection()
as undefined + if the current block is an active block. I use a local state variable calledshowToolbar
that I update when selection changes, and upon console logging, it does calculate the correct value.But, the problem is, the nest/unnest buttons are always disabled. I added a few of
debugger
statments and it seems like the canNestBlock variable here is somehow always undefined when something is selected in my version.Here's code to reproduce my issue. You can run this in any of the playgrounds on the blocknote website.
Another weird thing is that if I just replace showToolBar with true in the return statement, it works fine ha. Like so:
Here's some screenshots:

Any help/pointers on this are appreciated! Love this project :)
The text was updated successfully, but these errors were encountered: