Index: trunk/parsers/wikidom/lib/es/es.Content.Transaction.js |
— | — | @@ -0,0 +1,65 @@ |
| 2 | +/** |
| 3 | + * Creates an operation to be applied to a content object. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @param operations {Array} List of operations |
| 8 | + * @property operations {Array} List of operations |
| 9 | + */ |
| 10 | +es.Content.Transaction = function( operations ) { |
| 11 | + this.operations = operations || []; |
| 12 | +}; |
| 13 | + |
| 14 | +es.Content.Transaction.prototype.add = function( operation ) { |
| 15 | + this.operations.push( operation ); |
| 16 | +}; |
| 17 | + |
| 18 | +es.Content.Transaction.prototype.commit = function( from ) { |
| 19 | + var range = new es.Range(), |
| 20 | + to = new es.Content(); |
| 21 | + for ( var i = 0; i < this.operations.length; i++ ) { |
| 22 | + var operation = this.operations[i]; |
| 23 | + range.to = range.from + operation.getLength(); |
| 24 | + switch (operation.getType()) { |
| 25 | + case 'retain': |
| 26 | + to.insert( to.getLength(), from.getData( range ) ); |
| 27 | + range.from = range.to; |
| 28 | + break; |
| 29 | + case 'insert': |
| 30 | + to.insert( to.getLength(), operation.getContent() ); |
| 31 | + break; |
| 32 | + case 'delete': |
| 33 | + // TODO: Validate operation.getContent against content being skipped |
| 34 | + offset += operation.getLength(); |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + return to; |
| 39 | +}; |
| 40 | + |
| 41 | +es.Content.Transaction.prototype.rollback = function( from ) { |
| 42 | + var range = new es.Range(), |
| 43 | + to = new es.Content(); |
| 44 | + for ( var i = 0; i < this.operations.length; i++ ) { |
| 45 | + var operation = this.operations[i]; |
| 46 | + range.to = range.from + operation.getLength(); |
| 47 | + switch (operation.getType()) { |
| 48 | + case 'retain': |
| 49 | + to.insert( to.getLength(), from.getData( range ) ); |
| 50 | + range.from = range.to; |
| 51 | + break; |
| 52 | + case 'insert': |
| 53 | + // TODO: Validate operation.getContent against content being skipped |
| 54 | + offset += operation.getLength(); |
| 55 | + break; |
| 56 | + case 'delete': |
| 57 | + to.insert( to.getLength(), operation.getContent() ); |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + return to; |
| 62 | +}; |
| 63 | + |
| 64 | +es.Content.Transaction.prototype.optimize = function() { |
| 65 | + // reduce consecutive operations of the same type to single operations if possible |
| 66 | +}; |
Property changes on: trunk/parsers/wikidom/lib/es/es.Content.Transaction.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 67 | + native |
Added: svn:mime-type |
2 | 68 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.Content.Operation.js |
— | — | @@ -0,0 +1,33 @@ |
| 2 | +/** |
| 3 | + * Creates an operation to be applied to a content object. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @param type {String} Type of operation, e.g. insert, delete, annotate |
| 8 | + * @param content {es.Content} Content being inserted or modified |
| 9 | + * @param data {Object} Data of operation, e.g. range |
| 10 | + * @property type {String} Type of operation |
| 11 | + * @property content {es.Content} Content of operation |
| 12 | + * @property data {Object} Data of operation |
| 13 | + */ |
| 14 | +es.Content.Operation = function( type, content, data ) { |
| 15 | + this.type = type; |
| 16 | + this.contents = contents; |
| 17 | + this.data = data; |
| 18 | +}; |
| 19 | + |
| 20 | +es.Content.Operation.prototype.getType = function() { |
| 21 | + return this.type; |
| 22 | +}; |
| 23 | + |
| 24 | +es.Content.Operation.prototype.getContent = function() { |
| 25 | + return this.content; |
| 26 | +}; |
| 27 | + |
| 28 | +es.Content.Operation.prototype.getLength = function() { |
| 29 | + return this.content.getLength();; |
| 30 | +}; |
| 31 | + |
| 32 | +es.Content.Operation.prototype.getData = function() { |
| 33 | + return this.data; |
| 34 | +}; |
Property changes on: trunk/parsers/wikidom/lib/es/es.Content.Operation.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 35 | + native |
Added: svn:mime-type |
2 | 36 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.Content.js |
— | — | @@ -375,6 +375,22 @@ |
376 | 376 | }; |
377 | 377 | |
378 | 378 | /** |
| 379 | + * Gets content data within a specific range. |
| 380 | + * |
| 381 | + * @method |
| 382 | + * @param range {es.Range} Range of content to get |
| 383 | + * @returns {Array} Array of plain text and annotated characters within range |
| 384 | + */ |
| 385 | +es.Content.prototype.getData = function( range ) { |
| 386 | + if ( !range ) { |
| 387 | + range = new es.Range( 0, this.data.length ); |
| 388 | + } else { |
| 389 | + range.normalize(); |
| 390 | + } |
| 391 | + return this.data.slice( range.start, range.end ); |
| 392 | +}; |
| 393 | + |
| 394 | +/** |
379 | 395 | * Inserts content data at a specific position. |
380 | 396 | * |
381 | 397 | * Inserted content will inherit annotations from neighboring content. |