Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add textarea demo; call redo on stack.redo, not execute

  • Loading branch information...
commit 04992e4a0b307c99e12c0e899a7b0073b8ebc90f 1 parent 5a482cd
@jzaefferer authored
Showing with 78 additions and 2 deletions.
  1. +1 −1  demos/index.html
  2. +76 −0 demos/textarea.html
  3. +1 −1  undo.js
View
2  demos/index.html
@@ -10,7 +10,7 @@
</style>
<script>
$(function() {
- var stack = window.stack = new Undo.Stack(),
+ var stack = new Undo.Stack(),
UpCommand = Undo.Command.extend({
constructor: function(li) {
this.li = li;
View
76 demos/textarea.html
@@ -0,0 +1,76 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <script src="../undo.js"></script>
+ <script src="../vendor/jquery-1.5.1.js"></script>
+ <title>Undo.js demo</title>
+ <style>
+ body { font:62.5% Verdana,Arial,sans-serif; }
+ </style>
+ <script>
+ $(function() {
+ var stack = new Undo.Stack(),
+ EditCommand = Undo.Command.extend({
+ constructor: function(textarea, oldValue, newValue) {
+ this.textarea = textarea;
+ this.oldValue = oldValue;
+ this.newValue = newValue;
+ },
+ execute: function() {
+ },
+ undo: function() {
+ this.textarea.val(this.oldValue);
+ },
+
+ redo: function() {
+ this.textarea.val(this.newValue);
+ }
+ });
+ stack.changed = function() {
+ stackUI();
+ };
+
+ var undo = $(".undo"),
+ redo = $(".redo"),
+ dirty = $(".dirty");
+ function stackUI() {
+ undo.attr("disabled", !stack.canUndo());
+ redo.attr("disabled", !stack.canRedo());
+ dirty.toggle(stack.dirty());
+ }
+ stackUI();
+
+ $(document.body).delegate(".undo, .redo, .save", "click", function() {
+ var what = $(this).attr("class");
+ stack[what]();
+ return false;
+ });
+
+ var text = $("#text"),
+ startValue = text.val(),
+ timer;
+ $("#text").bind("keyup", function() {
+ // a way too simple algorithm in place of single-character undo
+ clearTimeout(timer);
+ timer = setTimeout(function() {
+ var newValue = text.val();
+ // ignore meta key presses
+ if (newValue != startValue) {
+ stack.execute(new EditCommand(text, startValue, newValue));
+ startValue = newValue;
+ }
+ }, 250);
+ });
+ });
+ </script>
+</head>
+<body>
+ <button class="undo" href="#">Undo</button>
+ <button class="redo" href="#">Redo</button>
+ <button class="save" href="#">Save<span class="dirty">*</span></button>
+ <form>
+ <textarea id="text"></textarea>
+ </form>
+</body>
+</html>
View
2  undo.js
@@ -70,7 +70,7 @@ extend(Undo.Stack.prototype, {
},
redo: function() {
this.stackPosition++;
- this.commands[this.stackPosition].execute();
+ this.commands[this.stackPosition].redo();
this.changed();
},
canRedo: function() {
Please sign in to comment.
Something went wrong with that request. Please try again.