import { CellRef, cellRefEquals } from "./types"; export abstract class Selection { readonly gridId: string; readonly activeCellRef: CellRef; constructor(gridId: string, activeCellRef: CellRef) { 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 type CellRange = [CellRef, CellRef]; export class RangeSelection extends Selection { #range: CellRange; get range() { return this.#range; } 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, ]); } }