Skip to content

Instantly share code, notes, and snippets.

@connecttobn
Created December 11, 2019 07:45
Show Gist options
  • Save connecttobn/cf97889d8d69a944780314a217981965 to your computer and use it in GitHub Desktop.
Save connecttobn/cf97889d8d69a944780314a217981965 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/hilefat
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var UndoAPI = function(updateService){
this._ll = [];
this._re = updateService;
this._curr = -1;
this._max =5;
}
UndoAPI.prototype = {
canRedo: function(){
var l = this._ll.length
return ( l !== 0 && this._curr !== l-1);
},
canUndo:function(){
var l = this._ll.length
return ( l !== 0 && this._curr !== -1);
},
register:function(data){
if(this._max == this._ll.length)
this._ll.shift();//remove oldest item
this._ll.push(data);
this._curr = this._ll.length-1;
return this._re(data);
},
undo:function(){
if(this.canUndo()){
this._curr--;
this._re(this._ll[this._curr]);
}
else
console.log("Can't UNDO");
},
redo:function(){
if(this.canRedo()){
this._curr++;
this._re(this._ll[this._curr]);
}else
console.log("Can't REDO");
},
clear: function(){
this._ll = []
this._curr =-1;
}
};
var print = function(d){
console.log("Current state:" + d.toString());
}
var tasksAPI = new UndoAPI(print)
tasksAPI.register(["a"]);
tasksAPI.register(["a", "b"]);
console.log("Can redo: "+tasksAPI.canRedo());
console.log("Can undo: "+tasksAPI.canUndo());
tasksAPI.register(["a", "b", "c"]);
tasksAPI.register(["a", "b", "c","d"]);
tasksAPI.undo();
tasksAPI.redo();
tasksAPI.register(["a", "b", "c","d", "e"]);
tasksAPI.register(["a", "b", "c","d", "e", "f"]);
tasksAPI.undo();
</script>
<script id="jsbin-source-javascript" type="text/javascript">var UndoAPI = function(updateService){
this._ll = [];
this._re = updateService;
this._curr = -1;
this._max =5;
}
UndoAPI.prototype = {
canRedo: function(){
var l = this._ll.length
return ( l !== 0 && this._curr !== l-1);
},
canUndo:function(){
var l = this._ll.length
return ( l !== 0 && this._curr !== -1);
},
register:function(data){
if(this._max == this._ll.length)
this._ll.shift();//remove oldest item
this._ll.push(data);
this._curr = this._ll.length-1;
return this._re(data);
},
undo:function(){
if(this.canUndo()){
this._curr--;
this._re(this._ll[this._curr]);
}
else
console.log("Can't UNDO");
},
redo:function(){
if(this.canRedo()){
this._curr++;
this._re(this._ll[this._curr]);
}else
console.log("Can't REDO");
},
clear: function(){
this._ll = []
this._curr =-1;
}
};
var print = function(d){
console.log("Current state:" + d.toString());
}
var tasksAPI = new UndoAPI(print)
tasksAPI.register(["a"]);
tasksAPI.register(["a", "b"]);
console.log("Can redo: "+tasksAPI.canRedo());
console.log("Can undo: "+tasksAPI.canUndo());
tasksAPI.register(["a", "b", "c"]);
tasksAPI.register(["a", "b", "c","d"]);
tasksAPI.undo();
tasksAPI.redo();
tasksAPI.register(["a", "b", "c","d", "e"]);
tasksAPI.register(["a", "b", "c","d", "e", "f"]);
tasksAPI.undo();
</script></body>
</html>
var UndoAPI = function(updateService){
this._ll = [];
this._re = updateService;
this._curr = -1;
this._max =5;
}
UndoAPI.prototype = {
canRedo: function(){
var l = this._ll.length
return ( l !== 0 && this._curr !== l-1);
},
canUndo:function(){
var l = this._ll.length
return ( l !== 0 && this._curr !== -1);
},
register:function(data){
if(this._max == this._ll.length)
this._ll.shift();//remove oldest item
this._ll.push(data);
this._curr = this._ll.length-1;
return this._re(data);
},
undo:function(){
if(this.canUndo()){
this._curr--;
this._re(this._ll[this._curr]);
}
else
console.log("Can't UNDO");
},
redo:function(){
if(this.canRedo()){
this._curr++;
this._re(this._ll[this._curr]);
}else
console.log("Can't REDO");
},
clear: function(){
this._ll = []
this._curr =-1;
}
};
var print = function(d){
console.log("Current state:" + d.toString());
}
var tasksAPI = new UndoAPI(print)
tasksAPI.register(["a"]);
tasksAPI.register(["a", "b"]);
console.log("Can redo: "+tasksAPI.canRedo());
console.log("Can undo: "+tasksAPI.canUndo());
tasksAPI.register(["a", "b", "c"]);
tasksAPI.register(["a", "b", "c","d"]);
tasksAPI.undo();
tasksAPI.redo();
tasksAPI.register(["a", "b", "c","d", "e"]);
tasksAPI.register(["a", "b", "c","d", "e", "f"]);
tasksAPI.undo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment