-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaction.ts
More file actions
59 lines (45 loc) · 1.01 KB
/
action.ts
File metadata and controls
59 lines (45 loc) · 1.01 KB
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 { Action } from "../actions/action";
import { RootStore } from "./root";
export class ActionStore {
readonly root: RootStore;
stack: {
undo: Action[];
redo: Action[];
};
constructor(root: RootStore) {
this.root = root;
this.stack = {
undo: [],
redo: [],
};
}
canUndo(): boolean {
return this.stack.undo.length > 0;
}
canRedo(): boolean {
return this.stack.redo.length > 0;
}
run(action: Action) {
action.run();
if (action.undo) {
this.stack.redo = [];
this.stack.undo.push(action);
}
}
undo() {
const action = this.stack.undo.pop();
if (!action) {
return;
}
action.undo!();
this.stack.redo.push(action);
}
redo() {
const action = this.stack.redo.pop();
if (!action) {
return;
}
action.run();
this.stack.undo.push(action);
}
}