r110043 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110042‎ | r110043 | r110044 >
Date:01:37, 26 January 2012
Author:inez
Status:deferred
Tags:visualeditor 
Comment:
Basic support for "enter" key
Modified paths:
  • /trunk/extensions/VisualEditor/contentEditable/main.js (modified) (history)
  • /trunk/extensions/VisualEditor/contentEditable/views/es.ContentView.js (modified) (history)
  • /trunk/extensions/VisualEditor/contentEditable/views/es.SurfaceView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/contentEditable/main.js
@@ -15,6 +15,13 @@
1616 }
1717 },
1818 {
 19+ 'type': 'textStyle/italic',
 20+ 'range': {
 21+ 'start': 10,
 22+ 'end': 17
 23+ }
 24+ },
 25+ {
1926 'type': 'object/template',
2027 'data': {
2128 'html': '<sup><small>[<a href="#">citation needed</a>]</small></sup>'
@@ -38,7 +45,7 @@
3946 window.surfaceModel = new es.SurfaceModel( window.documentModel );
4047 window.surfaceView = new es.SurfaceView( $( '#es-editor' ), window.surfaceModel );
4148
42 -
 49+ /*
4350 $('#es-editor')[0].addEventListener("DOMSubtreeModified", function() {
4451 var selection = rangy.getSelection();
4552 console.log(selection);
@@ -51,6 +58,7 @@
5259 console.log(selection);
5360 }
5461 });
 62+ */
5563
5664
5765 /*
Index: trunk/extensions/VisualEditor/contentEditable/views/es.SurfaceView.js
@@ -13,34 +13,96 @@
1414 this.$.keydown( function(e) {
1515 return _this.onKeyDown( e );
1616 } );
17 -
 17+
 18+ this.model.getDocument().on( 'update', function() {
 19+ _this.emit( 'update' );
 20+ } );
 21+
1822 this.documentView.renderContent();
1923 };
2024
2125 es.SurfaceView.prototype.onKeyDown = function( e ) {
2226 if ( e.which == 13 ) {
2327 e.preventDefault();
24 -
25 - console.log(this.getSelection());
 28+ var range = this.getSelection();
 29+ if ( range.start === range.end ) {
 30+ var tx = this.model.getDocument().prepareInsertion( range.start, [ { 'type': '/paragraph' }, { 'type': 'paragraph' } ]);
 31+ this.model.transact( tx );
 32+ }
2633 }
2734 };
2835
29 -es.SurfaceView.prototype.getSelection = function() {
30 - var selection = rangy.getSelection();
31 -
32 - var node = selection.anchorNode;
33 - var $node = $( node );
 36+es.SurfaceView.prototype.getOffset = function( localNode, localOffset ) {
 37+ var $node = $( localNode );
3438 while( !$node.hasClass( 'es-paragraphView' ) ) {
3539 $node = $node.parent();
3640 }
37 - var $contents = $node.contents();
38 - for( var i = 0; i < $contents.length; i++ ) {
39 - if ( $contents[i] == node ) {
40 - console.log(node);
 41+
 42+ var traverse = function( data, callback ) {
 43+ var current = [ data, 0 ];
 44+ var stack = [ current ];
 45+
 46+ while ( stack.length > 0 ) {
 47+ if ( current[1] >= current[0].length ) {
 48+ stack.pop();
 49+ current = stack[ stack.length - 1 ];
 50+ continue;
 51+ }
 52+ var item = current[0][current[1]];
 53+ var out = callback( item );
 54+ /*
 55+ * -1 = stop traversing
 56+ * 1 = deep traverse
 57+ * 0 = skip deep traverse
 58+ */
 59+ if ( out === -1 ) {
 60+ return;
 61+ } else if ( out === 1 && item.nodeType === 1 ) {
 62+ stack.push( [ $(item).contents() , 0 ] );
 63+ current[1]++;
 64+ current = stack[stack.length-1];
 65+ continue;
 66+ }
 67+ current[1]++;
4168 }
 69+ };
 70+
 71+ var offset = 0;
 72+
 73+ traverse( $node.contents(), function( item ) {
 74+ if ( item.nodeType === 3 ) {
 75+ if ( item === localNode ) {
 76+ offset += localOffset;
 77+ return -1;
 78+ } else {
 79+ offset += item.textContent.length;
 80+
 81+ }
 82+ } else {
 83+ if ( $( item ).attr('contentEditable') === "false" ) {
 84+ offset += 1;
 85+ return 0;
 86+ }
 87+ }
 88+ return 1;
 89+ } );
 90+
 91+ return offset + 1 + this.documentView.getOffsetFromNode( $node.data('view') );
 92+}
 93+
 94+es.SurfaceView.prototype.getSelection = function() {
 95+ var selection = rangy.getSelection();
 96+
 97+ if ( selection.anchorNode === selection.focusNode && selection.anchorOffset === selection.focusOffset ) {
 98+ // only one offset
 99+ var offset = this.getOffset( selection.anchorNode, selection.anchorOffset );
 100+ return new es.Range( offset, offset );
 101+ } else {
 102+ // two offsets
 103+ var offset1 = this.getOffset( selection.anchorNode, selection.anchorOffset );
 104+ var offset2 = this.getOffset( selection.focusNode, selection.focusOffset );
 105+ return new es.Range( offset1, offset2 );
42106 }
43 -
44 - return 0;
45107 };
46108
47109 /* Inheritance */
Index: trunk/extensions/VisualEditor/contentEditable/views/es.ContentView.js
@@ -5,6 +5,14 @@
66 // Properties
77 this.$ = $container;
88 this.model = model;
 9+
 10+ if ( model ) {
 11+ // Events
 12+ var _this = this;
 13+ this.model.on( 'update', function( offset ) {
 14+ _this.render( offset || 0 );
 15+ } );
 16+ }
917 }
1018
1119

Status & tagging log