r94588 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94587‎ | r94588 | r94589 >
Date:23:10, 15 August 2011
Author:inez
Status:deferred
Tags:
Comment:
Proper implementation and use of getLineIndex
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Block.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Flow.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ListBlock.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Surface.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js
@@ -141,6 +141,10 @@
142142 return this.flow.getPosition( offset );
143143 };
144144
 145+es.ParagraphBlock.prototype.getLineIndex = function( offset ) {
 146+ return this.flow.getLineIndex( offset );
 147+};
 148+
145149 /**
146150 * Gets the start and end points of the word closest a given offset.
147151 *
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js
@@ -97,11 +97,28 @@
9898 position.top += contentOffset.top - blockOffset.top;
9999 position.left += contentOffset.left - blockOffset.left;
100100 position.bottom += contentOffset.top - blockOffset.top;
101 - position.line += location.linesBefore;
102 -
 101+
103102 return position;
104103 };
105104
 105+es.ListBlock.prototype.getLineIndex = function( offset ) {
 106+ var globalOffset = 0,
 107+ lineIndex = 0,
 108+ itemLength;
 109+
 110+ this.list.traverseItems( function( item, index ) {
 111+ itemLength = item.content.getLength();
 112+ if ( offset >= globalOffset && offset <= globalOffset + itemLength ) {
 113+ lineIndex += item.flow.getLineIndex( offset - globalOffset );
 114+ return false;
 115+ }
 116+ globalOffset += itemLength + 1;
 117+ lineIndex += item.flow.lines.length;
 118+ } );
 119+
 120+ return lineIndex;
 121+};
 122+
106123 es.ListBlock.prototype.getText = function( range, render ) {
107124 return "";
108125 };
Index: trunk/parsers/wikidom/lib/es/es.Surface.js
@@ -452,13 +452,16 @@
453453 };
454454 if ( from.location.block === to.location.block ) {
455455 var block = from.location.block,
456 - blockOffset = block.$.offset();
 456+ blockOffset = block.$.offset(),
 457+ fromLineIndex = from.location.block.getLineIndex( this.selection.start.offset ),
 458+ toLineIndex = to.location.block.getLineIndex( this.selection.end.offset );
 459+
457460 if ( from.location.offset === to.location.offset ) {
458461 // No selection, just hide them all
459462 this.$rangeStart.hide();
460463 this.$rangeFill.hide();
461464 this.$rangeEnd.hide();
462 - } else if ( from.position.line === to.position.line ) {
 465+ } else if ( fromLineIndex === toLineIndex ) {
463466 // Single line selection
464467 this.$rangeStart
465468 .css( {
@@ -489,7 +492,7 @@
490493 'height': to.position.bottom - to.position.top
491494 } )
492495 .show();
493 - if ( from.position.line + 1 < to.position.line ) {
 496+ if ( fromLineIndex + 1 < toLineIndex ) {
494497 this.$rangeFill
495498 .css( {
496499 'top': blockOffset.top + from.position.bottom,
Index: trunk/parsers/wikidom/lib/es/es.Block.js
@@ -192,6 +192,10 @@
193193 throw 'Block.getPosition not implemented in this subclass.';
194194 };
195195
 196+es.Block.prototype.getLineIndex = function( offset ) {
 197+ throw 'Block.getLineIndex not implemented in this subclass.';
 198+};
 199+
196200 /**
197201 * Gets the start and end points of the word closest a given offset.
198202 *
Index: trunk/parsers/wikidom/lib/es/es.Flow.js
@@ -50,6 +50,14 @@
5151 this.scanBoundaries();
5252 }
5353
 54+es.Flow.prototype.getLineIndex = function( offset ) {
 55+ for ( var i = 0; i < this.lines.length; i++ ) {
 56+ if ( this.lines[i].range.containsOffset( offset ) ) {
 57+ return i;
 58+ }
 59+ }
 60+};
 61+
5462 /**
5563 * Gets offset within content closest to of a given position.
5664 *
@@ -74,14 +82,21 @@
7583 return 0;
7684 }
7785 // Find which line the position is inside of
78 - var lineIndex = this.getLineIndexFromPosition( position );
79 -
 86+ var i = 0,
 87+ top = 0;
 88+ while ( i < lineCount ) {
 89+ top += this.lines[i].height;
 90+ if ( position.top <= top ) {
 91+ break;
 92+ }
 93+ i++;
 94+ }
8095 // Positions below the last line always jump to the last offset
81 - if ( lineIndex == lineCount ) {
 96+ if ( i == lineCount ) {
8297 return this.content.getLength();
8398 }
8499 // Alias current line object
85 - var line = this.lines[lineIndex];
 100+ var line = this.lines[i];
86101
87102 /*
88103 * Offset finding
@@ -112,23 +127,6 @@
113128 };
114129
115130 /**
116 - * Gets a line index for a given position.
117 - *
118 - * @param position {Object} Position to find line index for
119 - * @return {Integer} Line index
120 - */
121 -es.Flow.prototype.getLineIndexFromPosition = function( position ) {
122 - var i, top = 0;
123 - for ( i = 0; i < this.lines.length; i++ ) {
124 - top += this.lines[i].height;
125 - if ( position.top <= top ) {
126 - break;
127 - }
128 - }
129 - return i;
130 -};
131 -
132 -/**
133131 * Gets position coordinates of a given offset.
134132 *
135133 * Offsets are boundaries between plain or annotated characters within content. Results are given in
@@ -161,20 +159,20 @@
162160 */
163161 var line,
164162 lineCount = this.lines.length,
 163+ lineIndex = 0,
165164 position = {
166165 'left': 0,
167166 'top': 0,
168 - 'bottom': 0,
169 - 'line': 0
 167+ 'bottom': 0
170168 };
171 - while ( position.line < lineCount ) {
172 - line = this.lines[position.line];
 169+ while ( lineIndex < lineCount ) {
 170+ line = this.lines[lineIndex];
173171 if ( line.range.containsOffset( offset ) ) {
174172 position.bottom = position.top + line.height;
175173 break;
176174 }
177175 position.top += line.height;
178 - position.line++;
 176+ lineIndex++;
179177 }
180178
181179 /*
@@ -184,8 +182,7 @@
185183 * line, a virtual n+1 position is supported. Offsets beyond this virtual position will cause
186184 * an exception to be thrown.
187185 */
188 - if ( position.line === lineCount ) {
189 - position.line--;
 186+ if ( lineIndex === lineCount ) {
190187 position.bottom = position.top;
191188 position.top -= line.height;
192189 }
@@ -560,4 +557,4 @@
561558 return { 'end': start, 'width': lineWidth };
562559 };
563560
564 -es.extend( es.Flow, es.EventEmitter );
 561+es.extend( es.Flow, es.EventEmitter );
\ No newline at end of file

Status & tagging log