"README.md" did not exist on "53cf9be02506124b0e148d0b00a009a08f1582c8"
Newer
Older
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
import { SerializableItem } from "./serializableitem";
interface SavePoint<DataType> {
description: string;
data: DataType;
}
export class History<HistoryDataType> {
public maxCheckpoints: number;
public currentCheckpoint: number;
private data: SerializableItem<never, HistoryDataType>;
private checkpoints: SavePoint<HistoryDataType>[];
constructor(
data: SerializableItem<never, HistoryDataType>,
maxCheckpoints = 20
) {
this.data = data;
this.maxCheckpoints = maxCheckpoints;
this.checkpoints = [];
this.currentCheckpoint = -1;
this.checkpoint("New History");
}
checkpoint(description: string) {
const checkpointData = this.data.toHistorySerializableObject();
const checkpoint = {
description: description,
data: JSON.parse(JSON.stringify(checkpointData)), // deepcopy
};
// Remove potential history which is not relevant anymore (maybe caused by undo ops)
this.currentCheckpoint++;
this.checkpoints.length = this.currentCheckpoint;
this.checkpoints.push(checkpoint);
}
historyDescription(): Array<string> {
return this.checkpoints.map((savepoint) => savepoint.description);
}
undo(): SavePoint<HistoryDataType> {
if (this.currentCheckpoint > 0) {
return this.checkpoints[this.currentCheckpoint--];
} else {
return this.checkpoints[0];
}
}
redo(): SavePoint<HistoryDataType> {
if (this.currentCheckpoint < this.checkpoints.length) {
return this.checkpoints[this.currentCheckpoint++];
} else {
return this.checkpoints[this.checkpoints.length - 1];
}
}
}