Permalink
Browse files

Add new function, 'undoAddTimed(action, timeout)'. Use it for moves.

  • Loading branch information...
mythmon committed May 10, 2012
1 parent 20f0de8 commit 3e3fd0e8bf7e436e54d20f2cf7a92fb297d95629
Showing with 22 additions and 1 deletion.
  1. +20 −0 web/js/board.js
  2. +0 −1 web/js/reductions.js
  3. +2 −0 web/js/tools.js
View
@@ -367,24 +367,43 @@ var Board = Class.extend({
undoAdd: function(self, action) {
self.action = action;
+ self.lastUndoAdded = {'time': +new Date(), 'action': action};
var text = self.serialize(false);
self.curUndo++;
var diff = self.undoLog.length - self.curUndo;
for (var i=0; i<diff; i++) {
self.undoLog.pop();
}
self.undoLog.push(text);
+ self.replayAdd(action);
+ },
+
+ /* Add an undo state. If the most recent undo was another instance of this,
+ * and happened less than timeout milliseconds ago, merge the two undos.
+ */
+ undoAddTimed: function(self, action, timeout) {
+ var now = +new Date();
+ var diff = now - self.lastUndoAdded.time;
+ if (action === self.lastUndoAdded.action && diff <= timeout) {
+ console.log("Merging timed undo. ({0}, {1}ms)".format(action, diff));
+ self.undoOverride(action);
+ } else {
+ console.log("Not merging timed undo. ({0}, {1}ms)".format(action, diff));
+ self.undoAdd(action);
+ }
},
undoOverride: function(self, action) {
self.action = action;
+ self.lastUndoAdded = {'time': +new Date(), 'action': action};
var text = self.serialize(false);
self.undoLog[self.curUndo] = text;
self.replayAdd(action);
},
undo: function(self) {
self.curUndo--;
+ self.lastUndoAdded = {};
if (self.curUndo < 0) {
self.curUndo = 0;
}
@@ -394,6 +413,7 @@ var Board = Class.extend({
redo: function(self) {
self.curUndo++;
+ self.lastUndoAdded = {};
if (self.curUndo > (self.undoLog.length-1)) {
self.curUndo = self.undoLog.length-1;
}
View
@@ -486,7 +486,6 @@ var Reduction = Class.extend({
n.y = Math.round(n.y/self.board.snap_size)*self.board.snap_size;
}
}
- self.board.undoAdd();
}
return kinetic;
View
@@ -119,6 +119,8 @@ var MoveTool = Tool.extend({
self.state[id].last_drag.x += dx;
self.state[id].last_drag.y += dy;
+
+ self.board.undoAddTimed('move', 30e3);
}
},

0 comments on commit 3e3fd0e

Please sign in to comment.