Index: trunk/extensions/VisualEditor/demos/ce/index.php |
— | — | @@ -125,8 +125,7 @@ |
126 | 126 | <script src="../../modules/ve/ce/ve.ce.LeafNode.js"></script> |
127 | 127 | <script src="../../modules/ve/ce/ve.ce.Content.js"></script> |
128 | 128 | <script src="../../modules/ve/ce/ve.ce.Surface.js"></script> |
129 | | - <script src="../../modules/ve/ce/ve.ce.CursorObserver.js"></script> |
130 | | - <script src="../../modules/ve/ce/ve.ce.ContentObserver.js"></script> |
| 129 | + <script src="../../modules/ve/ce/ve.ce.SurfaceObserver.js"></script> |
131 | 130 | |
132 | 131 | <script src="../../modules/ve/ce/nodes/ve.ce.DocumentNode.js"></script> |
133 | 132 | <script src="../../modules/ve/ce/nodes/ve.ce.HeadingNode.js"></script> |
Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.Surface.js |
— | — | @@ -28,18 +28,30 @@ |
29 | 29 | this.clipboard = {}; |
30 | 30 | this.autoRender = false; |
31 | 31 | |
| 32 | + // Content Observer |
| 33 | + this.surfaceObserver = new ve.ce.SurfaceObserver( this.documentView ); |
| 34 | + this.surfaceObserver.on( 'cursor', function( info ) { |
| 35 | + //console.log("cursor", info); |
| 36 | + } ) |
| 37 | + |
32 | 38 | // Events |
33 | 39 | this.documentView.$.bind( { |
34 | 40 | 'focus': function( e ) { |
| 41 | + _this.surfaceObserver.updateCursor( true ); |
35 | 42 | _this.documentOnFocus(); |
36 | 43 | $document.unbind( '.ce-surfaceView' ); |
37 | 44 | $document.bind( { |
38 | 45 | 'keydown.ce-surfaceView': function( e ) { |
| 46 | + _this.surfaceObserver.updateCursor( true ); |
39 | 47 | return _this.onKeyDown( e ); |
| 48 | + }, |
| 49 | + 'mousemove.ce-surfaceView': function( e ) { |
| 50 | + _this.surfaceObserver.updateCursor( true ); |
40 | 51 | } |
41 | 52 | } ); |
42 | 53 | }, |
43 | 54 | 'blur': function( e ) { |
| 55 | + _this.surfaceObserver.updateCursor( true ); |
44 | 56 | _this.documentOnBlur(); |
45 | 57 | $document.unbind( '.ce-surfaceView' ); |
46 | 58 | } |
— | — | @@ -53,7 +65,8 @@ |
54 | 66 | _this.onPaste( e ); |
55 | 67 | } ) |
56 | 68 | .on( 'mousedown', function( e ) { |
57 | | - return _this.onMouseDown( e ); |
| 69 | + _this.surfaceObserver.updateCursor( true ); |
| 70 | + return _this.onMouseDown( e ); |
58 | 71 | } ) |
59 | 72 | .on( 'compositionstart', function( e ) { |
60 | 73 | console.log('comp start'); |
— | — | @@ -324,6 +337,7 @@ |
325 | 338 | }; |
326 | 339 | |
327 | 340 | ve.ce.Surface.prototype.pollContent = function() { |
| 341 | + return; |
328 | 342 | var localOffset, text, hash; |
329 | 343 | |
330 | 344 | if ( this.poll.compositionStart !== null && this.poll.compositionEnd !== null ) { |
— | — | @@ -634,6 +648,10 @@ |
635 | 649 | }; |
636 | 650 | |
637 | 651 | ve.ce.Surface.prototype.getLeafNode = function( elem ) { |
| 652 | + return ve.ce.Surface.getLeafNode( elem ); |
| 653 | +}; |
| 654 | + |
| 655 | +ve.ce.Surface.getLeafNode = function( elem ) { |
638 | 656 | var $node = $( elem ); |
639 | 657 | while( !$node.hasClass( 'ce-leafNode' ) ) { |
640 | 658 | $node = $node.parent(); |
Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.SurfaceObserver.js |
— | — | @@ -0,0 +1,166 @@ |
| 2 | +ve.ce.SurfaceObserver = function( documentView ) { |
| 3 | + // Inheritance |
| 4 | + ve.EventEmitter.call( this ); |
| 5 | + |
| 6 | + this.documentView = documentView; |
| 7 | + |
| 8 | + this.anchorNode = null; |
| 9 | + this.anchorOffset = null; |
| 10 | + this.focusNode = null; |
| 11 | + this.focusOffset = null; |
| 12 | + this.range = null; |
| 13 | + |
| 14 | + this.$node = null; |
| 15 | + this.interval = null; |
| 16 | + this.frequency = 100; |
| 17 | + this.prevText = null; |
| 18 | + this.prevHash = null; |
| 19 | + this.prevRange = null; |
| 20 | + |
| 21 | + var _this = this; |
| 22 | + |
| 23 | + this.on( 'select', function( range ) { |
| 24 | + if ( range !== null && range.getLength() === 0 ) { |
| 25 | + var node = _this.documentView.getNodeFromOffset( range.start ); |
| 26 | + _this.setNode( node.$ ); |
| 27 | + } else { |
| 28 | + _this.stop(); |
| 29 | + } |
| 30 | + } ); |
| 31 | +}; |
| 32 | + |
| 33 | +ve.ce.SurfaceObserver.prototype.setNode = function( $node ) { |
| 34 | + if ( this.$node !== $node ) { |
| 35 | + this.stop(); |
| 36 | + |
| 37 | + this.$node = $node; |
| 38 | + this.prevText = ve.ce.Surface.getDOMText2( this.$node[0] ); |
| 39 | + this.prevHash = ve.ce.Surface.getDOMHash( this.$node[0] ); |
| 40 | + |
| 41 | + this.start(); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +ve.ce.SurfaceObserver.prototype.stop = function() { |
| 46 | + if ( this.interval !== null ) { |
| 47 | + clearInterval( this.interval ); |
| 48 | + this.interval = null; |
| 49 | + this.poll(); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +ve.ce.SurfaceObserver.prototype.start = function() { |
| 54 | + this.poll(); |
| 55 | + var _this = this; |
| 56 | + setTimeout( function() {_this.poll(); }, 0); |
| 57 | + this.interval = setInterval( function() { _this.poll(); }, this.frequency ); |
| 58 | +}; |
| 59 | + |
| 60 | +ve.ce.SurfaceObserver.prototype.poll = function() { |
| 61 | + var text = ve.ce.Surface.getDOMText2( this.$node[0] ); |
| 62 | + var hash = ve.ce.Surface.getDOMHash( this.$node[0] ); |
| 63 | + |
| 64 | + if ( text !== this.prevText || hash !== this.prevHash ) { |
| 65 | + console.log(1); |
| 66 | + this.emit('change', { |
| 67 | + $node: this.$node, |
| 68 | + prevText: this.prevText, |
| 69 | + text: text, |
| 70 | + prevHash: this.prevHash, |
| 71 | + hash: hash |
| 72 | + } ); |
| 73 | + this.prevText = text; |
| 74 | + this.prevHash = hash; |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +ve.ce.SurfaceObserver.prototype.updateCursor = function( async ) { |
| 79 | + if ( async ) { |
| 80 | + var _this = this; |
| 81 | + setTimeout( function() { |
| 82 | + _this.updateCursor ( false ); |
| 83 | + }, 0 ); |
| 84 | + } else { |
| 85 | + if ( !this.documentView.$.is(':focus') ) { |
| 86 | + if ( |
| 87 | + this.anchorNode !== null || |
| 88 | + this.anchorOffset !== null || |
| 89 | + this.focusNode !== null || |
| 90 | + this.focusOffset !== null |
| 91 | + ) { |
| 92 | + this.anchorNode = this.anchorOffset = this.focusNode = this.focusOffset = null; |
| 93 | + this.range = null; |
| 94 | + this.emit( 'select', this.range ); |
| 95 | + } |
| 96 | + } else { |
| 97 | + var rangySel = rangy.getSelection(); |
| 98 | + if ( |
| 99 | + rangySel.anchorNode !== this.anchorNode || |
| 100 | + rangySel.anchorOffset !== this.anchorOffset || |
| 101 | + rangySel.focusNode !== this.focusNode || |
| 102 | + rangySel.focusOffset !== this.focusOffset |
| 103 | + ) { |
| 104 | + this.anchorNode = rangySel.anchorNode; |
| 105 | + this.anchorOffset = rangySel.anchorOffset; |
| 106 | + this.focusNode = rangySel.focusNode; |
| 107 | + this.focusOffset = rangySel.focusOffset; |
| 108 | + if ( rangySel.isCollapsed ) { |
| 109 | + this.range = new ve.Range( this.getOffset( this.anchorNode, this.anchorOffset ) ); |
| 110 | + } else { |
| 111 | + this.range = new ve.Range( |
| 112 | + this.getOffset( this.anchorNode, this.anchorOffset ), |
| 113 | + this.getOffset( this.focusNode, this.focusOffset ) |
| 114 | + ); |
| 115 | + } |
| 116 | + this.emit( 'select', this.range ); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +ve.ce.SurfaceObserver.prototype.getOffset = function( selectionNode, selectionOffset ) { |
| 123 | + var $leafNode = ve.ce.Surface.getLeafNode( selectionNode ), |
| 124 | + current = [$leafNode.contents(), 0], |
| 125 | + stack = [current], |
| 126 | + offset = 0; |
| 127 | + |
| 128 | + while ( stack.length > 0 ) { |
| 129 | + if ( current[1] >= current[0].length ) { |
| 130 | + stack.pop(); |
| 131 | + current = stack[ stack.length - 1 ]; |
| 132 | + continue; |
| 133 | + } |
| 134 | + var item = current[0][current[1]]; |
| 135 | + var $item = current[0].eq( current[1] ); |
| 136 | + |
| 137 | + if ( item.nodeType === 3 ) { |
| 138 | + if ( item === selectionNode ) { |
| 139 | + offset += selectionOffset; |
| 140 | + break; |
| 141 | + } else { |
| 142 | + offset += item.textContent.length; |
| 143 | + } |
| 144 | + } else if ( item.nodeType === 1 ) { |
| 145 | + if ( $( item ).attr( 'contentEditable' ) === 'false' ) { |
| 146 | + offset += 1; |
| 147 | + } else { |
| 148 | + if ( item === selectionNode ) { |
| 149 | + offset += selectionOffset; |
| 150 | + break; |
| 151 | + } |
| 152 | + stack.push( [$item.contents(), 0] ); |
| 153 | + current[1]++; |
| 154 | + current = stack[stack.length-1]; |
| 155 | + continue; |
| 156 | + } |
| 157 | + } |
| 158 | + current[1]++; |
| 159 | + } |
| 160 | + return this.documentView.getOffsetFromNode( |
| 161 | + $leafNode.data( 'view' ) |
| 162 | + ) + 1 + offset; |
| 163 | +}; |
| 164 | + |
| 165 | +/* Inheritance */ |
| 166 | + |
| 167 | +ve.extendClass( ve.ce.SurfaceObserver , ve.EventEmitter ); |
\ No newline at end of file |