-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
MatrixNode.ts
65 lines (60 loc) · 2.25 KB
/
MatrixNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { KeyboardMemory } from '../../../KeyboardEngine/KeyboardMemory';
import { LatexConfiguration } from '../../../LatexConfiguration';
import { Placeholder } from '../../Placeholder/Placeholder';
import { BranchingNode } from '../Base/BranchingNode';
export class MatrixNode extends BranchingNode {
private readonly matrixType: string;
private readonly grid: Placeholder[][];
private readonly width: number;
constructor(matrixType: string, width: number, height: number) {
const grid: Placeholder[][] = [];
const leftToRight: Placeholder[] = [];
for (let i = 0; i < height; i++) {
const row: Placeholder[] = [];
for (let j = 0; j < width; j++) {
const placeholder = new Placeholder();
row.push(placeholder);
leftToRight.push(placeholder);
}
grid.push(row);
}
super(leftToRight);
this.grid = grid;
this.matrixType = matrixType;
this.width = width;
}
override getLatexPart(k: KeyboardMemory, latexConfiguration: LatexConfiguration): string {
let latex = String.raw`\begin{${this.matrixType}}`;
latex += this.grid.map((row) => row.map((placeholder) => placeholder.getLatex(k, latexConfiguration)).join(' & ')).join(String.raw` \\ `);
latex += String.raw`\end{${this.matrixType}}`;
return latex;
}
override getMoveDownSuggestion(fromPlaceholder: Placeholder): Placeholder | null {
const { rowIndex, columnIndex } = this.getPositionOf(fromPlaceholder);
if (rowIndex + 1 < this.grid.length) {
return this.grid[rowIndex + 1][columnIndex];
} else {
return null;
}
}
override getMoveUpSuggestion(fromPlaceholder: Placeholder): Placeholder | null {
const { rowIndex, columnIndex } = this.getPositionOf(fromPlaceholder);
if (rowIndex - 1 >= 0) {
return this.grid[rowIndex - 1][columnIndex];
} else {
return null;
}
}
private getPositionOf(placeholder: Placeholder): {
rowIndex: number;
columnIndex: number;
} {
const index = this.placeholders.indexOf(placeholder);
if (index == -1) {
throw 'The provided Placeholder is not part of this MatrixNode.';
}
const rowIndex = Math.floor(index / this.width);
const columnIndex = index - rowIndex * this.width;
return { rowIndex, columnIndex };
}
}