r110047 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110046‎ | r110047 | r110048 >
Date:06:55, 26 January 2012
Author:inez
Status:deferred
Tags:visualeditor 
Comment:
Small refactoring to getOffset method in SurfaceView
Modified paths:
  • /trunk/extensions/VisualEditor/contentEditable/views/es.SurfaceView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/contentEditable/views/es.SurfaceView.js
@@ -1,118 +1,104 @@
2 -es.SurfaceView = function( $container, model ) {
3 - // Inheritance
4 - es.EventEmitter.call( this );
5 -
6 - // References for use in closures
7 - var _this = this;
8 -
9 - // Properties
10 - this.model = model;
11 - this.documentView = new es.DocumentView( this.model.getDocument(), this );
12 - this.$ = $container.append( this.documentView.$ );
13 -
14 - this.$.keydown( function(e) {
15 - return _this.onKeyDown( e );
16 - } );
17 -
18 - this.model.getDocument().on( 'update', function() {
19 - _this.emit( 'update' );
20 - } );
21 -
22 - this.documentView.renderContent();
23 -};
24 -
25 -es.SurfaceView.prototype.onKeyDown = function( e ) {
26 - if ( e.which === 13 ) {
27 - e.preventDefault();
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 - }
33 - } else if ( e.which === 8 ) {
34 - console.log("A");
35 - e.preventDefault();
36 - var range = this.getSelection();
37 - if ( range.start != range.end ) {
38 - var tx = this.model.getDocument().prepareRemoval( range );
39 - this.model.transact( tx );
40 - }
41 - }
42 -};
43 -
44 -es.SurfaceView.prototype.getOffset = function( localNode, localOffset ) {
45 - var $node = $( localNode );
46 - while( !$node.hasClass( 'es-paragraphView' ) ) {
47 - $node = $node.parent();
48 - }
49 -
50 - var traverse = function( data, callback ) {
51 - var current = [ data, 0 ];
52 - var stack = [ current ];
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 out = callback( item );
62 - /*
63 - * -1 = stop traversing
64 - * 1 = deep traverse
65 - * 0 = skip deep traverse
66 - */
67 - if ( out === -1 ) {
68 - return;
69 - } else if ( out === 1 && item.nodeType === 1 ) {
70 - stack.push( [ $(item).contents() , 0 ] );
71 - current[1]++;
72 - current = stack[stack.length-1];
73 - continue;
74 - }
75 - current[1]++;
76 - }
77 - };
78 -
79 - var offset = 0;
80 -
81 - traverse( $node.contents(), function( item ) {
82 - if ( item.nodeType === 3 ) {
83 - if ( item === localNode ) {
84 - offset += localOffset;
85 - return -1;
86 - } else {
87 - offset += item.textContent.length;
88 -
89 - }
90 - } else {
91 - if ( $( item ).attr('contentEditable') === "false" ) {
92 - offset += 1;
93 - return 0;
94 - }
95 - }
96 - return 1;
97 - } );
98 -
99 - return offset + 1 + this.documentView.getOffsetFromNode( $node.data('view') );
100 -}
101 -
102 -es.SurfaceView.prototype.getSelection = function() {
103 - var selection = rangy.getSelection();
104 -
105 - if ( selection.anchorNode === selection.focusNode && selection.anchorOffset === selection.focusOffset ) {
106 - // only one offset
107 - var offset = this.getOffset( selection.anchorNode, selection.anchorOffset );
108 - return new es.Range( offset, offset );
109 - } else {
110 - // two offsets
111 - var offset1 = this.getOffset( selection.anchorNode, selection.anchorOffset );
112 - var offset2 = this.getOffset( selection.focusNode, selection.focusOffset );
113 - return new es.Range( offset1, offset2 );
114 - }
115 -};
116 -
117 -/* Inheritance */
118 -
 2+es.SurfaceView = function( $container, model ) {
 3+ // Inheritance
 4+ es.EventEmitter.call( this );
 5+
 6+ // References for use in closures
 7+ var _this = this;
 8+
 9+ // Properties
 10+ this.model = model;
 11+ this.documentView = new es.DocumentView( this.model.getDocument(), this );
 12+ this.$ = $container.append( this.documentView.$ );
 13+
 14+ this.$.keydown( function(e) {
 15+ return _this.onKeyDown( e );
 16+ } );
 17+
 18+ this.model.getDocument().on( 'update', function() {
 19+ _this.emit( 'update' );
 20+ } );
 21+
 22+ this.documentView.renderContent();
 23+};
 24+
 25+es.SurfaceView.prototype.onKeyDown = function( e ) {
 26+ if ( e.which === 13 ) {
 27+ e.preventDefault();
 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+ }
 33+ } else if ( e.which === 8 ) {
 34+ console.log("A");
 35+ e.preventDefault();
 36+ var range = this.getSelection();
 37+ if ( range.start != range.end ) {
 38+ var tx = this.model.getDocument().prepareRemoval( range );
 39+ this.model.transact( tx );
 40+ }
 41+ }
 42+};
 43+
 44+es.SurfaceView.prototype.getOffset = function( localNode, localOffset ) {
 45+ var $node = $( localNode );
 46+ while( !$node.hasClass( 'es-paragraphView' ) ) {
 47+ $node = $node.parent();
 48+ }
 49+
 50+ var current = [$node.contents(), 0];
 51+ var stack = [current];
 52+
 53+ var offset = 0;
 54+
 55+ while ( stack.length > 0 ) {
 56+ if ( current[1] >= current[0].length ) {
 57+ stack.pop();
 58+ current = stack[ stack.length - 1 ];
 59+ continue;
 60+ }
 61+ var item = current[0][current[1]];
 62+ var $item = current[0].eq( current[1] );
 63+
 64+ if ( item.nodeType === 3 ) {
 65+ if ( item === localNode ) {
 66+ offset += localOffset;
 67+ break;
 68+ } else {
 69+ offset += item.textContent.length;
 70+ }
 71+ } else if ( item.nodeType === 1 ) {
 72+ if ( $( item ).attr('contentEditable') === "false" ) {
 73+ console.log("in");
 74+ offset += 1;
 75+ } else {
 76+ stack.push( [$item.contents(), 0] );
 77+ current[1]++;
 78+ current = stack[stack.length-1];
 79+ continue;
 80+ }
 81+ }
 82+ current[1]++;
 83+ }
 84+
 85+ return this.documentView.getOffsetFromNode( $node.data('view') ) + 1 + offset;
 86+}
 87+
 88+es.SurfaceView.prototype.getSelection = function() {
 89+ var selection = rangy.getSelection();
 90+
 91+ if ( selection.anchorNode === selection.focusNode && selection.anchorOffset === selection.focusOffset ) {
 92+ // only one offset
 93+ var offset = this.getOffset( selection.anchorNode, selection.anchorOffset );
 94+ return new es.Range( offset, offset );
 95+ } else {
 96+ // two offsets
 97+ var offset1 = this.getOffset( selection.anchorNode, selection.anchorOffset );
 98+ var offset2 = this.getOffset( selection.focusNode, selection.focusOffset );
 99+ return new es.Range( offset1, offset2 );
 100+ }
 101+};
 102+
 103+/* Inheritance */
 104+
119105 es.extendClass( es.SurfaceView, es.EventEmitter );
\ No newline at end of file

Status & tagging log