r105468 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105467‎ | r105468 | r105469 >
Date:22:28, 7 December 2011
Author:neilk
Status:deferred
Tags:
Comment:
undo almost working, still anomalies in selects (particularly first)
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/es.Range.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/models/es.SurfaceModel.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/models/es.SurfaceModel.js
@@ -22,8 +22,8 @@
2323 this.lengthDifferenceLimit = 24;
2424
2525 // 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 ); } );
2828 };
2929
3030 /* Methods */
@@ -57,22 +57,17 @@
5858 *
5959 * @method
6060 * @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...?
6262 */
6363 es.SurfaceModel.prototype.select = function( selection, isManual ) {
6464 selection.normalize();
6565 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 ) )
7267 ) {
 68+ // check if the last thing is a selection, if so, swap it.
7369 this.selection = selection;
7470 if ( isManual ) {
75 - // check if the last thing is a selection, if so, swap it.
76 - this.pushSelection( selection );
 71+ this.historyPush( selection );
7772 }
7873 this.emit( 'select', this.selection.clone() );
7974 }
@@ -83,8 +78,26 @@
8479 * For the history, selections are just markers, so we don't want to record many of them in a row.
8580 *
8681 * @method
87 - * @param {es.Range} selection
 82+ * @param {es.Range|es.Transaction} historyItem
8883 */
 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+/*
89102 es.SurfaceModel.prototype.pushSelection = function( selection ) {
90103 if ( this.history[ this.history.length - 1 ] instanceof es.Range ) {
91104 this.history[ this.history.length - 1 ] = selection;
@@ -92,6 +105,7 @@
93106 this.history.push( selection );
94107 }
95108 };
 109+*/
96110
97111 /**
98112 * Applies a series of transactions to the content data.
@@ -122,14 +136,12 @@
123137 ( Math.abs( this.currentLengthDifference ) > this.lengthDifferenceLimit )
124138 )
125139 ) {
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 );
130142 }
131143
132 - this.history.push( transaction );
133 -
 144+ this.currentLengthDifference += d;
 145+ this.historyPush( transaction );
134146 this.emit( 'transact', transaction );
135147 };
136148
@@ -138,40 +150,38 @@
139151 * Reverses one or more history items.
140152 *
141153 * @method
142 - * @param {Integer} Number of history items to roll back
 154+ * @param {Integer} n Number of history items to roll back
143155 */
144 -es.SurfaceModel.prototype.undo = function( statesToUndo ) {
 156+es.SurfaceModel.prototype.undo = function( n ) {
145157
 158+ console.log( this.history );
146159 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 );
150161
151162 lengthDifference = 0;
 163+ var finalSelection = null;
152164
153 - while ( statesToUndo ) {
154 - statesToUndo--;
 165+ while ( n ) {
 166+ n--;
155167
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+ }
160177 }
161178 this.emit( 'undo', this.currentState );
162179 }
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 - }
168180 }
169181
170182 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 );
174184
175 - // TODO - make the appropriate selection now
 185+ this.select( finalSelection );
176186 };
177187
178188 /**
Index: trunk/extensions/VisualEditor/modules/es/es.Range.js
@@ -79,3 +79,14 @@
8080 this.end = this.from;
8181 }
8282 };
 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+};

Status & tagging log