http://www.mediawiki.org/wiki/Special:Code/MediaWiki/92478

Revision: 92478
Author:   tparscal
Date:     2011-07-18 21:37:25 +0000 (Mon, 18 Jul 2011)
Log Message:
-----------
Added double click to select word, tripple click to select block.

Modified Paths:
--------------
    trunk/parsers/wikidom/lib/es/es.Block.js
    trunk/parsers/wikidom/lib/es/es.Content.js
    trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js
    trunk/parsers/wikidom/lib/es/es.Surface.js

Modified: trunk/parsers/wikidom/lib/es/es.Block.js
===================================================================
--- trunk/parsers/wikidom/lib/es/es.Block.js    2011-07-18 21:26:33 UTC (rev 
92477)
+++ trunk/parsers/wikidom/lib/es/es.Block.js    2011-07-18 21:37:25 UTC (rev 
92478)
@@ -108,4 +108,8 @@
        throw 'Block.annotateContent not implemented in this subclass.';
 };
 
+Block.prototype.getWordBoundaries = function( offset ) {
+       throw 'Block.getWordBoundaries not implemented in this subclass.';
+};
+
 extend( Block, EventEmitter );

Modified: trunk/parsers/wikidom/lib/es/es.Content.js
===================================================================
--- trunk/parsers/wikidom/lib/es/es.Content.js  2011-07-18 21:26:33 UTC (rev 
92477)
+++ trunk/parsers/wikidom/lib/es/es.Content.js  2011-07-18 21:37:25 UTC (rev 
92478)
@@ -531,4 +531,32 @@
        return out;
 };
 
+Content.prototype.getWordBoundaries = function( offset ) {
+       if ( offset < 0 || offset > this.data.length ) {
+               throw 'Out of bounds error. Offset expected to be >= 0 and <= 
to ' + this.data.length;
+       }
+       var start = offset,
+               end = offset,
+               char;
+       while ( start > 0 ) {
+               start--;
+               char = ( typeof this.data[start] === 'string' ? 
this.data[start] : this.data[start][0] );
+               if ( char.match( /\B/ ) ) {
+                       start++;
+                       break;
+               }
+       }
+       while ( end < this.data.length ) {
+               char = ( typeof this.data[end] === 'string' ? this.data[end] : 
this.data[end][0] );
+               if ( char.match( /\B/ ) ) {
+                       break;
+               }
+               end++;
+       }
+       return {
+               'start': start,
+               'end': end
+       };
+};
+
 extend( Content, EventEmitter );

Modified: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js
===================================================================
--- trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js   2011-07-18 21:26:33 UTC 
(rev 92477)
+++ trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js   2011-07-18 21:37:25 UTC 
(rev 92478)
@@ -87,4 +87,8 @@
        this.content.annotate( method, annotation, start, end );
 };
 
+Block.prototype.getWordBoundaries = function( offset ) {
+       return this.content.getWordBoundaries( offset );
+};
+
 extend( ParagraphBlock, Block );

Modified: trunk/parsers/wikidom/lib/es/es.Surface.js
===================================================================
--- trunk/parsers/wikidom/lib/es/es.Surface.js  2011-07-18 21:26:33 UTC (rev 
92477)
+++ trunk/parsers/wikidom/lib/es/es.Surface.js  2011-07-18 21:37:25 UTC (rev 
92478)
@@ -11,10 +11,19 @@
        this.doc = doc;
        this.location = null;
        this.selection = new Selection();
-       this.mouseSelecting = false;
-       this.keyboardSelecting = false;
-       this.keydownTimeout = null;
        this.initialHorizontalCursorPosition = null;
+       this.mouse = {
+               'selecting': false,
+               'clicks': 0,
+               'clickDelay': 200,
+               'clickTimeout': null,
+               'clickX': null,
+               'clickY': null
+       };
+       this.keyboard = {
+               'selecting': false,
+               'keydownTimeout': null
+       };
        
        // MouseDown on surface
        this.$.bind({
@@ -52,7 +61,7 @@
                                        'keyup.editSurface' : function( e ) {
                                                return surface.onKeyUp( e );    
                
                                        },
-                               });             
+                               });
                        },
                        'blur': function( e ) {
                                $document.unbind('.editSurface');
@@ -105,8 +114,8 @@
        switch ( e.keyCode ) {
                case 16: // Shift
                        this.shiftDown = true;
-                       if ( !this.keyboardSelecting ) {
-                               this.keyboardSelecting = true;
+                       if ( !this.keyboard.selecting ) {
+                               this.keyboard.selecting = true;
                                if ( !this.selection.to ) {
                                        this.selection = new Selection( 
this.location );
                                }
@@ -119,7 +128,7 @@
                case 37: // Left arrow
                        this.initialHorizontalCursorPosition = null;
                        this.moveCursorLeft();
-                       if ( this.shiftDown && this.keyboardSelecting ) {
+                       if ( this.shiftDown && this.keyboard.selecting ) {
                                this.selection.to = this.location;
                        } else {
                                this.selection = new Selection();
@@ -128,7 +137,7 @@
                        break;
                case 38: // Up arrow
                        this.moveCursorUp();
-                       if ( this.shiftDown && this.keyboardSelecting ) {
+                       if ( this.shiftDown && this.keyboard.selecting ) {
                                this.selection.to = this.location;
                        } else {
                                this.selection = new Selection();
@@ -138,7 +147,7 @@
                case 39: // Right arrow
                        this.initialHorizontalCursorPosition = null;
                        this.moveCursorRight();
-                       if ( this.shiftDown && this.keyboardSelecting ) {
+                       if ( this.shiftDown && this.keyboard.selecting ) {
                                this.selection.to = this.location;
                        } else {
                                this.selection = new Selection();
@@ -147,7 +156,7 @@
                        break;
                case 40: // Down arrow
                        this.moveCursorDown();
-                       if ( this.shiftDown && this.keyboardSelecting ) {
+                       if ( this.shiftDown && this.keyboard.selecting ) {
                                this.selection.to = this.location;
                        } else {
                                this.selection = new Selection();
@@ -164,11 +173,11 @@
                default:
                        this.initialHorizontalCursorPosition = null;
                        this.cursor.hide();
-                       if ( this.keydownTimeout ) {
-                               clearTimeout( this.keydownTimeout );
+                       if ( this.keyboard.keydownTimeout ) {
+                               clearTimeout( this.keyboard.keydownTimeout );
                        }
                        var surface = this;
-                       this.keydownTimeout = setTimeout( function () {
+                       this.keyboard.keydownTimeout = setTimeout( function () {
                                var val = surface.$input.val();
                                surface.$input.val( '' );
                                if ( val.length > 0 ) {
@@ -189,8 +198,8 @@
        switch ( e.keyCode ) {
                case 16: // Shift
                        this.shiftDown = false;
-                       if ( this.keyboardSelecting ) {
-                               this.keyboardSelecting = false;
+                       if ( this.keyboard.selecting ) {
+                               this.keyboard.selecting = false;
                        }
                        break;
                case 17: // Control
@@ -231,14 +240,50 @@
 
 Surface.prototype.onMouseDown = function( e ) {
        if ( e.button === 0 ) {
+               clearTimeout( this.mouse.clickTimeout );
+               if ( this.mouse.clickX === e.pageX && this.mouse.clickY === 
e.pageY ) {
+                       // Same location, keep counting
+                       this.mouse.clicks++;
+                       var surface = this;
+                       this.mouse.clickTimeout = setTimeout( function() {
+                               surface.mouse.clicks = 0;
+                       }, this.mouse.clickDelay );
+               } else {
+                       // New location, start over
+                       this.mouse.clicks = 1;
+                       this.mouse.clickX = e.pageX;
+                       this.mouse.clickY = e.pageY;
+               }
                this.location = this.getLocationFromEvent( e );
-               this.selection = new Selection( this.location );
-               var cursorPosition = this.location.block.getPosition( 
this.location.offset );
-               this.cursor.show( cursorPosition, 
this.location.block.$.offset() );
-               this.$input.css( 'top', cursorPosition.top );
-               this.mouseSelecting = true;
-               this.drawSelection();
-               this.cursor.show();
+               switch ( this.mouse.clicks ) {
+                       case 1:
+                               // Clear selection and move cursor to nearest 
offset
+                               this.selection = new Selection( this.location );
+                               var cursorPosition = 
this.location.block.getPosition( this.location.offset );
+                               this.cursor.show( cursorPosition, 
this.location.block.$.offset() );
+                               this.$input.css( 'top', cursorPosition.top );
+                               this.mouse.selecting = true;
+                               this.drawSelection();
+                               this.cursor.show();
+                               break;
+                       case 2:
+                               // Select word offset is within
+                               var wordBoundaries = 
this.location.block.getWordBoundaries( this.location.offset );
+                               this.selection = new Selection(
+                                       new Location( this.location.block, 
wordBoundaries.start ),
+                                       new Location( this.location.block, 
wordBoundaries.end )
+                               );
+                               this.drawSelection();
+                               break;
+                       case 3:
+                               // Select block offset is within
+                               this.selection = new Selection(
+                                       new Location( this.location.block, 0 ),
+                                       new Location( this.location.block, 
this.location.block.getLength() )
+                               );
+                               this.drawSelection();
+                               break;
+               }
        }
        this.initialHorizontalCursorPosition = null;
        if ( !this.$input.is(':focus') ) {
@@ -248,7 +293,7 @@
 };
 
 Surface.prototype.onMouseMove = function( e ) {
-       if ( e.button === 0 && this.mouseSelecting ) {
+       if ( e.button === 0 && this.mouse.selecting ) {
                this.cursor.hide();
                this.selection.to = this.getLocationFromEvent( e );
                this.drawSelection();
@@ -261,7 +306,7 @@
                this.drawSelection();
                this.cursor.hide();
        }
-       this.mouseSelecting = false;
+       this.mouse.selecting = false;
 };
 
 /**


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to