@@ -2,13 +2,6 @@ import { PointRange } from "./point-range";
22import * as Point from "./point" ;
33import * as Matrix from "./matrix" ;
44
5- export enum Direction {
6- Left = "Left" ,
7- Right = "Right" ,
8- Top = "Top" ,
9- Bottom = "Bottom" ,
10- }
11-
125/** Selection from a spreadsheet */
136export abstract class Selection {
147 /** Get concrete range of the selection in the given data */
@@ -17,25 +10,20 @@ export abstract class Selection {
1710 /** Normalize the selection according to the given data */
1811 abstract normalizeTo ( data : Matrix . Matrix < unknown > ) : this;
1912
20- /** Return whether the given row is entirely selected in given selection */
13+ /** Determines whether the given row is entirely selected in given selection */
2114 abstract hasEntireRow ( row : number ) : boolean ;
2215
23- /** Return whether the given column is entirely selected in given selection */
16+ /** Determines whether the given column is entirely selected in given selection */
2417 abstract hasEntireColumn ( column : number ) : boolean ;
2518
2619 /** Get the number of selected points according to given data */
27- size ( data : Matrix . Matrix < unknown > ) : number {
28- const range = this . toRange ( data ) ;
29- return range ? range . size ( ) : 0 ;
30- }
20+ abstract size ( data : Matrix . Matrix < unknown > ) : number ;
3121
32- /** Return whether the given point is within the selection */
33- has ( data : Matrix . Matrix < unknown > , point : Point . Point ) : boolean {
34- const range = this . toRange ( data ) ;
35- return range !== null && range . has ( point ) ;
36- }
22+ /** Determines whether the given point is within the selection */
23+ abstract has ( data : Matrix . Matrix < unknown > , point : Point . Point ) : boolean ;
3724}
3825
26+ /** Selection of no cells */
3927export class EmptySelection extends Selection {
4028 toRange ( data : Matrix . Matrix < unknown > ) : PointRange | null {
4129 return null ;
@@ -49,8 +37,15 @@ export class EmptySelection extends Selection {
4937 hasEntireColumn ( column : number ) : boolean {
5038 return false ;
5139 }
40+ size ( ) : number {
41+ return 0 ;
42+ }
43+ has ( ) : boolean {
44+ return false ;
45+ }
5246}
5347
48+ /** Selection of a range of cells */
5449export class RangeSelection extends Selection {
5550 constructor ( public range : PointRange ) {
5651 super ( ) ;
@@ -74,10 +69,22 @@ export class RangeSelection extends Selection {
7469 hasEntireColumn ( column : number ) : boolean {
7570 return false ;
7671 }
72+
73+ size ( data : Matrix . Matrix < unknown > ) : number {
74+ const range = this . toRange ( data ) ;
75+ return range ? range . size ( ) : 0 ;
76+ }
77+
78+ has ( data : Matrix . Matrix < unknown > , point : Point . Point ) : boolean {
79+ const range = this . toRange ( data ) ;
80+ return range !== null && range . has ( point ) ;
81+ }
7782}
7883
84+ /** Selection of an entire part of the spreadsheet */
7985abstract class EntireSelection extends Selection { }
8086
87+ /** Selection of the entire table */
8188export class EntireTableSelection extends EntireSelection {
8289 toRange ( data : Matrix . Matrix < unknown > ) : PointRange {
8390 return getMatrixRange ( data ) ;
@@ -86,14 +93,25 @@ export class EntireTableSelection extends EntireSelection {
8693 normalizeTo ( data : Matrix . Matrix < unknown > ) : this {
8794 return this ;
8895 }
96+
8997 hasEntireColumn ( column : number ) : boolean {
9098 return true ;
9199 }
100+
92101 hasEntireRow ( row : number ) : boolean {
93102 return true ;
94103 }
104+
105+ size ( data : Matrix . Matrix < unknown > ) : number {
106+ return Matrix . getColumnsCount ( data ) * Matrix . getRowsCount ( data ) ;
107+ }
108+
109+ has ( data : Matrix . Matrix < unknown > , point : Point . Point ) : boolean {
110+ return true ;
111+ }
95112}
96113
114+ /** Selection of an entire axis in the spreadsheet */
97115export abstract class EntireAxisSelection extends EntireSelection {
98116 /** Selection start index, integer */
99117 readonly start : number ;
@@ -130,6 +148,7 @@ export abstract class EntireAxisSelection extends EntireSelection {
130148 }
131149}
132150
151+ /** Selection of entire rows in the spreadsheet */
133152export class EntireRowsSelection extends EntireAxisSelection {
134153 toRange ( data : Matrix . Matrix < unknown > ) : PointRange {
135154 const max = Matrix . maxPoint ( data ) ;
@@ -156,8 +175,18 @@ export class EntireRowsSelection extends EntireAxisSelection {
156175 hasEntireColumn ( column : number ) : boolean {
157176 return false ;
158177 }
178+
179+ size ( data : Matrix . Matrix < unknown > ) : number {
180+ const rows = this . end - this . start + 1 ;
181+ return rows * Matrix . getColumnsCount ( data ) ;
182+ }
183+
184+ has ( data : Matrix . Matrix < unknown > , point : Point . Point ) : boolean {
185+ return point . row >= this . start && point . row <= this . end ;
186+ }
159187}
160188
189+ /** Selection of entire columns in the spreadsheet */
161190export class EntireColumnsSelection extends EntireAxisSelection {
162191 toRange ( data : Matrix . Matrix < unknown > ) : PointRange {
163192 const max = Matrix . maxPoint ( data ) ;
@@ -184,6 +213,15 @@ export class EntireColumnsSelection extends EntireAxisSelection {
184213 hasEntireColumn ( column : number ) : boolean {
185214 return column >= this . start && column <= this . end ;
186215 }
216+
217+ size ( data : Matrix . Matrix < unknown > ) : number {
218+ const columns = this . end - this . start + 1 ;
219+ return columns * Matrix . getRowsCount ( data ) ;
220+ }
221+
222+ has ( data : Matrix . Matrix < unknown > , point : Point . Point ) : boolean {
223+ return point . column >= this . start && point . column <= this . end ;
224+ }
187225}
188226
189227/** Get the point range of given matrix */
@@ -192,12 +230,12 @@ export function getMatrixRange(data: Matrix.Matrix<unknown>): PointRange {
192230 return new PointRange ( Point . ORIGIN , maxPoint ) ;
193231}
194232
195- /** Returns whether given value is a valid index */
233+ /** Determines whether the given value is a valid index */
196234export function isIndex ( value : number ) : boolean {
197235 return Number . isInteger ( value ) && value >= 0 ;
198236}
199237
200- /** Error thrown when passing a non-index value where index is expected */
238+ /** Error thrown when passing a non-index value where an index value is expected */
201239export class InvalidIndexError extends Error {
202240 constructor ( name : string ) {
203241 super ( `${ name } is not a valid index. It must be 0 or a positive integer` ) ;
0 commit comments