Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.ContentObserver.js |
— | — | @@ -1,59 +1,59 @@ |
2 | | -ve.ce.ContentObserver = function( documentView ) {
|
3 | | - // Inheritance
|
4 | | - ve.EventEmitter.call( this );
|
5 | | -
|
6 | | - this.$node = null;
|
7 | | - this.interval = null;
|
8 | | - this.frequency = 100;
|
9 | | - this.prevText = null;
|
10 | | - this.prevHash = null;
|
11 | | -};
|
12 | | -
|
13 | | -ve.ce.ContentObserver.prototype.setElement = function( $node ) {
|
14 | | - if ( this.$node !== $node ) {
|
15 | | - this.stop();
|
16 | | -
|
17 | | - this.$node = $node;
|
18 | | - this.prevText = ve.ce.Surface.getDOMText2( this.$node[0] );
|
19 | | - this.prevHash = ve.ce.Surface.getDOMHash( this.$node[0] );
|
20 | | -
|
21 | | - this.start();
|
22 | | - }
|
23 | | -};
|
24 | | -
|
25 | | -ve.ce.ContentObserver.prototype.stop = function() {
|
26 | | - if ( this.interval !== null ) {
|
27 | | - clearInterval( this.interval );
|
28 | | - this.interval = null;
|
29 | | - this.poll();
|
30 | | - this.$node = null;
|
31 | | - }
|
32 | | -};
|
33 | | -
|
34 | | -ve.ce.ContentObserver.prototype.start = function() {
|
35 | | - this.poll();
|
36 | | - var _this = this;
|
37 | | - setTimeout( function() { _this.poll(); }, 0);
|
38 | | - this.interval = setInterval( function() { _this.poll(); }, this.frequency );
|
39 | | -};
|
40 | | -
|
41 | | -ve.ce.ContentObserver.prototype.poll = function() {
|
42 | | - var text = ve.ce.Surface.getDOMText2( this.$node[0] );
|
43 | | - var hash = ve.ce.Surface.getDOMHash( this.$node[0] );
|
44 | | -
|
45 | | - if ( text !== this.prevText || hash !== this.prevHash ) {
|
46 | | - this.emit('change', {
|
47 | | - $node: this.$node,
|
48 | | - prevText: this.prevText,
|
49 | | - text: text,
|
50 | | - prevHash: this.prevHash,
|
51 | | - hash: hash
|
52 | | - } );
|
53 | | - this.prevText = text;
|
54 | | - this.prevHash = hash;
|
55 | | - }
|
56 | | -};
|
57 | | -
|
58 | | -/* Inheritance */
|
59 | | -
|
| 2 | +ve.ce.ContentObserver = function( documentView ) { |
| 3 | + // Inheritance |
| 4 | + ve.EventEmitter.call( this ); |
| 5 | + |
| 6 | + this.$node = null; |
| 7 | + this.interval = null; |
| 8 | + this.frequency = 100; |
| 9 | + this.prevText = null; |
| 10 | + this.prevHash = null; |
| 11 | +}; |
| 12 | + |
| 13 | +ve.ce.ContentObserver.prototype.setElement = function( $node ) { |
| 14 | + if ( this.$node !== $node ) { |
| 15 | + this.stop(); |
| 16 | + |
| 17 | + this.$node = $node; |
| 18 | + this.prevText = ve.ce.Surface.getDOMText2( this.$node[0] ); |
| 19 | + this.prevHash = ve.ce.Surface.getDOMHash( this.$node[0] ); |
| 20 | + |
| 21 | + this.start(); |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +ve.ce.ContentObserver.prototype.stop = function() { |
| 26 | + if ( this.interval !== null ) { |
| 27 | + clearInterval( this.interval ); |
| 28 | + this.interval = null; |
| 29 | + this.poll(); |
| 30 | + this.$node = null; |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +ve.ce.ContentObserver.prototype.start = function() { |
| 35 | + this.poll(); |
| 36 | + var _this = this; |
| 37 | + setTimeout( function() { _this.poll(); }, 0); |
| 38 | + this.interval = setInterval( function() { _this.poll(); }, this.frequency ); |
| 39 | +}; |
| 40 | + |
| 41 | +ve.ce.ContentObserver.prototype.poll = function() { |
| 42 | + var text = ve.ce.Surface.getDOMText2( this.$node[0] ); |
| 43 | + var hash = ve.ce.Surface.getDOMHash( this.$node[0] ); |
| 44 | + |
| 45 | + if ( text !== this.prevText || hash !== this.prevHash ) { |
| 46 | + this.emit('change', { |
| 47 | + $node: this.$node, |
| 48 | + prevText: this.prevText, |
| 49 | + text: text, |
| 50 | + prevHash: this.prevHash, |
| 51 | + hash: hash |
| 52 | + } ); |
| 53 | + this.prevText = text; |
| 54 | + this.prevHash = hash; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +/* Inheritance */ |
| 59 | + |
60 | 60 | ve.extendClass( ve.ce.ContentObserver , ve.EventEmitter ); |
\ No newline at end of file |
Property changes on: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.ContentObserver.js |
___________________________________________________________________ |
Added: svn:eol-style |
61 | 61 | + native |
Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.CursorObserver.js |
— | — | @@ -1,98 +1,98 @@ |
2 | | -ve.ce.CursorObserver = function( documentView ) {
|
3 | | - // Inheritance
|
4 | | - ve.EventEmitter.call( this );
|
5 | | -
|
6 | | - this.documentView = documentView;
|
7 | | - this.anchorNode = null;
|
8 | | - this.anchorOffset = null;
|
9 | | - this.focusNode = null;
|
10 | | - this.focusOffset = null;
|
11 | | -};
|
12 | | -
|
13 | | -ve.ce.CursorObserver.prototype.update = function() {
|
14 | | - var _this = this;
|
15 | | -
|
16 | | - setTimeout( function() {
|
17 | | - if ( !_this.documentView.$.is(':focus') ) {
|
18 | | - if (
|
19 | | - _this.anchorNode !== null ||
|
20 | | - _this.anchorOffset !== null ||
|
21 | | - _this.focusNode !== null ||
|
22 | | - _this.focusOffset !== null
|
23 | | - ) {
|
24 | | - _this.anchorNode = _this.anchorOffset = _this.focusNode = _this.focusOffset = null;
|
25 | | - _this.emit( 'change', null );
|
26 | | - }
|
27 | | - } else {
|
28 | | - var rangySel = rangy.getSelection(),
|
29 | | - range;
|
30 | | - if ( rangySel.anchorNode !== _this.anchorNode ||
|
31 | | - rangySel.anchorOffset !== _this.anchorOffset ||
|
32 | | - rangySel.focusNode !== _this.focusNode ||
|
33 | | - rangySel.focusOffset !== _this.focusOffset
|
34 | | - ) {
|
35 | | - _this.anchorNode = rangySel.anchorNode;
|
36 | | - _this.anchorOffset = rangySel.anchorOffset;
|
37 | | - _this.focusNode = rangySel.focusNode;
|
38 | | - _this.focusOffset = rangySel.focusOffset;
|
39 | | -
|
40 | | - if ( rangySel.isCollapsed ) {
|
41 | | - range = new ve.Range( _this.getOffset( _this.anchorNode, _this.anchorOffset ) );
|
42 | | - } else {
|
43 | | - range = new ve.Range(
|
44 | | - _this.getOffset( _this.anchorNode, _this.anchorOffset ),
|
45 | | - _this.getOffset( _this.focusNode, _this.focusOffset )
|
46 | | - );
|
47 | | - }
|
48 | | - _this.emit( 'change', range );
|
49 | | - }
|
50 | | - }
|
51 | | - }, 0 );
|
52 | | -};
|
53 | | -
|
54 | | -ve.ce.CursorObserver.prototype.getOffset = function( selectionNode, selectionOffset ) {
|
55 | | - var $leafNode = ve.ce.Surface.getLeafNode( selectionNode ),
|
56 | | - current = [$leafNode.contents(), 0],
|
57 | | - stack = [current],
|
58 | | - offset = 0;
|
59 | | -
|
60 | | - while ( stack.length > 0 ) {
|
61 | | - if ( current[1] >= current[0].length ) {
|
62 | | - stack.pop();
|
63 | | - current = stack[ stack.length - 1 ];
|
64 | | - continue;
|
65 | | - }
|
66 | | - var item = current[0][current[1]];
|
67 | | - var $item = current[0].eq( current[1] );
|
68 | | -
|
69 | | - if ( item.nodeType === 3 ) {
|
70 | | - if ( item === selectionNode ) {
|
71 | | - offset += selectionOffset;
|
72 | | - break;
|
73 | | - } else {
|
74 | | - offset += item.textContent.length;
|
75 | | - }
|
76 | | - } else if ( item.nodeType === 1 ) {
|
77 | | - if ( $( item ).attr( 'contentEditable' ) === 'false' ) {
|
78 | | - offset += 1;
|
79 | | - } else {
|
80 | | - if ( item === selectionNode ) {
|
81 | | - offset += selectionOffset;
|
82 | | - break;
|
83 | | - }
|
84 | | - stack.push( [$item.contents(), 0] );
|
85 | | - current[1]++;
|
86 | | - current = stack[stack.length-1];
|
87 | | - continue;
|
88 | | - }
|
89 | | - }
|
90 | | - current[1]++;
|
91 | | - }
|
92 | | - return this.documentView.getOffsetFromNode(
|
93 | | - $leafNode.data( 'view' )
|
94 | | - ) + 1 + offset;
|
95 | | -};
|
96 | | -
|
97 | | -/* Inheritance */
|
98 | | -
|
99 | | -ve.extendClass( ve.ce.CursorObserver , ve.EventEmitter );
|
| 2 | +ve.ce.CursorObserver = function( documentView ) { |
| 3 | + // Inheritance |
| 4 | + ve.EventEmitter.call( this ); |
| 5 | + |
| 6 | + this.documentView = documentView; |
| 7 | + this.anchorNode = null; |
| 8 | + this.anchorOffset = null; |
| 9 | + this.focusNode = null; |
| 10 | + this.focusOffset = null; |
| 11 | +}; |
| 12 | + |
| 13 | +ve.ce.CursorObserver.prototype.update = function() { |
| 14 | + var _this = this; |
| 15 | + |
| 16 | + setTimeout( function() { |
| 17 | + if ( !_this.documentView.$.is(':focus') ) { |
| 18 | + if ( |
| 19 | + _this.anchorNode !== null || |
| 20 | + _this.anchorOffset !== null || |
| 21 | + _this.focusNode !== null || |
| 22 | + _this.focusOffset !== null |
| 23 | + ) { |
| 24 | + _this.anchorNode = _this.anchorOffset = _this.focusNode = _this.focusOffset = null; |
| 25 | + _this.emit( 'change', null ); |
| 26 | + } |
| 27 | + } else { |
| 28 | + var rangySel = rangy.getSelection(), |
| 29 | + range; |
| 30 | + if ( rangySel.anchorNode !== _this.anchorNode || |
| 31 | + rangySel.anchorOffset !== _this.anchorOffset || |
| 32 | + rangySel.focusNode !== _this.focusNode || |
| 33 | + rangySel.focusOffset !== _this.focusOffset |
| 34 | + ) { |
| 35 | + _this.anchorNode = rangySel.anchorNode; |
| 36 | + _this.anchorOffset = rangySel.anchorOffset; |
| 37 | + _this.focusNode = rangySel.focusNode; |
| 38 | + _this.focusOffset = rangySel.focusOffset; |
| 39 | + |
| 40 | + if ( rangySel.isCollapsed ) { |
| 41 | + range = new ve.Range( _this.getOffset( _this.anchorNode, _this.anchorOffset ) ); |
| 42 | + } else { |
| 43 | + range = new ve.Range( |
| 44 | + _this.getOffset( _this.anchorNode, _this.anchorOffset ), |
| 45 | + _this.getOffset( _this.focusNode, _this.focusOffset ) |
| 46 | + ); |
| 47 | + } |
| 48 | + _this.emit( 'change', range ); |
| 49 | + } |
| 50 | + } |
| 51 | + }, 0 ); |
| 52 | +}; |
| 53 | + |
| 54 | +ve.ce.CursorObserver.prototype.getOffset = function( selectionNode, selectionOffset ) { |
| 55 | + var $leafNode = ve.ce.Surface.getLeafNode( selectionNode ), |
| 56 | + current = [$leafNode.contents(), 0], |
| 57 | + stack = [current], |
| 58 | + offset = 0; |
| 59 | + |
| 60 | + while ( stack.length > 0 ) { |
| 61 | + if ( current[1] >= current[0].length ) { |
| 62 | + stack.pop(); |
| 63 | + current = stack[ stack.length - 1 ]; |
| 64 | + continue; |
| 65 | + } |
| 66 | + var item = current[0][current[1]]; |
| 67 | + var $item = current[0].eq( current[1] ); |
| 68 | + |
| 69 | + if ( item.nodeType === 3 ) { |
| 70 | + if ( item === selectionNode ) { |
| 71 | + offset += selectionOffset; |
| 72 | + break; |
| 73 | + } else { |
| 74 | + offset += item.textContent.length; |
| 75 | + } |
| 76 | + } else if ( item.nodeType === 1 ) { |
| 77 | + if ( $( item ).attr( 'contentEditable' ) === 'false' ) { |
| 78 | + offset += 1; |
| 79 | + } else { |
| 80 | + if ( item === selectionNode ) { |
| 81 | + offset += selectionOffset; |
| 82 | + break; |
| 83 | + } |
| 84 | + stack.push( [$item.contents(), 0] ); |
| 85 | + current[1]++; |
| 86 | + current = stack[stack.length-1]; |
| 87 | + continue; |
| 88 | + } |
| 89 | + } |
| 90 | + current[1]++; |
| 91 | + } |
| 92 | + return this.documentView.getOffsetFromNode( |
| 93 | + $leafNode.data( 'view' ) |
| 94 | + ) + 1 + offset; |
| 95 | +}; |
| 96 | + |
| 97 | +/* Inheritance */ |
| 98 | + |
| 99 | +ve.extendClass( ve.ce.CursorObserver , ve.EventEmitter ); |
Property changes on: trunk/extensions/VisualEditor/modules/ve/ce/ve.ce.CursorObserver.js |
___________________________________________________________________ |
Added: svn:eol-style |
100 | 100 | + native |