Index: trunk/extensions/VisualEditor/demos/ce/index.php |
— | — | @@ -158,5 +158,6 @@ |
159 | 159 | <script src="main.js"></script> |
160 | 160 | |
161 | 161 | <div id="paste" contenteditable="true" style="height: 1px; width: 1px; display: none; opacity: 0; position: absolute;"></div> |
| 162 | + <div id="fake-cursor" style="height: 20px; width: 1px; background: red; position: absolute; top: 0; left: 0;"></div> |
162 | 163 | </body> |
163 | 164 | </html> |
Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.es.Surface.js |
— | — | @@ -42,16 +42,98 @@ |
43 | 43 | _this.onPaste( e ); |
44 | 44 | } ); |
45 | 45 | |
| 46 | + this.$.on('mouseup', function( e ) { |
| 47 | + var offset = _this.getSelection().start; |
46 | 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 | + console.log(index); |
| 58 | + |
| 59 | + while ( stack.length > 0 ) { |
| 60 | + if ( current[1] >= current[0].length ) { |
| 61 | + stack.pop(); |
| 62 | + current = stack[ stack.length - 1 ]; |
| 63 | + continue; |
| 64 | + } |
| 65 | + var item = current[0][current[1]]; |
| 66 | + var $item = current[0].eq( current[1] ); |
| 67 | + |
| 68 | + if ( item.nodeType === 3 ) { |
| 69 | + var length = item.textContent.length; |
| 70 | + if ( offset >= index && offset <= index + length ) { |
| 71 | + node = item; |
| 72 | + localOffset = offset - index; |
| 73 | + break; |
| 74 | + } else { |
| 75 | + index += length; |
| 76 | + } |
| 77 | + } else if ( item.nodeType === 1 ) { |
| 78 | + if ( $( item ).attr('contentEditable') === "false" ) { |
| 79 | + index += 1; |
| 80 | + } else { |
| 81 | + stack.push( [$item.contents(), 0] ); |
| 82 | + current[1]++; |
| 83 | + current = stack[stack.length-1]; |
| 84 | + continue; |
| 85 | + } |
| 86 | + } |
| 87 | + current[1]++; |
| 88 | + } |
| 89 | + |
| 90 | + console.log(node); |
| 91 | + console.log(localOffset); |
| 92 | + |
| 93 | + |
| 94 | + setTimeout(function() { |
| 95 | + var sel = rangy.getSelection(); |
| 96 | + var range1 = sel.getRangeAt(0); |
| 97 | + var range2 = rangy.createRange(); |
| 98 | + |
| 99 | + range2.setStart(node, localOffset); |
| 100 | + |
| 101 | + sel.setSingleRange(range2); |
| 102 | + |
| 103 | + var position = rangy.getSelection().getStartDocumentPos(); |
| 104 | + $('#fake-cursor').css('top', position.y).css('left', position.x); |
| 105 | + |
| 106 | + sel.setSingleRange(range1); |
| 107 | + }, 3000); |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + /* |
| 114 | + clearTimeout( _this.cursorTimeout ); |
| 115 | + _this.broadcastCursor(); |
| 116 | + */ |
| 117 | + } ); |
| 118 | + |
47 | 119 | // Initialization |
48 | 120 | this.documentView.renderContent(); |
49 | 121 | |
50 | 122 | this.worker = null; |
51 | 123 | this.node = null; |
| 124 | + |
| 125 | + //this.broadcastCursor(); |
52 | 126 | }; |
53 | 127 | |
54 | 128 | /* Methods */ |
55 | 129 | |
| 130 | +ve.es.Surface.prototype.broadcastCursor = function() { |
| 131 | + var _this = this; |
| 132 | + |
| 133 | + //console.log( rangy.getSelection().getStartDocumentPos() ); |
| 134 | + |
| 135 | + _this.cursorTimeout = setTimeout($.proxy(arguments.callee, _this), 5000); |
| 136 | +}; |
| 137 | + |
56 | 138 | ve.es.Surface.prototype.onCutCopy = function( e ) { |
57 | 139 | var _this = this, |
58 | 140 | key = rangy.getSelection().getRangeAt(0).toString().replace(/( |\r\n|\n|\r|\t)/gm,""); |
— | — | @@ -213,8 +295,6 @@ |
214 | 296 | if ( $( item ).attr('contentEditable') === "false" ) { |
215 | 297 | offset += 1; |
216 | 298 | } else { |
217 | | - console.log(item); |
218 | | - console.log(localNode); |
219 | 299 | if ( item === localNode ) { |
220 | 300 | offset += localOffset; |
221 | 301 | break; |