-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (52 loc) · 1.21 KB
/
index.js
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
'use strict'
const ACTIONS_TO_KEEP = 50
const status = {
canUndo: false,
canRedo: false
}
const actionTracker = {
list: [],
_pointer: -1,
get index() { return this._pointer },
set index(val) {
this._pointer = val
status.canUndo = this.index >= 0
status.canRedo = this.index < this.list.length - 1
},
add(action) {
this.list.splice(this.index + 1)
this.list.push(action)
this.index++
if (this.index >= ACTIONS_TO_KEEP) {
this.list.shift()
this.index--
}
return action.execute(action.value)
},
undo() {
const action = this.list[this.index]
this.index--
return action.undo(action.value)
},
redo() {
const action = this.list[this.index + 1]
this.index++
return action.execute(action.value)
}
}
const undoRedoManager = {
status,
get canUndo() { return status.canUndo },
get canRedo() { return status.canRedo },
execute: (execute, undo, value) => {
return actionTracker.add({ execute, undo, value })
},
undo: () => {
if (status.canUndo) return actionTracker.undo()
},
redo: () => {
if (status.canRedo) return actionTracker.redo()
}
}
module.exports = undoRedoManager
module.exports.default = undoRedoManager