r110048 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110047‎ | r110048 | r110049 >
Date:07:14, 26 January 2012
Author:inez
Status:deferred
Tags:visualeditor 
Comment:
Proof of concept for cursor repositioning - showCursorAt method
Modified paths:
  • /trunk/extensions/VisualEditor/contentEditable/views/es.SurfaceView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/contentEditable/views/es.SurfaceView.js
@@ -28,6 +28,7 @@
2929 if ( range.start === range.end ) {
3030 var tx = this.model.getDocument().prepareInsertion( range.start, [ { 'type': '/paragraph' }, { 'type': 'paragraph' } ]);
3131 this.model.transact( tx );
 32+ this.showCursorAt( range.start );
3233 }
3334 } else if ( e.which === 8 ) {
3435 console.log("A");
@@ -40,6 +41,55 @@
4142 }
4243 };
4344
 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+
4494 es.SurfaceView.prototype.getOffset = function( localNode, localOffset ) {
4595 var $node = $( localNode );
4696 while( !$node.hasClass( 'es-paragraphView' ) ) {
@@ -69,7 +119,6 @@
70120 }
71121 } else if ( item.nodeType === 1 ) {
72122 if ( $( item ).attr('contentEditable') === "false" ) {
73 - console.log("in");
74123 offset += 1;
75124 } else {
76125 stack.push( [$item.contents(), 0] );

Status & tagging log