| Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js |
| — | — | @@ -894,7 +894,6 @@ |
| 895 | 895 | tx.pushRetain( this.data.length - offset ); |
| 896 | 896 | } |
| 897 | 897 | |
| 898 | | - tx.optimize(); |
| 899 | 898 | return tx; |
| 900 | 899 | |
| 901 | 900 | /* |
| — | — | @@ -1036,7 +1035,6 @@ |
| 1037 | 1036 | } |
| 1038 | 1037 | } |
| 1039 | 1038 | |
| 1040 | | - tx.optimize(); |
| 1041 | 1039 | return tx; |
| 1042 | 1040 | }; |
| 1043 | 1041 | |
| — | — | @@ -1099,7 +1097,7 @@ |
| 1100 | 1098 | if ( range.end < this.data.length ) { |
| 1101 | 1099 | tx.pushRetain( this.data.length - range.end ); |
| 1102 | 1100 | } |
| 1103 | | - tx.optimize(); |
| | 1101 | + |
| 1104 | 1102 | return tx; |
| 1105 | 1103 | }; |
| 1106 | 1104 | |
| — | — | @@ -1124,7 +1122,7 @@ |
| 1125 | 1123 | if ( offset < this.data.length ) { |
| 1126 | 1124 | tx.pushRetain( this.data.length - offset ); |
| 1127 | 1125 | } |
| 1128 | | - tx.optimize(); |
| | 1126 | + |
| 1129 | 1127 | return tx; |
| 1130 | 1128 | }; |
| 1131 | 1129 | |
| Index: trunk/extensions/VisualEditor/modules/es/models/es.TransactionModel.js |
| — | — | @@ -33,43 +33,21 @@ |
| 34 | 34 | }; |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | | - * Merges consecutive operations of the same type. |
| 38 | | - * |
| 39 | | - * @method |
| 40 | | - */ |
| 41 | | -es.TransactionModel.prototype.optimize = function() { |
| 42 | | - for ( var i = 0; i < this.operations.length - 1; i++ ) { |
| 43 | | - var a = this.operations[i]; |
| 44 | | - var b = this.operations[i + 1]; |
| 45 | | - if ( a.type === b.type ) { |
| 46 | | - switch ( a.type ) { |
| 47 | | - case 'retain': |
| 48 | | - a.length += b.length; |
| 49 | | - this.operations.splice( i + 1, 1 ); |
| 50 | | - i--; |
| 51 | | - break; |
| 52 | | - case 'insert': |
| 53 | | - case 'remove': |
| 54 | | - a.data = a.data.concat( b.data ); |
| 55 | | - this.operations.splice( i + 1, 1 ); |
| 56 | | - i--; |
| 57 | | - break; |
| 58 | | - } |
| 59 | | - } |
| 60 | | - } |
| 61 | | -}; |
| 62 | | - |
| 63 | | -/** |
| 64 | 37 | * Adds a retain operation. |
| 65 | 38 | * |
| 66 | 39 | * @method |
| 67 | 40 | * @param {Integer} length Length of content data to retain |
| 68 | 41 | */ |
| 69 | 42 | es.TransactionModel.prototype.pushRetain = function( length ) { |
| 70 | | - this.operations.push( { |
| 71 | | - 'type': 'retain', |
| 72 | | - 'length': length |
| 73 | | - } ); |
| | 43 | + var end = this.operations.length - 1; |
| | 44 | + if ( this.operations.length && this.operations[end].type === 'retain' ) { |
| | 45 | + this.operations[end].length += length; |
| | 46 | + } else { |
| | 47 | + this.operations.push( { |
| | 48 | + 'type': 'retain', |
| | 49 | + 'length': length |
| | 50 | + } ); |
| | 51 | + } |
| 74 | 52 | }; |
| 75 | 53 | |
| 76 | 54 | /** |
| — | — | @@ -79,10 +57,15 @@ |
| 80 | 58 | * @param {Array} data Data to retain |
| 81 | 59 | */ |
| 82 | 60 | es.TransactionModel.prototype.pushInsert = function( data ) { |
| 83 | | - this.operations.push( { |
| 84 | | - 'type': 'insert', |
| 85 | | - 'data': data |
| 86 | | - } ); |
| | 61 | + var end = this.operations.length - 1; |
| | 62 | + if ( this.operations.length && this.operations[end].type === 'insert' ) { |
| | 63 | + this.operations[end].data = this.operations[end].data.concat( data ); |
| | 64 | + } else { |
| | 65 | + this.operations.push( { |
| | 66 | + 'type': 'insert', |
| | 67 | + 'data': data |
| | 68 | + } ); |
| | 69 | + } |
| 87 | 70 | this.lengthDifference += data.length; |
| 88 | 71 | }; |
| 89 | 72 | |
| — | — | @@ -93,10 +76,15 @@ |
| 94 | 77 | * @param {Array} data Data to remove |
| 95 | 78 | */ |
| 96 | 79 | es.TransactionModel.prototype.pushRemove = function( data ) { |
| 97 | | - this.operations.push( { |
| 98 | | - 'type': 'remove', |
| 99 | | - 'data': data |
| 100 | | - } ); |
| | 80 | + var end = this.operations.length - 1; |
| | 81 | + if ( this.operations.length && this.operations[end].type === 'remove' ) { |
| | 82 | + this.operations[end].data = this.operations[end].data.concat( data ); |
| | 83 | + } else { |
| | 84 | + this.operations.push( { |
| | 85 | + 'type': 'remove', |
| | 86 | + 'data': data |
| | 87 | + } ); |
| | 88 | + } |
| 101 | 89 | this.lengthDifference -= data.length; |
| 102 | 90 | }; |
| 103 | 91 | |