r112365 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112364‎ | r112365 | r112366 >
Date:22:37, 24 February 2012
Author:inez
Status:deferred
Tags:
Comment:
New approach for handling input in ContentEditable - compare not only plain text but also hash of DOM structure of the particular leafnode (it let's detect spellcheck weird behaviour and react to it)
Modified paths:
  • /trunk/extensions/VisualEditor/demos/playground/playground.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/demos/playground/playground.js
@@ -32,37 +32,46 @@
3333 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>");
3434 this.$editor.addClass('leafNode');
3535
36 - this.lastKeydown = null;
3736 this.keydown = false;
3837 this.keyup = false;
3938 this.keypress = false;
4039 this.mousedown = false;
41 - this.diff = false;
42 - this.prevText = app.getDOMText(this.$editor[0]);
43 - this.prevOffset = null;
 40+
4441 this.loop = false;
45 -
4642 this.logkey = false;
 43+
 44+ this.prevText = app.getDOMText(this.$editor[0]).replace(String.fromCharCode(32), " ").replace(String.fromCharCode(160), " ");
 45+
 46+ this.prevHash = app.getDOMHash(this.$editor[0]);
 47+ console.log("Initial hash", this.prevHash);
 48+
4749 };
4850
4951 app.prototype.onKeyDown = function( e ) {
50 - if(this.logkey) console.log("onKeyDown", e.which);
 52+ if ( this.logkey ) {
 53+ console.log("onKeyDown", e.which);
 54+ }
5155 this.keydown = true;
52 - this.lastKeydown = e.which;
5356 };
5457
5558 app.prototype.onKeyUp = function( e ) {
56 - if(this.logkey) console.log("onKeyUp", e.which);
 59+ if ( this.logkey ) {
 60+ console.log("onKeyUp", e.which);
 61+ }
5762 this.keyup = true;
5863 };
5964
6065 app.prototype.onKeyPress = function( e ) {
61 - if(this.logkey) console.log("onKeyPress");
 66+ if ( this.logkey ) {
 67+ console.log("onKeyPress");
 68+ }
6269 this.keypress = true;
6370 };
6471
6572 app.prototype.onMouseDown = function( e ) {
66 - if(this.logkey) console.log("onMouseDown");
 73+ if ( this.logkey ) {
 74+ console.log("onMouseDown");
 75+ }
6776 this.mousedown = true;
6877
6978 if(this.loop == false) {
@@ -75,76 +84,26 @@
7685
7786 app.prototype.loopFunc = function() {
7887 var text = app.getDOMText(this.$editor[0]).replace(String.fromCharCode(32), " ").replace(String.fromCharCode(160), " ");
 88+ var hash = app.getDOMHash(this.$editor[0]);
 89+
7990 var selection = rangy.getSelection();
 91+
8092 if ( !selection.anchorNode ) {
81 - console.log("och");
8293 return;
8394 }
 95+
8496 var offset = this.getOffset(selection.anchorNode, selection.anchorOffset);
8597
8698 if(text != this.prevText) {
87 - var textDiffLength = text.length - this.prevText.length;
88 - var offsetDiff = offset - this.prevOffset;
89 - var sameFromLeft = 0;
90 - var l = text.length;
91 - while ( sameFromLeft < l && this.prevText[sameFromLeft] == text[sameFromLeft] ) {
92 - ++sameFromLeft;
93 - }
94 -
95 -
96 - if(false) {
97 - console.log("change start", sameFromLeft, offset);
98 - console.log("different content", textDiffLength);
99 - console.log("different offset", offsetDiff);
100 - }
101 -
102 - if ( sameFromLeft != offset - textDiffLength ) {
103 - console.log('spell');
104 - }
105 -
106 -
107 -
108 -
109 - /*
110 - else if(textDiffLength === -1 && offsetDiff === -1 && offset === sameFromLeft ) {
111 - console.log("IME.1");
112 - } else if(textDiffLength === 0 && offsetDiff === 0 && offset-1 === sameFromLeft ) {
113 - console.log("IME.2");
114 - } else if ( textDiffLength < 0 || ( offset - textDiffLength ) !== sameFromLeft ) {
115 - console.log("SPELLCHECK");
116 - } else {
117 - console.log("!");
118 - }
119 - */
120 -
121 -
122 -
123 - if ( textDiffLength !== offsetDiff ) {
124 - //console.log("!!!!#####!!!!!");
125 - }
126 -
127 - if(!this.keydown) {
128 - //console.log("## not keyboard");
129 - } else {
130 - //console.log("@@ keyboard");
131 - }
132 -
133 -
134 -
135 -/*
136 - if((this.keydown || this.keyup) && this.lastKeydown !== 229) {
137 - console.log("");
138 - } else {
139 - console.log("Do NOT re-render");
140 - }
141 -*/
 99+ console.log("new text");
142100 this.prevText = text;
143 - this.diff = true;
144 - } else {
145 - this.diff = false;
146101 }
147102
148 - this.prevOffset = offset;
 103+ if(hash != this.prevHash) {
 104+ console.log("new DOM", hash);
 105+ this.prevHash = hash;
 106+ }
 107+
149108 this.keypress = false;
150109 this.keyup = false;
151110 this.keydown = false;
@@ -175,6 +134,23 @@
176135 return ret;
177136 };
178137
 138+app.getDOMHash = function( elem ) {
 139+ var nodeType = elem.nodeType,
 140+ nodeName = elem.nodeName,
 141+ ret = '';
 142+
 143+ if ( nodeType === 3 || nodeType === 4 ) {
 144+ return '#';
 145+ } else if ( nodeType === 1 || nodeType === 9 ) {
 146+ ret += '<' + nodeName + '>';
 147+ for ( elem = elem.firstChild; elem; elem = elem.nextSibling) {
 148+ ret += app.getDOMHash( elem );
 149+ }
 150+ ret += '</' + nodeName + '>';
 151+ }
 152+ return ret;
 153+};
 154+
179155 app.prototype.getOffset = function( localNode, localOffset ) {
180156 var $node = $( localNode );
181157

Status & tagging log