Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.es.Surface.js |
— | — | @@ -34,79 +34,17 @@ |
35 | 35 | return _this.onMouseDown( e ); |
36 | 36 | } ); |
37 | 37 | |
38 | | - this.$.on('cut copy', function( e ) { |
39 | | - _this.onCutCopy( e ); |
| 38 | + this.$.mouseup( function( e ) { |
| 39 | + var selection = _this.getSelection(); |
| 40 | + _this.showFakeCursorAt(selection.start); |
40 | 41 | } ); |
41 | 42 | |
42 | | - this.$.on('paste', function( e ) { |
| 43 | + this.$.paste( function( e ) { |
43 | 44 | _this.onPaste( e ); |
44 | 45 | } ); |
45 | 46 | |
46 | | - this.$.on('m---ouseup', function( e ) { |
47 | | - var offset = _this.getSelection().start; |
48 | | - |
49 | | - var $node = _this.documentView.getNodeFromOffset( offset ).$; |
50 | | - var current = [$node.contents(), 0]; |
51 | | - var stack = [current]; |
52 | | - var node; |
53 | | - var localOffset; |
54 | | - |
55 | | - var index = 1 + _this.documentView.getOffsetFromNode( $node.data('view') ); |
56 | | - |
57 | | - while ( stack.length > 0 ) { |
58 | | - if ( current[1] >= current[0].length ) { |
59 | | - stack.pop(); |
60 | | - current = stack[ stack.length - 1 ]; |
61 | | - continue; |
62 | | - } |
63 | | - var item = current[0][current[1]]; |
64 | | - var $item = current[0].eq( current[1] ); |
65 | | - |
66 | | - if ( item.nodeType === 3 ) { |
67 | | - var length = item.textContent.length; |
68 | | - if ( offset >= index && offset <= index + length ) { |
69 | | - node = item; |
70 | | - localOffset = offset - index; |
71 | | - break; |
72 | | - } else { |
73 | | - index += length; |
74 | | - } |
75 | | - } else if ( item.nodeType === 1 ) { |
76 | | - if ( $( item ).attr('contentEditable') === "false" ) { |
77 | | - index += 1; |
78 | | - } else { |
79 | | - stack.push( [$item.contents(), 0] ); |
80 | | - current[1]++; |
81 | | - current = stack[stack.length-1]; |
82 | | - continue; |
83 | | - } |
84 | | - } |
85 | | - current[1]++; |
86 | | - } |
87 | | - |
88 | | - setTimeout(function() { |
89 | | - var sel = rangy.getSelection(); |
90 | | - var range1 = sel.getRangeAt(0); |
91 | | - var range2 = rangy.createRange(); |
92 | | - |
93 | | - range2.setStart(node, localOffset); |
94 | | - |
95 | | - sel.setSingleRange(range2); |
96 | | - |
97 | | - var position = rangy.getSelection().getStartDocumentPos(); |
98 | | - $('#fake-cursor').css('top', position.y).css('left', position.x); |
99 | | - |
100 | | - sel.setSingleRange(range1); |
101 | | - }, 3000); |
102 | | - |
103 | | - |
104 | | - |
105 | | - |
106 | | - |
107 | | - /* |
108 | | - clearTimeout( _this.cursorTimeout ); |
109 | | - _this.broadcastCursor(); |
110 | | - */ |
| 47 | + this.$.on('cut copy', function( e ) { |
| 48 | + _this.onCutCopy( e ); |
111 | 49 | } ); |
112 | 50 | |
113 | 51 | // Initialization |
— | — | @@ -115,19 +53,10 @@ |
116 | 54 | this.worker = null; |
117 | 55 | this.node = null; |
118 | 56 | |
119 | | - //this.broadcastCursor(); |
120 | 57 | }; |
121 | 58 | |
122 | 59 | /* Methods */ |
123 | 60 | |
124 | | -ve.es.Surface.prototype.broadcastCursor = function() { |
125 | | - var _this = this; |
126 | | - |
127 | | - //console.log( rangy.getSelection().getStartDocumentPos() ); |
128 | | - |
129 | | - _this.cursorTimeout = setTimeout($.proxy(arguments.callee, _this), 5000); |
130 | | -}; |
131 | | - |
132 | 61 | ve.es.Surface.prototype.onCutCopy = function( e ) { |
133 | 62 | var _this = this, |
134 | 63 | key = rangy.getSelection().getRangeAt(0).toString().replace(/( |\r\n|\n|\r|\t)/gm,""); |
— | — | @@ -371,6 +300,66 @@ |
372 | 301 | sel.addRange(range); |
373 | 302 | }; |
374 | 303 | |
| 304 | +ve.es.Surface.prototype.showFakeCursorAt = function( offset ) { |
| 305 | + var _this = this; |
| 306 | + |
| 307 | + var $node = _this.documentView.getNodeFromOffset( offset ).$; |
| 308 | + var current = [$node.contents(), 0]; |
| 309 | + var stack = [current]; |
| 310 | + var node; |
| 311 | + var localOffset; |
| 312 | + |
| 313 | + var index = 1 + _this.documentView.getOffsetFromNode( $node.data('view') ); |
| 314 | + |
| 315 | + while ( stack.length > 0 ) { |
| 316 | + if ( current[1] >= current[0].length ) { |
| 317 | + stack.pop(); |
| 318 | + current = stack[ stack.length - 1 ]; |
| 319 | + continue; |
| 320 | + } |
| 321 | + var item = current[0][current[1]]; |
| 322 | + var $item = current[0].eq( current[1] ); |
| 323 | + |
| 324 | + if ( item.nodeType === 3 ) { |
| 325 | + var length = item.textContent.length; |
| 326 | + if ( offset >= index && offset <= index + length ) { |
| 327 | + node = item; |
| 328 | + localOffset = offset - index; |
| 329 | + break; |
| 330 | + } else { |
| 331 | + index += length; |
| 332 | + } |
| 333 | + } else if ( item.nodeType === 1 ) { |
| 334 | + if ( $( item ).attr('contentEditable') === "false" ) { |
| 335 | + index += 1; |
| 336 | + } else { |
| 337 | + stack.push( [$item.contents(), 0] ); |
| 338 | + current[1]++; |
| 339 | + current = stack[stack.length-1]; |
| 340 | + continue; |
| 341 | + } |
| 342 | + } |
| 343 | + current[1]++; |
| 344 | + } |
| 345 | + |
| 346 | + // Delay by 3 seconds - just for demo |
| 347 | + setTimeout(function() { |
| 348 | + var sel = rangy.getSelection(); |
| 349 | + var range1 = sel.getRangeAt(0); |
| 350 | + var range2 = rangy.createRange(); |
| 351 | + |
| 352 | + range2.setStart(node, localOffset); |
| 353 | + |
| 354 | + sel.setSingleRange(range2); |
| 355 | + |
| 356 | + var position = rangy.getSelection().getStartDocumentPos(); |
| 357 | + $('#fake-cursor').css('top', position.y).css('left', position.x); |
| 358 | + |
| 359 | + sel.setSingleRange(range1); |
| 360 | + }, 3000); |
| 361 | +} |
| 362 | + |
| 363 | + |
375 | 364 | /* Inheritance */ |
376 | 365 | |
377 | 366 | ve.extendClass( ve.es.Surface, ve.EventEmitter ); |