https://www.mediawiki.org/wiki/Special:Code/MediaWiki/102053
Revision: 102053
Author: inez
Date: 2011-11-04 21:40:17 +0000 (Fri, 04 Nov 2011)
Log Message:
-----------
Fix cursor and selection
Modified Paths:
--------------
trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
Modified: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
2011-11-04 21:40:09 UTC (rev 102052)
+++ trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
2011-11-04 21:40:17 UTC (rev 102053)
@@ -230,105 +230,98 @@
es.SurfaceView.prototype.moveCursor = function( instruction ) {
this.selection.normalize();
- var offset;
- if ( instruction === 'left') {
- if ( !this.keyboard.keys.shift ) {
- this.selection.from = this.selection.to =
-
this.documentView.getModel().getRelativeContentOffset(
- this.selection.getLength() ?
this.selection.start : this.selection.to, -1
- );
- this.documentView.clearSelection();
- this.showCursor();
- } else {
- this.selection.to =
this.documentView.getModel().getRelativeContentOffset(
- this.selection.to, -1
- );
- this.documentView.drawSelection( this.selection );
- this.hideCursor();
- }
- } else if ( instruction === 'right' ) {
- if ( !this.keyboard.keys.shift ) {
- this.selection.from = this.selection.to =
-
this.documentView.getModel().getRelativeContentOffset(
- this.selection.getLength() ?
this.selection.end : this.selection.to, 1
- );
- this.documentView.clearSelection();
- this.showCursor();
- } else {
- this.selection.to =
this.documentView.getModel().getRelativeContentOffset(
- this.selection.to, 1
- );
- this.documentView.drawSelection( this.selection );
- this.hideCursor();
- }
- } else if ( instruction === 'up' || instruction === 'down' ) {
- var currentPosition =
this.documentView.getRenderedPositionFromOffset( this.selection.to );
- if ( this.cursor.initialLeft === null ) {
- this.cursor.initialLeft = currentPosition.left;
- }
+ var from, to;
- var fakePosition = new es.Position(
this.cursor.initialLeft, currentPosition.top ),
- step,
- edge,
- i = 0;
+ if ( instruction === 'up' || instruction === 'down' ) {
+ /*
+ * Looks for the in-document character position that would
match up with the same horizontal
+ * position - jumping a few pixels up/down at a time until we
reach the next/previous line
+ */
- if ( instruction === 'up' ) {
- step = -5;
- edge = 0;
- } else {
- step = 5;
- edge = this.documentView.getContentLength();
+ var position = this.documentView.getRenderedPositionFromOffset(
this.selection.to );
+ if ( this.cursor.initialLeft === null ) {
+ this.cursor.initialLeft = position.left;
}
+ var fakePosition = new es.Position(
this.cursor.initialLeft, position.top ),
+ i = 0,
+ step = instruction === 'up' ? -5 : 5,
+ top = this.$.position().top;
+
do {
fakePosition.top += ++i * step;
- offset =
this.documentView.getOffsetFromRenderedPosition( fakePosition );
- fakePosition =
this.documentView.getRenderedPositionFromOffset( offset );
+ if ( fakePosition.top < top || fakePosition.top > top +
this.dimensions.height ) {
+ break;
+ }
+ fakePosition =
this.documentView.getRenderedPositionFromOffset(
+
this.documentView.getOffsetFromRenderedPosition( fakePosition )
+ );
fakePosition.left = this.cursor.initialLeft;
- } while ( currentPosition.top === fakePosition.top && offset
!== edge );
+ } while ( position.top === fakePosition.top );
+ to = this.documentView.getOffsetFromRenderedPosition(
fakePosition );
if ( !this.keyboard.keys.shift ) {
- this.selection.from = this.selection.to =
-
this.documentView.getOffsetFromRenderedPosition( fakePosition );
- this.documentView.clearSelection();
- this.showCursor();
+ from = to;
+ }
+
+ } else if ( instruction === 'left' ) {
+ this.cursor.initialLeft = null;
+ if ( !this.keyboard.keys.shift ) {
+ from = to =
this.documentView.getModel().getRelativeContentOffset(
+ this.selection.getLength() ?
this.selection.start : this.selection.to, -1 );
} else {
- this.selection.to =
this.documentView.getOffsetFromRenderedPosition( fakePosition );
- this.documentView.drawSelection( this.selection );
- this.hideCursor();
+ to =
this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 );
}
+ } else if ( instruction === 'right' ) {
+ this.cursor.initialLeft = null;
+ if ( !this.keyboard.keys.shift ) {
+ from = to =
this.documentView.getModel().getRelativeContentOffset(
+ this.selection.getLength() ? this.selection.end
: this.selection.to, 1 );
+ } else {
+ to =
this.documentView.getModel().getRelativeContentOffset( this.selection.to, 1 );
+ }
} else if ( instruction === 'home' ) {
- offset = this.documentView.getRenderedLineRangeFromOffset(
- this.cursor.initialBias ?
-
this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 ) :
- this.selection.to
+ this.cursor.initialLeft = null;
+ to = this.documentView.getRenderedLineRangeFromOffset(
+ this.cursor.initialBias
+ ?
this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 )
+ : this.selection.to
).start;
if ( !this.keyboard.keys.shift ) {
- this.selection.from = this.selection.to = offset;
- this.documentView.clearSelection();
- this.showCursor();
- } else {
- this.selection.to = offset;
- this.documentView.drawSelection( this.selection );
- this.hideCursor();
+ from = to;
}
} else if ( instruction === 'end' ) {
- offset = this.documentView.getRenderedLineRangeFromOffset(
- this.cursor.initialBias ?
-
this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 ) :
- this.selection.to
+ this.cursor.initialLeft = null;
+ to = this.documentView.getRenderedLineRangeFromOffset(
+ this.cursor.initialBias
+ ?
this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 )
+ : this.selection.to
).end;
if ( !this.keyboard.keys.shift ) {
- this.selection.from = this.selection.to = offset;
+ from = to;
+ }
+ }
+
+ if ( from === to ) {
+ if ( this.selection.from !== this.selection.to ) {
this.documentView.clearSelection();
- this.showCursor();
- } else {
- this.selection.to = offset;
- this.documentView.drawSelection( this.selection );
- this.hideCursor();
}
+ this.selection.from = this.selection.to = to;
+ } else {
+ this.selection.to = to;
+ this.documentView.drawSelection( this.selection );
}
+
+ if ( this.selection.from !== this.selection.to ) {
+ this.hideCursor();
+ if(instruction === 'home')
+ this.cursor.initialBias = false;
+ else if(instruction === 'end')
+ this.cursor.initialBias = true;
+ } else {
+ this.showCursor( instruction === 'end' );
+ }
};
/**
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs