Undoredo.js
888 Bytes
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
class Undoredo {
constructor() {
this.undoStack = [];
this.redoStack = [];
}
currentStatusPush = (data) => {
this.undoStack.push(data);
if (this.redoStack.length > 0) {
this.redoStack = [];
}
};
undoPush = (data) => {
this.undoStack.push(data);
};
redoPush = (data) => {
this.redoStack.push(data);
};
undo = () => {
if (this.undoStack.length > 0) {
return this.undoStack.pop();
}
return [];
};
redo = () => {
if (this.redoStack.length > 0) {
return this.redoStack.pop();
}
return [];
};
clear = () => {
this.undoStack = [];
this.redoStack = [];
};
isUndo = () => this.undoStack.length > 0;
isRedo = () => this.redoStack.length > 0;
}
export default Undoredo;