Last active
November 5, 2016 22:50
-
-
Save Mamdouh-Freelancer/8939f4a221afcbf24a4bf684850980e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
@ Created By Mamdouh Freelancer | |
@ Feel free to contact me any time | |
# https://fb.me/Mamdouh.Freelancer | |
# https://stackoverflow.com/users/7040620/mamdouh-freelancer | |
*/ | |
function RedoUndo(a) { | |
if (!a) console.error("Add textarea using new RedoUndo(textareaElement)"); | |
var curr = 0; | |
this.redoButton = function(b) { | |
if (!b) console.error("Add Redo Button using .redoButton(buttonElement)"); | |
b.addEventListener("click", redo, false); | |
} | |
this.undoButton = function(b) { | |
if (!b) console.error("Add Undo Button using .undoButton(buttonElement)"); | |
b.addEventListener("click", undo, false); | |
} | |
var inputs = [""]; | |
if (a.className.indexOf("RedoUndo") > -1) a.className += " RedoUndo"; | |
a.addEventListener("keyup", push2arr, false); | |
a.addEventListener("change", push2arr, false); | |
function redo() { | |
if (curr < inputs.length - 1 && curr >= 0) { | |
curr++; | |
a.value = inputs[curr]; | |
} | |
} | |
function undo() { | |
if (curr < inputs.length && curr > 0) { | |
curr--; | |
a.value = inputs[curr]; | |
} | |
} | |
function push2arr() { | |
if (curr < inputs.length - 1) { | |
inputs.splice(curr + 1, inputs.length); | |
} | |
console.log(curr + " - " + inputs.length); | |
if (inputs[inputs.length - 1] != this.value) { | |
curr++; | |
inputs.push(this.value); | |
} | |
} | |
} | |
//usage | |
var trgt = document.getElementById("trgt"); | |
var redo = document.getElementById("red"); | |
var undo = document.getElementById("und"); | |
var ru = new RedoUndo(trgt); | |
ru.redoButton(redo); | |
ru.undoButton(und); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment