r102070 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102069‎ | r102070 | r102071 >
Date:23:28, 4 November 2011
Author:inez
Status:deferred
Tags:
Comment:
Cleanup for moveCursor and showCursor code
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
@@ -124,7 +124,8 @@
125125 this.selection.from = this.selection.to;
126126 var position = es.Position.newFromEventPagePosition( e ),
127127 nodeView = this.documentView.getNodeFromOffset( this.selection.to, false );
128 - this.showCursor( position.left > nodeView.$.offset().left );
 128+ this.cursor.initialBias = position.left > nodeView.$.offset().left;
 129+ this.showCursor();
129130 }
130131 }
131132 if ( !this.$input.is( ':focus' ) ) {
@@ -231,97 +232,89 @@
232233 es.SurfaceView.prototype.moveCursor = function( instruction ) {
233234 this.selection.normalize();
234235
235 - var from, to;
 236+ if ( instruction !== 'up' && instruction !== 'down' ) {
 237+ this.cursor.initialLeft = null;
 238+ }
 239+
 240+ var newTo;
236241
237 - if ( instruction === 'up' || instruction === 'down' ) {
238 - /*
239 - * Looks for the in-document character position that would match up with the same horizontal
240 - * position - jumping a few pixels up/down at a time until we reach the next/previous line
241 - */
242 -
243 - var position = this.documentView.getRenderedPositionFromOffset( this.selection.to );
244 - if ( this.cursor.initialLeft === null ) {
245 - this.cursor.initialLeft = position.left;
246 - }
247 -
248 - var fakePosition = new es.Position( this.cursor.initialLeft, position.top ),
249 - i = 0,
250 - step = instruction === 'up' ? -5 : 5,
251 - top = this.$.position().top;
252 -
253 - do {
254 - fakePosition.top += ++i * step;
255 - if ( fakePosition.top < top || fakePosition.top > top + this.dimensions.height ) {
256 - break;
 242+ switch ( instruction ) {
 243+ case 'left' :
 244+ case 'right' :
 245+ var offset;
 246+ if ( this.keyboard.keys.shift ) {
 247+ offset = this.selection.to;
 248+ } else {
 249+ offset = this.selection.from === this.selection.to ?
 250+ this.selection.to :
 251+ instruction === 'left' ? this.selection.start : this.selection.end;
257252 }
258 - fakePosition = this.documentView.getRenderedPositionFromOffset(
259 - this.documentView.getOffsetFromRenderedPosition( fakePosition )
 253+ newTo = this.documentView.getModel().getRelativeContentOffset(
 254+ offset,
 255+ instruction === 'left' ? -1 : 1
260256 );
261 - fakePosition.left = this.cursor.initialLeft;
262 - } while ( position.top === fakePosition.top );
 257+ break;
 258+ case 'home' :
 259+ case 'end' :
 260+ var range = this.documentView.getRenderedLineRangeFromOffset(
 261+ this.cursor.initialBias ?
 262+ this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 ) :
 263+ this.selection.to
 264+ );
 265+ newTo = instruction === 'home' ? range.start : range.end;
 266+ break;
 267+ case 'up' :
 268+ case 'down' :
 269+ /*
 270+ * Looks for the in-document character position that would match up with the same
 271+ * horizontal position - jumping a few pixels up/down at a time until we reach
 272+ * the next/previous line
 273+ */
263274
264 - to = this.documentView.getOffsetFromRenderedPosition( fakePosition );
265 - if ( !this.keyboard.keys.shift ) {
266 - from = to;
267 - }
268 -
269 - } else if ( instruction === 'left' ) {
270 - this.cursor.initialLeft = null;
271 - if ( !this.keyboard.keys.shift ) {
272 - from = to = this.documentView.getModel().getRelativeContentOffset(
273 - this.selection.getLength() ? this.selection.start : this.selection.to, -1 );
274 - } else {
275 - to = this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 );
276 - }
277 - } else if ( instruction === 'right' ) {
278 - this.cursor.initialLeft = null;
279 - if ( !this.keyboard.keys.shift ) {
280 - from = to = this.documentView.getModel().getRelativeContentOffset(
281 - this.selection.getLength() ? this.selection.end : this.selection.to, 1 );
282 - } else {
283 - to = this.documentView.getModel().getRelativeContentOffset( this.selection.to, 1 );
284 - }
285 - } else if ( instruction === 'home' ) {
286 - this.cursor.initialLeft = null;
287 - to = this.documentView.getRenderedLineRangeFromOffset(
288 - this.cursor.initialBias ?
289 - this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 ) :
290 - this.selection.to
291 - ).start;
292 - if ( !this.keyboard.keys.shift ) {
293 - from = to;
294 - }
295 - } else if ( instruction === 'end' ) {
296 - this.cursor.initialLeft = null;
297 - to = this.documentView.getRenderedLineRangeFromOffset(
298 - this.cursor.initialBias ?
299 - this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 ) :
300 - this.selection.to
301 - ).end;
302 - if ( !this.keyboard.keys.shift ) {
303 - from = to;
304 - }
 275+ var position = this.documentView.getRenderedPositionFromOffset( this.selection.to );
 276+ if ( this.cursor.initialLeft === null ) {
 277+ this.cursor.initialLeft = position.left;
 278+ }
 279+ var fakePosition = new es.Position( this.cursor.initialLeft, position.top ),
 280+ i = 0,
 281+ step = instruction === 'up' ? -5 : 5,
 282+ top = this.$.position().top;
 283+ do {
 284+ fakePosition.top += ++i * step;
 285+ if ( fakePosition.top < top || fakePosition.top > top + this.dimensions.height ) {
 286+ break;
 287+ }
 288+ fakePosition = this.documentView.getRenderedPositionFromOffset(
 289+ this.documentView.getOffsetFromRenderedPosition( fakePosition )
 290+ );
 291+ fakePosition.left = this.cursor.initialLeft;
 292+ } while ( position.top === fakePosition.top );
 293+ newTo = this.documentView.getOffsetFromRenderedPosition( fakePosition );
 294+ break;
305295 }
306296
307 - if ( from === to ) {
308 - if ( this.selection.from !== this.selection.to ) {
 297+
 298+ if( instruction === 'end' ) {
 299+ this.cursor.initialBias = true;
 300+ } else {
 301+ this.cursor.initialBias = false;
 302+ }
 303+
 304+ if ( !this.keyboard.keys.shift ) {
 305+ if ( this.selection.from !== this.selection.to ) {
309306 this.documentView.clearSelection();
310307 }
311 - this.selection.from = this.selection.to = to;
 308+ this.selection.from = this.selection.to = newTo;
 309+ this.showCursor();
312310 } else {
313 - this.selection.to = to;
314 - this.documentView.drawSelection( this.selection );
 311+ this.selection.to = newTo;
 312+ if ( this.selection.from !== this.selection.to ) {
 313+ this.documentView.drawSelection( this.selection );
 314+ this.hideCursor();
 315+ } else {
 316+ this.showCursor();
 317+ }
315318 }
316 -
317 - if ( this.selection.from !== this.selection.to ) {
318 - this.hideCursor();
319 - if(instruction === 'home')
320 - this.cursor.initialBias = false;
321 - else if(instruction === 'end')
322 - this.cursor.initialBias = true;
323 - } else {
324 - this.showCursor( instruction === 'end' );
325 - }
326319 };
327320
328321 /**
@@ -330,10 +323,9 @@
331324 * @method
332325 * @param offset {Integer} Position to show the cursor at
333326 */
334 -es.SurfaceView.prototype.showCursor = function( leftBias ) {
335 - this.cursor.initialBias = leftBias ? true : false;
 327+es.SurfaceView.prototype.showCursor = function() {
336328 var position = this.documentView.getRenderedPositionFromOffset(
337 - this.selection.to, leftBias
 329+ this.selection.to, this.cursor.initialBias
338330 );
339331 this.cursor.$.css( {
340332 'left': position.left,

Status & tagging log