r102082 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102081‎ | r102082 | r102083 >
Date:02:06, 5 November 2011
Author:inez
Status:deferred
Tags:
Comment:
Very simple support for undo/redo (with keyboard only: ctrl+z/ctrl+y)
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
@@ -151,6 +151,9 @@
152152 }
153153 };
154154
 155+var transactionStack = [];
 156+var undoCounter = 0;
 157+
155158 es.SurfaceView.prototype.onKeyDown = function( e ) {
156159 switch ( e.keyCode ) {
157160 case 16: // Shift
@@ -196,22 +199,50 @@
197200 break;
198201 case 8: // Backspace
199202 var transaction = this.documentView.model.prepareRemoval( new es.Range( this.selection.to, this.selection.to - 1 ) );
 203+ transactionStack.push ( transaction );
 204+
200205 this.documentView.model.commit ( transaction );
201206 this.selection.from = this.selection.to -= 1;
202207 this.showCursor();
203208 break;
204209 case 46: // Delete
205210 var transaction = this.documentView.model.prepareRemoval( new es.Range( this.selection.to, this.selection.to + 1 ) );
 211+ transactionStack.push ( transaction );
206212 this.documentView.model.commit ( transaction );
207213 break;
 214+ case 89: // Y
 215+ if ( this.keyboard.keys.control ) {
 216+ if ( transactionStack.length > 0 && undoCounter > 0) {
 217+ this.documentView.model.commit ( transactionStack[ transactionStack.length - undoCounter ] );
 218+ undoCounter--;
 219+ this.selection.from = this.selection.to += 1;
 220+ this.showCursor();
 221+ }
 222+ }
 223+ break;
 224+ case 90: // Z
 225+ if ( this.keyboard.keys.control ) {
 226+ if ( transactionStack.length > 0 ) {
 227+ this.documentView.model.rollback ( transactionStack[ transactionStack.length - 1 - undoCounter ] );
 228+ undoCounter++;
 229+ this.selection.from = this.selection.to -= 1;
 230+ this.showCursor();
 231+ }
 232+ }
 233+ break;
208234 default: // Insert content (maybe)
 235+ if (undoCounter) {
 236+ transactionStack = transactionStack.slice(0, transactionStack.length - undoCounter);
 237+ undoCounter = 0;
 238+ }
 239+
209240 if ( this.keyboard.keydownTimeout ) {
210241 clearTimeout( this.keyboard.keydownTimeout );
211242 }
212243 var surface = this;
213244 this.keyboard.keydownTimeout = setTimeout( function () {
214245 surface.insertFromInput();
215 - }, 10 );
 246+ }, 0 );
216247 break;
217248 }
218249 return true;
@@ -222,6 +253,7 @@
223254 this.$input.val( '' );
224255 if ( val.length > 0 ) {
225256 var transaction = this.documentView.model.prepareInsertion( this.selection.to, val.split('') );
 257+ transactionStack.push ( transaction );
226258 this.documentView.model.commit ( transaction );
227259 this.selection.from = this.selection.to += val.length;
228260 this.showCursor();

Status & tagging log