Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js |
— | — | @@ -30,6 +30,7 @@ |
31 | 31 | this.insertionAnnotations = []; |
32 | 32 | this.updateSelectionTimeout = undefined; |
33 | 33 | this.emitUpdateTimeout = undefined; |
| 34 | + this.emitCursorTimeout = undefined; |
34 | 35 | |
35 | 36 | // Interaction states |
36 | 37 | |
— | — | @@ -145,6 +146,42 @@ |
146 | 147 | }; |
147 | 148 | |
148 | 149 | /* Methods */ |
| 150 | +es.SurfaceView.prototype.emitCursor = function() { |
| 151 | + if ( this.emitCursorTimeout ) { |
| 152 | + clearTimeout( this.emitCursorTimeout ); |
| 153 | + } |
| 154 | + var _this = this; |
| 155 | + this.emitCursorTimeout = setTimeout( function() { |
| 156 | + var annotations, |
| 157 | + nodes = [], |
| 158 | + model = _this.documentView.model; |
| 159 | + |
| 160 | + if( _this.currentSelection.from === _this.currentSelection.to ) { |
| 161 | + var insertionAnnotations = _this.getInsertionAnnotations(); |
| 162 | + annotations = { |
| 163 | + 'full': insertionAnnotations, |
| 164 | + 'partial': [], |
| 165 | + 'all': insertionAnnotations |
| 166 | + }; |
| 167 | + nodes.push( model.getNodeFromOffset( _this.currentSelection.from ) ); |
| 168 | + } else { |
| 169 | + annotations = model.getAnnotationsFromRange( _this.currentSelection ); |
| 170 | + var startNode = model.getNodeFromOffset( _this.currentSelection.start ), |
| 171 | + endNode = model.getNodeFromOffset( _this.currentSelection.end ); |
| 172 | + if ( startNode === endNode ) { |
| 173 | + nodes.push( startNode ); |
| 174 | + } else { |
| 175 | + model.traverseLeafNodes( function( node ) { |
| 176 | + nodes.push( node ); |
| 177 | + if( node === endNode ) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + }, startNode ); |
| 181 | + } |
| 182 | + } |
| 183 | + _this.emit( 'cursor', annotations, nodes ); |
| 184 | + }, 50 ); |
| 185 | +}; |
149 | 186 | |
150 | 187 | es.SurfaceView.prototype.getInsertionAnnotations = function() { |
151 | 188 | return this.insertionAnnotations; |
— | — | @@ -152,7 +189,7 @@ |
153 | 190 | |
154 | 191 | es.SurfaceView.prototype.addInsertionAnnotation = function( annotation ) { |
155 | 192 | this.insertionAnnotations.push( annotation ); |
156 | | - this.emit( 'cursor' ); |
| 193 | + this.emitCursor(); |
157 | 194 | }; |
158 | 195 | |
159 | 196 | es.SurfaceView.prototype.loadInsertionAnnotations = function( annotation ) { |
— | — | @@ -165,7 +202,7 @@ |
166 | 203 | i--; |
167 | 204 | } |
168 | 205 | } |
169 | | - this.emit( 'cursor' ); |
| 206 | + this.emitCursor(); |
170 | 207 | }; |
171 | 208 | |
172 | 209 | es.SurfaceView.prototype.removeInsertionAnnotation = function( annotation ) { |
— | — | @@ -173,12 +210,12 @@ |
174 | 211 | if ( index !== -1 ) { |
175 | 212 | this.insertionAnnotations.splice( index, 1 ); |
176 | 213 | } |
177 | | - this.emit( 'cursor' ); |
| 214 | + this.emitCursor(); |
178 | 215 | }; |
179 | 216 | |
180 | 217 | es.SurfaceView.prototype.clearInsertionAnnotations = function() { |
181 | 218 | this.insertionAnnotations = []; |
182 | | - this.emit( 'cursor' ); |
| 219 | + this.emitCursor(); |
183 | 220 | }; |
184 | 221 | |
185 | 222 | es.SurfaceView.prototype.getModel = function() { |
Index: trunk/extensions/VisualEditor/modules/es/views/es.ToolbarView.js |
— | — | @@ -40,12 +40,12 @@ |
41 | 41 | } |
42 | 42 | } |
43 | 43 | } ); |
44 | | - this.surfaceView.model.on( 'select', function() { |
45 | | - _this.updateState(); |
| 44 | + |
| 45 | + this.surfaceView.on( 'cursor', function( annotations, nodes ) { |
| 46 | + for( var i = 0; i < _this.tools.length; i++ ) { |
| 47 | + _this.tools[i].updateState( annotations, nodes ); |
| 48 | + } |
46 | 49 | } ); |
47 | | - this.surfaceView.on( 'cursor', function() { |
48 | | - _this.updateState(); |
49 | | - } ); |
50 | 50 | |
51 | 51 | this.config = config || [ |
52 | 52 | { 'name': 'history', 'items' : ['undo', 'redo'] }, |
— | — | @@ -56,40 +56,6 @@ |
57 | 57 | this.setup(); |
58 | 58 | }; |
59 | 59 | |
60 | | -es.ToolbarView.prototype.updateState = function() { |
61 | | - var selection = this.surfaceView.currentSelection, |
62 | | - annotations, |
63 | | - nodes = []; |
64 | | - |
65 | | - if( selection.from === selection.to ) { |
66 | | - var insertionAnnotations = this.surfaceView.getInsertionAnnotations(); |
67 | | - annotations = { |
68 | | - 'full': insertionAnnotations, |
69 | | - 'partial': [], |
70 | | - 'all': insertionAnnotations |
71 | | - }; |
72 | | - nodes.push( this.surfaceView.documentView.model.getNodeFromOffset( selection.from ) ); |
73 | | - } else { |
74 | | - annotations = this.surfaceView.documentView.model.getAnnotationsFromRange( selection ); |
75 | | - var startNode = this.surfaceView.documentView.model.getNodeFromOffset( selection.start ), |
76 | | - endNode = this.surfaceView.documentView.model.getNodeFromOffset( selection.end ); |
77 | | - if ( startNode === endNode ) { |
78 | | - nodes.push( startNode ); |
79 | | - } else { |
80 | | - this.surfaceView.documentView.model.traverseLeafNodes( function( node ) { |
81 | | - nodes.push( node ); |
82 | | - if( node === endNode ) { |
83 | | - return false; |
84 | | - } |
85 | | - }, startNode ); |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - for( var i = 0; i < this.tools.length; i++ ) { |
90 | | - this.tools[i].updateState( annotations, nodes ); |
91 | | - } |
92 | | -}; |
93 | | - |
94 | 60 | es.ToolbarView.prototype.setup = function() { |
95 | 61 | for ( var i = 0; i < this.config.length; i++ ) { |
96 | 62 | var $group = $( '<div>' ) |