Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 4dcebbf

Browse files
committed
Add gh-pages script
1 parent 5db19fa commit 4dcebbf

File tree

3 files changed

+161
-2
lines changed

3 files changed

+161
-2
lines changed

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</div>
5757
</div>
5858

59-
<script type="text/javascript" src="../lib/undomanager.js"></script>
59+
<script type="text/javascript" src="js/undomanager.js"></script>
6060
<script type="text/javascript" src="js/demo.js"></script>
6161
<script type="text/javascript" src="js/circledrawer.js"></script>
6262
</body>

demo/js/undomanager.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
Simple JavaScript undo and redo.
3+
https://github.com/ArthurClemens/JavaScript-Undo-Manager
4+
*/
5+
6+
(function () {
7+
'use strict';
8+
9+
function removeFromTo(array, from, to) {
10+
array.splice(
11+
from,
12+
!to ||
13+
1 +
14+
to -
15+
from +
16+
(!((to < 0) ^ (from >= 0)) && (to < 0 || -1) * array.length),
17+
);
18+
return array.length;
19+
}
20+
21+
let UndoManager = function () {
22+
let commands = [],
23+
index = -1,
24+
limit = 0,
25+
isExecuting = false,
26+
callback,
27+
// functions
28+
execute;
29+
30+
execute = function (command, action) {
31+
if (!command || typeof command[action] !== 'function') {
32+
return this;
33+
}
34+
isExecuting = true;
35+
36+
command[action]();
37+
38+
isExecuting = false;
39+
return this;
40+
};
41+
42+
return {
43+
/*
44+
Add a command to the queue.
45+
*/
46+
add: function (command) {
47+
if (isExecuting) {
48+
return this;
49+
}
50+
// if we are here after having called undo,
51+
// invalidate items higher on the stack
52+
commands.splice(index + 1, commands.length - index);
53+
54+
commands.push(command);
55+
56+
// if limit is set, remove items from the start
57+
if (limit && commands.length > limit) {
58+
removeFromTo(commands, 0, -(limit + 1));
59+
}
60+
61+
// set the current index to the end
62+
index = commands.length - 1;
63+
if (callback) {
64+
callback();
65+
}
66+
return this;
67+
},
68+
69+
/*
70+
Pass a function to be called on undo and redo actions.
71+
*/
72+
setCallback: function (callbackFunc) {
73+
callback = callbackFunc;
74+
},
75+
76+
/*
77+
Perform undo: call the undo function at the current index and decrease the index by 1.
78+
*/
79+
undo: function () {
80+
let command = commands[index];
81+
if (!command) {
82+
return this;
83+
}
84+
execute(command, 'undo');
85+
index -= 1;
86+
if (callback) {
87+
callback();
88+
}
89+
return this;
90+
},
91+
92+
/*
93+
Perform redo: call the redo function at the next index and increase the index by 1.
94+
*/
95+
redo: function () {
96+
let command = commands[index + 1];
97+
if (!command) {
98+
return this;
99+
}
100+
execute(command, 'redo');
101+
index += 1;
102+
if (callback) {
103+
callback();
104+
}
105+
return this;
106+
},
107+
108+
/*
109+
Clear the memory, losing all stored states. Reset the index.
110+
*/
111+
clear: function () {
112+
let prev_size = commands.length;
113+
114+
commands = [];
115+
index = -1;
116+
117+
if (callback && prev_size > 0) {
118+
callback();
119+
}
120+
},
121+
122+
hasUndo: function () {
123+
return index !== -1;
124+
},
125+
126+
hasRedo: function () {
127+
return index < commands.length - 1;
128+
},
129+
130+
getCommands: function () {
131+
return commands;
132+
},
133+
134+
getIndex: function () {
135+
return index;
136+
},
137+
138+
setLimit: function (l) {
139+
limit = l;
140+
},
141+
};
142+
};
143+
144+
if (
145+
typeof define === 'function' &&
146+
typeof define.amd === 'object' &&
147+
define.amd
148+
) {
149+
// AMD. Register as an anonymous module.
150+
define(function () {
151+
return UndoManager;
152+
});
153+
} else if (typeof module !== 'undefined' && module.exports) {
154+
module.exports = UndoManager;
155+
} else {
156+
window.UndoManager = UndoManager;
157+
}
158+
})();

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
},
99
"scripts": {
1010
"lint": "eslint --ext .js --fix .",
11-
"test": "jasmine-browser-runner runSpecs"
11+
"test": "jasmine-browser-runner runSpecs",
12+
"github": "cp -f ./lib/undomanager.js ./demo/js/ && cp -R demo/* ../gh-pages"
1213
},
1314
"devDependencies": {
1415
"chromedriver": "^110.0.0",

0 commit comments

Comments
 (0)