r89744 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89743‎ | r89744 | r89745 >
Date:20:31, 8 June 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Made selection more accurate
Modified paths:
  • /trunk/parsers/wikidom/lib/jquery.editSurface.css (modified) (history)
  • /trunk/parsers/wikidom/lib/jquery.editSurface.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/jquery.editSurface.css
@@ -1,6 +1,6 @@
22 body {
33 font-family: "Arial";
4 - font-size: 0.8em;
 4+ font-size: 1em;
55 }
66
77 #selection {
Index: trunk/parsers/wikidom/lib/jquery.editSurface.js
@@ -24,21 +24,21 @@
2525 // TODO: If the target is not a line, find the nearest line to the cursor and use it
2626 if ( $target.is( '.editSurface-line' ) ) {
2727 e.preventDefault();
28 - e.stopPropagation();
2928 sel = {
3029 'active': true,
3130 'from': null,
3231 'to': null,
33 - 'start': getSelection( e ),
 32+ 'start': getCursorPosition( e ),
3433 'end': null
3534 };
 35+ console.log( sel.start );
3636 cursor.show();
3737 drawSelection( $target.parent() );
3838 // Move cursor
3939 if ( sel.start ) {
4040 cursor.$.css( {
4141 'top': sel.start.top,
42 - 'left': sel.start.left
 42+ 'left': sel.start.x
4343 } );
4444 }
4545 }
@@ -61,8 +61,8 @@
6262 var $target = $( e.target );
6363 // TODO: If the target is not a line, find the nearest line to the cursor and use it
6464 if ( $target.is( '.editSurface-line' ) && sel.active ) {
65 - sel.end = getSelection( e );
66 - if ( sel.start.line < sel.end.line
 65+ sel.end = getCursorPosition( e );
 66+ if ( sel.start && sel.end && sel.start.line < sel.end.line
6767 || ( sel.start.line === sel.end.line
6868 && sel.start.index < sel.end.index ) ) {
6969 sel.from = sel.start;
@@ -106,7 +106,8 @@
107107 }
108108 return text;
109109 }
110 - function getSelection( e ) {
 110+ // TODO: Take x and y, and infer the target
 111+ function getCursorPosition( e ) {
111112 var $target = $( e.target );
112113 var metrics = $target.data( 'metrics' );
113114 var text = $target.data( 'text' );
@@ -115,23 +116,22 @@
116117 return null;
117118 }
118119 var to = metrics.length - 1;
119 - var l = 0;
120 - var r = 0;
 120+ var a;
 121+ var b = { 'l': 0, 'c': 0, 'r': 0 };
 122+ var x = e.layerX;
121123 for ( var i = 0; i <= to; i++ ) {
122 - l = r;
123 - r += metrics[i];
124 - if ( ( e.layerX > l && e.layerX <= r ) || ( e.layerX >= r && i == to ) ) {
 124+ a = b;
 125+ b = { 'l': a.r, 'c': a.r + ( metrics[i] / 2 ), 'r': a.r + metrics[i] };
 126+ if ( ( i === 0 && x < a.l ) || ( x > a.c && x <= b.c ) || ( i === to && x >= b.r ) ) {
125127 var offset = $target.offset();
126128 var height = $target.height();
127129 return {
128130 '$target': $target,
129131 'index': i,
130132 'line': line,
 133+ 'x': offset.left + b.l,
131134 'top': offset.top,
132 - 'left': offset.left + l,
133 - 'right': r,
134135 'bottom': offset.top + height,
135 - 'width': r - l,
136136 'height': height
137137 };
138138 }
@@ -145,9 +145,9 @@
146146 // 1 line
147147 if ( sel.from.index !== sel.to.index ) {
148148 ranges.$first.show().css( {
149 - 'left': sel.from.left,
 149+ 'left': sel.from.x,
150150 'top': sel.from.top,
151 - 'width': sel.to.right - sel.from.right,
 151+ 'width': sel.to.x - sel.from.x,
152152 'height': sel.from.height
153153 } );
154154 ranges.$fill.hide();
@@ -159,9 +159,9 @@
160160 } else if ( sel.from.line < sel.to.line ) {
161161 // 2+ lines
162162 ranges.$first.show().css( {
163 - 'left': sel.from.left,
 163+ 'left': sel.from.x,
164164 'top': sel.from.top,
165 - 'width': ( $container.innerWidth() - sel.from.left )
 165+ 'width': ( $container.innerWidth() - sel.from.x )
166166 + $container.offset().left,
167167 'height': sel.from.height
168168 } );
@@ -178,7 +178,7 @@
179179 ranges.$last.show().css( {
180180 'left': $container.offset().left,
181181 'top': sel.to.top,
182 - 'width': sel.to.left - $container.offset().left,
 182+ 'width': sel.to.x - $container.offset().left,
183183 'height': sel.to.height
184184 } );
185185 // XXX: Demo code!

Status & tagging log