Index: trunk/parsers/wikidom/lib/es/es.Document.js |
— | — | @@ -4,6 +4,7 @@ |
5 | 5 | * @returns {Document} |
6 | 6 | */ |
7 | 7 | function Document( blocks ) { |
| 8 | + EventEmitter.call( this ); |
8 | 9 | this.blocks = []; |
9 | 10 | var i; |
10 | 11 | for( i = 0; i < blocks.length; i++ ) { |
— | — | @@ -39,6 +40,9 @@ |
40 | 41 | */ |
41 | 42 | Document.prototype.appendBlock = function( block ) { |
42 | 43 | block.document = this; |
| 44 | + block.on( 'update', function() { |
| 45 | + block.document.emit( 'update' ); |
| 46 | + } ); |
43 | 47 | this.blocks.push( block ); |
44 | 48 | }; |
45 | 49 | |
— | — | @@ -49,6 +53,9 @@ |
50 | 54 | */ |
51 | 55 | Document.prototype.prependBlock = function( block ) { |
52 | 56 | block.document = this; |
| 57 | + block.on( 'update', function() { |
| 58 | + block.document.emit( 'update' ); |
| 59 | + } ); |
53 | 60 | this.blocks.unshift( block ); |
54 | 61 | }; |
55 | 62 | |
— | — | @@ -60,6 +67,9 @@ |
61 | 68 | */ |
62 | 69 | Document.prototype.insertBlockBefore = function( block, before ) { |
63 | 70 | block.document = this; |
| 71 | + block.on( 'update', function() { |
| 72 | + block.document.emit( 'update' ); |
| 73 | + } ); |
64 | 74 | if ( before ) { |
65 | 75 | this.blocks.splice( before.getIndex(), 0, block ); |
66 | 76 | } else { |
— | — | @@ -73,6 +83,9 @@ |
74 | 84 | */ |
75 | 85 | Document.prototype.insertBlockAfter = function( block, after ) { |
76 | 86 | block.document = this; |
| 87 | + block.on( 'update', function() { |
| 88 | + block.document.emit( 'update' ); |
| 89 | + } ); |
77 | 90 | if ( after ) { |
78 | 91 | this.blocks.splice( after.getIndex() + 1, 0, block ); |
79 | 92 | } else { |
— | — | @@ -86,22 +99,23 @@ |
87 | 100 | * @param {Block} Block to remove |
88 | 101 | */ |
89 | 102 | Document.prototype.removeBlock = function( block ) { |
| 103 | + block.removeAllListeners( 'update' ); |
90 | 104 | this.blocks.splice( block.getIndex(), 1 ); |
91 | 105 | block.document = null; |
92 | 106 | }; |
93 | 107 | |
94 | | -Document.prototype.renderBlocks = function( offset, callback ) { |
| 108 | +Document.prototype.renderBlocks = function( offset ) { |
95 | 109 | // Remember width, to avoid updates when without width changes |
96 | 110 | this.width = this.$.innerWidth(); |
97 | 111 | // Render blocks |
98 | 112 | var i; |
99 | 113 | for ( i = 0; i < this.blocks.length; i++ ) { |
100 | 114 | this.$.append( this.blocks[i].$ ); |
101 | | - this.blocks[i].renderContent( offset, callback ); |
| 115 | + this.blocks[i].renderContent( offset ); |
102 | 116 | } |
103 | 117 | }; |
104 | 118 | |
105 | | -Document.prototype.updateBlocks = function( offset, callback ) { |
| 119 | +Document.prototype.updateBlocks = function( offset ) { |
106 | 120 | // Bypass rendering when width has not changed |
107 | 121 | var width = this.$.innerWidth(); |
108 | 122 | if ( this.width === width ) { |
— | — | @@ -111,6 +125,8 @@ |
112 | 126 | // Render blocks |
113 | 127 | var doc; |
114 | 128 | this.$.children( '.editSurface-block' ).each( function( i ) { |
115 | | - $(this).data( 'block' ).renderContent( offset, callback ); |
| 129 | + $(this).data( 'block' ).renderContent( offset ); |
116 | 130 | } ); |
117 | 131 | }; |
| 132 | + |
| 133 | +extend( Document, EventEmitter ); |
Index: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js |
— | — | @@ -9,6 +9,10 @@ |
10 | 10 | this.$ = $( '<div class="editSurface-block editSurface-paragraph"></div>' ) |
11 | 11 | .data( 'block', this ); |
12 | 12 | this.flow = new TextFlow( this.$, this.content ); |
| 13 | + var block = this; |
| 14 | + this.flow.on( 'render', function() { |
| 15 | + block.emit( 'update' ); |
| 16 | + } ); |
13 | 17 | } |
14 | 18 | |
15 | 19 | ParagraphBlock.prototype.getLength = function() { |
— | — | @@ -48,8 +52,8 @@ |
49 | 53 | * |
50 | 54 | * @param $container {jQuery Selection} Container to render into |
51 | 55 | */ |
52 | | -ParagraphBlock.prototype.renderContent = function( offset, callback ) { |
53 | | - this.flow.render( offset, callback ); |
| 56 | +ParagraphBlock.prototype.renderContent = function( offset ) { |
| 57 | + this.flow.render( offset ); |
54 | 58 | }; |
55 | 59 | |
56 | 60 | /** |
Index: trunk/parsers/wikidom/lib/es/es.Surface.js |
— | — | @@ -9,7 +9,6 @@ |
10 | 10 | |
11 | 11 | this.$ = $container.addClass( 'editSurface' ); |
12 | 12 | this.doc = doc; |
13 | | - this.rendered = false; |
14 | 13 | this.location = null; |
15 | 14 | this.selection = new Selection(); |
16 | 15 | this.mouseSelecting = false; |
— | — | @@ -35,42 +34,44 @@ |
36 | 35 | this.$.append( this.cursor.$ ); |
37 | 36 | |
38 | 37 | // Hidden input |
| 38 | + var $document = $(document); |
39 | 39 | this.$input = $( '<input class="editSurface-input" />' ) |
40 | 40 | .prependTo( this.$ ) |
41 | 41 | .bind({ |
42 | 42 | 'focus' : function() { |
43 | 43 | $(document).bind({ |
44 | | - 'mousemove.es' : function(e) { |
| 44 | + 'mousemove.editSurface' : function(e) { |
45 | 45 | return surface.onMouseMove( e ); |
46 | 46 | }, |
47 | | - 'mouseup.es' : function(e) { |
| 47 | + 'mouseup.editSurface' : function(e) { |
48 | 48 | return surface.onMouseUp( e ); |
49 | 49 | }, |
50 | | - 'keydown.es' : function( e ) { |
| 50 | + 'keydown.editSurface' : function( e ) { |
51 | 51 | return surface.onKeyDown( e ); |
52 | 52 | }, |
53 | | - 'keyup.es' : function( e ) { |
| 53 | + 'keyup.editSurface' : function( e ) { |
54 | 54 | return surface.onKeyUp( e ); |
55 | 55 | }, |
56 | 56 | }); |
57 | 57 | }, |
58 | 58 | 'blur': function( e ) { |
59 | | - $(document).unbind('mousemove.es'); |
60 | | - $(document).unbind('mouseup.es'); |
61 | | - $(document).unbind('keydown.es'); |
62 | | - $(document).unbind('keyup.es'); |
| 59 | + $document.unbind('.editSurface'); |
63 | 60 | surface.cursor.hide(); |
64 | 61 | } |
65 | 62 | }); |
66 | | - |
| 63 | + |
67 | 64 | $(window).resize( function() { |
68 | | - surface.render( 0, function() { |
69 | | - surface.drawSelection(); |
70 | | - } ); |
| 65 | + document.updateBlocks(); |
71 | 66 | } ); |
72 | 67 | |
| 68 | + this.doc.on( 'update', function() { |
| 69 | + surface.drawSelection(); |
| 70 | + // TODO: Update the cursor position |
| 71 | + } ); |
| 72 | + |
73 | 73 | // First render |
74 | | - this.render(); |
| 74 | + this.$.append( this.doc.$ ); |
| 75 | + this.doc.renderBlocks(); |
75 | 76 | } |
76 | 77 | |
77 | 78 | Surface.prototype.getLocationFromEvent = function( e ) { |
— | — | @@ -504,21 +505,6 @@ |
505 | 506 | }; |
506 | 507 | |
507 | 508 | /** |
508 | | - * Updates the rendered view. |
509 | | - * |
510 | | - * @param offset Location: Where to start re-flowing from (optional) |
511 | | - */ |
512 | | -Surface.prototype.render = function( offset, callback ) { |
513 | | - if ( !this.rendered ) { |
514 | | - this.rendered = true; |
515 | | - this.$.append( this.doc.$ ); |
516 | | - this.doc.renderBlocks( offset, callback ); |
517 | | - } else { |
518 | | - this.doc.updateBlocks( offset, callback ); |
519 | | - } |
520 | | -}; |
521 | | - |
522 | | -/** |
523 | 509 | * Applies an annotation to a given selection. |
524 | 510 | * |
525 | 511 | * If a selection argument is not provided, the current selection will be annotated. |
— | — | @@ -542,9 +528,6 @@ |
543 | 529 | if ( from.block === to.block ) { |
544 | 530 | // Single block annotation |
545 | 531 | from.block.annotateContent( method, annotation, from.offset, to.offset ); |
546 | | - from.block.renderContent( from.offset, function() { |
547 | | - surface.drawSelection(); |
548 | | - } ); |
549 | 532 | } else { |
550 | 533 | // Multiple block annotation |
551 | 534 | for ( i = from.block.getIndex(), end = to.block.getIndex(); i <= end; i++ ) { |
— | — | @@ -552,21 +535,12 @@ |
553 | 536 | if ( block === from.block ) { |
554 | 537 | // From offset to length |
555 | 538 | block.annotateContent( method, annotation, from.offset, block.getLength() ); |
556 | | - block.renderContent( from.offset, function() { |
557 | | - surface.drawSelection(); |
558 | | - } ); |
559 | 539 | } else if ( block === to.block ) { |
560 | 540 | // From 0 to offset |
561 | 541 | block.annotateContent( method, annotation, 0, to.offset ); |
562 | | - block.renderContent( 0, function() { |
563 | | - surface.drawSelection(); |
564 | | - } ); |
565 | 542 | } else { |
566 | 543 | // Full coverage |
567 | 544 | block.annotateContent( method, annotation, 0, block.getLength() ); |
568 | | - block.renderContent( 0, function() { |
569 | | - surface.drawSelection(); |
570 | | - } ); |
571 | 545 | } |
572 | 546 | } |
573 | 547 | } |
Index: trunk/parsers/wikidom/lib/es/es.Block.js |
— | — | @@ -3,6 +3,7 @@ |
4 | 4 | * @returns {Block} |
5 | 5 | */ |
6 | 6 | function Block() { |
| 7 | + EventEmitter.call( this ); |
7 | 8 | this.document = null; |
8 | 9 | } |
9 | 10 | |
— | — | @@ -106,3 +107,5 @@ |
107 | 108 | Block.prototype.annotateContent = function( method, annotation, start, end ) { |
108 | 109 | throw 'Block.annotateContent not implemented in this subclass.'; |
109 | 110 | }; |
| 111 | + |
| 112 | +extend( Block, EventEmitter ); |
Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js |
— | — | @@ -5,6 +5,7 @@ |
6 | 6 | * @returns {TextFlow} |
7 | 7 | */ |
8 | 8 | function TextFlow( $container, content ) { |
| 9 | + EventEmitter.call( this ); |
9 | 10 | this.$ = $container; |
10 | 11 | this.content = content || new Content(); |
11 | 12 | this.boundaries = []; |
— | — | @@ -13,6 +14,22 @@ |
14 | 15 | this.boundaryTest = /([ \-\t\r\n\f])/g; |
15 | 16 | this.widthCache = {}; |
16 | 17 | this.renderState = {}; |
| 18 | + |
| 19 | + var flow = this; |
| 20 | + this.content.on( 'change', function() { |
| 21 | + flow.scanBoundaries(); |
| 22 | + } ); |
| 23 | + this.content.on( 'insert', function( args ) { |
| 24 | + flow.render( args.offset ); |
| 25 | + } ); |
| 26 | + this.content.on( 'remove', function( args ) { |
| 27 | + flow.render( args.start ); |
| 28 | + } ); |
| 29 | + this.content.on( 'annotate', function( args ) { |
| 30 | + flow.render( args.start ); |
| 31 | + } ); |
| 32 | + this.scanBoundaries(); |
| 33 | + this.render(); |
17 | 34 | } |
18 | 35 | |
19 | 36 | /** |
— | — | @@ -225,9 +242,7 @@ |
226 | 243 | .nextAll() |
227 | 244 | .remove(); |
228 | 245 | rs.timeout = undefined; |
229 | | - if ( $.isFunction( rs.callback ) ) { |
230 | | - rs.callback(); |
231 | | - } |
| 246 | + this.emit( 'render' ); |
232 | 247 | } else { |
233 | 248 | rs.ruler.innerHTML = ''; |
234 | 249 | var flow = this; |
— | — | @@ -249,7 +264,7 @@ |
250 | 265 | * |
251 | 266 | * @param offset {Integer} Offset to re-render from, if possible (not yet implemented) |
252 | 267 | */ |
253 | | -TextFlow.prototype.render = function( offset, callback ) { |
| 268 | +TextFlow.prototype.render = function( offset ) { |
254 | 269 | var rs = this.renderState; |
255 | 270 | |
256 | 271 | // Stop iterating from last render |
— | — | @@ -261,8 +276,6 @@ |
262 | 277 | |
263 | 278 | this.widthCache = {}; |
264 | 279 | |
265 | | - this.scanBoundaries(); |
266 | | - |
267 | 280 | /* |
268 | 281 | * Container measurement |
269 | 282 | * |
— | — | @@ -293,7 +306,6 @@ |
294 | 307 | rs.wordCount = this.boundaries.length; |
295 | 308 | rs.ruler = rs.$ruler.addClass('editSurface-ruler')[0]; |
296 | 309 | rs.iterationLimit = 3; |
297 | | - rs.callback = callback; |
298 | 310 | |
299 | 311 | this.renderIteration(); |
300 | 312 | }; |
— | — | @@ -424,3 +436,5 @@ |
425 | 437 | } while ( start < end ); |
426 | 438 | return { 'end': start, 'width': lineWidth }; |
427 | 439 | }; |
| 440 | + |
| 441 | +extend( TextFlow, EventEmitter ); |