r91691 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91690‎ | r91691 | r91692 >
Date:21:46, 7 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Improved block and line "closest-to" functionality.
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
@@ -23,13 +23,26 @@
2424 this.document = document;
2525 this.rendered = false;
2626 this.location = null;
 27+ this.selection = null;
2728 this.render();
28 -
 29+
 30+ this.state = {
 31+ 'selection': {
 32+ 'active': false
 33+ }
 34+ };
 35+
2936 var surface = this;
3037
3138 this.$.bind({
3239 'mousedown' : function(e) {
3340 return surface.onMouseDown( e );
 41+ },
 42+ 'mousemove' : function(e) {
 43+ return surface.onMouseMove( e );
 44+ },
 45+ 'mouseup' : function(e) {
 46+ return surface.onMouseUp( e );
3447 }
3548 });
3649
@@ -72,40 +85,51 @@
7386 ? $target : $target.closest( '.editSurface-block' );
7487 // Not a block or child of a block? Find the nearest block...
7588 if( !$block.length ) {
76 - var minDistance,
77 - $blocks = this.$.find( '> .editSurface-document .editSurface-block' );
 89+ $blocks = this.$.find( '> .editSurface-document .editSurface-block' );
 90+ $block = $blocks.first();
7891 $blocks.each( function() {
79 - var top = $(this).offset().top,
80 - bottom = top + $(this).height();
81 - // Inside test
82 - if ( e.pageY >= top && e.pageY < bottom ) {
83 - $block = $(this);
 92+ // Stop looking when mouse is above top
 93+ if ( e.pageY <= $(this).offset().top ) {
8494 return false;
8595 }
86 - // Distance test
87 - var distance = Math.abs( e.pageY - top );
88 - if ( typeof minDistance === 'undefined' || distance < minDistance ) {
89 - minDistance = distance;
90 - $block = $(this);
91 - }
 96+ $block = $(this);
9297 } );
93 - if ( !$block.length ) {
94 - $block = $blocks.first();
95 - }
9698 }
9799 var block = $block.data( 'block' )
98 - blockOffset = $block.offset()
99 - mousePosition = new Position( e.pageX - blockOffset.left, e.pageY - blockOffset.top )
 100+ blockPosition = $block.offset()
 101+ mousePosition = new Position( e.pageX - blockPosition.left, e.pageY - blockPosition.top )
100102 nearestOffset = block.flow.getOffset( mousePosition ),
101103 cursorPosition = block.flow.getPosition( nearestOffset );
102104
103 - this.cursor.show( cursorPosition, blockOffset );
 105+ this.cursor.show( cursorPosition, blockPosition );
104106 this.location = new Location( block, nearestOffset );
105107
 108+ this.state.selection = {
 109+ 'active': true,
 110+ 'from': null,
 111+ 'to': null,
 112+ 'start': nearestOffset,
 113+ 'end': null
 114+ };
 115+
106116 this.$input.focus();
107117 return false;
108118 };
109119
 120+Surface.prototype.onMouseMove = function( e ) {
 121+ var sel = this.state.selection;
 122+ if ( sel.active ) {
 123+ //
 124+ }
 125+};
 126+
 127+Surface.prototype.onMouseUp = function( e ) {
 128+ var sel = this.state.selection;
 129+ if ( sel.active ) {
 130+ sel.active = false;
 131+ }
 132+};
 133+
110134 /**
111135 * Sets the selection to a new range.
112136 *
Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js
@@ -47,11 +47,10 @@
4848 * @return {Integer} Offset within content nearest the given coordinates
4949 */
5050 TextFlow.prototype.getOffset = function( position ) {
51 - var line = 0,
52 - lineCount = this.lines.length,
 51+ var lineCount = this.lines.length,
 52+ line = 0,
5353 offset = 0,
54 - top = 0,
55 - bottom = 0;
 54+ top = 0;
5655
5756 /*
5857 * Line finding
@@ -62,13 +61,12 @@
6362 * positions, which is a nice benefit of this method.
6463 */
6564 while ( line < lineCount ) {
66 - bottom += this.lines[line].height;
67 - if ( position.top >= top && position.top < bottom ) {
 65+ top += this.lines[line].height;
 66+ if ( position.top <= top ) {
6867 break;
6968 }
70 - top = bottom;
7169 line++;
72 - };
 70+ }
7371
7472 /*
7573 * Offset finding
@@ -79,6 +77,9 @@
8078 * TODO: The offset needs to be chosen based on nearest offset to the cursor, not offset before
8179 * the cursor.
8280 */
 81+ var virtual = line < this.lines.length - 1
 82+ && this.boundaryTest.exec( this.lines[line].text.substr( -1 ) ) ? -1 : 0;
 83+ line = Math.min( line, this.lines.length - 1 );
8384 var $ruler = $( '<div class="editSurface-line"></div>' ).appendTo( this.$ )
8485 ruler = $ruler[0],
8586 fit = this.fitCharacters(
@@ -92,9 +93,6 @@
9394 $ruler.remove();
9495 // Reset RegExp object's state
9596 this.boundaryTest.lastIndex = 0;
96 - var virtual = line < this.lines.length - 1
97 - && this.boundaryTest.exec( this.lines[line].text.substr( -1, 1 ) )
98 - ? -1 : 0;
9997 return Math.min(
10098 fit.end + ( position.left >= center ? 1 : 0 ),
10199 this.lines[line].end + virtual

Status & tagging log