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
| export default {
past: [],
present: undefined,
future: [],
thereIsAPresent: function() { return this.present != undefined; },
thereIsAPast: function() { return this.past.length > 0; },
thereIsAFuture: function() { return this.future.length > 0; },
setPresent: function(state) { this.present = state; },
movePresentToPast: function() { this.past.push(this.present); },
movePresentToFuture: function() { this.future.push(this.present); },
movePastToPresent: function() { this.setPresent(this.past.pop()); },
moveFutureToPresent: function() { this.setPresent(this.future.pop()); },
push: function(currentState) {
if(this.thereIsAPresent()) {
this.movePresentToPast();
}
this.setPresent(currentState);
},
undo: function() {
if(this.thereIsAPresent()) {
this.movePresentToFuture(); // Moving back in time
this.movePastToPresent(); // Moving back in time
}
},
redo: function() {
if(!this.thereIsAFuture()) { // No future!
return;
}
if(this.thereIsAPresent()) {
this.movePresentToPast(); // Moving forward in time
}
this.moveFutureToPresent(); // Moving forward in time
},
gotoState: function(i) {
const index = Number(i);
const allstates = [...this.past, this.present, ...this.future];
this.present = allstates[index]
this.past = allstates.slice(0, index)
this.future = allstates.slice(index+1, allstates.length)
}
}
|