Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js |
— | — | @@ -345,54 +345,7 @@ |
346 | 346 | this.handleDelete(); |
347 | 347 | break; |
348 | 348 | case 13: // Enter |
349 | | - if ( this.selection.from === this.selection.to ) { |
350 | | - var tx, |
351 | | - node = this.documentView.getNodeFromOffset( this.selection.to, false ), |
352 | | - nodeOffset = this.documentView.getOffsetFromNode( node, false ); |
353 | | - |
354 | | - if ( |
355 | | - nodeOffset + node.getContentLength() + 1 === this.selection.to && |
356 | | - node === es.DocumentViewNode.getSplitableNode( node ) |
357 | | - ) { |
358 | | - tx = this.documentView.model.prepareInsertion( |
359 | | - nodeOffset + node.getElementLength(), |
360 | | - [ { 'type': 'paragraph' }, { 'type': '/paragraph' } ] |
361 | | - ); |
362 | | - this.documentView.model.commit( tx ); |
363 | | - this.selection.from = this.selection.to = |
364 | | - nodeOffset + node.getElementLength() + 1; |
365 | | - this.showCursor(); |
366 | | - } else { |
367 | | - var stack = [], |
368 | | - splitable = false; |
369 | | - |
370 | | - es.DocumentNode.traverseUpstream( node, function( node ) { |
371 | | - var elementType = node.model.getElementType(); |
372 | | - if ( |
373 | | - splitable === true && |
374 | | - es.DocumentView.splitRules[ elementType ].children === true |
375 | | - ) { |
376 | | - return false; |
377 | | - } |
378 | | - stack.splice( |
379 | | - stack.length / 2, |
380 | | - 0, |
381 | | - { 'type': '/' + elementType }, |
382 | | - { |
383 | | - 'type': elementType, |
384 | | - 'attributes': es.copyObject( node.model.element.attributes ) |
385 | | - } |
386 | | - ); |
387 | | - splitable = es.DocumentView.splitRules[ elementType ].self; |
388 | | - } ); |
389 | | - tx = this.documentView.model.prepareInsertion( this.selection.to, stack ); |
390 | | - this.documentView.model.commit( tx ); |
391 | | - this.selection.from = this.selection.to = |
392 | | - this.documentView.getModel() |
393 | | - .getRelativeContentOffset( this.selection.to, 1 ); |
394 | | - this.showCursor(); |
395 | | - } |
396 | | - } |
| 349 | + this.handleEnter(); |
397 | 350 | e.preventDefault(); |
398 | 351 | break; |
399 | 352 | default: // Insert content (maybe) |
— | — | @@ -491,6 +444,55 @@ |
492 | 445 | } |
493 | 446 | }; |
494 | 447 | |
| 448 | +es.SurfaceView.prototype.handleEnter = function() { |
| 449 | + if ( this.selection.from !== this.selection.to ) { |
| 450 | + this.handleDelete(); |
| 451 | + } |
| 452 | + var node = this.documentView.getNodeFromOffset( this.selection.to, false ), |
| 453 | + nodeOffset = this.documentView.getOffsetFromNode( node, false ); |
| 454 | + |
| 455 | + if ( |
| 456 | + nodeOffset + node.getContentLength() + 1 === this.selection.to && |
| 457 | + node === es.DocumentViewNode.getSplitableNode( node ) |
| 458 | + ) { |
| 459 | + var tx = this.documentView.model.prepareInsertion( |
| 460 | + nodeOffset + node.getElementLength(), |
| 461 | + [ { 'type': 'paragraph' }, { 'type': '/paragraph' } ] |
| 462 | + ); |
| 463 | + this.documentView.model.commit( tx ); |
| 464 | + this.selection.from = this.selection.to = nodeOffset + node.getElementLength() + 1; |
| 465 | + this.showCursor(); |
| 466 | + } else { |
| 467 | + var stack = [], |
| 468 | + splitable = false; |
| 469 | + |
| 470 | + es.DocumentNode.traverseUpstream( node, function( node ) { |
| 471 | + var elementType = node.model.getElementType(); |
| 472 | + if ( |
| 473 | + splitable === true && |
| 474 | + es.DocumentView.splitRules[ elementType ].children === true |
| 475 | + ) { |
| 476 | + return false; |
| 477 | + } |
| 478 | + stack.splice( |
| 479 | + stack.length / 2, |
| 480 | + 0, |
| 481 | + { 'type': '/' + elementType }, |
| 482 | + { |
| 483 | + 'type': elementType, |
| 484 | + 'attributes': es.copyObject( node.model.element.attributes ) |
| 485 | + } |
| 486 | + ); |
| 487 | + splitable = es.DocumentView.splitRules[ elementType ].self; |
| 488 | + } ); |
| 489 | + var tx = this.documentView.model.prepareInsertion( this.selection.to, stack ); |
| 490 | + this.documentView.model.commit( tx ); |
| 491 | + this.selection.from = this.selection.to = |
| 492 | + this.documentView.getModel().getRelativeContentOffset( this.selection.to, 1 ); |
| 493 | + this.showCursor(); |
| 494 | + } |
| 495 | +}; |
| 496 | + |
495 | 497 | es.SurfaceView.prototype.insertFromInput = function() { |
496 | 498 | var val = this.$input.val(); |
497 | 499 | this.$input.val( '' ); |
— | — | @@ -511,8 +513,6 @@ |
512 | 514 | } |
513 | 515 | }; |
514 | 516 | |
515 | | - |
516 | | - |
517 | 517 | /** |
518 | 518 | * @param {String} direction up | down | left | right |
519 | 519 | * @param {String} unit char | word | line | node | page |
— | — | @@ -662,6 +662,15 @@ |
663 | 663 | 'height': position.bottom - position.top |
664 | 664 | }); |
665 | 665 | |
| 666 | + // Auto scroll to cursor |
| 667 | + var inputTop = this.$input.offset().top, |
| 668 | + inputBottom = inputTop + position.bottom - position.top; |
| 669 | + if ( inputTop < this.dimensions.scrollTop ) { |
| 670 | + this.$window.scrollTop( inputTop ); |
| 671 | + } else if ( inputBottom > ( this.dimensions.scrollTop + this.dimensions.height ) ) { |
| 672 | + this.$window.scrollTop( inputBottom - this.dimensions.height ); |
| 673 | + } |
| 674 | + |
666 | 675 | // cursor blinking |
667 | 676 | if ( this.cursor.interval ) { |
668 | 677 | clearInterval( this.cursor.interval ); |