Giter Club home page Giter Club logo

undo-manager's Introduction

undo-manager

A JavaScript object to manage your undo/redo stack.

Installation

Install with component:

$ component install graouts/undo-manager

Or standalone using one of the releases.

Usage

First, create an instance:

var UndoManager = require("undo-manager");
var manager = new UndoManager();

And then interact with the undo stack with calls to:

manager.undo();

and…

manager.redo();

When you're calling code that would enter a state you would like to be able to undo, you need to call one of the three registration methods, registerPropertyUndo, registerMethodUndo and registerFunctionUndo. For instance, say you're within a property setter:

Object.defineProperty(anObject, "foo", {
    set: function(foo)
    {
        manager.registerPropertyUndo(this, "foo", this._foo);
        this._foo = foo;
    }
});

… or a function that sets a property:

MyClass.prototype.setFoo = function(foo)
{
    manager.registerMethodUndo(this, this.setFoo, this._foo);
    this._foo = foo;
}

… or simple want to revert a method call:

function deleteFile(file)
{
    manager.registerFunctionUndo(function() {
        addFile(file);
    });
    // … function code to delete file.
}

You can also group a series of undo registrations into groups such that all registered actions are undone and redone together. For instance, say you implement some code to drag an element on screen, you would first manager.beginGroup() as you process the mousedown event, and then manager.endGroup() as you process the mouseup event. Undo groups support nesting.

API

#####.canUndo

A bool value indicating whether the undo stack has any registered action on it to undo.

#####.canRedo

A bool value indicating whether the undo stack has any registered action on it to redo.

#####.undo()

Closes the top-level undo group if necessary and performs the next logical undo operation.

#####.redo()

Performs the next logical redo operation.

#####.beginGroup()

Marks the beginning of an undo group. All individual undo operations before a subsequent call to endGroup() call are grouped together and reversed by a later undo() call.

#####.endGroup()

Marks the end of an undo group. All individual undo operations back to the matching beginGroup() call are grouped together and reversed by a later undo() call.

#####.registerPropertyUndo(target, propertyName, value)

Records a single undo operation so that when an undo is performed the specified propertyName on the target is reset to the provided value.

#####.registerMethodUndo(target, method, [, arg1[, arg2[, ...]]])

Records a single undo operation so that when an undo is performed the specified method is called on the target as the this object and any additional arguments are passed during the undo operation.

#####.registerFunctionUndo(func)

Records a single undo operation so that when an undo is performed, the provided function is called.

undo-manager's People

Contributors

graouts avatar

Stargazers

Jason S. avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.