Index: trunk/extensions/VisualEditor/contentEditable/views/es.SurfaceView.js |
— | — | @@ -28,6 +28,7 @@ |
29 | 29 | if ( range.start === range.end ) { |
30 | 30 | var tx = this.model.getDocument().prepareInsertion( range.start, [ { 'type': '/paragraph' }, { 'type': 'paragraph' } ]); |
31 | 31 | this.model.transact( tx ); |
| 32 | + this.showCursorAt( range.start ); |
32 | 33 | } |
33 | 34 | } else if ( e.which === 8 ) { |
34 | 35 | console.log("A"); |
— | — | @@ -40,6 +41,55 @@ |
41 | 42 | } |
42 | 43 | }; |
43 | 44 | |
| 45 | +es.SurfaceView.prototype.showCursorAt = function( offset ) { |
| 46 | + var $node = this.documentView.getNodeFromOffset( offset ).$; |
| 47 | + var current = [$node.contents(), 0]; |
| 48 | + var stack = [current]; |
| 49 | + var node; |
| 50 | + var localOffset; |
| 51 | + |
| 52 | + var index = 1 + this.documentView.getOffsetFromNode( $node.data('view') ); |
| 53 | + |
| 54 | + while ( stack.length > 0 ) { |
| 55 | + if ( current[1] >= current[0].length ) { |
| 56 | + stack.pop(); |
| 57 | + current = stack[ stack.length - 1 ]; |
| 58 | + continue; |
| 59 | + } |
| 60 | + var item = current[0][current[1]]; |
| 61 | + var $item = current[0].eq( current[1] ); |
| 62 | + |
| 63 | + if ( item.nodeType === 3 ) { |
| 64 | + var length = item.textContent.length; |
| 65 | + if ( offset > index && offset <= index + length ) { |
| 66 | + node = item; |
| 67 | + localOffset = offset - index; |
| 68 | + } else { |
| 69 | + index += length; |
| 70 | + } |
| 71 | + } else if ( item.nodeType === 1 ) { |
| 72 | + if ( $( item ).attr('contentEditable') === "false" ) { |
| 73 | + index += 1; |
| 74 | + } else { |
| 75 | + stack.push( [$item.contents(), 0] ); |
| 76 | + current[1]++; |
| 77 | + current = stack[stack.length-1]; |
| 78 | + continue; |
| 79 | + } |
| 80 | + } |
| 81 | + current[1]++; |
| 82 | + } |
| 83 | + var range = document.createRange(); |
| 84 | + range.collapsed = true; |
| 85 | + range.setStart(node, localOffset); |
| 86 | + |
| 87 | + var sel = window.getSelection(); |
| 88 | + sel.removeAllRanges(); |
| 89 | + sel.addRange(range); |
| 90 | + |
| 91 | + |
| 92 | +}; |
| 93 | + |
44 | 94 | es.SurfaceView.prototype.getOffset = function( localNode, localOffset ) { |
45 | 95 | var $node = $( localNode ); |
46 | 96 | while( !$node.hasClass( 'es-paragraphView' ) ) { |
— | — | @@ -69,7 +119,6 @@ |
70 | 120 | } |
71 | 121 | } else if ( item.nodeType === 1 ) { |
72 | 122 | if ( $( item ).attr('contentEditable') === "false" ) { |
73 | | - console.log("in"); |
74 | 123 | offset += 1; |
75 | 124 | } else { |
76 | 125 | stack.push( [$item.contents(), 0] ); |