r91678 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91677‎ | r91678 | r91679 >
Date:20:27, 7 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
* Re-enabled (and fixed) virtual n+1 position support code
* Fixed cursor position for end of line
* Added nearest block calculation for position to location lookups using positions outside of blocks
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Surface.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.TextFlow.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.Surface.js
@@ -67,16 +67,31 @@
6868
6969 Surface.prototype.onMouseDown = function( e ) {
7070 var $target = $( e.target );
71 - $block = $target.is( '.editSurface-block' ) ? $target : $target.closest( '.editSurface-block' ),
72 - block = $block.data( 'block' );
73 -
74 - if( !block ) {
75 - return false;
 71+ $block = $target.is( '.editSurface-block' )
 72+ ? $target : $target.closest( '.editSurface-block' );
 73+ // Not a block or child of a block? Find the nearest block...
 74+ if( !$block.length ) {
 75+ var minDistance;
 76+ this.$.find( '> .editSurface-document .editSurface-block' ).each( function() {
 77+ var top = $(this).offset().top,
 78+ bottom = top + $(this).height();
 79+ // Inside test
 80+ if ( e.pageY >= top && e.pageY < bottom ) {
 81+ $block = $(this);
 82+ return false;
 83+ }
 84+ // Distance test
 85+ var distance = Math.abs( e.pageY - top );
 86+ if ( typeof minDistance === 'undefined' || distance < minDistance ) {
 87+ minDistance = distance;
 88+ $block = $(this);
 89+ }
 90+ } );
7691 }
77 -
78 - var position = new Position( e.pageX - $block.offset().left,
79 - e.pageY - $block.offset().top );
80 - var offset = block.flow.getOffset( position );
 92+ var block = $block.data( 'block' )
 93+ blockOffset = $block.offset()
 94+ position = new Position( e.pageX - blockOffset.left, e.pageY - blockOffset.top )
 95+ offset = block.flow.getOffset( position );
8196 this.cursor.show( new Location( block, offset ) );
8297 this.$input.focus();
8398 return false;
Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js
@@ -15,6 +15,7 @@
1616 if ( text !== undefined ) {
1717 this.setText( text );
1818 }
 19+ this.boundaryTest = /([ \.\,\;\:\-\t\r\n\f])/g;
1920 }
2021
2122 /**
@@ -87,7 +88,12 @@
8889 var right = ruler.clientWidth;
8990 var center = Math.round( left + ( ( right - left ) / 2 ) );
9091 $ruler.remove();
91 - return fit.end + ( position.x >= center ? 1 : 0 );
 92+ // Reset RegExp object's state
 93+ this.boundaryTest.lastIndex = 0;
 94+ var virtual = line < this.lines.length - 1
 95+ && this.boundaryTest( this.lines[line].text.substr( -1, 1 ) )
 96+ ? -1 : 0;
 97+ return Math.min( fit.end + ( position.x >= center ? 1 : 0 ), this.lines[line].end + virtual );
9298 };
9399
94100 /**
@@ -127,7 +133,7 @@
128134 position.top += this.lines[line].height;
129135 line++;
130136 };
131 -
 137+
132138 /*
133139 * Virtual n+1 position
134140 *
@@ -135,17 +141,15 @@
136142 * line, a virtual n+1 position is supported. Offsets beyond this virtual position will cause
137143 * an exception to be thrown.
138144 */
139 -/*
140145 if ( line === lineCount ) {
141 - if ( offset !== lines[line].end + 1 ) {
 146+ if ( offset !== this.lines[line - 1].end + 1 ) {
142147 line--;
143148 position.bottom = position.top;
144 - position.top -= lines[line].height;
 149+ position.top -= this.lines[line].height;
145150 } else {
146151 throw 'Out of range error. Offset is expected to be less than or equal to text length.';
147152 }
148153 }
149 -*/
150154
151155 /*
152156 * Offset measuring
@@ -186,12 +190,13 @@
187191 // Purge "boundaries" and "words" arrays
188192 this.boundaries = [];
189193 this.words = [];
 194+ // Reset RegExp object's state
 195+ this.boundaryTest.lastIndex = 0;
190196 // Iterate over each word+boundary sequence, capturing offsets and encoding text as we go
191 - var boundary = /([ \.\,\;\:\-\t\r\n\f])/g,
192 - match,
 197+ var match,
193198 start = 0,
194199 end;
195 - while ( match = boundary.exec( text ) ) {
 200+ while ( match = this.boundaryTest.exec( text ) ) {
196201 // Include the boundary character in the range
197202 end = match.index + 1;
198203 // Store the boundary offset
@@ -387,7 +392,7 @@
388393 if ( ruler.clientWidth > width ) {
389394 // Detect impossible fit (the first character won't fit by itself)
390395 if (middle - offset === 1) {
391 - start = middle;
 396+ start = middle - 1;
392397 break;
393398 }
394399 // Words after "middle" won't fit

Status & tagging log