Textarea undo redo (20.11.2014)
Заведем объект для хранения истории контента поля ввода:
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();
    }
  }
});
JavaScript
comments powered by Disqus
JavaScript (13)
PHP (11)
Brainfuck (8)
adm (8)
Joomla (4)
Canvas (3)
answers (2)
API (2)
CMS (2)
Modx (2)
jQuery (1)
Ajax (1)
SQL (1)
Shell (1)
batch (1)
10-6