diff options
| author | Josh Kingsley <josh@joshkingsley.me> | 2025-10-26 14:28:55 +0200 |
|---|---|---|
| committer | Josh Kingsley <josh@joshkingsley.me> | 2025-10-26 14:28:55 +0200 |
| commit | 0324660a26684a5382b2c6c18cd0a4e9f0169631 (patch) | |
| tree | 64c16e8a4a4815f050f7e06a3b9486a668f2b4d4 /web/src/components/grid/cellAtCoord.ts | |
| parent | 1b8d05bf83d7bd9ab425852f519ea81bdc379444 (diff) | |
feat(web): add dummy toolbar + tailwindcss colors
Diffstat (limited to 'web/src/components/grid/cellAtCoord.ts')
| -rw-r--r-- | web/src/components/grid/cellAtCoord.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/web/src/components/grid/cellAtCoord.ts b/web/src/components/grid/cellAtCoord.ts new file mode 100644 index 0000000..dd594a4 --- /dev/null +++ b/web/src/components/grid/cellAtCoord.ts @@ -0,0 +1,40 @@ +import Coord from "../../math/Coord"; +import { CellRef } from "../../types"; +import { RenderedGrid, RenderedRow } from "./renderGrid"; + +function rowAtCoord(grid: RenderedGrid, coord: Coord): RenderedRow | undefined { + if (coord.y <= grid.rect.topLeft.y) { + return grid.renderedRows[0]; + } + + if (coord.y >= grid.rect.bottomRight.y) { + return grid.renderedRows.at(-1); + } + + return grid.renderedRows.find((row) => + row.rect.verticallyContainsCoord(coord), + ); +} + +export default function cellAtCoord( + grid: RenderedGrid, + x: number, + y: number, +): CellRef | undefined { + const coord = new Coord(x, y); + const row = rowAtCoord(grid, coord); + + if (!row) return; + + if (x <= row.rect.topLeft.x) { + return row.renderedCells[0]?.cellRef; + } + + if (x >= row.rect.bottomRight.x) { + return row.renderedCells.at(-1)?.cellRef; + } + + return row.renderedCells.find((cell) => + cell.rect.horizontallyContainsCoord(coord), + )?.cellRef; +} |
