From af8cf348feb8e6bb4bda4a277b06a0f41ff890d9 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Sun, 26 Oct 2025 19:41:01 +0200 Subject: feat(web): show selected subdivisions --- web/src/selection.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'web/src/selection.ts') 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); + }); + } } -- cgit v1.2.3