Files
outline/shared/editor/selection/RowSelection.ts
Tom MoorandClaude Opus 5 125d863485 fix: Shift-click table grips to select a range of rows or columns (#13447)
* fix: Shift-click table grips to select a range of rows or columns

Shift or cmd clicking a second row or column grip now extends the
selection to cover every row or column in between.

Track the anchor and head index on RowSelection and ColumnSelection so
every grip in the range highlights, and a cell that spans rows or
columns no longer pulls its neighbours into the selection.

The inline selection menu collapsed the selection on pointerdown, before
the grip handler could read it, so there was nothing left to extend.
Leave the selection alone when the interaction is on a grip.

Hide the insert and move items in the row and column menus when the
selection covers more than one row or column, as they act on a single
row or column.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Take the selected row and column range from the selection

selectedRect grows past the selected rows or columns when a cell spans
them, so read the range from the selection where it is known. Keeps the
menus and the commands that act on a selection in step with the range
the grips highlight.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-15 17:27:15 -04:00

116 lines
3.2 KiB
TypeScript

import type { ResolvedPos } from "prosemirror-model";
import { type Node } from "prosemirror-model";
import { Selection } from "prosemirror-state";
import { CellSelection, inSameTable, TableMap } from "prosemirror-tables";
import type { Mappable } from "prosemirror-transform";
export class RowSelection extends CellSelection {
constructor(
public $anchorCell: ResolvedPos,
public $headCell: ResolvedPos,
public anchorIndex: number = 0,
public headIndex: number = anchorIndex
) {
super($anchorCell, $headCell);
}
/**
* Check whether a row is part of the selection.
*
* @param index the visual index of the row.
* @returns true if the row is within the selected range.
*/
containsRow(index: number): boolean {
return (
index >= Math.min(this.anchorIndex, this.headIndex) &&
index <= Math.max(this.anchorIndex, this.headIndex)
);
}
getBookmark(): RowBookmark {
return new RowBookmark(
this.$anchorCell.pos,
this.$headCell.pos,
this.anchorIndex,
this.headIndex
);
}
public static rowSelection(
$anchorCell: ResolvedPos,
$headCell: ResolvedPos = $anchorCell,
anchorIndex: number = 0,
headIndex: number = anchorIndex
): CellSelection {
const table = $anchorCell.node(-1);
const map = TableMap.get(table);
const tableStart = $anchorCell.start(-1);
const anchorRect = map.findCell($anchorCell.pos - tableStart);
const headRect = map.findCell($headCell.pos - tableStart);
const doc = $anchorCell.node(0);
if (anchorRect.left <= headRect.left) {
if (anchorRect.left > 0) {
$anchorCell = doc.resolve(
tableStart + map.map[anchorRect.top * map.width]
);
}
if (headRect.right < map.width) {
$headCell = doc.resolve(
tableStart + map.map[map.width * (headRect.top + 1) - 1]
);
}
} else {
if (headRect.left > 0) {
$headCell = doc.resolve(tableStart + map.map[headRect.top * map.width]);
}
if (anchorRect.right < map.width) {
$anchorCell = doc.resolve(
tableStart + map.map[map.width * (anchorRect.top + 1) - 1]
);
}
}
return new RowSelection($anchorCell, $headCell, anchorIndex, headIndex);
}
}
export class RowBookmark {
constructor(
public anchor: number,
public head: number,
public anchorIndex: number = 0,
public headIndex: number = anchorIndex
) {}
map(mapping: Mappable): RowBookmark {
return new RowBookmark(
mapping.map(this.anchor),
mapping.map(this.head),
this.anchorIndex,
this.headIndex
);
}
resolve(doc: Node): CellSelection | Selection {
const $anchorCell = doc.resolve(this.anchor),
$headCell = doc.resolve(this.head);
if (
$anchorCell.parent.type.spec.tableRole === "row" &&
$headCell.parent.type.spec.tableRole === "row" &&
$anchorCell.index() < $anchorCell.parent.childCount &&
$headCell.index() < $headCell.parent.childCount &&
inSameTable($anchorCell, $headCell)
) {
return new RowSelection(
$anchorCell,
$headCell,
this.anchorIndex,
this.headIndex
);
} else {
return Selection.near($headCell, 1);
}
}
}