Index: trunk/extensions/VisualEditor/demos/playground/playground.js |
— | — | @@ -32,37 +32,46 @@ |
33 | 33 | 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 | 34 | this.$editor.addClass('leafNode');
|
35 | 35 |
|
36 | | - this.lastKeydown = null;
|
37 | 36 | this.keydown = false;
|
38 | 37 | this.keyup = false;
|
39 | 38 | this.keypress = false;
|
40 | 39 | this.mousedown = false;
|
41 | | - this.diff = false;
|
42 | | - this.prevText = app.getDOMText(this.$editor[0]);
|
43 | | - this.prevOffset = null;
|
| 40 | +
|
44 | 41 | this.loop = false;
|
45 | | -
|
46 | 42 | 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 | +
|
47 | 49 | };
|
48 | 50 |
|
49 | 51 | 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 | + }
|
51 | 55 | this.keydown = true;
|
52 | | - this.lastKeydown = e.which;
|
53 | 56 | };
|
54 | 57 |
|
55 | 58 | 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 | + }
|
57 | 62 | this.keyup = true;
|
58 | 63 | };
|
59 | 64 |
|
60 | 65 | app.prototype.onKeyPress = function( e ) {
|
61 | | - if(this.logkey) console.log("onKeyPress");
|
| 66 | + if ( this.logkey ) {
|
| 67 | + console.log("onKeyPress");
|
| 68 | + }
|
62 | 69 | this.keypress = true;
|
63 | 70 | };
|
64 | 71 |
|
65 | 72 | app.prototype.onMouseDown = function( e ) {
|
66 | | - if(this.logkey) console.log("onMouseDown");
|
| 73 | + if ( this.logkey ) {
|
| 74 | + console.log("onMouseDown");
|
| 75 | + }
|
67 | 76 | this.mousedown = true;
|
68 | 77 |
|
69 | 78 | if(this.loop == false) {
|
— | — | @@ -75,76 +84,26 @@ |
76 | 85 |
|
77 | 86 | app.prototype.loopFunc = function() {
|
78 | 87 | var text = app.getDOMText(this.$editor[0]).replace(String.fromCharCode(32), " ").replace(String.fromCharCode(160), " ");
|
| 88 | + var hash = app.getDOMHash(this.$editor[0]);
|
| 89 | +
|
79 | 90 | var selection = rangy.getSelection();
|
| 91 | +
|
80 | 92 | if ( !selection.anchorNode ) {
|
81 | | - console.log("och");
|
82 | 93 | return;
|
83 | 94 | }
|
| 95 | +
|
84 | 96 | var offset = this.getOffset(selection.anchorNode, selection.anchorOffset);
|
85 | 97 |
|
86 | 98 | 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");
|
142 | 100 | this.prevText = text;
|
143 | | - this.diff = true;
|
144 | | - } else {
|
145 | | - this.diff = false;
|
146 | 101 | }
|
147 | 102 |
|
148 | | - this.prevOffset = offset;
|
| 103 | + if(hash != this.prevHash) {
|
| 104 | + console.log("new DOM", hash);
|
| 105 | + this.prevHash = hash;
|
| 106 | + }
|
| 107 | +
|
149 | 108 | this.keypress = false;
|
150 | 109 | this.keyup = false;
|
151 | 110 | this.keydown = false;
|
— | — | @@ -175,6 +134,23 @@ |
176 | 135 | return ret;
|
177 | 136 | };
|
178 | 137 |
|
| 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 | +
|
179 | 155 | app.prototype.getOffset = function( localNode, localOffset ) {
|
180 | 156 | var $node = $( localNode );
|
181 | 157 |
|