Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.Surface.js |
— | — | @@ -17,6 +17,7 @@ |
18 | 18 | |
19 | 19 | // Properties |
20 | 20 | this.model = model; |
| 21 | + this.currentSelection = new ve.Range(); |
21 | 22 | this.documentView = new ve.ce.DocumentNode( this.model.getDocument(), this ); |
22 | 23 | this.contextView = null; |
23 | 24 | this.$ = $container |
— | — | @@ -79,6 +80,18 @@ |
80 | 81 | 'compositionStart': null, |
81 | 82 | 'compositionEnd': null |
82 | 83 | }; |
| 84 | + |
| 85 | + this.model.on( 'select', function( selection ) { |
| 86 | + // Keep a copy of the current selection on hand |
| 87 | + _this.currentSelection = selection.clone(); |
| 88 | + // Respond to selection changes |
| 89 | + _this.updateSelection(); |
| 90 | + if ( selection.getLength() ) { |
| 91 | + _this.clearInsertionAnnotations(); |
| 92 | + } else { |
| 93 | + _this.loadInsertionAnnotations(); |
| 94 | + } |
| 95 | + } ); |
83 | 96 | }; |
84 | 97 | |
85 | 98 | /* Methods */ |
— | — | @@ -242,6 +255,31 @@ |
243 | 256 | return this.model; |
244 | 257 | }; |
245 | 258 | |
| 259 | +ve.ce.Surface.prototype.updateSelection = function( delay ) { |
| 260 | + var _this = this; |
| 261 | + function update() { |
| 262 | + if ( _this.currentSelection.getLength() ) { |
| 263 | + _this.clearInsertionAnnotations(); |
| 264 | + } |
| 265 | + if ( _this.contextView ) { |
| 266 | + if ( _this.currentSelection.getLength() ) { |
| 267 | + _this.contextView.set(); |
| 268 | + } else { |
| 269 | + _this.contextView.clear(); |
| 270 | + } |
| 271 | + } |
| 272 | + _this.updateSelectionTimeout = undefined; |
| 273 | + } |
| 274 | + if ( delay ) { |
| 275 | + if ( this.updateSelectionTimeout !== undefined ) { |
| 276 | + return; |
| 277 | + } |
| 278 | + this.updateSelectionTimeout = setTimeout( update, delay ); |
| 279 | + } else { |
| 280 | + update(); |
| 281 | + } |
| 282 | +}; |
| 283 | + |
246 | 284 | ve.ce.Surface.prototype.documentOnFocus = function() { |
247 | 285 | this.startPolling(); |
248 | 286 | }; |
— | — | @@ -517,6 +555,11 @@ |
518 | 556 | } |
519 | 557 | }; |
520 | 558 | |
| 559 | +ve.ce.Surface.prototype.getSelectionRect = function() { |
| 560 | + var sel = rangy.getSelection(); |
| 561 | + return sel.getBoundingClientRect(); |
| 562 | +}; |
| 563 | + |
521 | 564 | ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) { |
522 | 565 | var $node = this.documentView.getNodeFromOffset( offset ).$, |
523 | 566 | nodeOffset = this.documentView.getOffsetFromNode( $node.data('view') ) + 1, |
— | — | @@ -579,6 +622,8 @@ |
580 | 623 | range.setStart( start.node, start.offset ); |
581 | 624 | range.setEnd( stop.node, stop.offset ); |
582 | 625 | sel.setSingleRange( range ); |
| 626 | + // Trigger select event |
| 627 | + this.model.select( this.getSelectionRange() ); |
583 | 628 | }; |
584 | 629 | |
585 | 630 | ve.ce.Surface.prototype.getLeafNode = function( elem ) { |
Index: trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Context.js |
— | — | @@ -95,6 +95,9 @@ |
96 | 96 | var selection = this.surfaceView.getModel().getSelection(), |
97 | 97 | offset; |
98 | 98 | this.position = null; |
| 99 | + |
| 100 | + console.log( this.surfaceView.getSelectionRect() ); |
| 101 | + |
99 | 102 | if ( selection.from < selection.to ) { |
100 | 103 | var $lastRange = this.surfaceView.$.find( '.es-contentView-range:visible:last' ); |
101 | 104 | if ( $lastRange.length ) { |