scratch – Blame information for rev 84
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
84 | office | 1 | # simple-undo |
2 | |||
3 | [![Build Status](https://travis-ci.org/mattjmattj/simple-undo.svg)](https://travis-ci.org/mattjmattj/simple-undo) |
||
4 | |||
5 | simple-undo is a very basic javascript undo/redo stack for managing histories of basically anything. |
||
6 | |||
7 | Initially created to help fixing an issue in [drawingboard.js](https://github.com/Leimi/drawingboard.js/issues/29). |
||
8 | |||
9 | ## Installation |
||
10 | |||
11 | ### Bower |
||
12 | |||
13 | `bower install simple-undo`. |
||
14 | |||
15 | ### NPM |
||
16 | |||
17 | `npm install simple-undo` |
||
18 | |||
19 | ## Usage |
||
20 | |||
21 | 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. |
||
22 | |||
23 | If you are using simple-undo as a nodejs module, just do `var SimpleUndo = require('simple-undo');` |
||
24 | |||
25 | ```javascript |
||
26 | |||
27 | var myObject = {}; |
||
28 | |||
29 | function myObjectSerializer(done) { |
||
30 | done(JSON.stringify(myObject)); |
||
31 | } |
||
32 | |||
33 | function myObjectUnserializer(serialized) { |
||
34 | myObject = JSON.parse(serialized); |
||
35 | } |
||
36 | |||
37 | var history = new SimpleUndo({ |
||
38 | maxLength: 10, |
||
39 | provider: myObjectSerializer |
||
40 | }); |
||
41 | |||
42 | myObject.foo = 'bar'; |
||
43 | history.save(); |
||
44 | myObject.foo = 'baz'; |
||
45 | history.save(); |
||
46 | |||
47 | history.undo(myObjectUnserializer); |
||
48 | // myObject.foo == 'bar' |
||
49 | history.redo(myObjectUnserializer); |
||
50 | // myObject.foo == 'baz' |
||
51 | |||
52 | ``` |
||
53 | |||
54 | Another example is available on the [GitHub page of the project](http://mattjmattj.github.io/simple-undo/) |
||
55 | |||
56 | ## Options and API |
||
57 | |||
58 | Accepted options are |
||
59 | |||
60 | * `provider` : required. a function to call on `save`, which should provide the current state of the historized object through the given `done` callback |
||
61 | * `maxLength` : the maximum number of items in history |
||
62 | * `opUpdate` : a function to call to notify of changes in history. Will be called on `save`, `undo`, `redo` and `clear` |
||
63 | |||
64 | SimpleUndo |
||
65 | |||
66 | * `initialize (initialState)` : registers the initial state of the managed object. If not call the default initial state is NULL |
||
67 | * `save ()` : calls the `provider` and registers whatever it gives |
||
68 | * `undo (callback)` : calls the callback with the previous state of the managed object in history |
||
69 | * `redo (callback)` : calls the callback with the next state of the managed object in history |
||
70 | * `clear ()` : clears the whole history, except the inital state if any |
||
71 | * `count ()` : returns the count of elements in history, apart from the inital state |
||
72 | |||
73 | ## License |
||
74 | |||
75 | simple-undo is licensed under the terms of the [Beerware license](LICENSE). |
||
76 | |||
77 | 2014 - Matthias Jouan |