https://www.mediawiki.org/wiki/Special:Code/MediaWiki/102082

Revision: 102082
Author:   inez
Date:     2011-11-05 02:06:51 +0000 (Sat, 05 Nov 2011)
Log Message:
-----------
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: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js    
2011-11-05 01:47:35 UTC (rev 102081)
+++ trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js    
2011-11-05 02:06:51 UTC (rev 102082)
@@ -151,6 +151,9 @@
        }
 };
 
+var transactionStack = [];
+var undoCounter = 0;
+
 es.SurfaceView.prototype.onKeyDown = function( e ) {
        switch ( e.keyCode ) {
                case 16: // Shift
@@ -196,22 +199,50 @@
                        break;
                case 8: // Backspace
                        var transaction = 
this.documentView.model.prepareRemoval( new es.Range( this.selection.to, 
this.selection.to - 1 ) );
+                       transactionStack.push ( transaction );
+                       
                        this.documentView.model.commit ( transaction );
                        this.selection.from = this.selection.to -= 1;
                        this.showCursor();
                        break;
                case 46: // Delete
                        var transaction = 
this.documentView.model.prepareRemoval( new es.Range( this.selection.to, 
this.selection.to + 1 ) );
+                       transactionStack.push ( transaction );
                        this.documentView.model.commit ( transaction );
                        break;
+               case 89: // Y
+                       if ( this.keyboard.keys.control ) {
+                               if ( transactionStack.length > 0 && undoCounter 
> 0) {
+                                       this.documentView.model.commit ( 
transactionStack[ transactionStack.length - undoCounter ] );
+                                       undoCounter--;
+                                       this.selection.from = this.selection.to 
+= 1;
+                                       this.showCursor();
+                               }
+                       }
+                       break;
+               case 90: // Z
+                       if ( this.keyboard.keys.control ) {
+                               if ( transactionStack.length > 0 ) {
+                                       this.documentView.model.rollback ( 
transactionStack[ transactionStack.length - 1 - undoCounter ] );
+                                       undoCounter++;
+                                       this.selection.from = this.selection.to 
-= 1;
+                                       this.showCursor();
+                               }
+                       }
+                       break;
                default: // Insert content (maybe)
+                       if (undoCounter) {
+                               transactionStack = transactionStack.slice(0, 
transactionStack.length - undoCounter);
+                               undoCounter = 0;
+                       }
+                       
                        if ( this.keyboard.keydownTimeout ) {
                                clearTimeout( this.keyboard.keydownTimeout );
                        }
                        var surface = this;
                        this.keyboard.keydownTimeout = setTimeout( function () {
                                surface.insertFromInput();
-                       }, 10 );
+                       }, 0 );
                        break;
        }
        return true;
@@ -222,6 +253,7 @@
        this.$input.val( '' );
        if ( val.length > 0 ) {
                var transaction = this.documentView.model.prepareInsertion( 
this.selection.to, val.split('') );
+               transactionStack.push ( transaction );
                this.documentView.model.commit ( transaction );
                this.selection.from = this.selection.to += val.length;
                this.showCursor();


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to