From 8f4f0763408b15559b1896b4a469149922e8ff9a Mon Sep 17 00:00:00 2001 From: Inez Korczynski Date: Thu, 24 May 2012 20:55:45 -0700 Subject: [PATCH] Added handles for mouse events (down, up, move). Implemented method SurfaceView.getOffset and applied significant fixes to SurfaceView.getDOMNodeAndOffset. Change-Id: I06cbfa9e0e7afc7aaf19d5cda8b91fe93aeb51f8 --- modules/ve2/ce/ve.ce.Surface.js | 179 +++++++++++++++++++++++++++++++- 1 file changed, 178 insertions(+), 1 deletion(-) diff --git a/modules/ve2/ce/ve.ce.Surface.js b/modules/ve2/ce/ve.ce.Surface.js index 912449896f..583dda8b08 100644 --- a/modules/ve2/ce/ve.ce.Surface.js +++ b/modules/ve2/ce/ve.ce.Surface.js @@ -14,6 +14,14 @@ ve.ce.Surface = function( $container, model ) { this.documentView = new ve.ce.Document( model.getDocument() ); this.contextView = new ve.ui.Context( this ); this.$ = $container; + this.isMouseDown = false; + + // Events + this.$.on( { + 'mousedown': this.proxy( this.onMouseDown ), + 'mouseup': this.proxy( this.onMouseUp ), + 'mousemove': this.proxy( this.onMouseMove ), + } ); // Initialization this.$.append( this.documentView.documentNode.$ ); @@ -34,10 +42,163 @@ ve.ce.Surface.prototype.proxy = function( func ) { }); }; +ve.ce.Surface.prototype.onMouseDown = function( e ) { + this.isMouseDown = true; + + var $closestLeaf = $( e.target ).closest( '.ve-ce-leafNode' ); + + if ( $closestLeaf.length > 0 ) { + var nodeModel = $closestLeaf.data( 'node' ).getModel(); + var offset = nodeModel.getRoot().getOffsetFromNode( nodeModel ); + this.showCursor( offset ); + return false; + } +}; + +ve.ce.Surface.prototype.onMouseUp = function( e ) { + this.isMouseDown = false; + + var rangySel = rangy.getSelection(); + console.log("anchor", rangySel.anchorNode, rangySel.anchorOffset); + + var offset1 = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset ); + console.log( 'offset1', offset1 ); + + var offset2 = this.documentView.model.getNearestContentOffset( offset1 ); + console.log( 'offset2', offset2 ); + + var offset3 = this.documentView.model.getNearestStructuralOffset( offset1, 0, true ); + console.log( 'offset3', offset3 ); + + + + this.showCursor( offset1 ); + + + +}; + +ve.ce.Surface.prototype.onMouseMove = function( e ) { + if ( this.isMouseDown === true ) { + //... + } +}; + +ve.ce.Surface.prototype.getOffset = function( DOMnode, DOMoffset ) { + if ( DOMnode.nodeType === 3 ) { + var $branch = $( DOMnode ).closest( '.ve-ce-branchNode' ); + var current = [$branch.contents(), 0], + stack = [current], + offset = 0; + while ( stack.length > 0 ) { + if ( current[1] >= current[0].length ) { + stack.pop(); + current = stack[ stack.length - 1 ]; + continue; + } + var item = current[0][current[1]]; + var $item = current[0].eq( current[1] ); + if ( item.nodeType === 3 ) { + if ( item === DOMnode ) { + offset += DOMoffset; + break; + } else { + offset += item.textContent.length; + } + } else if ( item.nodeType === 1 ) { + if ( $( item ).hasClass( 've-ce-leafNode' ) ) { + offset += 2; + } else { + if ( item === DOMnode ) { + offset += DOMoffset; + break; + } + stack.push( [$item.contents(), 0] ); + current[1]++; + current = stack[stack.length-1]; + continue; + } + } + current[1]++; + } + var branchModel = $branch.data( 'node' ).getModel(); + var branchOffset = branchModel.getRoot().getOffsetFromNode( branchModel ); + return offset + 1 + branchOffset; + } else if ( DOMnode.nodeType === 1 ) { + if ( DOMoffset === 0 ) { + throw new "Not implemented"; + } else { + var $node = $( DOMnode ).contents().eq( DOMoffset - 1 ); + var nodeModel = $node.data( 'node' ).getModel(); + var nodeOffset = nodeModel.getRoot().getOffsetFromNode( nodeModel ); + return nodeOffset + nodeModel.getOuterLength(); + } + } else { + throw new "Not implemented"; + } +}; + /** * @method */ ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) { + var $node = this.documentView.documentNode.getNodeFromOffset( offset ).$.closest( '.ve-ce-branchNode' ), + startOffset = this.documentView.documentNode.getOffsetFromNode( $node.data( 'node' ) ) + 1, + current = [$node.contents(), 0], + stack = [current]; + + while ( stack.length > 0 ) { + if ( current[1] >= current[0].length ) { + stack.pop(); + current = stack[ stack.length - 1 ]; + continue; + } + + var item = current[0][current[1]], + $item = current[0].eq( current[1] ); + + if ( item.nodeType === 3 ) { + var length = item.textContent.length; + + if ( offset >= startOffset && offset <= startOffset + length ) { + console.log("Success 1"); + return { node: item, offset: offset - startOffset }; + } else { + startOffset += length; + } + } else if ( item.nodeType === 1 ) { + if ( $item.hasClass('ve-ce-slug') ) { + return { node: $item[0], offset: 1 }; + } else if ( $item.is( '.ve-ce-branchNode, .ve-ce-leafNode' ) ) { + var length = $item.data( 'node' ).model.getOuterLength(); + + if ( offset >= startOffset && offset < startOffset + length ) { + stack.push( [$item.contents(), 0] ); + current[1]++; + current = stack[stack.length-1]; + continue; + } else { + startOffset += length; + } + } else { + stack.push( [$item.contents(), 0] ); + current[1]++; + current = stack[stack.length-1]; + continue; + } + + } + current[1]++; + } + throw new 'Shouldn\'t happen'; + return; + + + + + + + var $node = this.documentView.documentNode.getNodeFromOffset( offset ).$.closest('.ve-ce-branchNode'), nodeOffset = this.documentView.documentNode.getOffsetFromNode( $node.data('node') ) + 1, current = [$node.contents(), 0], @@ -45,6 +206,11 @@ ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) { localNode, localOffset; + console.log($node); + return; + + + while ( stack.length > 0 ) { if ( current[1] >= current[0].length ) { stack.pop(); @@ -54,6 +220,8 @@ ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) { var item = current[0][current[1]], $item = current[0].eq( current[1] ); + console.log(nodeOffset, $item); + if ( item.nodeType === 3 ) { var length = item.textContent.length; if ( offset >= nodeOffset && offset <= nodeOffset + length ) { @@ -65,7 +233,7 @@ ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) { nodeOffset += length; } } else if ( item.nodeType === 1 ) { - if ( $( item ).is('.ve-ce-alienBlockNode, .ve-ce-alienInlineNode') ) { + if ( $( item ).is('.ve-ce-alienBlockNode, .ve-ce-alienInlineNode, .ve-ce-imageNode') ) { nodeOffset += 2; } else if ( $( item ).hasClass('ve-ce-slug') ) { if (nodeOffset == offset) { @@ -75,6 +243,15 @@ ve.ce.Surface.prototype.getDOMNodeAndOffset = function( offset ) { }; } } else { + +console.log(this.documentView.model.data); + if ( ve.dm.Document.isStructuralOffset( this.documentView.model.data, nodeOffset) ) { + console.log("EVER?"); + nodeOffset++; + } + + + stack.push( [$item.contents(), 0] ); current[1]++; current = stack[stack.length-1];