Заведем объект для хранения истории контента поля ввода:
var buffer = {
content: [],
add: function(el) {
this.counter = this.current = this.content.push(el)-1;
},
undo: function() {
if(this.current>0) this.current-= 1;
return this.content[this.current];
},
redo: function() {
if(this.current<this.counter) this.current+= 1;
return this.content[this.current];
},
counter: 0,
current: 0
}
И установим обработчик событий:
window.onload = (function() {
document.getElementById("content").onkeydown = function(e) {
if(this.value!=buffer.content[buffer.current]) buffer.add(this.value);
if (e.keyCode == 90 && e.ctrlKey) { //ctrl+z
e.preventDefault();
this.value = buffer.undo();
}
if (e.keyCode == 89 && e.ctrlKey) { //ctrl+y
e.preventDefault();
this.value = buffer.redo();
}
}
});