r102717 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102716‎ | r102717 | r102718 >
Date:23:30, 10 November 2011
Author:inez
Status:deferred
Tags:
Comment:
Improvements to selecting with mouse (double, triple clicks)
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
@@ -130,49 +130,44 @@
131131 };
132132
133133 es.SurfaceView.prototype.onMouseDown = function( e ) {
134 - if ( e.button === 0 /* left mouse button */ ) {
135 - switch ( e.originalEvent.detail ) {
136 - case 1: // single click
137 - this.mouse.selectingMode = 1;
138 -
139 - this.selection.to = this.documentView.getOffsetFromEvent( e );
140 - //console.log(this.selection.to);
141 - if ( this.keyboard.keys.shift ) {
142 - this.documentView.drawSelection( this.selection );
143 - this.hideCursor();
144 - } else {
 134+ if ( e.button === 0 ) { // left mouse button
 135+ var offset = this.documentView.getOffsetFromEvent( e );
 136+ if ( e.originalEvent.detail === 1 ) { // single click
 137+ this.mouse.selectingMode = 1; // used in mouseMove handler
 138+ if ( this.keyboard.keys.shift && offset !== this.selection.from ) {
 139+ this.selection.to = offset;
 140+ } else {
 141+ if ( this.selection.to !== this.selection.from ) {
145142 this.documentView.clearSelection();
146 - this.selection.from = this.selection.to;
147 - var position = es.Position.newFromEventPagePosition( e ),
148 - nodeView = this.documentView.getNodeFromOffset( this.selection.to, false );
149 - this.cursor.initialBias = position.left > nodeView.$.offset().left;
150 - this.showCursor();
151143 }
152 - break;
153 - case 2: // double click
154 - this.mouse.selectingMode = 2;
 144+ this.selection.from = this.selection.to = offset;
 145+ var position = es.Position.newFromEventPagePosition( e ),
 146+ nodeView = this.documentView.getNodeFromOffset( offset, false );
 147+ this.cursor.initialBias = position.left > nodeView.contentView.$.offset().left;
 148+ }
 149+ } else if ( e.originalEvent.detail === 2 ) { // double click
 150+ this.mouse.selectingMode = 2; // used in mouseMove handler
 151+ var wordRange = this.documentView.model.getWordBoundaries( offset );
 152+ if( wordRange ) {
 153+ this.selection = wordRange;
 154+ this.mouse.selectedRange = this.selection.clone();
 155+ }
 156+ } else if ( e.originalEvent.detail >= 3 ) { // triple click
 157+ this.mouse.selectingMode = 3; // used in mouseMove handler
 158+ var node = this.documentView.getNodeFromOffset( offset );
 159+ this.selection.from = this.documentView.getOffsetFromNode( node, false );
 160+ this.selection.to = this.selection.from + node.getElementLength() - 1;
 161+ this.mouse.selectedRange = this.selection.clone();
 162+ }
155163
156 - this.selection = this.documentView.model.getWordBoundaries(
157 - this.documentView.getOffsetFromEvent( e )
158 - );
159 - this.documentView.drawSelection( this.selection );
160 - this.hideCursor();
161 - this.mouse.selectedRange = new es.Range( this.selection.from, this.selection.to );
162 - break;
163 - default: // 3 and more
164 - this.mouse.selectingMode = 3;
165 -
166 - var node = this.documentView.getNodeFromOffset(
167 - this.documentView.getOffsetFromEvent( e )
168 - );
169 - this.selection.from = this.documentView.getOffsetFromNode( node, false );
170 - this.selection.to = this.selection.from + node.getElementLength() - 1;
171 - this.documentView.drawSelection( this.selection );
172 - this.hideCursor();
173 - this.mouse.selectedRange = new es.Range( this.selection.from, this.selection.to );
174 - break;
 164+ if ( this.selection.from === this.selection.to ) {
 165+ this.showCursor();
 166+ } else {
 167+ this.hideCursor();
 168+ this.documentView.drawSelection( this.selection );
175169 }
176170 }
 171+
177172 if ( !this.$input.is( ':focus' ) ) {
178173 this.$input.focus().select();
179174 }
@@ -182,80 +177,45 @@
183178 };
184179
185180 es.SurfaceView.prototype.onMouseMove = function( e ) {
186 - var wordBoundaries;
187 - if ( e.button === 0 /* left mouse button */ && this.mouse.selectingMode ) {
 181+ if ( e.button === 0 && this.mouse.selectingMode ) { // left mouse button and in selecting mode
 182+ var offset = this.documentView.getOffsetFromEvent( e );
188183 if ( this.mouse.selectingMode === 1 ) {
189 - this.selection.to = this.documentView.getOffsetFromEvent( e );
 184+ this.selection.to = offset;
190185 } else if ( this.mouse.selectingMode === 2 ) {
191 - wordBoundaries = this.documentView.model.getWordBoundaries(
192 - this.documentView.getOffsetFromEvent( e )
193 - );
194 - if ( wordBoundaries.to <= this.mouse.selectedRange.from ) {
195 - this.selection.to = wordBoundaries.from;
196 - this.selection.from = this.mouse.selectedRange.to;
 186+ var wordRange = this.documentView.model.getWordBoundaries( offset );
 187+ if ( wordRange ) {
 188+ if ( wordRange.to <= this.mouse.selectedRange.from ) {
 189+ this.selection.to = wordRange.from;
 190+ this.selection.from = this.mouse.selectedRange.to;
 191+ } else {
 192+ this.selection.from = this.mouse.selectedRange.from;
 193+ this.selection.to = wordRange.to;
 194+ }
197195 } else {
198 - this.selection.from = this.mouse.selectedRange.from;
199 - this.selection.to = wordBoundaries.to;
200 - }
 196+ this.selection.to = offset;
 197+ }
201198 } else if ( this.mouse.selectingMode === 3 ) {
202 - var node = this.documentView.getNodeFromOffset(
203 - this.documentView.getOffsetFromEvent( e )
204 - );
205 - var nodeBoundaries = new es.Range();
206 - nodeBoundaries.from = this.documentView.getOffsetFromNode( node, false );
207 - nodeBoundaries.to = nodeBoundaries.from + node.getElementLength() - 1;
208 -
209 - if ( nodeBoundaries.to <= this.mouse.selectedRange.from ) {
210 - this.selection.to = nodeBoundaries.from;
 199+ var node = this.documentView.getNodeFromOffset( offset );
 200+ var nodeRange = new es.Range();
 201+ nodeRange.from = this.documentView.getOffsetFromNode( node, false );
 202+ nodeRange.to = nodeRange.from + node.getElementLength() - 1;
 203+ if ( nodeRange.to <= this.mouse.selectedRange.from ) {
 204+ this.selection.to = nodeRange.from;
211205 this.selection.from = this.mouse.selectedRange.to;
212206 } else {
213207 this.selection.from = this.mouse.selectedRange.from;
214 - this.selection.to = nodeBoundaries.to;
215 - }
216 - }
 208+ this.selection.to = nodeRange.to;
 209+ }
 210+ }
217211 this.documentView.drawSelection( this.selection );
218 - if ( this.selection.getLength() ) {
 212+ if ( this.selection.from !== this.selection.to ) {
219213 this.hideCursor();
220 - }
221 - }
222 -
223 - if ( e.button === 0 /* left mouse button */ && this.mouse.selected ) {
224 -
225 - var offset = this.documentView.getOffsetFromEvent( e );
226 - if ( this.mouse.selected.containsOffset( offset ) ) {
227 - //return;
228214 }
229 - wordBoundaries = this.documentView.model.getWordBoundaries( offset );
230 - if ( wordBoundaries.to <= this.mouse.selected.from ) {
231 - this.selection.to = wordBoundaries.from;
232 - this.selection.from = this.mouse.selected.to;
233 - } else {
234 - this.selection.from = this.mouse.selected.from;
235 - this.selection.to = wordBoundaries.to;
236 - }
237 -
238 - /*
239 - var to = this.documentView.getOffsetFromEvent( e );
240 -
241 - if ( to <= this.mouse.selected.from ) {
242 - this.selection.to = to;
243 - this.selection.from = this.mouse.selected.to;
244 - } else if ( to >= this.mouse.selected.to ) {
245 - this.selection.from = this.mouse.selected.from;
246 - this.selection.to = to;
247 - }
248 - */
249 -
250 - this.documentView.drawSelection( this.selection );
251 -
252 - } else if ( e.button === 0 /* left mouse button */ && this.mouse.selecting ) {
253 - this.selection.to = this.documentView.getOffsetFromEvent( e );
254215 }
255 - this.emitSelect();
256216 };
257217
258218 es.SurfaceView.prototype.onMouseUp = function( e ) {
259 - if ( e.button === 0 /* left mouse button */ ) {
 219+ if ( e.button === 0 ) { // left mouse button
260220 this.mouse.selectingMode = this.mouse.selectedRange = null;
261221 }
262222 };
@@ -396,10 +356,10 @@
397357 if ( instruction === 'left' ) {
398358 newTo = wordBoundaries.from;
399359
400 - newTo = this.documentView.getModel().getRelativeContentOffset(
401 - newTo,
402 - instruction === 'left' ? -1 : 1
403 - );
 360+ newTo = this.documentView.getModel().getRelativeContentOffset(
 361+ newTo,
 362+ -1
 363+ );
404364
405365 } else {
406366 newTo = wordBoundaries.to;

Status & tagging log