r89840 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89839‎ | r89840 | r89841 >
Date:19:00, 10 June 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Replaced closestToOffset plugin with much simpler function that only takes vertical axis into account.
Modified paths:
  • /trunk/parsers/wikidom/demos/surface/index.html (modified) (history)
  • /trunk/parsers/wikidom/lib/jquery.closestToOffset.js (deleted) (history)
  • /trunk/parsers/wikidom/lib/jquery.editSurface.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/jquery.closestToOffset.js
@@ -1,33 +0,0 @@
2 -$.fn.closestToOffset = function( offset ) {
3 - var $e = null,
4 - x = offset.left,
5 - y = offset.top,
6 - o,
7 - d,
8 - dx,
9 - dy,
10 - md;
11 - this.each( function() {
12 - var o = $(this).offset();
13 - if ( ( x >= o.left ) && ( x <= o.right ) && ( y >= o.top ) && ( y <= o.bottom ) ) {
14 - $e = $(this);
15 - return false;
16 - }
17 - var offsets = [
18 - [o.left, o.top],
19 - [o.right, o.top],
20 - [o.left, o.bottom],
21 - [o.right, o.bottom]
22 - ];
23 - for ( var i = 0; i < offsets.length; i++ ) {
24 - dx = offsets[i][0] - x;
25 - dy = offsets[i][1] - y;
26 - d = Math.sqrt( ( dx * dx ) + ( dy * dy ) );
27 - if ( md === undefined || d < md ) {
28 - md = d;
29 - $e = $(this);
30 - }
31 - }
32 - } );
33 - return $e;
34 -};
Index: trunk/parsers/wikidom/lib/jquery.editSurface.js
@@ -32,7 +32,7 @@
3333 'mousedown': function( e ) {
3434 var $target = $( e.target );
3535 if ( $target.is( '.editSurface-paragraph' ) ) {
36 - $target = $target.children().closestToOffset( { 'left': e.pageX, 'top': e.pageY } );
 36+ $target = getNearestLine( $target.children(), e.pageY );
3737 }
3838 if ( !$target.is( '.editSurface-line' ) ) {
3939 var $line = $target.closest( '.editSurface-line' );
@@ -81,14 +81,9 @@
8282 if ( sel.active ) {
8383 var $target = $( e.target );
8484 if ( !$target.is( '.editSurface-line' ) ) {
85 - $target = sel.start.$target.parent().children().closestToOffset(
86 - { 'left': e.pageX, 'top': e.pageY }
87 - );
 85+ $target = getNearestLine( sel.start.$target.parent().children(), e.pageY );
8886 }
8987 sel.end = getCursorPosition( e.pageX, e.pageY, $target );
90 - //console.log( [sel.start.char, sel.end.char] );
91 - //console.log( [sel.start.word, sel.end.word] );
92 - //console.log( [sel.start.line, sel.end.line] );
9388 if ( sel.start.line < sel.end.line
9489 || ( sel.start.line === sel.end.line
9590 && sel.start.char < sel.end.char ) ) {
@@ -105,6 +100,26 @@
106101 } );
107102
108103 // Functions
 104+ function getNearestLine( $lines, y ) {
 105+ var $line,
 106+ minDistance;
 107+ $lines.each( function() {
 108+ var top = $(this).offset().top;
 109+ var bottom = top + $(this).height();
 110+ // Inside test
 111+ if ( y > top && y < bottom ) {
 112+ $line = $(this);
 113+ return false;
 114+ }
 115+ // Distance test
 116+ var distance = Math.abs( y - top );
 117+ if ( minDistance === undefined || distance < minDistance ) {
 118+ minDistance = distance;
 119+ $line = $(this);
 120+ }
 121+ } );
 122+ return $line;
 123+ }
109124 function getSelectionText() {
110125 var text;
111126 if ( sel.from && sel.to ) {
Index: trunk/parsers/wikidom/demos/surface/index.html
@@ -12,7 +12,6 @@
1313
1414 <!-- EditSurface -->
1515 <script type="text/javascript" src="../../lib/jquery.js"></script>
16 - <script type="text/javascript" src="../../lib/jquery.closestToOffset.js"></script>
1716 <script type="text/javascript" src="../../lib/jquery.flow.js"></script>
1817 <script type="text/javascript" src="../../lib/jquery.editSurface.js"></script>
1918

Status & tagging log