Skip to content

Commit 12f6880

Browse files
committed
Move out direction and optimize selection methods
1 parent 9c484d8 commit 12f6880

3 files changed

Lines changed: 66 additions & 22 deletions

File tree

src/reducer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import reducer, {
99
modifyRangeSelectionEdge,
1010
modifyEntireRowsSelection,
1111
modifyEntireColumnsSelection,
12+
Direction,
1213
} from "./reducer";
1314
import { createEmptyMatrix } from "./util";
1415
import * as Point from "./point";
@@ -20,7 +21,6 @@ import {
2021
EntireTableSelection,
2122
EntireRowsSelection,
2223
EntireColumnsSelection,
23-
Direction,
2424
EmptySelection,
2525
} from "./selection";
2626
import "./areModelsEqual";

src/reducer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
EntireColumnsSelection,
1010
EntireRowsSelection,
1111
EntireTableSelection,
12-
Direction,
1312
} from "./selection";
1413
import { isActive } from "./util";
1514
import * as Actions from "./actions";
@@ -484,6 +483,13 @@ export function getActive(state: Types.StoreState): Types.CellBase | null {
484483
return activeCell || null;
485484
}
486485

486+
export enum Direction {
487+
Left = "Left",
488+
Right = "Right",
489+
Top = "Top",
490+
Bottom = "Bottom",
491+
}
492+
487493
/** Modify given edge according to given active point and data */
488494
export function modifyEdge<T extends Selection>(
489495
selection: T,

src/selection.ts

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import { PointRange } from "./point-range";
22
import * as Point from "./point";
33
import * 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 */
136
export 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 */
3927
export 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 */
5449
export 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 */
7985
abstract class EntireSelection extends Selection {}
8086

87+
/** Selection of the entire table */
8188
export 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 */
97115
export 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 */
133152
export 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 */
161190
export 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 */
196234
export 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 */
201239
export 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

Comments
 (0)