r113963 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113962‎ | r113963 | r113964 >
Date:22:06, 15 March 2012
Author:rmoen
Status:deferred
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Context.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Toolbar.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.Surface.js
@@ -28,12 +28,24 @@
2929 this.clipboard = {};
3030 this.autoRender = false;
3131
32 - // Content Observer
 32+ // Surface Observer
3333 this.surfaceObserver = new ve.ce.SurfaceObserver( this.documentView );
34 - this.surfaceObserver.on( 'cursor', function( info ) {
35 - //console.log("cursor", info);
36 - } )
 34+ this.surfaceObserver.on( 'select', function( selection ) {
 35+
 36+ if ( selection !== null ) {
 37+ // Keep a copy of the current selection on hand
 38+ _this.currentSelection = selection.clone();
 39+ // Respond to selection changes
 40+ _this.updateSelection();
3741
 42+ if ( selection.getLength() ) {
 43+ _this.clearInsertionAnnotations();
 44+ } else {
 45+ _this.loadInsertionAnnotations();
 46+ }
 47+ }
 48+ } );
 49+
3850 // Events
3951 this.documentView.$.bind( {
4052 'focus': function( e ) {
@@ -100,18 +112,6 @@
101113 document.execCommand("enableObjectResizing", false, false);
102114 } catch(e) {
103115 }
104 -
105 - this.model.on( 'select', function( selection ) {
106 - // Keep a copy of the current selection on hand
107 - _this.currentSelection = selection.clone();
108 - // Respond to selection changes
109 - _this.updateSelection();
110 - if ( selection.getLength() ) {
111 - _this.clearInsertionAnnotations();
112 - } else {
113 - _this.loadInsertionAnnotations();
114 - }
115 - } );
116116 };
117117
118118 /* Methods */
@@ -278,11 +278,11 @@
279279 ve.ce.Surface.prototype.updateSelection = function( delay ) {
280280 var _this = this;
281281 function update() {
282 - if ( _this.currentSelection.getLength() ) {
 282+ if ( _this.surfaceObserver.range.getLength() ) {
283283 _this.clearInsertionAnnotations();
284284 }
285285 if ( _this.contextView ) {
286 - if ( _this.currentSelection.getLength() ) {
 286+ if ( _this.surfaceObserver.range.getLength() ) {
287287 _this.contextView.set();
288288 } else {
289289 _this.contextView.clear();
@@ -577,8 +577,11 @@
578578 };
579579
580580 ve.ce.Surface.prototype.getSelectionRect = function() {
581 - var sel = rangy.getSelection();
582 - return sel.getBoundingClientRect();
 581+ var rangySel = rangy.getSelection();
 582+ return {
 583+ start: rangySel.getStartClientPos(),
 584+ end: rangySel.getEndClientPos()
 585+ };
583586 };
584587
585588 ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) {
@@ -643,8 +646,6 @@
644647 range.setStart( start.node, start.offset );
645648 range.setEnd( stop.node, stop.offset );
646649 sel.setSingleRange( range );
647 - // Trigger select event
648 - this.model.select( this.getSelectionRange() );
649650 };
650651
651652 ve.ce.Surface.prototype.getLeafNode = function( elem ) {
Index: trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Toolbar.js
@@ -16,12 +16,36 @@
1717 this.$groups = $( '<div class="es-toolbarGroups"></div>' ).prependTo( this.$ );
1818 this.tools = [];
1919
20 - this.surfaceView.on( 'cursor', function( annotations, nodes ) {
21 - for( var i = 0; i < _this.tools.length; i++ ) {
22 - _this.tools[i].updateState( annotations, nodes );
 20+ this.surfaceView.surfaceObserver.on( 'select', function ( selection ) {
 21+ var annotations = _this.surfaceView.getAnnotations(),
 22+ nodes = [],
 23+ model = _this.surfaceView.documentView.model;
 24+
 25+ if ( selection.from === selection.to ) {
 26+ nodes.push( model.getNodeFromOffset( selection.from ) );
 27+ } else {
 28+ var startNode = model.getNodeFromOffset( selection.start ),
 29+ endNode = model.getNodeFromOffset( selection.end );
 30+ if ( startNode === endNode ) {
 31+ nodes.push( startNode );
 32+ } else {
 33+ model.traverseLeafNodes( function( node ) {
 34+ nodes.push( node );
 35+ if( node === endNode ) {
 36+ return false;
 37+ }
 38+ }, startNode );
 39+ }
2340 }
24 - } );
2541
 42+ if ( selection !== null ) {
 43+ for( var i = 0; i < _this.tools.length; i++ ) {
 44+ _this.tools[i].updateState( annotations, nodes );
 45+ }
 46+ }
 47+
 48+ });
 49+
2650 this.config = config || [
2751 { 'name': 'history', 'items' : ['undo', 'redo'] },
2852 { 'name': 'textStyle', 'items' : ['format'] },
Index: trunk/extensions/VisualEditor/modules/ve/ui/ve.ui.Context.js
@@ -92,16 +92,16 @@
9393
9494 ve.ui.Context.prototype.positionIcon = function() {
9595 this.$.removeClass( 'es-contextView-position-start es-contextView-position-end' );
96 - var selection = this.surfaceView.getModel().getSelection(),
 96+ var selection = this.surfaceView.currentSelection,
9797 selectionRect = this.surfaceView.getSelectionRect();
9898 this.position = null;
9999
100100 if ( selection.from < selection.to ) {
101 - this.position = new ve.Position( selectionRect.right, selectionRect.bottom );
 101+ this.position = new ve.Position( selectionRect.end.x, selectionRect.end.y );
102102 this.$.addClass( 'es-contextView-position-end' );
103103
104104 } else if ( selection.from > selection.to ) {
105 - this.position = new ve.Position( selectionRect.left, selectionRect.top );
 105+ this.position = new ve.Position( selectionRect.start.x, selectionRect.start.y );
106106 this.$.addClass( 'es-contextView-position-start' );
107107 }
108108

Status & tagging log