UndoRedo.js
Summary
Allows Mozile to store a stack of past document states, and restore them on command.
Version: 0.7
Author: James A. Overton
MozileMediator.prototype.undoStack = new Array();
MozileMediator.prototype.undoCounter = 0;
MozileMediator.prototype.storeState = function(command) {
var f = new Array();
f["File"] = "UndoRedo/UndoRedo.js";
f["Function"] = "Mozile.storeState()";
this.debug(f,1,"Storing document");
if(this.changesSaved == true) {
this.status(f,1,"Editing");
this.changesSaved = false;
}
this.keyCounter = 0;
var selection = mozile.getSelection();
var states = new Array();
var editorArray;
this.debug(f,1,"Number of editors: " + this.getEditors().length);
for(var i=0; i<this.getEditors().length; i++) {
editorArray = new Array(this.getEditors()[i], this.getEditors()[i].cloneNode(true) );
states.push(editorArray);
}
var selectionArray = this.storeSelection();
if(this.undoCounter < this.undoStack.length - 1) {
this.undoStack = this.undoStack.slice(0,this.undoCounter+1);
}
var stateArray = new Array(command, states, selectionArray)
this.undoStack.push(stateArray);
this.undoCounter = this.undoStack.length - 1;
return true;
}
MozileMediator.prototype.restoreState = function(index) {
var f = new Array();
f["File"] = "UndoRedo/UndoRedo.js";
f["Function"] = "Mozile.restoreState()";
this.debug(f,1,"Restoring state "+ index);
var command = this.undoStack[index][0];
var states = this.undoStack[index][1];
var selectionArray = this.undoStack[index][2];
var i,j=0;
var editor, editorCopy;
for(j=0; j<states.length; j++) {
editor = states[j][0];
editorCopy = states[j][1];
while(editor.childNodes.length) {
editor.removeChild(editor.firstChild);
}
var children = editorCopy.childNodes;
for(i=0; i<children.length; i++) {
editor.appendChild(children[i].cloneNode(true));
}
}
this.restoreSelection(selectionArray);
window.getSelection().collapseToEnd();
return true;
}
mozile.getModule("UndoRedo").init = function() {
var mozileUndo = mozile.getCommandList().createCommand("MozileCommand: id=Mozile-Undo, label=Undo, tooltip='Undo the previous action', accelerator='Command-Z', image='"+mozile.getRoot()+"images/undo.png'");
mozileUndo.execute = function() {
if(mozile.undoCounter <= 0) {
return false;
}
else {
mozile.undoCounter--;
mozile.restoreState(mozile.undoCounter);
return true;
}
}
var mozileRedo = mozile.getCommandList().createCommand("MozileCommand: id=Mozile-Redo, label=Redo, tooltip='Redo the next action', accelerator='Command-Shift-Z', image='"+mozile.getRoot()+"images/redo.png'");
mozileRedo.execute = function() {
if(mozile.undoCounter >= mozile.undoStack.length - 1) {
return false;
}
else {
mozile.undoCounter++;
mozile.restoreState(mozile.undoCounter);
return true;
}
}
mozile.getCommandList().createCommand("MozileCommandSeparator: id=Mozile-UndoSeparator");
}
if(mozile.getCommandList()) {
mozile.getModule("UndoRedo").init();
}
Documentation generated by
JSDoc on Thu Feb 16 20:20:37 2006