From a984c69e3cca4bcf822989bb78a8befe2397e487 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Sun, 26 Oct 2025 18:31:23 +0200 Subject: feat(web): draw selection --- web/src/selection.ts | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'web/src/selection.ts') diff --git a/web/src/selection.ts b/web/src/selection.ts index 88d394b..3d18417 100644 --- a/web/src/selection.ts +++ b/web/src/selection.ts @@ -1,4 +1,4 @@ -import { CellRef } from "./types"; +import { CellRef, cellRefEquals } from "./types"; export abstract class Selection { readonly gridId: string; @@ -8,12 +8,45 @@ export abstract class Selection { this.gridId = gridId; this.activeCellRef = activeCellRef; } + + abstract extend(cellRef: CellRef): Selection; +} + +export class ActiveCellSelection extends Selection { + extend(cellRef: CellRef): Selection { + if (cellRefEquals(cellRef, this.activeCellRef)) { + return this; + } + + return new RangeSelection(this.gridId, this.activeCellRef, [ + this.activeCellRef, + cellRef, + ]); + } } -export class ActiveCellSelection extends Selection {} +export type CellRange = [CellRef, CellRef]; -export class RangeSelection extends Selection {} +export class RangeSelection extends Selection { + #range: CellRange; -export class AllSelection extends Selection {} + get range() { + return this.#range; + } -export class PendingSelection extends Selection {} + constructor(gridId: string, activeCellRef: CellRef, range: CellRange) { + super(gridId, activeCellRef); + this.#range = range; + } + + extend(cellRef: CellRef): Selection { + if (cellRefEquals(cellRef, this.activeCellRef)) { + return new ActiveCellSelection(this.gridId, cellRef); + } + + return new RangeSelection(this.gridId, this.activeCellRef, [ + this.#range[0], + cellRef, + ]); + } +} -- cgit v1.2.3