summaryrefslogtreecommitdiff
path: root/web/src/selection.ts
diff options
context:
space:
mode:
authorJosh Kingsley <josh@joshkingsley.me>2025-10-26 19:41:01 +0200
committerJosh Kingsley <josh@joshkingsley.me>2025-10-26 19:41:01 +0200
commitaf8cf348feb8e6bb4bda4a277b06a0f41ff890d9 (patch)
treece7df39a6edb7e6df3d3fb0d903972391333a272 /web/src/selection.ts
parent43ba019bc0d3af502b806169dad5fcbbfc87d2b7 (diff)
feat(web): show selected subdivisions
Diffstat (limited to 'web/src/selection.ts')
-rw-r--r--web/src/selection.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/web/src/selection.ts b/web/src/selection.ts
index 3d18417..294db52 100644
--- a/web/src/selection.ts
+++ b/web/src/selection.ts
@@ -1,3 +1,8 @@
+import {
+ getRenderedCell,
+ RenderedCell,
+ RenderedGrid,
+} from "./components/grid/renderGrid";
import { CellRef, cellRefEquals } from "./types";
export abstract class Selection {
@@ -10,6 +15,8 @@ export abstract class Selection {
}
abstract extend(cellRef: CellRef): Selection;
+
+ abstract getSelectedCells(grid: RenderedGrid): RenderedCell[][];
}
export class ActiveCellSelection extends Selection {
@@ -23,6 +30,10 @@ export class ActiveCellSelection extends Selection {
cellRef,
]);
}
+
+ getSelectedCells(grid: RenderedGrid): RenderedCell[][] {
+ return [[getRenderedCell(grid, this.activeCellRef)!]];
+ }
}
export type CellRange = [CellRef, CellRef];
@@ -49,4 +60,45 @@ export class RangeSelection extends Selection {
cellRef,
]);
}
+
+ getSelectedCells(grid: RenderedGrid): RenderedCell[][] {
+ const startCell = getRenderedCell(grid, this.range[0]);
+ const endCell = getRenderedCell(grid, this.range[1]);
+
+ if (!startCell || !endCell) return [];
+
+ const firstRowIndex = Math.min(
+ startCell.renderedRowIndex,
+ endCell.renderedRowIndex,
+ );
+
+ const lastRowIndex = Math.max(
+ startCell.renderedRowIndex,
+ endCell.renderedRowIndex,
+ );
+
+ const startRatio =
+ startCell.startRatio.compare(endCell.startRatio) <= 0
+ ? startCell.startRatio
+ : endCell.startRatio;
+
+ const endRatio =
+ startCell.endRatio.compare(endCell.endRatio) >= 0
+ ? startCell.endRatio
+ : endCell.endRatio;
+
+ return grid.renderedRows
+ .slice(firstRowIndex, lastRowIndex + 1)
+ .map((row) => {
+ const firstCellIndex = row.renderedCells.findIndex(
+ (cell) => cell.startRatio.compare(startRatio) >= 0,
+ );
+
+ const lastCellIndex = row.renderedCells.findLastIndex(
+ (cell) => cell.endRatio.compare(endRatio) <= 0,
+ );
+
+ return row.renderedCells.slice(firstCellIndex, lastCellIndex + 1);
+ });
+ }
}