simple-undo is a very basic javascript undo/redo stack for managing histories of basically anything.
Initially created to help fixing an issue in drawingboard.js.
bower install simple-undo
.
npm install simple-undo
If you are using simple-undo in the browser, a SimpleUndo object is exported in window
after including simple-undo in your page, so it is very easy to use.
If you are using simple-undo as a nodejs module, just do var SimpleUndo = require('simple-undo');
var myObject = {};
function myObjectSerializer(done) {
done(JSON.stringify(myObject));
}
function myObjectUnserializer(serialized) {
myObject = JSON.parse(serialized);
}
var history = new SimpleUndo({
maxLength: 10,
provider: myObjectSerializer
});
myObject.foo = 'bar';
history.save();
myObject.foo = 'baz';
history.save();
history.undo(myObjectUnserializer);
// myObject.foo == 'bar'
history.redo(myObjectUnserializer);
// myObject.foo == 'baz'
Another example is available on the GitHub page of the project
Accepted options are
provider
: required. a function to call onsave
, which should provide the current state of the historized object through the givendone
callbackmaxLength
: the maximum number of items in historyonUpdate
: a function to call to notify of changes in history. Will be called onsave
,undo
,redo
andclear
SimpleUndo
initialize (initialState)
: registers the initial state of the managed object. If not call the default initial state is NULLsave ()
: calls theprovider
and registers whatever it givesundo (callback)
: calls the callback with the previous state of the managed object in historyredo (callback)
: calls the callback with the next state of the managed object in historyclear ()
: clears the whole history, except the inital state if anycount ()
: returns the count of elements in history, apart from the inital state
simple-undo is licensed under the terms of the Beerware license.
2014 - Matthias Jouan