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

Revision: 113963
Author:   rmoen
Date:     2012-03-15 22:06:40 +0000 (Thu, 15 Mar 2012)
Log Message:
-----------
add surfaceObserver on select methods to ui toolbar and surfaceView for 
responding to select event. tools now updating to selected node annotations. 
revise getSelectionRect to return actual start & end pos. context icon properly 
showing on range selection

Modified Paths:
--------------
    trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.Surface.js
    trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Context.js
    trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Toolbar.js

Modified: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.Surface.js
===================================================================
--- trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.Surface.js        
2012-03-15 21:34:35 UTC (rev 113962)
+++ trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.Surface.js        
2012-03-15 22:06:40 UTC (rev 113963)
@@ -28,12 +28,24 @@
        this.clipboard = {};
        this.autoRender = false;
 
-       // Content Observer
+       // Surface Observer
        this.surfaceObserver = new ve.ce.SurfaceObserver( this.documentView );
-       this.surfaceObserver.on( 'cursor', function( info ) {
-               //console.log("cursor", info);
-       } )
+       this.surfaceObserver.on( 'select', function( selection ) {
+               
+               if ( selection !== null ) {
+                       // Keep a copy of the current selection on hand
+                       _this.currentSelection = selection.clone();
+                       // Respond to selection changes
+                       _this.updateSelection();
 
+                       if ( selection.getLength() ) {
+                               _this.clearInsertionAnnotations();
+                       } else {
+                               _this.loadInsertionAnnotations();
+                       }
+               }
+       } );
+
        // Events
        this.documentView.$.bind( {
                'focus': function( e ) {
@@ -100,18 +112,6 @@
                document.execCommand("enableObjectResizing", false, false);
        } catch(e) {
        }
-
-       this.model.on( 'select', function( selection ) {
-               // Keep a copy of the current selection on hand
-               _this.currentSelection = selection.clone();
-               // Respond to selection changes
-               _this.updateSelection();
-               if ( selection.getLength() ) {
-                       _this.clearInsertionAnnotations();
-               } else {
-                       _this.loadInsertionAnnotations();
-               }
-       } );
 };
 
 /* Methods */
@@ -278,11 +278,11 @@
 ve.ce.Surface.prototype.updateSelection = function( delay ) {
        var _this = this;
        function update() {
-               if ( _this.currentSelection.getLength() ) {
+               if ( _this.surfaceObserver.range.getLength() ) {
                        _this.clearInsertionAnnotations();
                }
                if ( _this.contextView ) {
-                       if ( _this.currentSelection.getLength() ) {
+                       if ( _this.surfaceObserver.range.getLength() ) {
                                _this.contextView.set();
                        } else {
                                _this.contextView.clear();
@@ -577,8 +577,11 @@
 };
 
 ve.ce.Surface.prototype.getSelectionRect = function() {
-       var sel = rangy.getSelection();
-       return sel.getBoundingClientRect();
+       var rangySel = rangy.getSelection();
+       return {
+               start: rangySel.getStartClientPos(),
+               end: rangySel.getEndClientPos()
+       };
 };
 
 ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) {
@@ -643,8 +646,6 @@
        range.setStart( start.node, start.offset );
        range.setEnd( stop.node, stop.offset );
        sel.setSingleRange( range );
-       // Trigger select event
-       this.model.select( this.getSelectionRange() );
 };
 
 ve.ce.Surface.prototype.getLeafNode = function( elem ) {

Modified: trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Context.js
===================================================================
--- trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Context.js        
2012-03-15 21:34:35 UTC (rev 113962)
+++ trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Context.js        
2012-03-15 22:06:40 UTC (rev 113963)
@@ -92,16 +92,16 @@
 
 ve.ui.Context.prototype.positionIcon = function() {
        this.$.removeClass( 'es-contextView-position-start 
es-contextView-position-end' );
-       var selection = this.surfaceView.getModel().getSelection(),
+       var selection = this.surfaceView.currentSelection,
                selectionRect = this.surfaceView.getSelectionRect();
        this.position = null;
 
        if ( selection.from < selection.to ) {
-               this.position = new ve.Position( selectionRect.right, 
selectionRect.bottom );
+               this.position = new ve.Position( selectionRect.end.x, 
selectionRect.end.y );
                this.$.addClass( 'es-contextView-position-end' );
                
        } else if ( selection.from > selection.to ) {
-               this.position = new ve.Position( selectionRect.left, 
selectionRect.top );
+               this.position = new ve.Position( selectionRect.start.x, 
selectionRect.start.y );
                this.$.addClass( 'es-contextView-position-start' );
        }
 

Modified: trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Toolbar.js
===================================================================
--- trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Toolbar.js        
2012-03-15 21:34:35 UTC (rev 113962)
+++ trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Toolbar.js        
2012-03-15 22:06:40 UTC (rev 113963)
@@ -16,12 +16,36 @@
        this.$groups = $( '<div class="es-toolbarGroups"></div>' ).prependTo( 
this.$ );
        this.tools = [];
 
-       this.surfaceView.on( 'cursor', function( annotations, nodes ) {
-               for( var i = 0; i < _this.tools.length; i++ ) {
-                       _this.tools[i].updateState( annotations, nodes );
+       this.surfaceView.surfaceObserver.on( 'select', function ( selection ) {
+               var     annotations = _this.surfaceView.getAnnotations(),
+                       nodes = [],
+                       model = _this.surfaceView.documentView.model;
+       
+               if ( selection.from === selection.to ) {
+                       nodes.push( model.getNodeFromOffset( selection.from ) );
+               } else {
+                       var     startNode = model.getNodeFromOffset( 
selection.start ),
+                               endNode = model.getNodeFromOffset( 
selection.end );
+                       if ( startNode === endNode ) {
+                               nodes.push( startNode );
+                       } else {
+                               model.traverseLeafNodes( function( node ) {
+                                       nodes.push( node );
+                                       if( node === endNode ) {
+                                               return false;
+                                       }
+                               }, startNode );
+                       }
                }
-       } );
 
+               if ( selection !== null ) {
+                       for( var i = 0; i < _this.tools.length; i++ ) {
+                               _this.tools[i].updateState( annotations, nodes 
);
+                       }
+               }
+               
+       });
+
        this.config = config || [
                { 'name': 'history', 'items' : ['undo', 'redo'] },
                { 'name': 'textStyle', 'items' : ['format'] },


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

Reply via email to