browser/devtools/shared/undo.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * A simple undo stack manager.
michael@0 9 *
michael@0 10 * Actions are added along with the necessary code to
michael@0 11 * reverse the action.
michael@0 12 *
michael@0 13 * @param function aChange Called whenever the size or position
michael@0 14 * of the undo stack changes, to use for updating undo-related
michael@0 15 * UI.
michael@0 16 * @param integer aMaxUndo Maximum number of undo steps.
michael@0 17 * defaults to 50.
michael@0 18 */
michael@0 19 function UndoStack(aMaxUndo)
michael@0 20 {
michael@0 21 this.maxUndo = aMaxUndo || 50;
michael@0 22 this._stack = [];
michael@0 23 }
michael@0 24
michael@0 25 exports.UndoStack = UndoStack;
michael@0 26
michael@0 27 UndoStack.prototype = {
michael@0 28 // Current index into the undo stack. Is positioned after the last
michael@0 29 // currently-applied change.
michael@0 30 _index: 0,
michael@0 31
michael@0 32 // The current batch depth (see startBatch() for details)
michael@0 33 _batchDepth: 0,
michael@0 34
michael@0 35 destroy: function Undo_destroy()
michael@0 36 {
michael@0 37 this.uninstallController();
michael@0 38 delete this._stack;
michael@0 39 },
michael@0 40
michael@0 41 /**
michael@0 42 * Start a collection of related changes. Changes will be batched
michael@0 43 * together into one undo/redo item until endBatch() is called.
michael@0 44 *
michael@0 45 * Batches can be nested, in which case the outer batch will contain
michael@0 46 * all items from the inner batches. This allows larger user
michael@0 47 * actions made up of a collection of smaller actions to be
michael@0 48 * undone as a single action.
michael@0 49 */
michael@0 50 startBatch: function Undo_startBatch()
michael@0 51 {
michael@0 52 if (this._batchDepth++ === 0) {
michael@0 53 this._batch = [];
michael@0 54 }
michael@0 55 },
michael@0 56
michael@0 57 /**
michael@0 58 * End a batch of related changes, performing its action and adding
michael@0 59 * it to the undo stack.
michael@0 60 */
michael@0 61 endBatch: function Undo_endBatch()
michael@0 62 {
michael@0 63 if (--this._batchDepth > 0) {
michael@0 64 return;
michael@0 65 }
michael@0 66
michael@0 67 // Cut off the end of the undo stack at the current index,
michael@0 68 // and the beginning to prevent a stack larger than maxUndo.
michael@0 69 let start = Math.max((this._index + 1) - this.maxUndo, 0);
michael@0 70 this._stack = this._stack.slice(start, this._index);
michael@0 71
michael@0 72 let batch = this._batch;
michael@0 73 delete this._batch;
michael@0 74 let entry = {
michael@0 75 do: function() {
michael@0 76 for (let item of batch) {
michael@0 77 item.do();
michael@0 78 }
michael@0 79 },
michael@0 80 undo: function() {
michael@0 81 for (let i = batch.length - 1; i >= 0; i--) {
michael@0 82 batch[i].undo();
michael@0 83 }
michael@0 84 }
michael@0 85 };
michael@0 86 this._stack.push(entry);
michael@0 87 this._index = this._stack.length;
michael@0 88 entry.do();
michael@0 89 this._change();
michael@0 90 },
michael@0 91
michael@0 92 /**
michael@0 93 * Perform an action, adding it to the undo stack.
michael@0 94 *
michael@0 95 * @param function aDo Called to perform the action.
michael@0 96 * @param function aUndo Called to reverse the action.
michael@0 97 */
michael@0 98 do: function Undo_do(aDo, aUndo) {
michael@0 99 this.startBatch();
michael@0 100 this._batch.push({ do: aDo, undo: aUndo });
michael@0 101 this.endBatch();
michael@0 102 },
michael@0 103
michael@0 104 /*
michael@0 105 * Returns true if undo() will do anything.
michael@0 106 */
michael@0 107 canUndo: function Undo_canUndo()
michael@0 108 {
michael@0 109 return this._index > 0;
michael@0 110 },
michael@0 111
michael@0 112 /**
michael@0 113 * Undo the top of the undo stack.
michael@0 114 *
michael@0 115 * @return true if an action was undone.
michael@0 116 */
michael@0 117 undo: function Undo_canUndo()
michael@0 118 {
michael@0 119 if (!this.canUndo()) {
michael@0 120 return false;
michael@0 121 }
michael@0 122 this._stack[--this._index].undo();
michael@0 123 this._change();
michael@0 124 return true;
michael@0 125 },
michael@0 126
michael@0 127 /**
michael@0 128 * Returns true if redo() will do anything.
michael@0 129 */
michael@0 130 canRedo: function Undo_canRedo()
michael@0 131 {
michael@0 132 return this._stack.length > this._index;
michael@0 133 },
michael@0 134
michael@0 135 /**
michael@0 136 * Redo the most recently undone action.
michael@0 137 *
michael@0 138 * @return true if an action was redone.
michael@0 139 */
michael@0 140 redo: function Undo_canRedo()
michael@0 141 {
michael@0 142 if (!this.canRedo()) {
michael@0 143 return false;
michael@0 144 }
michael@0 145 this._stack[this._index++].do();
michael@0 146 this._change();
michael@0 147 return true;
michael@0 148 },
michael@0 149
michael@0 150 _change: function Undo__change()
michael@0 151 {
michael@0 152 if (this._controllerWindow) {
michael@0 153 this._controllerWindow.goUpdateCommand("cmd_undo");
michael@0 154 this._controllerWindow.goUpdateCommand("cmd_redo");
michael@0 155 }
michael@0 156 },
michael@0 157
michael@0 158 /**
michael@0 159 * ViewController implementation for undo/redo.
michael@0 160 */
michael@0 161
michael@0 162 /**
michael@0 163 * Install this object as a command controller.
michael@0 164 */
michael@0 165 installController: function Undo_installController(aControllerWindow)
michael@0 166 {
michael@0 167 this._controllerWindow = aControllerWindow;
michael@0 168 aControllerWindow.controllers.appendController(this);
michael@0 169 },
michael@0 170
michael@0 171 /**
michael@0 172 * Uninstall this object from the command controller.
michael@0 173 */
michael@0 174 uninstallController: function Undo_uninstallController()
michael@0 175 {
michael@0 176 if (!this._controllerWindow) {
michael@0 177 return;
michael@0 178 }
michael@0 179 this._controllerWindow.controllers.removeController(this);
michael@0 180 },
michael@0 181
michael@0 182 supportsCommand: function Undo_supportsCommand(aCommand)
michael@0 183 {
michael@0 184 return (aCommand == "cmd_undo" ||
michael@0 185 aCommand == "cmd_redo");
michael@0 186 },
michael@0 187
michael@0 188 isCommandEnabled: function Undo_isCommandEnabled(aCommand)
michael@0 189 {
michael@0 190 switch(aCommand) {
michael@0 191 case "cmd_undo": return this.canUndo();
michael@0 192 case "cmd_redo": return this.canRedo();
michael@0 193 };
michael@0 194 return false;
michael@0 195 },
michael@0 196
michael@0 197 doCommand: function Undo_doCommand(aCommand)
michael@0 198 {
michael@0 199 switch(aCommand) {
michael@0 200 case "cmd_undo": return this.undo();
michael@0 201 case "cmd_redo": return this.redo();
michael@0 202 }
michael@0 203 },
michael@0 204
michael@0 205 onEvent: function Undo_onEvent(aEvent) {},
michael@0 206 }

mercurial