Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js |
— | — | @@ -318,8 +318,82 @@ |
319 | 319 | } |
320 | 320 | break; |
321 | 321 | case 8: // Backspace |
| 322 | + if ( this.selection.from === this.selection.to ) { |
| 323 | + var sourceOffset = this.selection.to, |
| 324 | + targetOffset = this.documentView.getModel().getRelativeContentOffset( sourceOffset, -1), |
| 325 | + sourceNode = this.documentView.getNodeFromOffset( sourceOffset, false ), |
| 326 | + targetNode = this.documentView.getNodeFromOffset( targetOffset, false ), |
| 327 | + sourceSplitableNode = es.DocumentViewNode.getSplitableNode( sourceNode ), |
| 328 | + targetSplitableNode = es.DocumentViewNode.getSplitableNode( targetNode ); |
| 329 | + |
| 330 | + if ( sourceNode === targetNode || ( sourceNode.model.getElementType() === targetNode.model.getElementType() && sourceSplitableNode.getParent() === targetSplitableNode.getParent() ) ) { |
| 331 | + this.selection.from = this.selection.to = targetOffset; |
| 332 | + this.showCursor(); |
| 333 | + var tx = this.documentView.model.prepareRemoval( |
| 334 | + new es.Range( targetOffset, sourceOffset ) |
| 335 | + ); |
| 336 | + this.documentView.model.commit ( tx ); |
| 337 | + } else { |
| 338 | + this.selection.from = this.selection.to = targetOffset; |
| 339 | + this.showCursor(); |
| 340 | + var tx = this.documentView.model.prepareInsertion( |
| 341 | + targetOffset, sourceNode.model.getContent() |
| 342 | + ); |
| 343 | + this.documentView.model.commit( tx ); |
| 344 | + |
| 345 | + var nodeToDelete = sourceNode; |
| 346 | + es.DocumentNode.traverseUpstream( nodeToDelete, function( node ) { |
| 347 | + if ( node.getParent().children.length === 1 ) { |
| 348 | + nodeToDelete = sourceNode.getParent(); |
| 349 | + } else { |
| 350 | + return false; |
| 351 | + } |
| 352 | + } ); |
| 353 | + var range = new es.Range(); |
| 354 | + range.from = this.documentView.getOffsetFromNode( nodeToDelete, false ); |
| 355 | + range.to = range.from + nodeToDelete.getElementLength(); |
| 356 | + tx = this.documentView.model.prepareRemoval( range ); |
| 357 | + this.documentView.model.commit ( tx ); |
| 358 | + } |
| 359 | + } |
322 | 360 | break; |
323 | 361 | case 46: // Delete |
| 362 | + if ( this.selection.from === this.selection.to ) { |
| 363 | + var sourceOffset = this.documentView.getModel().getRelativeContentOffset( this.selection.to, 1), |
| 364 | + targetOffset = this.selection.to, |
| 365 | + sourceNode = this.documentView.getNodeFromOffset( sourceOffset, false ), |
| 366 | + targetNode = this.documentView.getNodeFromOffset( targetOffset, false ), |
| 367 | + sourceSplitableNode = es.DocumentViewNode.getSplitableNode( sourceNode ), |
| 368 | + targetSplitableNode = es.DocumentViewNode.getSplitableNode( targetNode ); |
| 369 | + |
| 370 | + if ( sourceNode === targetNode || ( sourceNode.model.getElementType() === targetNode.model.getElementType() && sourceSplitableNode.getParent() === targetSplitableNode.getParent() ) ) { |
| 371 | + var tx = this.documentView.model.prepareRemoval( |
| 372 | + new es.Range( targetOffset, sourceOffset ) |
| 373 | + ); |
| 374 | + this.documentView.model.commit ( tx ); |
| 375 | + } else { |
| 376 | + this.selection.from = this.selection.to = targetOffset; |
| 377 | + this.showCursor(); |
| 378 | + var tx = this.documentView.model.prepareInsertion( |
| 379 | + targetOffset, sourceNode.model.getContent() |
| 380 | + ); |
| 381 | + this.documentView.model.commit( tx ); |
| 382 | + |
| 383 | + var nodeToDelete = sourceNode; |
| 384 | + es.DocumentNode.traverseUpstream( nodeToDelete, function( node ) { |
| 385 | + if ( node.getParent().children.length === 1 ) { |
| 386 | + nodeToDelete = nodeToDelete.getParent(); |
| 387 | + } else { |
| 388 | + return false; |
| 389 | + } |
| 390 | + } ); |
| 391 | + var range = new es.Range(); |
| 392 | + range.from = this.documentView.getOffsetFromNode( nodeToDelete, false ); |
| 393 | + range.to = range.from + nodeToDelete.getElementLength(); |
| 394 | + tx = this.documentView.model.prepareRemoval( range ); |
| 395 | + this.documentView.model.commit ( tx ); |
| 396 | + } |
| 397 | + } |
324 | 398 | break; |
325 | 399 | case 13: // Enter |
326 | 400 | if ( this.selection.from === this.selection.to ) { |