Index: trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js |
— | — | @@ -256,41 +256,43 @@ |
257 | 257 | }; |
258 | 258 | |
259 | 259 | es.TransactionProcessor.prototype.insert = function( op ) { |
260 | | - var node; |
261 | | - // If the cursor is on the left of a structural offset, it's going to the the parent node of |
262 | | - // the element we want to be inserting into |
263 | | - if ( es.DocumentModel.isStructuralOffset( this.model.data, this.cursor ) ) { |
264 | | - node = this.model.getNodeFromOffset( this.cursor + 1 ); |
265 | | - } else { |
266 | | - node = this.model.getNodeFromOffset( this.cursor ); |
267 | | - } |
268 | | - var offset; |
269 | | - if ( node.getParent() === this.model ) { |
270 | | - offset = this.model.getOffsetFromNode( node ); |
271 | | - } else { |
272 | | - node = this.getScope( node, op.data ); |
273 | | - offset = this.model.getOffsetFromNode( node ); |
274 | | - } |
275 | | - if ( es.DocumentModel.containsElementData( op.data ) ) { |
276 | | - // Perform insert on linear data model |
277 | | - es.insertIntoArray( this.model.data, this.cursor, op.data ); |
278 | | - this.applyAnnotations( this.cursor + op.data.length ); |
279 | | - // Synchronize model tree |
280 | | - if ( offset === -1 ) { |
281 | | - throw 'Invalid offset error. Node is not in model tree'; |
282 | | - } |
| 260 | + var node = this.model.getNodeFromOffset( this.cursor ), |
| 261 | + offset; |
| 262 | + // Perform insert on linear data model |
| 263 | + es.insertIntoArray( this.model.data, this.cursor, op.data ); |
| 264 | + this.applyAnnotations( this.cursor + op.data.length ); |
| 265 | + if ( |
| 266 | + es.DocumentModel.isStructuralOffset( this.model.data, this.cursor ) && |
| 267 | + ( typeof op.data[0].type === 'string' && op.data[0].type.charAt( 0 ) !== '/' ) |
| 268 | + ) { |
| 269 | + offset = node === this.model ? 0 : this.model.getOffsetFromNode( node ); |
283 | 270 | this.rebuildNodes( |
284 | | - this.model.data.slice( offset, offset + node.getElementLength() + op.data.length ), |
285 | | - [node] |
| 271 | + this.model.data.slice( this.cursor, op.data.length ), |
| 272 | + null, |
| 273 | + node, |
| 274 | + node.getIndexFromOffset( this.cursor - offset ) |
286 | 275 | ); |
287 | 276 | } else { |
288 | | - // Perform insert on linear data model |
289 | | - // TODO this is duplicated from above |
290 | | - es.insertIntoArray( this.model.data, this.cursor, op.data ); |
291 | | - this.applyAnnotations( this.cursor + op.data.length ); |
292 | | - // Update model tree |
293 | | - node.adjustContentLength( op.data.length, true ); |
294 | | - node.emit( 'update', this.cursor - offset ); |
| 277 | + if ( node.getParent() === this.model ) { |
| 278 | + offset = this.model.getOffsetFromNode( node ); |
| 279 | + } else { |
| 280 | + node = this.getScope( node, op.data ); |
| 281 | + offset = this.model.getOffsetFromNode( node ); |
| 282 | + } |
| 283 | + if ( es.DocumentModel.containsElementData( op.data ) ) { |
| 284 | + // Synchronize model tree |
| 285 | + if ( offset === -1 ) { |
| 286 | + throw 'Invalid offset error. Node is not in model tree'; |
| 287 | + } |
| 288 | + this.rebuildNodes( |
| 289 | + this.model.data.slice( offset, offset + node.getElementLength() + op.data.length ), |
| 290 | + [node] |
| 291 | + ); |
| 292 | + } else { |
| 293 | + // Update model tree |
| 294 | + node.adjustContentLength( op.data.length, true ); |
| 295 | + node.emit( 'update', this.cursor - offset ); |
| 296 | + } |
295 | 297 | } |
296 | 298 | this.cursor += op.data.length; |
297 | 299 | }; |