state-rewind

5 min read Original article ↗

JS StateRewind Build Status

Simple state management with the ability to undo, redo & squash history.

Want to skip to a full working example? https://jsfiddle.net/p4rebqw2/15/


Install

Usage

Init a state object

const state = new StateRewind;

init with console logs for debug

const state = new StateRewind({ log: true });

Set the state

or exec to run/execute a function at the same time that can later be undone or redone

state.exec(5, function () { console.log('foward'); }, function () { console.log('backward'); });

State can be anything, strings, numbers, arrays, objects, anything, e.g. (works with set & exec)

state.set({

    x: "test",

    y: [1, 2, 3]

});

Get the current state

Undo/rewind state change

& then redo/fast-forward current state

You can also check if undo or redo are available with:

state.canUndo(); 

state.canRedo(); 

Get all recorded state changes

Callbacks

Optionally listen in for changes to the state:

state.onChange(function () { ... });

Each set/exec can pass a forward and backward callback e.g. to handle the changes visually etc.

You can also set a default function that's given a direction of either forward or backward and the change where you can instruct how it should handle this. e.g.

state.setDefaultForwardBackwardCallback(function (direction, change) {

    if (direction == 'forward') {

    } else if (direction == 'backward') {

    }

});

Editing history

Squash history, e.g. to remove duplicates or squash down similar objects such as changes to text, if the same elements text changes multiple times you might want to squash that down to just the latest change

state.squash(function (prev, next) {

    return prev == next;

});

state.squash(function (prev, next) {

    return prev.selector == next.selector && prev.type == next.type;

});

or just run squash against the last set value

state.squashLast(function (prev, next) {

    return prev == next;

});

The squash functions can both also take a 2nd callback to modify the data as it squashes down, e.g. if these are text changes to the same thing, you'd probably want the original "from text", but the latest "to text".

state.squashLast(function (prev, next) {

    return prev.selector == next.selector && prev.type == next.type;

}, function (prev, next) {

    next.change.from = prev.change.from; 

    return next;

});

Remove specific entries from the history.

Clear/reset all history.

Starting from stored data

You can load in initial data with the load() command:

This can be used in combination with setDefaultForwardBackwardCallback to have it auto run the callbacks such as visually changing the page to adapt to the loaded history.

state.setDefaultForwardBackwardCallback(function (direction, change) { ... });

state.load([{ text: "x" }, { text: "y" }], { exec: true });

Initial locked states

You can also start the state with an initial value. By default it starts as undefined but you can set initialState as an option when creating the StateRewind instance. This lets you have a default state available via get() that cannot be undone and will always show at the start of the getAll() array.

See the test.js file for a unit test showing this in use & many other example uses.


Tips

Keyboard shortcuts

You may want to setup keyboard shortcuts for undo & redo.

This could be done like so, for ctrl+z (undo) & ctrl+y (redo)

document.addEventListener('keydown', function (e) {

    if (e.ctrlKey || e.metaKey) { 

        if (e.key == 'y' || (e.key == 'Z' && e.shiftKey)) {

            state.redo();

        } else if (e.key == 'z') {

            state.undo();

        }

    }

});

Debounce

Depending on how you're saving data, if it's user based such as on input, you may want to use this with a debounce function to not save constantly, find out more here or here's a package for debounce on npm.

Chaining

Almost all functions are chainable (except get, getAll, canUndo and canRedo).

E.g.

state.set(3).set(5).undo().get()

Change events

You may want to hook into the change event of the state, we expose a onChange() function for this.

This is especially useful for setting up undo and redo buttons, e.g.

<button id="undo-btn" disabled>Undo</button>

<button id="redo-btn" disabled>Redo</button>

let $undoBtn = document.querySelector('#undo-btn');

let $redoBtn = document.querySelector('#redo-btn');

state.onChange(function () {

    $undoBtn.disabled = ! state.canUndo();

    $redoBtn.disabled = ! state.canRedo();

});

$undoBtn.addEventListener('click', function (e) {

    e.preventDefault();

    state.undo();

});

$redoBtn.addEventListener('click', function (e) {

    e.preventDefault();

    state.redo();

});


Example workflow

E.g. here's an example where you could track changes to elements on a page with timestamps:

const state = new StateRewind;

state.exec({

    timestamp: (new Date).toISOString(),

    selector: '#el span',

    type: 'text',

    change: {

        from: 'ABC',

        to: 'XYZ'

    }

}, function () { console.log('A'); }, function () { console.log('B'); });

state.exec({

    timestamp: (new Date).toISOString(),

    selector: '.another strong',

    type: 'text',

    change: {

        from: 'Hello world',

        to: 'one thing'

    }

}, function () { console.log('C'); }, function () { console.log('D'); });

state.undo();

state.redo();

state.exec({

    timestamp: (new Date).toISOString(),

    selector: '.another strong',

    type: 'text',

    change: {

        from: 'one thing',

        to: 'Something else!'

    }

}, function () { console.log('E'); }, function () { console.log('F'); });

state.squashLast(function (prev, next) {

    return prev.selector == next.selector && prev.type == next.type;

});

state.getAll(); 


Local development

Run tests

Publish new version

Update the package.json's version, commit, push and then:

Contributing

Please run the tests locally and add new tests for new features/options added. Thank you.