Undo / redo

Issue #5 new
Isaiah Odhner created an issue
  • If you already have serializing to files, you can reuse that serialization.
  • You should support Ctrl+Z for undo, and both Ctrl+Shift+Z and Ctrl+Y for redo - these are both very common shortcuts a user might try (Ctrl+Shift+Z is easier to type, Ctrl+Y is more of a Windows thing)
  • Here’s a simple model of undo history for reference:
undos = [];
redos = [];

function undo() {
    if (undos.length > 0) {
        redos.push(saveState());
        loadState(undos.pop());
    }
}

function redo() {
    if (redos.length > 0) {
        undos.push(saveState());
        loadState(redos.pop());
    }
}

function undoable() {
    undos.push(saveState());

    // DESTROY any redos (as is the common practice...)
    redos = [];
}

// then anywhere the document state is changed, dragging a node or anything,
// just call undoable() beforehand

and a more detailed example that might turn into an article someday: https://gist.github.com/1j01/bd2329547904b97abc52fd5e76b008d8

(please don’t take my over-explanation to mean i think you couldn’t do it all yourself, i just wanna make it easy/straightforward/approachable 🙂 (and i enjoy slowly refining my “simple undo example”))

Comments (3)

  1. Lionel Barret repo owner

    @Isaiah Odhner (late) but quick question : do you want the move of the nodes to be included in the undo. At first glance it looks natural but it is also quite orthogonal to the edition of values. What do you think ?

  2. Log in to comment