Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js |
— | — | @@ -37,35 +37,65 @@ |
38 | 38 | |
39 | 39 | }; |
40 | 40 | |
41 | | -TextFlow.prototype.getPosition = function( start, end ) { |
42 | | - var i = 0, |
43 | | - startLine, |
44 | | - endLine, |
| 41 | +TextFlow.prototype.getPosition = function( offset ) { |
| 42 | + if ( offset < 0 ) { |
| 43 | + throw 'Out of range error. Offset is expected to be greater than or equal to 0.'; |
| 44 | + } |
| 45 | + var line = 0, |
| 46 | + lineCount = this.lines.length, |
45 | 47 | position = { |
| 48 | + 'left': 0, |
46 | 49 | 'top': 0, |
47 | | - 'right': 0, |
48 | | - 'bottom': 0, |
49 | | - 'left': 0 |
| 50 | + 'bottom': 0 |
50 | 51 | }; |
51 | | - while ( i++ < lines.length ) { |
52 | | - if ( start >= lines[i].start && start <= lines[i].end ) { |
53 | | - startLine = i; |
| 52 | + |
| 53 | + /* |
| 54 | + * Line finding |
| 55 | + * |
| 56 | + * It's possible that a more efficient method could be used here, but the number of lines to be |
| 57 | + * iterated through will rarely be over 100, so it's unlikely that any significant gains will be |
| 58 | + * had. Plus, as long as we are iterating over each line, we can also sum up the top and bottom |
| 59 | + * positions, which is a nice benefit of this method. |
| 60 | + */ |
| 61 | + while ( line < lineCount ) { |
| 62 | + if ( offset >= lines[line].start && offset < lines[line].end ) { |
| 63 | + position.bottom = position.top + lines[line].height; |
54 | 64 | break; |
55 | 65 | } |
56 | | - position.top += lines[i].height; |
| 66 | + position.top += lines[line].height; |
| 67 | + line++; |
57 | 68 | }; |
58 | | - position.bottom = top |
59 | | - if ( length !== undefined ) { |
60 | | - i = startLine; |
61 | | - while ( i++ < lines.length ) { |
62 | | - if ( end >= lines[i].start && end <= lines[i].end ) { |
63 | | - startLine = i; |
64 | | - position.bottom += lines[i].height; |
65 | | - break; |
66 | | - } |
67 | | - }; |
| 69 | + |
| 70 | + /* |
| 71 | + * Virtual n+1 position |
| 72 | + * |
| 73 | + * To allow access to position information of the right side of the last character on the last |
| 74 | + * line, a virtual n+1 position is supported. Offsets beyond this virtual position will cause |
| 75 | + * an exception to be thrown. |
| 76 | + */ |
| 77 | + if ( line === lineCount ) { |
| 78 | + if ( offset !== lines[line].end + 1 ) { |
| 79 | + line--; |
| 80 | + position.bottom = position.top; |
| 81 | + position.top -= lines[line].height; |
| 82 | + } else { |
| 83 | + throw 'Out of range error. Offset is expected to be less than or equal to text length.'; |
| 84 | + } |
68 | 85 | } |
69 | | - // TODO: Calculate left and right positions |
| 86 | + |
| 87 | + /* |
| 88 | + * Offset measuring |
| 89 | + * |
| 90 | + * Since the left position will be zero for the first character in the line, so we can skip |
| 91 | + * measuring for those cases. |
| 92 | + */ |
| 93 | + if ( lines[line].start < offset ) { |
| 94 | + var $lineRuler = $( '<div class="editSurface-line"></div>' ).appendTo( this.$ ), |
| 95 | + lineRuler = $lineRuler[0]; |
| 96 | + lineRuler.innerHTML = this.htmlEncode( text.substring( lines[startLine].start, offset ) ); |
| 97 | + position.left = lineRuler.clientWidth; |
| 98 | + $lineRuler.remove(); |
| 99 | + } |
70 | 100 | return position; |
71 | 101 | }; |
72 | 102 | |