Index: trunk/extensions/VisualEditor/modules/es/models/es.SurfaceModel.js |
— | — | @@ -22,8 +22,8 @@ |
23 | 23 | this.lengthDifferenceLimit = 24; |
24 | 24 | |
25 | 25 | // DEBUG don't commit |
26 | | - //var _this = this; |
27 | | - //this.addListener( 'transact', function() { console.log( _this.history ); } ); |
| 26 | + var _this = this; |
| 27 | + this.addListener( 'transact', function() { console.log( _this.history ); } ); |
28 | 28 | }; |
29 | 29 | |
30 | 30 | /* Methods */ |
— | — | @@ -57,22 +57,17 @@ |
58 | 58 | * |
59 | 59 | * @method |
60 | 60 | * @param {es.Range} selection |
61 | | - * @param {Boolean} combine Whether to prevent this transaction from causing a history push |
| 61 | + * @param {Boolean} isManual Whether this selection was the result of a user action, and thus should be recorded in history...? |
62 | 62 | */ |
63 | 63 | es.SurfaceModel.prototype.select = function( selection, isManual ) { |
64 | 64 | selection.normalize(); |
65 | 65 | if ( |
66 | | - // First selection |
67 | | - !this.selection || |
68 | | - // From changed |
69 | | - selection.from !== this.selection.from || |
70 | | - // To changed |
71 | | - selection.to !== this.selection.to |
| 66 | + ( ! this.selection ) || ( ! this.selection.equals( selection ) ) |
72 | 67 | ) { |
| 68 | + // check if the last thing is a selection, if so, swap it. |
73 | 69 | this.selection = selection; |
74 | 70 | if ( isManual ) { |
75 | | - // check if the last thing is a selection, if so, swap it. |
76 | | - this.pushSelection( selection ); |
| 71 | + this.historyPush( selection ); |
77 | 72 | } |
78 | 73 | this.emit( 'select', this.selection.clone() ); |
79 | 74 | } |
— | — | @@ -83,8 +78,26 @@ |
84 | 79 | * For the history, selections are just markers, so we don't want to record many of them in a row. |
85 | 80 | * |
86 | 81 | * @method |
87 | | - * @param {es.Range} selection |
| 82 | + * @param {es.Range|es.Transaction} historyItem |
88 | 83 | */ |
| 84 | + |
| 85 | + |
| 86 | +/** |
| 87 | + * TODO docs |
| 88 | + */ |
| 89 | +es.SurfaceModel.prototype.historyPush = function ( historyItem ) { |
| 90 | + // truncate anything past our current history position |
| 91 | + this.history.splice( this.historyIndex ); |
| 92 | + |
| 93 | + // push the next item. Could be combined with above splice given sufficient cleverness |
| 94 | + this.history.push( historyItem ); |
| 95 | + |
| 96 | + // get ready to insert at the end |
| 97 | + this.historyIndex = this.history.length; |
| 98 | + |
| 99 | +}; |
| 100 | + |
| 101 | +/* |
89 | 102 | es.SurfaceModel.prototype.pushSelection = function( selection ) { |
90 | 103 | if ( this.history[ this.history.length - 1 ] instanceof es.Range ) { |
91 | 104 | this.history[ this.history.length - 1 ] = selection; |
— | — | @@ -92,6 +105,7 @@ |
93 | 106 | this.history.push( selection ); |
94 | 107 | } |
95 | 108 | }; |
| 109 | +*/ |
96 | 110 | |
97 | 111 | /** |
98 | 112 | * Applies a series of transactions to the content data. |
— | — | @@ -122,14 +136,12 @@ |
123 | 137 | ( Math.abs( this.currentLengthDifference ) > this.lengthDifferenceLimit ) |
124 | 138 | ) |
125 | 139 | ) { |
126 | | - this.currentLengthDifference = d; |
127 | | - this.history.push( this.selection ); |
128 | | - } else { |
129 | | - this.currentLengthDifference += d; |
| 140 | + this.currentLengthDifference = 0; |
| 141 | + this.historyPush( this.selection ); |
130 | 142 | } |
131 | 143 | |
132 | | - this.history.push( transaction ); |
133 | | - |
| 144 | + this.currentLengthDifference += d; |
| 145 | + this.historyPush( transaction ); |
134 | 146 | this.emit( 'transact', transaction ); |
135 | 147 | }; |
136 | 148 | |
— | — | @@ -138,40 +150,38 @@ |
139 | 151 | * Reverses one or more history items. |
140 | 152 | * |
141 | 153 | * @method |
142 | | - * @param {Integer} Number of history items to roll back |
| 154 | + * @param {Integer} n Number of history items to roll back |
143 | 155 | */ |
144 | | -es.SurfaceModel.prototype.undo = function( statesToUndo ) { |
| 156 | +es.SurfaceModel.prototype.undo = function( n ) { |
145 | 157 | |
| 158 | + console.log( this.history ); |
146 | 159 | console.log( 'about to undo...' ); |
147 | | - console.log( this.states ); |
148 | | - console.log( 'currentState: ' + this.currentState ); |
149 | | - console.log( 'currentStateIndex: ' + this.currentStateIndex ); |
| 160 | + console.log( "historyIndex: " + this.historyIndex ); |
150 | 161 | |
151 | 162 | lengthDifference = 0; |
| 163 | + var finalSelection = null; |
152 | 164 | |
153 | | - while ( statesToUndo ) { |
154 | | - statesToUndo--; |
| 165 | + while ( n ) { |
| 166 | + n--; |
155 | 167 | |
156 | | - if ( this.currentState.length ) { |
157 | | - for (var i = this.currentState.length - 1; i >= 0; i-- ) { |
158 | | - lengthDifference += this.currentState[i].getLengthDifference(); |
159 | | - this.doc.rollback( this.currentState[i] ); |
| 168 | + if ( this.history.length ) { |
| 169 | + for (var i = this.history.length - 1; i >= 0; i-- ) { |
| 170 | + this.historyIndex = i; |
| 171 | + if ( this.history[i] instanceof es.Range ) { |
| 172 | + finalSelection = this.history[i]; |
| 173 | + break; |
| 174 | + } else { |
| 175 | + this.doc.rollback( this.history[i] ); |
| 176 | + } |
160 | 177 | } |
161 | 178 | this.emit( 'undo', this.currentState ); |
162 | 179 | } |
163 | | - |
164 | | - // do we also want all the effects of initializeState? currentStateDistance to be 0, currentStateLengthDifference? |
165 | | - if ( this.currentStateIndex > 0 ) { |
166 | | - this.initializeState( this.currentStateIndex - 1 ); |
167 | | - } |
168 | 180 | } |
169 | 181 | |
170 | 182 | console.log( 'after undo...' ); |
171 | | - console.log( this.states ); |
172 | | - console.log( 'currentState: ' + this.currentState ); |
173 | | - console.log( 'currentStateIndex: ' + this.currentStateIndex ); |
| 183 | + console.log( "historyIndex: " + this.historyIndex ); |
174 | 184 | |
175 | | - // TODO - make the appropriate selection now |
| 185 | + this.select( finalSelection ); |
176 | 186 | }; |
177 | 187 | |
178 | 188 | /** |
Index: trunk/extensions/VisualEditor/modules/es/es.Range.js |
— | — | @@ -79,3 +79,14 @@ |
80 | 80 | this.end = this.from; |
81 | 81 | } |
82 | 82 | }; |
| 83 | + |
| 84 | +/** |
| 85 | + * Determines if two Ranges are equal. Direction counts. |
| 86 | + * |
| 87 | + * @method |
| 88 | + * @param {es.Range} |
| 89 | + * @returns {Boolean} |
| 90 | + */ |
| 91 | +es.Range.prototype.equals = function( other ) { |
| 92 | + return this.from === other.from && this.to === other.to; |
| 93 | +}; |