Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.Surface.js |
— | — | @@ -44,11 +44,11 @@ |
45 | 45 | |
46 | 46 | this.$ |
47 | 47 | .on( 'cut copy', function( e ) { |
48 | | - _this.onCutCopy( e ); |
49 | | - } ) |
| 48 | + _this.onCutCopy( e ); |
| 49 | + } ) |
50 | 50 | .on( 'paste', function( e ) { |
51 | | - _this.onPaste( e ); |
52 | | - } ) |
| 51 | + _this.onPaste( e ); |
| 52 | + } ) |
53 | 53 | .on( 'mousedown', function( e ) { |
54 | 54 | return _this.onMouseDown( e ); |
55 | 55 | } ) |
— | — | @@ -62,20 +62,20 @@ |
63 | 63 | } ) |
64 | 64 | .on('dragover drop', function( e ) { |
65 | 65 | e.preventDefault(); |
66 | | - }); |
| 66 | + } ); |
67 | 67 | |
68 | 68 | // Initialization |
69 | 69 | this.documentView.renderContent(); |
70 | 70 | |
71 | 71 | this.poll = { |
72 | | - interval: null, |
73 | | - frequency: 75, |
74 | | - node: null, |
75 | | - prevText: null, |
76 | | - prevHash: null, |
77 | | - prevOffset: null, |
78 | | - compositionStart: null, |
79 | | - compositionEnd: null |
| 72 | + 'interval': null, |
| 73 | + 'frequency': 75, |
| 74 | + 'node': null, |
| 75 | + 'prevText': null, |
| 76 | + 'prevHash': null, |
| 77 | + 'prevOffset': null, |
| 78 | + 'compositionStart': null, |
| 79 | + 'compositionEnd': null |
80 | 80 | }; |
81 | 81 | }; |
82 | 82 | |
— | — | @@ -113,8 +113,8 @@ |
114 | 114 | |
115 | 115 | ve.ce.Surface.prototype.onCutCopy = function( e ) { |
116 | 116 | var _this = this, |
117 | | - rangySel = rangy.getSelection(), |
118 | | - key = rangySel.getRangeAt(0).toString().replace(/\s/gm,""); |
| 117 | + sel = rangy.getSelection(), |
| 118 | + key = sel.getRangeAt(0).toString().replace( /\s/gm, '' ); |
119 | 119 | |
120 | 120 | _this.clipboard[key] = ve.copyArray( _this.documentView.model.getData( _this.getSelection() ) ); |
121 | 121 | |
— | — | @@ -134,7 +134,10 @@ |
135 | 135 | _this.getLeafNode( node ).data( 'view' ).renderContent(); |
136 | 136 | |
137 | 137 | // clear the prev information from poll object (probably a better way to do this) |
138 | | - _this.poll.prevText = _this.poll.prevHash = _this.poll.prevOffset = _this.poll.node = null; |
| 138 | + _this.poll.prevText = |
| 139 | + _this.poll.prevHash = |
| 140 | + _this.poll.prevOffset = |
| 141 | + _this.poll.node = null; |
139 | 142 | |
140 | 143 | // place cursor |
141 | 144 | _this.showCursorAt( selection.start ); |
— | — | @@ -147,21 +150,31 @@ |
148 | 151 | insertionPoint = _this.getSelection().start, |
149 | 152 | node = rangy.getSelection().anchorNode; |
150 | 153 | |
151 | | - $('#paste').html('').show().css( 'top', $(window).scrollTop() ).css(' left', $(window).scrollLeft() ).focus(); |
| 154 | + $('#paste') |
| 155 | + .html('') |
| 156 | + .show() |
| 157 | + .css( 'top', $(window).scrollTop() ) |
| 158 | + .css(' left', $(window).scrollLeft() ) |
| 159 | + .focus(); |
152 | 160 | |
153 | 161 | setTimeout( function() { |
154 | | - var key = $('#paste').hide().text().replace(/\s/gm,""); |
| 162 | + var key = $('#paste').hide().text().replace( /\s/gm, '' ); |
155 | 163 | |
156 | 164 | if ( _this.clipboard[key] ) { |
157 | 165 | // transact |
158 | | - var tx = _this.documentView.model.prepareInsertion( insertionPoint, _this.clipboard[key]); |
| 166 | + var tx = _this.documentView.model.prepareInsertion( |
| 167 | + insertionPoint, _this.clipboard[key] |
| 168 | + ); |
159 | 169 | _this.model.transact( tx ); |
160 | 170 | |
161 | 171 | // re-render |
162 | 172 | _this.getLeafNode( node ).data( 'view' ).renderContent(); |
163 | 173 | |
164 | 174 | // clear the prev information from poll object (probably a better way to do this) |
165 | | - _this.poll.prevText = _this.poll.prevHash = _this.poll.prevOffset = _this.poll.node = null; |
| 175 | + _this.poll.prevText = |
| 176 | + _this.poll.prevHash = |
| 177 | + _this.poll.prevOffset = |
| 178 | + _this.poll.node = null; |
166 | 179 | |
167 | 180 | // place cursor |
168 | 181 | _this.showCursorAt( insertionPoint + _this.clipboard[key].length ); |
— | — | @@ -173,13 +186,13 @@ |
174 | 187 | |
175 | 188 | ve.ce.Surface.prototype.onCompositionStart = function( e ) { |
176 | 189 | this.stopPolling(); |
177 | | - var rangySel = rangy.getSelection(); |
178 | | - this.poll.compositionStart = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, false ); |
| 190 | + var sel = rangy.getSelection(); |
| 191 | + this.poll.compositionStart = this.getOffset( sel.anchorNode, sel.anchorOffset, false ); |
179 | 192 | }; |
180 | 193 | |
181 | 194 | ve.ce.Surface.prototype.onCompositionEnd = function( e ) { |
182 | | - var rangySel = rangy.getSelection(); |
183 | | - this.poll.compositionEnd = this.getOffset( rangySel.focusNode, rangySel.focusOffset, false ); |
| 195 | + var sel = rangy.getSelection(); |
| 196 | + this.poll.compositionEnd = this.getOffset( sel.focusNode, sel.focusOffset, false ); |
184 | 197 | this.startPolling(); |
185 | 198 | }; |
186 | 199 | |
— | — | @@ -230,20 +243,20 @@ |
231 | 244 | this.poll.compositionEnd = null; |
232 | 245 | |
233 | 246 | } else { |
234 | | - var rangySel = rangy.getSelection(); |
| 247 | + var sel = rangy.getSelection(); |
235 | 248 | |
236 | | - if ( rangySel.anchorNode === null ) { |
| 249 | + if ( sel.anchorNode === null ) { |
237 | 250 | return; |
238 | 251 | } |
239 | 252 | |
240 | | - var node = this.getLeafNode( rangySel.anchorNode )[0]; |
| 253 | + var node = this.getLeafNode( sel.anchorNode )[0]; |
241 | 254 | text = ve.ce.Surface.getDOMText2( node ); |
242 | 255 | hash = ve.ce.Surface.getDOMHash( node ); |
243 | 256 | |
244 | | - if ( rangySel.anchorNode !== rangySel.focusNode || rangySel.anchorOffset !== rangySel.focusOffset ) { |
| 257 | + if ( sel.anchorNode !== sel.focusNode || sel.anchorOffset !== sel.focusOffset ) { |
245 | 258 | localOffset = null; |
246 | 259 | } else { |
247 | | - localOffset = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, false ); |
| 260 | + localOffset = this.getOffset( sel.anchorNode, sel.anchorOffset, false ); |
248 | 261 | } |
249 | 262 | |
250 | 263 | if ( node !== this.poll.node ) { |
— | — | @@ -261,11 +274,18 @@ |
262 | 275 | if ( text !== this.poll.prevText ) { |
263 | 276 | var nodeOffset = this.documentView.getOffsetFromNode( $( this.poll.node ).data( 'view' ) ), |
264 | 277 | lengthDiff = text.length - this.poll.prevText.length, |
265 | | - offsetDiff = ( localOffset !== null && this.poll.prevOffset !== null ) ? localOffset - this.poll.prevOffset : null; |
| 278 | + offsetDiff = ( localOffset !== null && this.poll.prevOffset !== null ) ? |
| 279 | + localOffset - this.poll.prevOffset : null; |
266 | 280 | |
267 | | - if ( lengthDiff === offsetDiff && this.poll.prevText.substring( 0, this.poll.prevOffset ) === text.substring( 0, this.poll.prevOffset ) ) { |
| 281 | + if ( |
| 282 | + lengthDiff === offsetDiff && |
| 283 | + this.poll.prevText.substring( 0, this.poll.prevOffset ) === |
| 284 | + text.substring( 0, this.poll.prevOffset ) |
| 285 | + ) { |
268 | 286 | newData = text.substring( this.poll.prevOffset, localOffset ).split( '' ); |
269 | | - annotations = this.model.getDocument().getAnnotationsFromOffset( nodeOffset + 1 + this.poll.prevOffset - 1, true ); |
| 287 | + annotations = this.model.getDocument().getAnnotationsFromOffset( |
| 288 | + nodeOffset + 1 + this.poll.prevOffset - 1, true |
| 289 | + ); |
270 | 290 | ve.dm.DocumentNode.addAnnotationsToData( newData, annotations ); |
271 | 291 | this.model.transact( this.documentView.model.prepareInsertion( |
272 | 292 | nodeOffset + 1 + this.poll.prevOffset, |
— | — | @@ -274,15 +294,22 @@ |
275 | 295 | } else { |
276 | 296 | var sameFromLeft = 0, |
277 | 297 | sameFromRight = 0, |
278 | | - l = text.length > this.poll.prevText.length ? this.poll.prevText.length : text.length; |
| 298 | + l = text.length > this.poll.prevText.length ? |
| 299 | + this.poll.prevText.length : text.length; |
279 | 300 | while ( sameFromLeft < l && this.poll.prevText[sameFromLeft] === text[sameFromLeft] ) { |
280 | 301 | ++sameFromLeft; |
281 | 302 | } |
282 | 303 | l = l - sameFromLeft; |
283 | | - while ( sameFromRight < l && this.poll.prevText[this.poll.prevText.length - 1 - sameFromRight] === text[text.length - 1 - sameFromRight] ) { |
284 | | - ++sameFromRight; |
| 304 | + while ( |
| 305 | + sameFromRight < l && |
| 306 | + this.poll.prevText[this.poll.prevText.length - 1 - sameFromRight] === |
| 307 | + text[text.length - 1 - sameFromRight] |
| 308 | + ) { |
| 309 | + ++sameFromRight; |
285 | 310 | } |
286 | | - annotations = this.model.getDocument().getAnnotationsFromOffset( nodeOffset + 1 + sameFromLeft, true ); |
| 311 | + annotations = this.model.getDocument().getAnnotationsFromOffset( |
| 312 | + nodeOffset + 1 + sameFromLeft, true |
| 313 | + ); |
287 | 314 | this.model.transact( this.documentView.model.prepareRemoval( new ve.Range( |
288 | 315 | nodeOffset + 1 + sameFromLeft, |
289 | 316 | nodeOffset + 1 + this.poll.prevText.length - sameFromRight |
— | — | @@ -319,6 +346,11 @@ |
320 | 347 | this.pollContent(); |
321 | 348 | this.startPolling(); |
322 | 349 | } |
| 350 | + var sel, |
| 351 | + globalOffset, |
| 352 | + newOffset, |
| 353 | + node, |
| 354 | + nodeOffset; |
323 | 355 | switch ( e.keyCode ) { |
324 | 356 | // Enter |
325 | 357 | case 13: |
— | — | @@ -337,13 +369,15 @@ |
338 | 370 | break; |
339 | 371 | // Left arrow |
340 | 372 | case 37: |
341 | | - var rangySel = rangy.getSelection(); |
342 | | - if ( rangySel.anchorOffset === 0 ) { |
343 | | - var globalOffset = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, true ), |
344 | | - node = this.documentView.getNodeFromOffset( globalOffset ), |
345 | | - nodeOffset = surfaceView.documentView.getOffsetFromNode( node ); |
| 373 | + sel = rangy.getSelection(); |
| 374 | + if ( sel.anchorOffset === 0 ) { |
| 375 | + globalOffset = this.getOffset( sel.anchorNode, sel.anchorOffset, true ); |
| 376 | + node = this.documentView.getNodeFromOffset( globalOffset ); |
| 377 | + nodeOffset = surfaceView.documentView.getOffsetFromNode( node ); |
346 | 378 | if ( nodeOffset + 1 === globalOffset ) { |
347 | | - var newOffset = this.documentView.model.getRelativeContentOffset( globalOffset, -1 ); |
| 379 | + newOffset = this.documentView.model.getRelativeContentOffset( |
| 380 | + globalOffset, -1 |
| 381 | + ); |
348 | 382 | this.showCursorAt(newOffset); |
349 | 383 | e.preventDefault(); |
350 | 384 | } |
— | — | @@ -351,13 +385,15 @@ |
352 | 386 | break; |
353 | 387 | // Right arrow |
354 | 388 | case 39: |
355 | | - var rangySel = rangy.getSelection(); |
356 | | - if ( rangySel.anchorOffset === rangySel.anchorNode.length ) { |
357 | | - var globalOffset = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, true ), |
358 | | - node = this.documentView.getNodeFromOffset( globalOffset ), |
359 | | - nodeOffset = surfaceView.documentView.getOffsetFromNode( node ); |
| 389 | + sel = rangy.getSelection(); |
| 390 | + if ( sel.anchorOffset === sel.anchorNode.length ) { |
| 391 | + globalOffset = this.getOffset( sel.anchorNode, sel.anchorOffset, true ); |
| 392 | + node = this.documentView.getNodeFromOffset( globalOffset ); |
| 393 | + nodeOffset = surfaceView.documentView.getOffsetFromNode( node ); |
360 | 394 | if ( nodeOffset + 1 + node.getContentLength() === globalOffset ) { |
361 | | - var newOffset = this.documentView.model.getRelativeContentOffset( globalOffset, 1 ); |
| 395 | + newOffset = this.documentView.model.getRelativeContentOffset( |
| 396 | + globalOffset, 1 |
| 397 | + ); |
362 | 398 | this.showCursorAt(newOffset); |
363 | 399 | e.preventDefault(); |
364 | 400 | } |
— | — | @@ -393,7 +429,7 @@ |
394 | 430 | localOffset += item.textContent.length; |
395 | 431 | } |
396 | 432 | } else if ( item.nodeType === 1 ) { |
397 | | - if ( $( item ).attr( 'contentEditable' ) === "false" ) { |
| 433 | + if ( $( item ).attr( 'contentEditable' ) === 'false' ) { |
398 | 434 | offset += 1; |
399 | 435 | } else { |
400 | 436 | if ( item === elem ) { |
— | — | @@ -443,7 +479,7 @@ |
444 | 480 | index += length; |
445 | 481 | } |
446 | 482 | } else if ( item.nodeType === 1 ) { |
447 | | - if ( $( item ).attr('contentEditable') === "false" ) { |
| 483 | + if ( $( item ).attr('contentEditable') === 'false' ) { |
448 | 484 | index += 1; |
449 | 485 | } else { |
450 | 486 | stack.push( [$item.contents(), 0] ); |
— | — | @@ -463,16 +499,16 @@ |
464 | 500 | }; |
465 | 501 | |
466 | 502 | ve.ce.Surface.prototype.getSelection = function() { |
467 | | - var rangySel = rangy.getSelection(), |
| 503 | + var sel = rangy.getSelection(), |
468 | 504 | range; |
469 | 505 | |
470 | | - if ( rangySel.anchorNode === rangySel.focusNode && rangySel.anchorOffset === rangySel.focusOffset ) { |
471 | | - var offset = this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, true ); |
| 506 | + if ( sel.anchorNode === sel.focusNode && sel.anchorOffset === sel.focusOffset ) { |
| 507 | + var offset = this.getOffset( sel.anchorNode, sel.anchorOffset, true ); |
472 | 508 | range = new ve.Range( offset, offset ); |
473 | 509 | } else { |
474 | 510 | range = new ve.Range( |
475 | | - this.getOffset( rangySel.anchorNode, rangySel.anchorOffset, true ), |
476 | | - this.getOffset( rangySel.focusNode, rangySel.focusOffset, true ) |
| 511 | + this.getOffset( sel.anchorNode, sel.anchorOffset, true ), |
| 512 | + this.getOffset( sel.focusNode, sel.focusOffset, true ) |
477 | 513 | ); |
478 | 514 | } |
479 | 515 | range.normalize(); |
— | — | @@ -489,38 +525,38 @@ |
490 | 526 | |
491 | 527 | ve.ce.Surface.getDOMText2 = function( elem ) { |
492 | 528 | // TODO: there must be some better way to write this regex replace |
493 | | - var regex = new RegExp("[" + String.fromCharCode(32) + String.fromCharCode(160) + "]", "g"); |
494 | | - return ve.ce.Surface.getDOMText( elem ).replace( regex, " " ); |
| 529 | + var regex = new RegExp('[' + String.fromCharCode(32) + String.fromCharCode(160) + ']', 'g'); |
| 530 | + return ve.ce.Surface.getDOMText( elem ).replace( regex, ' ' ); |
495 | 531 | }; |
496 | 532 | |
497 | 533 | ve.ce.Surface.getDOMText = function( elem ) { |
498 | | - var nodeType = elem.nodeType, |
499 | | - ret = ''; |
| 534 | + var nodeType = elem.nodeType, |
| 535 | + ret = ''; |
500 | 536 | |
501 | | - if ( nodeType === 1 || nodeType === 9 ) { |
502 | | - // Use textContent || innerText for elements |
503 | | - if ( typeof elem.textContent === 'string' ) { |
504 | | - return elem.textContent; |
505 | | - } else if ( typeof elem.innerText === 'string' ) { |
506 | | - // Replace IE's carriage returns |
507 | | - return elem.innerText.replace( /\r\n/g, '' ); |
508 | | - } else { |
509 | | - // Traverse it's children |
510 | | - for ( elem = elem.firstChild; elem; elem = elem.nextSibling) { |
511 | | - ret += ve.ce.Surface.getDOMText( elem ); |
512 | | - } |
513 | | - } |
514 | | - } else if ( nodeType === 3 || nodeType === 4 ) { |
515 | | - return elem.nodeValue; |
516 | | - } |
| 537 | + if ( nodeType === 1 || nodeType === 9 ) { |
| 538 | + // Use textContent || innerText for elements |
| 539 | + if ( typeof elem.textContent === 'string' ) { |
| 540 | + return elem.textContent; |
| 541 | + } else if ( typeof elem.innerText === 'string' ) { |
| 542 | + // Replace IE's carriage returns |
| 543 | + return elem.innerText.replace( /\r\n/g, '' ); |
| 544 | + } else { |
| 545 | + // Traverse it's children |
| 546 | + for ( elem = elem.firstChild; elem; elem = elem.nextSibling) { |
| 547 | + ret += ve.ce.Surface.getDOMText( elem ); |
| 548 | + } |
| 549 | + } |
| 550 | + } else if ( nodeType === 3 || nodeType === 4 ) { |
| 551 | + return elem.nodeValue; |
| 552 | + } |
517 | 553 | |
518 | | - return ret; |
| 554 | + return ret; |
519 | 555 | }; |
520 | 556 | |
521 | 557 | ve.ce.Surface.getDOMHash = function( elem ) { |
522 | | - var nodeType = elem.nodeType, |
| 558 | + var nodeType = elem.nodeType, |
523 | 559 | nodeName = elem.nodeName, |
524 | | - ret = ''; |
| 560 | + ret = ''; |
525 | 561 | |
526 | 562 | if ( nodeType === 3 || nodeType === 4 ) { |
527 | 563 | return '#'; |
— | — | @@ -529,8 +565,8 @@ |
530 | 566 | // Traverse it's children |
531 | 567 | for ( elem = elem.firstChild; elem; elem = elem.nextSibling) { |
532 | 568 | ret += ve.ce.Surface.getDOMHash( elem ); |
533 | | - } |
534 | | - ret += '</' + nodeName + '>'; |
| 569 | + } |
| 570 | + ret += '</' + nodeName + '>'; |
535 | 571 | } |
536 | 572 | return ret; |
537 | 573 | }; |