r112163 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112162‎ | r112163 | r112164 >
Date:23:31, 22 February 2012
Author:inez
Status:deferred
Tags:
Comment:
Implementation of setinterval loop that checks for changes in contenteditable content
Modified paths:
  • /trunk/extensions/VisualEditor/playground/index.html (modified) (history)
  • /trunk/extensions/VisualEditor/playground/playground.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/playground/playground.js
@@ -10,6 +10,12 @@
1111 $document.bind( {
1212 'keydown.surfaceView': function( e ) {
1313 return _this.onKeyDown( e );
 14+ },
 15+ 'keyup.surfaceView': function( e ) {
 16+ return _this.onKeyUp( e );
 17+ },
 18+ 'keypress.surfaceView': function( e ) {
 19+ return _this.onKeyPress( e );
1420 }
1521 } );
1622 },
@@ -24,24 +30,46 @@
2531
2632 // Set initial content for the "editor"
2733 this.$editor.html("<b>Lorem Ipsum is simply dummy text</b> of the printing and typesetting industry. <b>Lorem Ipsum has been the <i>industry's</i> standard</b> dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it <u>to <b>make <i>a type</i> specimen</b> book.</u>");
 34+ this.$editor.addClass('leafNode');
2835
2936 this.keydown = false;
3037 this.mousedown = false;
 38+ this.inime = false;
3139 this.prevText = app.getDOMText(this.$editor[0]);
3240
3341 setInterval(function() {
3442 _this.loopFunc();
35 - }, 200);
 43+ }, 100);
3644 };
3745
38 -app.prototype.onKeyDown = function() {
 46+app.prototype.onKeyPress = function() {
 47+ console.log("onKeyPress");
 48+};
 49+
 50+app.prototype.onKeyUp = function() {
 51+ //console.log("onKeyUp");
 52+ if ( this.inime ) {
 53+ this.inime = false;
 54+ console.log("inime = false");
 55+ }
 56+};
 57+
 58+app.prototype.onKeyDown = function( e ) {
3959 this.keydown = true;
4060 //console.log("onKeyDown");
 61+ if ( e.which === 229 ) {
 62+ this.inime = true;
 63+ console.log("inime = true");
 64+ }
4165 };
4266
4367 app.prototype.onMouseDown = function() {
4468 this.mousedown = true;
4569 //console.log("onMouseDown");
 70+ if ( this.inime ) {
 71+ this.inime = false;
 72+ console.log("inime = false");
 73+ }
4674 };
4775
4876 app.prototype.loopFunc = function() {
@@ -50,10 +78,26 @@
5179 if(text != this.prevText) {
5280 //console.log(text);
5381
54 - if(this.keydown) {
55 - console.log("change from keyboard");
 82+ if(this.keydown || this.inime) {
 83+ //console.log("change from keyboard");
 84+ // we are going to need a cursor position
 85+ var selection = rangy.getSelection();
 86+ var offset = this.getOffset( selection.anchorNode, selection.anchorOffset );
 87+ var diffLength = text.length - this.prevText.length;
 88+ //console.log("diffLength: " + diffLength);
 89+
 90+ if ( diffLength > 0 ) {
 91+ console.log( text.substring(offset - diffLength, offset) );
 92+ } else if ( diffLength === 0 ) {
 93+ console.log( text.substring(offset - 1, offset) );
 94+ }
 95+
 96+
 97+
 98+
5699 } else {
57100 console.log("change not from keyboard");
 101+ // find a change and commit to the model
58102 }
59103
60104 this.prevText = text;
@@ -87,6 +131,59 @@
88132 return ret;
89133 };
90134
 135+app.prototype.getOffset = function( localNode, localOffset ) {
 136+ var $node = $( localNode );
 137+
 138+ if ( $node.hasClass( 'leafNode' ) ) {
 139+ return localOffset;
 140+ }
 141+
 142+ while( !$node.hasClass( 'leafNode' ) ) {
 143+ $node = $node.parent();
 144+ }
 145+
 146+ var current = [$node.contents(), 0];
 147+ var stack = [current];
 148+
 149+ var offset = 0;
 150+
 151+ while ( stack.length > 0 ) {
 152+ if ( current[1] >= current[0].length ) {
 153+ stack.pop();
 154+ current = stack[ stack.length - 1 ];
 155+ continue;
 156+ }
 157+ var item = current[0][current[1]];
 158+ var $item = current[0].eq( current[1] );
 159+
 160+ if ( item.nodeType === 3 ) {
 161+ if ( item === localNode ) {
 162+ offset += localOffset;
 163+ break;
 164+ } else {
 165+ offset += item.textContent.length;
 166+ }
 167+ } else if ( item.nodeType === 1 ) {
 168+ if ( $( item ).attr('contentEditable') === "false" ) {
 169+ offset += 1;
 170+ } else {
 171+ if ( item === localNode ) {
 172+ offset += localOffset;
 173+ break;
 174+ }
 175+
 176+ stack.push( [$item.contents(), 0] );
 177+ current[1]++;
 178+ current = stack[stack.length-1];
 179+ continue;
 180+ }
 181+ }
 182+ current[1]++;
 183+ }
 184+
 185+ return offset;
 186+};
 187+
91188 $(function() {
92189 new app();
93190 });
\ No newline at end of file
Index: trunk/extensions/VisualEditor/playground/index.html
@@ -11,6 +11,7 @@
1212 }
1313 </style>
1414 <script src="../modules/jquery/jquery.js"></script>
 15+ <script src="../modules/rangy/rangy-core.js"></script>
1516 <script src="playground.js"></script>
1617 </head>
1718 <body>

Status & tagging log