undo-stack

1.0.0 • Public • Published

undo-stack

A simple undo/redo library with optional undo chunking (batch undos) and a pluggable logger with nice output for your viewing pleasure.

NPM

build status

EXAMPLE

var History = require('undo-stack');
 
var history = new History({
    logger: console.log,
    limit: 200
});
 
var x = 5;
 
// To add a history step
history.add({
    name: 'plus 3',
    redo: function () { x += 3; },
    undo: function () { x -= 3; }
}); // x is still 5
 
// To add and call use .do()
// shortcut for .add(stackStepObj, true)
history.do({
    name: 'call on add',
    redo: function () { x += 3; },
    undo: function () { x -= 3; }
}); // x is 8 now
 
history
    .undo() // x is 5
    .redo(); // x is 8 again
 
// Chunks
history.startChunk('plus 7')
    .do({
        name: 'plus 2',
        redo: function () {+= 2;},
        undo: function () {-= 2;}
    }) // x is 10
    .do({
        name: 'plus 5',
        redo: function () {+= 5;},
        undo: function () {-= 5;}
    }) // x is 15
    .endChunk();
 
history
    .undo() // x is 8
    .redo(); // x is 15 again

LICENSE

MIT

Dependents (0)

Package Sidebar

Install

npm i undo-stack

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • willhoag