r92325 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92324‎ | r92325 | r92326 >
Date:22:12, 15 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Documentation and code cleanup
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.TextFlow.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js
@@ -3,10 +3,14 @@
44 *
55 * @extends {EventEmitter}
66 * @param $container {jQuery Selection} Element to render into
 7+ * @param content {Content} Initial content to render
78 * @returns {TextFlow}
89 */
910 function TextFlow( $container, content ) {
 11+ // Inheritance
1012 EventEmitter.call( this );
 13+
 14+ // Members
1115 this.$ = $container;
1216 this.content = content || new Content();
1317 this.boundaries = [];
@@ -16,6 +20,7 @@
1721 this.widthCache = {};
1822 this.renderState = {};
1923
 24+ // Events
2025 var flow = this;
2126 this.content.on( 'change', function() {
2227 flow.scanBoundaries();
@@ -29,8 +34,9 @@
3035 this.content.on( 'annotate', function( args ) {
3136 flow.render( args.start );
3237 } );
 38+
 39+ // Initialization
3340 this.scanBoundaries();
34 - this.render();
3541 }
3642
3743 /**
@@ -104,8 +110,8 @@
105111 /**
106112 * Gets position coordinates of a given offset.
107113 *
108 - * Offsets are boundaries between content. Results are given in left, top and bottom positions,
109 - * which could be used to draw a cursor, highlighting painting, etc.
 114+ * Offsets are boundaries between plain or annotated characters within content. Results are given in
 115+ * left, top and bottom positions, which could be used to draw a cursor, highlighting, etc.
110116 *
111117 * @param offset {Integer} Offset within content
112118 * @return {Object} Object containing left, top and bottom properties, each positions in pixels
@@ -179,6 +185,9 @@
180186 return position;
181187 };
182188
 189+/**
 190+ * Updates the word boundary cache, which is used for word fitting.
 191+ */
183192 TextFlow.prototype.scanBoundaries = function() {
184193 /*
185194 * Word boundary scan
@@ -213,6 +222,13 @@
214223 }
215224 };
216225
 226+/**
 227+ * Renders a batch of lines and then yields execution before rendering another batch.
 228+ *
 229+ * In cases where a single word is too long to fit on a line, the word will be "virtually" wrapped,
 230+ * causing them to be fragmented. Word fragments are rendered on their own lines, except for their
 231+ * remainder, which is combined with whatever proceeding words can fit on the same line.
 232+ */
217233 TextFlow.prototype.renderIteration = function() {
218234 var rs = this.renderState,
219235 iteration = 0;
@@ -268,25 +284,28 @@
269285 /**
270286 * Renders text into a series of HTML elements, each a single line of wrapped text.
271287 *
272 - * In cases where a single word is too long to fit on a line, the word will be "virtually" wrapped,
273 - * causing them to be fragmented. Word fragments are rendered on their own lines, except for their
274 - * remainder, which is combined with whatever proceeding words can fit on the same line.
275 - *
276288 * The offset parameter can be used to reduce the amount of work involved in re-rendering the same
277289 * text, but will be automatically ignored if the text or width of the container has changed.
278290 *
 291+ * Rendering happens asynchronously, and yields execution between iterations. Iterative rendering
 292+ * provides the JavaScript engine an ability to process events between rendering batches of lines,
 293+ * allowing rendering to be interrupted and restarted if changes to content are happening before
 294+ * rendering of all lines is complete.
 295+ *
279296 * @param offset {Integer} Offset to re-render from, if possible (not yet implemented)
280297 */
281298 TextFlow.prototype.render = function( offset ) {
282299 var rs = this.renderState;
283300
284 - // Stop iterating from last render
 301+ // Check if rendering is currently underway
285302 if ( rs.timeout !== undefined ) {
 303+ // Cancel the active rendering process
286304 clearTimeout( rs.timeout );
287305 // Cleanup
288306 rs.$ruler.remove();
289307 }
290308
 309+ // Clear caches that were specific to the previous render
291310 this.widthCache = {};
292311
293312 /*
@@ -299,17 +318,17 @@
300319 rs.$ruler = $( '<div>&nbsp;</div>' ).appendTo( this.$ );
301320 rs.width = rs.$ruler.innerWidth();
302321
303 - // TODO: Take offset into account
304322 // Ignore offset optimization if the width has changed or the text has never been flowed before
305 - //if (this.width !== width) {
306 - // offset = undefined;
307 - //}
 323+ if (this.width !== rs.width) {
 324+ offset = undefined;
 325+ }
 326+ this.width = rs.width;
308327
309 - // TODO: Take offset into account and only work from there
310 -
 328+ // TODO: Only clear lines that are below the line above the offset, or the nearest line above
 329+ // that which is not a fractional line
311330 this.lines = [];
312 - // Iterate over each word that will fit in a line, appending them to the DOM as we go
313331
 332+ // Reset the render state
314333 rs.wordOffset = 0;
315334 rs.lineStart = 0;
316335 rs.lineEnd = 0;
@@ -320,6 +339,7 @@
321340 rs.ruler = rs.$ruler.addClass('editSurface-ruler')[0];
322341 rs.iterationLimit = 3;
323342
 343+ // Begin rendering
324344 this.renderIteration();
325345 };
326346

Status & tagging log