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
It is not possible to select a single block via the setSelection function or seemingly any other way. Passing the same block or block id throws the error: Attempting to set selection with the same anchor and head blocks (id ${startBlockId}).
The following also does not set the selection for the given block:
editor.setSelection(block.id); or editor.setSelection(block.id, block.id); should select a single block from its start to its end.
Steps to reproduce
Using the setSelection function pass it the same block id for the startBlock and endBlock.
The error is thrown, rather than the block being selected from beginning to end.
BlockNote version
0.47.1
Environment
React 18.2
Additional context
I'm currently trying to build a custom SideMenu button that on click selects the corresponding block and opens a custom FormattingToolbar that uses editor.getSelection().
Contribution
I'd be interested in contributing a fix for this issue
Sponsor
I'm a sponsor and would appreciate if you could look into this sooner than later 💖
Hi @piemasters, yes this is known flaw in that API that we've been aiming to address with the Locations API. Unfortunately it has been de-prioritized for now. What you can do instead is to:
import{getNodeById}from"@blocknote/core";import{NodeSelection}from"prosemirror-state";editor.transact((tr)=>{constnodePos=getNodeById("id",tr.doc)?.posBeforeNode;if(nodePos===undefined){thrownewError("Node with id 'id' not found");}// set the selection to the node with the id "id"tr.setSelection(NodeSelection.create(tr.doc,nodePos));});
Thanks for the reply! I'm not sure if BlockNote reacts to the ProseMirror node selection as editor.getSelection() still returns undefined after running the code above, although I think the proseMirrorState is correct. Ideally I want to set the BlockNote selection for the AIExtension:
Sorry I guess I was expecting the editor.getSelection() text selection to update to match the contents of the editor.prosemirrorState.selection node. I also assumed the AIExtension was essentially calling getSelection() but it does seem to be using the prosemirrorState selection, so the invokeAI call is working now with your code above, thanks!
Ideally I need to set the text selection to the contents of the block at the same time to avoid UX issues. I'm not sure if that's possible, or if I'll just have to set it to something like editor.setSelection(block.id, nextBlock.id) for now so there is at least some text selection
@nperez0111 for my use case of clicking a custom SideMenu button and having it visibly select the block and open a custom FormattingToolbar with the block in context the following code seems to work (editor.getSelection() still returns undefined but I can work around that):
Activity
nperez0111 commentedon May 5, 2026
Hi @piemasters, yes this is known flaw in that API that we've been aiming to address with the Locations API. Unfortunately it has been de-prioritized for now. What you can do instead is to:
piemasters commentedon May 5, 2026
Thanks for the reply! I'm not sure if BlockNote reacts to the ProseMirror node selection as
editor.getSelection()still returns undefined after running the code above, although I think theproseMirrorStateis correct. Ideally I want to set the BlockNote selection for the AIExtension:nperez0111 commentedon May 5, 2026
Sorry, now you have me confused here @piemasters, you wanted to select a block, and the code I gave does just that (I tested it).
If you don't want a node selection, then you can use a text selection like what editor.setSelection does. So I'm unclear one what the issue is.
piemasters commentedon May 5, 2026
Sorry I guess I was expecting the
editor.getSelection()text selection to update to match the contents of theeditor.prosemirrorState.selectionnode. I also assumed the AIExtension was essentially callinggetSelection()but it does seem to be using theprosemirrorStateselection, so theinvokeAIcall is working now with your code above, thanks!Ideally I need to set the text selection to the contents of the block at the same time to avoid UX issues. I'm not sure if that's possible, or if I'll just have to set it to something like
editor.setSelection(block.id, nextBlock.id)for now so there is at least some text selectionpiemasters commentedon May 6, 2026
@nperez0111 for my use case of clicking a custom SideMenu button and having it visibly select the block and open a custom FormattingToolbar with the block in context the following code seems to work (
editor.getSelection()still returns undefined but I can work around that):Thanks for the help