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 @@ |
33 | 33 | 'mousedown': function( e ) { |
34 | 34 | var $target = $( e.target ); |
35 | 35 | if ( $target.is( '.editSurface-paragraph' ) ) { |
36 | | - $target = $target.children().closestToOffset( { 'left': e.pageX, 'top': e.pageY } ); |
| 36 | + $target = getNearestLine( $target.children(), e.pageY ); |
37 | 37 | } |
38 | 38 | if ( !$target.is( '.editSurface-line' ) ) { |
39 | 39 | var $line = $target.closest( '.editSurface-line' ); |
— | — | @@ -81,14 +81,9 @@ |
82 | 82 | if ( sel.active ) { |
83 | 83 | var $target = $( e.target ); |
84 | 84 | 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 ); |
88 | 86 | } |
89 | 87 | 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] ); |
93 | 88 | if ( sel.start.line < sel.end.line |
94 | 89 | || ( sel.start.line === sel.end.line |
95 | 90 | && sel.start.char < sel.end.char ) ) { |
— | — | @@ -105,6 +100,26 @@ |
106 | 101 | } ); |
107 | 102 | |
108 | 103 | // 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 | + } |
109 | 124 | function getSelectionText() { |
110 | 125 | var text; |
111 | 126 | if ( sel.from && sel.to ) { |
Index: trunk/parsers/wikidom/demos/surface/index.html |
— | — | @@ -12,7 +12,6 @@ |
13 | 13 | |
14 | 14 | <!-- EditSurface --> |
15 | 15 | <script type="text/javascript" src="../../lib/jquery.js"></script> |
16 | | - <script type="text/javascript" src="../../lib/jquery.closestToOffset.js"></script> |
17 | 16 | <script type="text/javascript" src="../../lib/jquery.flow.js"></script> |
18 | 17 | <script type="text/javascript" src="../../lib/jquery.editSurface.js"></script> |
19 | 18 | |