Skip to content

Can't select a single block #2708

Open
Open

Description

@piemasters

What’s broken?

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.setTextCursorPosition(block.id, "start");
editor.focus();

What did you expect to happen?

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

  1. Using the setSelection function pass it the same block id for the startBlock and endBlock.
  2. 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 💖

Activity

  1. added
    needs-triageIssue has not yet been reviewed or classified by maintainers.
    on May 5, 2026
  2. nperez0111 commented on May 5, 2026

    @nperez0111
    Contributor

    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) => {
      const nodePos = getNodeById("id", tr.doc)?.posBeforeNode;
      if (nodePos === undefined) {
        throw new Error("Node with id 'id' not found");
      }
      // set the selection to the node with the id "id"
      tr.setSelection(NodeSelection.create(tr.doc, nodePos));
    });
  3. piemasters commented on May 5, 2026

    @piemasters
    Author

    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:

    editor.getExtension(AIExtension)?.invokeAI({
      useSelection: true,
      ...
    
  4. nperez0111 commented on May 5, 2026

    @nperez0111
    Contributor

    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.

  5. piemasters commented on May 5, 2026

    @piemasters
    Author

    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

  6. piemasters commented on May 6, 2026

    @piemasters
    Author

    @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):

    editor.setTextCursorPosition(block.id, "start");
    editor.focus();
    
    editor.transact((tr) => {
      const nodePos = getNodeById(block.id, tr.doc)?.posBeforeNode;
      if (nodePos) {
        tr.setSelection(NodeSelection.create(tr.doc, nodePos));
      }
    });
    
    editor.getExtension(ShowSelectionExtension)?.showSelection(true, "");
    
    const formattingToolbar = editor.getExtension("formattingToolbar");
    if (formattingToolbar) {
      formattingToolbar.store?.setState(true);
    }
    

    Thanks for the help

  7. removed
    needs-triageIssue has not yet been reviewed or classified by maintainers.
    on May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions