-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (98 loc) · 2.42 KB
/
Copy pathindex.js
File metadata and controls
122 lines (98 loc) · 2.42 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module.exports = UndoManager;
function UndoManager()
{
this._undoStack = [];
this._redoStack = [];
this._groupedActions = [];
this._groupingLevel = 0;
this._isUndoing = false;
this._isRedoing = false;
}
UndoManager.prototype = {
constructor: UndoManager,
// Public
get canUndo()
{
return this._undoStack.length > 0;
},
get canRedo()
{
return this._redoStack.length > 0;
},
undo: function()
{
if (!this.canUndo)
return;
while (this._groupingLevel)
this.endGroup();
this._isUndoing = true;
this.beginGroup();
this._executeAction(this._undoStack.pop());
this.endGroup();
this._isUndoing = false;
},
redo: function()
{
if (!this.canRedo)
return;
this._isRedoing = true;
this.beginGroup();
this._executeAction(this._redoStack.pop());
this.endGroup();
this._isRedoing = false;
},
beginGroup: function()
{
this._groupingLevel++;
},
endGroup: function()
{
if (!this._groupingLevel)
return;
this._groupingLevel--;
if (this._groupingLevel > 0)
return;
this._registerAction(this._groupedActions);
this._groupedActions = [];
},
registerPropertyUndo: function(target, propertyName, value)
{
this.registerFunctionUndo(function() {
target[propertyName] = value;
});
},
registerMethodUndo: function(target, method)
{
var args = Array.prototype.slice.call(arguments, 2);
this.registerFunctionUndo(function() {
method.apply(target, args);
});
},
registerFunctionUndo: function(fun)
{
if (this._groupingLevel > 0)
this._groupedActions.push(fun);
else
this._registerAction(fun);
},
// Private
_registerAction: function(action)
{
if (this._isUndoing)
this._redoStack.push(action);
else {
this._undoStack.push(action);
if (!this._isRedoing)
this._redoStack = [];
}
},
_executeAction: function(action)
{
if (typeof action === "function")
action();
else if (Array.isArray(action)) {
while (action.length)
action.pop()();
}
}
};