From bc26561ada43b027f6c31dc6fb2f840e1b024eb3 Mon Sep 17 00:00:00 2001 From: Inez Korczynski Date: Thu, 26 Jan 2012 01:37:12 +0000 Subject: [PATCH] Basic support for "enter" key --- contentEditable/main.js | 10 ++- contentEditable/views/es.ContentView.js | 8 +++ contentEditable/views/es.SurfaceView.js | 92 +++++++++++++++++++++---- 3 files changed, 94 insertions(+), 16 deletions(-) diff --git a/contentEditable/main.js b/contentEditable/main.js index 8aaaec0efb..1c770bb405 100644 --- a/contentEditable/main.js +++ b/contentEditable/main.js @@ -14,6 +14,13 @@ $(document).ready( function() { 'end': 14 } }, + { + 'type': 'textStyle/italic', + 'range': { + 'start': 10, + 'end': 17 + } + }, { 'type': 'object/template', 'data': { @@ -38,7 +45,7 @@ $(document).ready( function() { window.surfaceModel = new es.SurfaceModel( window.documentModel ); window.surfaceView = new es.SurfaceView( $( '#es-editor' ), window.surfaceModel ); - + /* $('#es-editor')[0].addEventListener("DOMSubtreeModified", function() { var selection = rangy.getSelection(); console.log(selection); @@ -51,6 +58,7 @@ $(document).ready( function() { console.log(selection); } }); + */ /* diff --git a/contentEditable/views/es.ContentView.js b/contentEditable/views/es.ContentView.js index 3976457594..4b36e778aa 100644 --- a/contentEditable/views/es.ContentView.js +++ b/contentEditable/views/es.ContentView.js @@ -5,6 +5,14 @@ es.ContentView = function( $container, model ) { // Properties this.$ = $container; this.model = model; + + if ( model ) { + // Events + var _this = this; + this.model.on( 'update', function( offset ) { + _this.render( offset || 0 ); + } ); + } } diff --git a/contentEditable/views/es.SurfaceView.js b/contentEditable/views/es.SurfaceView.js index 82f18d91d2..203425df72 100644 --- a/contentEditable/views/es.SurfaceView.js +++ b/contentEditable/views/es.SurfaceView.js @@ -13,34 +13,96 @@ es.SurfaceView = function( $container, model ) { this.$.keydown( function(e) { return _this.onKeyDown( e ); } ); - + + this.model.getDocument().on( 'update', function() { + _this.emit( 'update' ); + } ); + this.documentView.renderContent(); }; es.SurfaceView.prototype.onKeyDown = function( e ) { if ( e.which == 13 ) { e.preventDefault(); - - console.log(this.getSelection()); + var range = this.getSelection(); + if ( range.start === range.end ) { + var tx = this.model.getDocument().prepareInsertion( range.start, [ { 'type': '/paragraph' }, { 'type': 'paragraph' } ]); + this.model.transact( tx ); + } } }; +es.SurfaceView.prototype.getOffset = function( localNode, localOffset ) { + var $node = $( localNode ); + while( !$node.hasClass( 'es-paragraphView' ) ) { + $node = $node.parent(); + } + + var traverse = function( data, callback ) { + var current = [ data, 0 ]; + var 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]]; + var out = callback( item ); + /* + * -1 = stop traversing + * 1 = deep traverse + * 0 = skip deep traverse + */ + if ( out === -1 ) { + return; + } else if ( out === 1 && item.nodeType === 1 ) { + stack.push( [ $(item).contents() , 0 ] ); + current[1]++; + current = stack[stack.length-1]; + continue; + } + current[1]++; + } + }; + + var offset = 0; + + traverse( $node.contents(), function( item ) { + if ( item.nodeType === 3 ) { + if ( item === localNode ) { + offset += localOffset; + return -1; + } else { + offset += item.textContent.length; + + } + } else { + if ( $( item ).attr('contentEditable') === "false" ) { + offset += 1; + return 0; + } + } + return 1; + } ); + + return offset + 1 + this.documentView.getOffsetFromNode( $node.data('view') ); +} + es.SurfaceView.prototype.getSelection = function() { var selection = rangy.getSelection(); - var node = selection.anchorNode; - var $node = $( node ); - while( !$node.hasClass( 'es-paragraphView' ) ) { - $node = $node.parent(); + 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 ); } - var $contents = $node.contents(); - for( var i = 0; i < $contents.length; i++ ) { - if ( $contents[i] == node ) { - console.log(node); - } - } - - return 0; }; /* Inheritance */