GithubHelp home page GithubHelp logo

colinfrick / javascript-undo-manager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arthurclemens/javascript-undo-manager

0.0 2.0 0.0 283 KB

Simple Javascript undo and redo independent of other libraries

License: Other

CSS 2.12% JavaScript 97.88%

javascript-undo-manager's Introduction

JavaScript Undo Manager

Simple undo manager to provide undo and redo actions in your JavaScript application.

Demo

See Undo Manager with canvas drawing.

Background

Each action the user does (typing a character, moving an object) must be undone AND recreated. We combine this destruction and recreation as one "command" and put it on the undo stack:

var undoManager = new UndoManager();
undoManager.add({
    undo: function() {
        // ...
    },
    redo: function() {
        // ...
    }
});

Note that you are responsible for the initial creation; Undo Manager only bothers with destruction and recreation.

Methods

undoManager.undo();

Performs the undo action.

undoManager.redo();

Performs the redo action.

undoManager.clear();

Clears all stored states.

var hasUndo = undoManager.hasUndo();

Tests if any undo actions exist.

var hasRedo = undoManager.hasRedo();

Tests if any redo actions exist.

undoManager.setCallback(myCallback);

Get notified on changes.

Example

var undoManager = new UndoManager(),
    people = {},
    addPerson,
    removePerson,
    createPerson;        

addPerson = function(id, name) {
    people[id] = name;
};

removePerson = function(id) {
    delete people[id];
};

createPerson = function (id, name) {

    // initial storage
    addPerson(id, name);

    // make undo-able
    undoManager.add({
        undo: function() {
            removePerson(id)
        },
        redo: function() {
            addPerson(id, name);
        }
    });
}

createPerson(101, "John");
createPerson(102, "Mary");

console.log("people", people);
// people Object {101: "John", 102: "Mary"} 

undoManager.undo();
console.log("people", people);
// people Object {101: "John"} 

undoManager.undo();
console.log("people", people);
// people Object {} 

undoManager.redo();
console.log("people", people);
// people Object {101: "John"}

Loading with RequireJS

If you are using RequireJS, you need to use the shim config parameter.

Assuming require.js and domReady.js are located in js/extern, the index.html load call would be:

<script src="js/extern/require.js" data-main="js/demo"></script>

And demo.js would look like this:

requirejs.config({
    baseUrl: "js",
    paths: {
        domReady: "extern/domReady",
        app: "../demo",
        undomanager: "../../js/undomanager",
        circledrawer: "circledrawer"
    },
    shim: {
        "undomanager": {
            exports: "UndoManager"
        },
        "circledrawer": {
            exports: "CircleDrawer"
        }
    }
});

require(["domReady", "undomanager", "circledrawer"], function(domReady, UndoManager, CircleDrawer) {
    "use strict";

    var undoManager,
        circleDrawer,
        btnUndo,
        btnRedo,
        btnClear;

    undoManager = new UndoManager();
    circleDrawer = new CircleDrawer("view", undoManager);
    
    // etcetera
});

javascript-undo-manager's People

Contributors

arthurclemens avatar stannedelchev avatar ynonp 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.