Small refactoring to getOffset method in SurfaceView

This commit is contained in:
Inez Korczynski 2012-01-26 06:55:46 +00:00
parent b2db4c43eb
commit ffc304ef6c
Notes: Gabriel Wicke 2012-02-27 16:40:01 +00:00

View file

@ -1,118 +1,104 @@
es.SurfaceView = function( $container, model ) { es.SurfaceView = function( $container, model ) {
// Inheritance // Inheritance
es.EventEmitter.call( this ); es.EventEmitter.call( this );
// References for use in closures // References for use in closures
var _this = this; var _this = this;
// Properties // Properties
this.model = model; this.model = model;
this.documentView = new es.DocumentView( this.model.getDocument(), this ); this.documentView = new es.DocumentView( this.model.getDocument(), this );
this.$ = $container.append( this.documentView.$ ); this.$ = $container.append( this.documentView.$ );
this.$.keydown( function(e) { this.$.keydown( function(e) {
return _this.onKeyDown( e ); return _this.onKeyDown( e );
} ); } );
this.model.getDocument().on( 'update', function() { this.model.getDocument().on( 'update', function() {
_this.emit( 'update' ); _this.emit( 'update' );
} ); } );
this.documentView.renderContent(); this.documentView.renderContent();
}; };
es.SurfaceView.prototype.onKeyDown = function( e ) { es.SurfaceView.prototype.onKeyDown = function( e ) {
if ( e.which === 13 ) { if ( e.which === 13 ) {
e.preventDefault(); e.preventDefault();
var range = this.getSelection(); var range = this.getSelection();
if ( range.start === range.end ) { if ( range.start === range.end ) {
var tx = this.model.getDocument().prepareInsertion( range.start, [ { 'type': '/paragraph' }, { 'type': 'paragraph' } ]); var tx = this.model.getDocument().prepareInsertion( range.start, [ { 'type': '/paragraph' }, { 'type': 'paragraph' } ]);
this.model.transact( tx ); this.model.transact( tx );
} }
} else if ( e.which === 8 ) { } else if ( e.which === 8 ) {
console.log("A"); console.log("A");
e.preventDefault(); e.preventDefault();
var range = this.getSelection(); var range = this.getSelection();
if ( range.start != range.end ) { if ( range.start != range.end ) {
var tx = this.model.getDocument().prepareRemoval( range ); var tx = this.model.getDocument().prepareRemoval( range );
this.model.transact( tx ); this.model.transact( tx );
} }
} }
}; };
es.SurfaceView.prototype.getOffset = function( localNode, localOffset ) { es.SurfaceView.prototype.getOffset = function( localNode, localOffset ) {
var $node = $( localNode ); var $node = $( localNode );
while( !$node.hasClass( 'es-paragraphView' ) ) { while( !$node.hasClass( 'es-paragraphView' ) ) {
$node = $node.parent(); $node = $node.parent();
} }
var traverse = function( data, callback ) { var current = [$node.contents(), 0];
var current = [ data, 0 ]; var stack = [current];
var stack = [ current ];
var offset = 0;
while ( stack.length > 0 ) {
if ( current[1] >= current[0].length ) { while ( stack.length > 0 ) {
stack.pop(); if ( current[1] >= current[0].length ) {
current = stack[ stack.length - 1 ]; stack.pop();
continue; current = stack[ stack.length - 1 ];
} continue;
var item = current[0][current[1]]; }
var out = callback( item ); var item = current[0][current[1]];
/* var $item = current[0].eq( current[1] );
* -1 = stop traversing
* 1 = deep traverse if ( item.nodeType === 3 ) {
* 0 = skip deep traverse if ( item === localNode ) {
*/ offset += localOffset;
if ( out === -1 ) { break;
return; } else {
} else if ( out === 1 && item.nodeType === 1 ) { offset += item.textContent.length;
stack.push( [ $(item).contents() , 0 ] ); }
current[1]++; } else if ( item.nodeType === 1 ) {
current = stack[stack.length-1]; if ( $( item ).attr('contentEditable') === "false" ) {
continue; console.log("in");
} offset += 1;
current[1]++; } else {
} stack.push( [$item.contents(), 0] );
}; current[1]++;
current = stack[stack.length-1];
var offset = 0; continue;
}
traverse( $node.contents(), function( item ) { }
if ( item.nodeType === 3 ) { current[1]++;
if ( item === localNode ) { }
offset += localOffset;
return -1; return this.documentView.getOffsetFromNode( $node.data('view') ) + 1 + offset;
} else { }
offset += item.textContent.length;
es.SurfaceView.prototype.getSelection = function() {
} var selection = rangy.getSelection();
} else {
if ( $( item ).attr('contentEditable') === "false" ) { if ( selection.anchorNode === selection.focusNode && selection.anchorOffset === selection.focusOffset ) {
offset += 1; // only one offset
return 0; var offset = this.getOffset( selection.anchorNode, selection.anchorOffset );
} return new es.Range( offset, offset );
} } else {
return 1; // two offsets
} ); var offset1 = this.getOffset( selection.anchorNode, selection.anchorOffset );
var offset2 = this.getOffset( selection.focusNode, selection.focusOffset );
return offset + 1 + this.documentView.getOffsetFromNode( $node.data('view') ); return new es.Range( offset1, offset2 );
} }
};
es.SurfaceView.prototype.getSelection = function() {
var selection = rangy.getSelection(); /* Inheritance */
if ( selection.anchorNode === selection.focusNode && selection.anchorOffset === selection.focusOffset ) {
// only one offset
var offset = this.getOffset( selection.anchorNode, selection.anchorOffset );
return new es.Range( offset, offset );
} else {
// two offsets
var offset1 = this.getOffset( selection.anchorNode, selection.anchorOffset );
var offset2 = this.getOffset( selection.focusNode, selection.focusOffset );
return new es.Range( offset1, offset2 );
}
};
/* Inheritance */
es.extendClass( es.SurfaceView, es.EventEmitter ); es.extendClass( es.SurfaceView, es.EventEmitter );