@@ -11,78 +11,192 @@ editor.setHighlightSelectedWord(true);
11
11
editor . setDisplayIndentGuides ( true ) ;
12
12
editor . renderer . setShowPrintMargin ( false ) ;
13
13
14
+ var pointerMapping = { } ;
15
+ var editorScrollTop = 0 ;
16
+ var editorScrollLeft = 0 ;
17
+
14
18
var isSilent = false ;
15
19
16
- connection . on ( "ReceiveContent" , function ( content , row , pos ) {
17
- isSilent = true ;
18
- editor . getSession ( ) . setValue ( content ) ;
19
- editor . selection . moveCursorTo ( row , pos ) ;
20
- isSilent = false ;
20
+ function startWithDelay ( code , ms , conditionTrigger )
21
+ {
22
+ var timer = null ;
23
+
24
+ var run = function ( )
25
+ {
26
+ var args = arguments ;
27
+ var pThis = this ;
28
+
29
+ if ( timer )
30
+ {
31
+ clearTimeout ( timer ) ;
32
+ }
33
+
34
+ timer = setTimeout (
35
+ function ( )
36
+ {
37
+ if ( conditionTrigger && ! conditionTrigger ( ) )
38
+ {
39
+ run . apply ( pThis , args ) ;
40
+ return ;
41
+ }
42
+
43
+ code . apply ( pThis , args ) ;
44
+ } ,
45
+ ms ) ;
46
+ }
47
+
48
+ return run ;
49
+ }
50
+
51
+ function computeHash ( str )
52
+ {
53
+ var hash = 0 ;
54
+ for ( var i = 0 ; i < str . length ; i ++ )
55
+ {
56
+ var char = str . charCodeAt ( i ) ;
57
+ hash += char ;
58
+ }
59
+ return hash ;
60
+ }
61
+
62
+ connection . on ( "ReceiveContent" , function ( content , row , pos )
63
+ {
64
+ isSilent = true ;
65
+ editor . getSession ( ) . setValue ( content ) ;
66
+ editor . selection . moveCursorTo ( row , pos ) ;
67
+ isSilent = false ;
21
68
} ) ;
22
69
23
- connection . on ( "ReceiveCursor" , function ( row , pos ) {
24
- isSilent = true ;
25
- editor . selection . moveCursorTo ( row , pos ) ;
26
- isSilent = false ;
70
+ connection . on ( "ReceiveCursor" , function ( row , pos )
71
+ {
72
+ isSilent = true ;
73
+ editor . selection . moveCursorTo ( row , pos ) ;
74
+ isSilent = false ;
27
75
} ) ;
28
76
29
- connection . on ( "ReceiveSelection" , function ( srow , spos , erow , epos ) {
30
- isSilent = true ;
31
- editor . selection . setSelectionAnchor ( srow , spos ) ;
32
- editor . selection . moveCursorTo ( erow , epos ) ;
33
- isSilent = false ;
77
+ connection . on ( "ReceiveSelection" , function ( srow , spos , erow , epos )
78
+ {
79
+ isSilent = true ;
80
+ editor . selection . setSelectionAnchor ( srow , spos ) ;
81
+ editor . selection . moveCursorTo ( erow , epos ) ;
82
+ isSilent = false ;
34
83
} ) ;
35
84
36
- connection . start ( ) . then ( function ( )
85
+ connection . on ( "Connected" , function ( connectionId )
37
86
{
38
- connection . invoke ( "Join" , SESSION_ID ) . catch ( function ( err ) {
39
- return console . error ( err . toString ( ) ) ;
40
- } ) ;
41
-
42
- editor . getSession ( ) . on ( 'change' , function ( ) {
43
-
44
- if ( isSilent ) {
45
- return ;
46
- }
47
-
48
- var cursor = editor . selection . getCursor ( ) ;
49
-
50
- connection . invoke ( "UpdateContent" , editor . getSession ( ) . getValue ( ) , cursor . row , cursor . column ) . catch ( function ( err ) {
51
- return console . error ( err . toString ( ) ) ;
52
- } ) ;
53
-
54
- } ) ;
55
-
56
- editor . selection . on ( 'changeCursor' , function ( ) {
87
+ isSilent = true ;
57
88
58
- if ( isSilent ) {
59
- return ;
60
- }
61
89
62
- var cursor = editor . selection . getCursor ( ) ;
63
-
64
- connection . invoke ( "UpdateCursor" , cursor . row , cursor . column ) . catch ( function ( err ) {
65
- return console . error ( err . toString ( ) ) ;
66
- } ) ;
67
-
68
- } ) ;
69
-
70
- editor . selection . on ( 'changeSelection' , function ( ) {
71
-
72
- if ( isSilent ) {
73
- return ;
74
- }
75
-
76
- var range = editor . selection . getRange ( ) ;
90
+ isSilent = false ;
91
+ } ) ;
77
92
78
- connection . invoke ( "UpdateSelection" , range . start . row , range . start . column , range . end . row , range . end . column ) . catch ( function ( err ) {
79
- return console . error ( err . toString ( ) ) ;
80
- } ) ;
93
+ connection . on ( "Disconnected" , function ( connectionId )
94
+ {
95
+ isSilent = true ;
96
+ pointerMapping [ connectionId ] . style . display = 'none' ;
97
+ isSilent = false ;
98
+ } ) ;
81
99
82
- } ) ;
100
+ connection . on ( "ReceivePointer" , function ( connectionId , x , y )
101
+ {
102
+ isSilent = true ;
103
+
104
+ if ( ! pointerMapping [ connectionId ] )
105
+ {
106
+ var pointer = pointerMapping [ connectionId ] = document . createElement ( "DIV" ) ;
107
+ pointer . className = 'pointer' ;
108
+ pointer . style . filter = 'hue-rotate(' + ( computeHash ( connectionId ) % 360 ) + 'deg)' ;
109
+ document . body . appendChild ( pointer ) ;
110
+ }
111
+
112
+ pointerMapping [ connectionId ] . style . left = x + 'px' ;
113
+ pointerMapping [ connectionId ] . style . top = y + 'px' ;
114
+ isSilent = false ;
115
+ } ) ;
83
116
117
+ connection . start ( ) . then ( function ( )
118
+ {
119
+ connection . invoke ( "Join" , SESSION_ID ) . catch ( function ( err )
120
+ {
121
+ return console . error ( err . toString ( ) ) ;
122
+ } ) ;
123
+
124
+ editor . getSession ( ) . on ( 'change' , function ( )
125
+ {
126
+
127
+ if ( isSilent )
128
+ {
129
+ return ;
130
+ }
131
+
132
+ var cursor = editor . selection . getCursor ( ) ;
133
+
134
+ connection . invoke ( "UpdateContent" , editor . getSession ( ) . getValue ( ) , cursor . row , cursor . column ) . catch ( function ( err )
135
+ {
136
+ return console . error ( err . toString ( ) ) ;
137
+ } ) ;
138
+
139
+ } ) ;
140
+
141
+ editor . selection . on ( 'changeCursor' , function ( )
142
+ {
143
+
144
+ if ( isSilent )
145
+ {
146
+ return ;
147
+ }
148
+
149
+ var cursor = editor . selection . getCursor ( ) ;
150
+
151
+ connection . invoke ( "UpdateCursor" , cursor . row , cursor . column ) . catch ( function ( err )
152
+ {
153
+ return console . error ( err . toString ( ) ) ;
154
+ } ) ;
155
+
156
+ } ) ;
157
+
158
+ editor . selection . on ( 'changeSelection' , function ( )
159
+ {
160
+
161
+ if ( isSilent )
162
+ {
163
+ return ;
164
+ }
165
+
166
+ var range = editor . selection . getRange ( ) ;
167
+
168
+ connection . invoke ( "UpdateSelection" , range . start . row , range . start . column , range . end . row , range . end . column ) . catch ( function ( err )
169
+ {
170
+ return console . error ( err . toString ( ) ) ;
171
+ } ) ;
172
+
173
+ } ) ;
174
+
175
+ editor . getSession ( ) . on ( 'changeScrollTop' ,
176
+ function ( scroll )
177
+ {
178
+ editorScrollTop = parseInt ( scroll ) || 0 ;
179
+ }
180
+ ) ;
181
+ editor . getSession ( ) . on ( 'changeScrollLeft' ,
182
+ function ( scroll )
183
+ {
184
+ editorScrollLeft = parseInt ( scroll ) || 0 ;
185
+ }
186
+ ) ;
187
+
188
+ document . onmousemove = startWithDelay ( function ( e )
189
+ {
190
+ var style = document . getElementsByClassName ( '.ace_text-input' ) . style ;
191
+
192
+ connection . invoke ( "UpdatePointer" , e . pageX + editorScrollLeft , e . pageY + editorScrollTop ) . catch ( function ( err )
193
+ {
194
+ return console . error ( err . toString ( ) ) ;
195
+ } ) ;
196
+ } ,
197
+ 10 ) ;
84
198
85
199
} ) . catch ( function ( err )
86
200
{
87
- return console . error ( err . toString ( ) ) ;
201
+ return console . error ( err . toString ( ) ) ;
88
202
} ) ;
0 commit comments