Skip to content

RangeError: Position -1 out of range when clicking left margin with table #2748

@GuiLeme

Description

@GuiLeme

What’s broken?

When having a static formatting toolbar, and a table as the first block of the editor, clicking in the left margin of it will crash the editor (the whole client to feel more precise) - You have to click the empty space, avoid clicking the plus sign or the drag icon.

An easier way to reproduce it is having a table as the first block, then create a second block beneath it and nest it. Then you click the margin on the nested block (there's plenty of space to click now) the crash occurs again.

I tested a possible fix in packages/core/src/extensions/TableHandles/TableHandles.ts in getCellSelection:

<...>
return editor.transact((tr) => {
        <...>
        } else {
          const fromCellPos =
            selection.$from.pos - selection.$from.parentOffset - 1;
          const toCellPos =
            selection.$to.pos - selection.$to.parentOffset - 1;

          // Opt-out when the selection is not pointing into cells
          if (fromCellPos <= 1 || toCellPos <= 1) {
            return undefined;
          }

          $fromCell = tr.doc.resolve(fromCellPos);
          $toCell = tr.doc.resolve(toCellPos);
        }
<...>

And that worked, but I am not sure if that's the right approach, seems buggy to select a magic number like 1 to be the threshold of fromCellPos and toCellPos.

What did you expect to happen?

I'd expect it not to crash if I clicked anywhere, really.

Steps to reproduce

  1. Create a new document with static toolbar (there's an example in the playground with it);
  2. With an empty document, create a table as the first block in the editor;
  3. Create another block and then nest it;
  4. When hovering the nested block you'll see the plus sign and the drag button a bit to the right, click on the empty space to its left;
  5. See the whole thing crashes, with error RangeError: Position -1 out of range;

BlockNote version

I could reproduce with v0.50.0, but I know it also happened with v0.48.1

Environment

Chrome v140.0.7339.80 - Ubuntu 22.04

Additional context

Following videos done with example static-formatting-toolbar from playground

I'll link a video of the problem:

The easiest way to reproduce:

bn-table-crash.mp4

A bit harderbut still able:

bn-table-crash-only-table.mp4

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageIssue has not yet been reviewed or classified by maintainers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions