simple-undo
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.
Installation
Bower
bower install simple-undo
.
NPM
npm install simple-undo
Usage
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 = {}; { ;} { myObject = JSON;} var history = maxLength: 10 provider: myObjectSerializer; myObjectfoo = 'bar';history;myObjectfoo = 'baz';history; history;// myObject.foo == 'bar'history;// myObject.foo == 'baz'
Another example is available on the GitHub page of the project
Options and API
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
License
simple-undo is licensed under the terms of the Beerware license.
2014 - Matthias Jouan