@@ -54,37 +54,31 @@ class GutterKeyboardHandler {
54
54
/** @this {GutterKeyboardHandler} */
55
55
function ( ) {
56
56
var index = this . $rowToRowIndex ( this . gutterLayer . $cursorCell . row ) ;
57
- var nearestFoldIndex = this . $findNearestFoldWidget ( index ) ;
57
+ var nearestFoldLaneWidgetIndex = this . $findNearestFoldLaneWidget ( index ) ;
58
58
var nearestAnnotationIndex = this . $findNearestAnnotation ( index ) ;
59
59
60
- if ( nearestFoldIndex === null && nearestAnnotationIndex === null ) return ;
60
+ if ( nearestFoldLaneWidgetIndex === null && nearestAnnotationIndex === null ) return ;
61
61
62
- if ( nearestFoldIndex === null && nearestAnnotationIndex !== null ) {
63
- this . activeRowIndex = nearestAnnotationIndex ;
64
- this . activeLane = "annotation" ;
65
- this . $focusAnnotation ( this . activeRowIndex ) ;
66
- return ;
67
- }
62
+ var futureActiveRowIndex = this . $findClosestNumber ( nearestFoldLaneWidgetIndex , nearestAnnotationIndex , index ) ;
68
63
69
- if ( nearestFoldIndex !== null && nearestAnnotationIndex === null ) {
70
- this . activeRowIndex = nearestFoldIndex ;
64
+ if ( futureActiveRowIndex === nearestFoldLaneWidgetIndex ) {
71
65
this . activeLane = "fold" ;
72
- this . $focusFoldWidget ( this . activeRowIndex ) ;
73
- return ;
66
+ this . activeRowIndex = nearestFoldLaneWidgetIndex ;
67
+ if ( this . $isCustomWidgetVisible ( nearestFoldLaneWidgetIndex ) ) {
68
+ this . $focusCustomWidget ( this . activeRowIndex ) ;
69
+ return ;
70
+ }
71
+ else {
72
+ this . $focusFoldWidget ( this . activeRowIndex ) ;
73
+ return ;
74
+ }
74
75
}
75
-
76
- if ( Math . abs ( nearestAnnotationIndex - index ) < Math . abs ( nearestFoldIndex - index ) ) {
76
+ else {
77
77
this . activeRowIndex = nearestAnnotationIndex ;
78
78
this . activeLane = "annotation" ;
79
79
this . $focusAnnotation ( this . activeRowIndex ) ;
80
80
return ;
81
81
}
82
- else {
83
- this . activeRowIndex = nearestFoldIndex ;
84
- this . activeLane = "fold" ;
85
- this . $focusFoldWidget ( this . activeRowIndex ) ;
86
- return ;
87
- }
88
82
} . bind ( this ) , 10 ) ;
89
83
return ;
90
84
}
@@ -164,22 +158,26 @@ class GutterKeyboardHandler {
164
158
165
159
switch ( this . activeLane ) {
166
160
case "fold" :
167
- if ( this . gutterLayer . session . foldWidgets [ this . $rowIndexToRow ( this . activeRowIndex ) ] === 'start' ) {
168
- var rowFoldingWidget = this . $rowIndexToRow ( this . activeRowIndex ) ;
161
+ var row = this . $rowIndexToRow ( this . activeRowIndex ) ;
162
+ var customWidget = this . editor . session . $gutterCustomWidgets [ row ] ;
163
+ if ( customWidget ) {
164
+ if ( customWidget . callbacks && customWidget . callbacks . onClick ) {
165
+ customWidget . callbacks . onClick ( e , row ) ;
166
+ }
167
+ }
168
+ else if ( this . gutterLayer . session . foldWidgets [ row ] === 'start' ) {
169
169
this . editor . session . onFoldWidgetClick ( this . $rowIndexToRow ( this . activeRowIndex ) , e ) ;
170
-
171
170
// After folding, check that the right fold widget is still in focus.
172
171
// If not (e.g. folding close to bottom of doc), put right widget in focus.
173
172
setTimeout (
174
173
/** @this {GutterKeyboardHandler} */
175
174
function ( ) {
176
- if ( this . $rowIndexToRow ( this . activeRowIndex ) !== rowFoldingWidget ) {
175
+ if ( this . $rowIndexToRow ( this . activeRowIndex ) !== row ) {
177
176
this . $blurFoldWidget ( this . activeRowIndex ) ;
178
- this . activeRowIndex = this . $rowToRowIndex ( rowFoldingWidget ) ;
177
+ this . activeRowIndex = this . $rowToRowIndex ( row ) ;
179
178
this . $focusFoldWidget ( this . activeRowIndex ) ;
180
179
}
181
180
} . bind ( this ) , 10 ) ;
182
-
183
181
break ;
184
182
} else if ( this . gutterLayer . session . foldWidgets [ this . $rowIndexToRow ( this . activeRowIndex ) ] === 'end' ) {
185
183
/* TO DO: deal with 'end' fold widgets */
@@ -205,6 +203,7 @@ class GutterKeyboardHandler {
205
203
switch ( this . activeLane ) {
206
204
case "fold" :
207
205
this . $blurFoldWidget ( this . activeRowIndex ) ;
206
+ this . $blurCustomWidget ( this . activeRowIndex ) ;
208
207
break ;
209
208
210
209
case "annotation" :
@@ -225,6 +224,12 @@ class GutterKeyboardHandler {
225
224
return isRowFullyVisible && isIconVisible ;
226
225
}
227
226
227
+ $isCustomWidgetVisible ( index ) {
228
+ var isRowFullyVisible = this . editor . isRowFullyVisible ( this . $rowIndexToRow ( index ) ) ;
229
+ var isIconVisible = ! ! this . $getCustomWidget ( index ) ;
230
+ return isRowFullyVisible && isIconVisible ;
231
+ }
232
+
228
233
$isAnnotationVisible ( index ) {
229
234
var isRowFullyVisible = this . editor . isRowFullyVisible ( this . $rowIndexToRow ( index ) ) ;
230
235
var isIconVisible = this . $getAnnotation ( index ) . style . display !== "none" ;
@@ -237,22 +242,37 @@ class GutterKeyboardHandler {
237
242
return element . childNodes [ 1 ] ;
238
243
}
239
244
245
+ $getCustomWidget ( index ) {
246
+ var cell = this . lines . get ( index ) ;
247
+ var element = cell . element ;
248
+ return element . childNodes [ 3 ] ;
249
+ }
250
+
240
251
$getAnnotation ( index ) {
241
252
var cell = this . lines . get ( index ) ;
242
253
var element = cell . element ;
243
254
return element . childNodes [ 2 ] ;
244
255
}
245
256
246
- // Given an index, find the nearest index with a foldwidget
247
- $findNearestFoldWidget ( index ) {
257
+ // Given an index, find the nearest index with a widget in fold lane
258
+ $findNearestFoldLaneWidget ( index ) {
259
+ // If custom widget exists at index, return index
260
+ if ( this . $isCustomWidgetVisible ( index ) )
261
+ return index ;
262
+
248
263
// If fold widget exists at index, return index.
249
264
if ( this . $isFoldWidgetVisible ( index ) )
250
265
return index ;
251
266
252
- // else, find the nearest index with fold widget within viewport.
267
+ // else, find the nearest index with widget within viewport.
253
268
var i = 0 ;
254
269
while ( index - i > 0 || index + i < this . lines . getLength ( ) - 1 ) {
255
270
i ++ ;
271
+ if ( index - i >= 0 && this . $isCustomWidgetVisible ( index - i ) )
272
+ return index - i ;
273
+
274
+ if ( index + i <= this . lines . getLength ( ) - 1 && this . $isCustomWidgetVisible ( index + i ) )
275
+ return index + i ;
256
276
257
277
if ( index - i >= 0 && this . $isFoldWidgetVisible ( index - i ) )
258
278
return index - i ;
@@ -261,7 +281,7 @@ class GutterKeyboardHandler {
261
281
return index + i ;
262
282
}
263
283
264
- // If there are no fold widgets within the viewport, return null.
284
+ // If there are no widgets within the viewport, return null.
265
285
return null ;
266
286
}
267
287
@@ -297,6 +317,17 @@ class GutterKeyboardHandler {
297
317
foldWidget . focus ( ) ;
298
318
}
299
319
320
+ $focusCustomWidget ( index ) {
321
+ if ( index == null )
322
+ return ;
323
+
324
+ var customWidget = this . $getCustomWidget ( index ) ;
325
+ if ( customWidget ) {
326
+ customWidget . classList . add ( this . editor . renderer . keyboardFocusClassName ) ;
327
+ customWidget . focus ( ) ;
328
+ }
329
+ }
330
+
300
331
$focusAnnotation ( index ) {
301
332
if ( index == null )
302
333
return ;
@@ -314,6 +345,14 @@ class GutterKeyboardHandler {
314
345
foldWidget . blur ( ) ;
315
346
}
316
347
348
+ $blurCustomWidget ( index ) {
349
+ var customWidget = this . $getCustomWidget ( index ) ;
350
+ if ( customWidget ) {
351
+ customWidget . classList . remove ( this . editor . renderer . keyboardFocusClassName ) ;
352
+ customWidget . blur ( ) ;
353
+ }
354
+ }
355
+
317
356
$blurAnnotation ( index ) {
318
357
var annotation = this . $getAnnotation ( index ) ;
319
358
@@ -327,10 +366,16 @@ class GutterKeyboardHandler {
327
366
while ( index > 0 ) {
328
367
index -- ;
329
368
330
- if ( this . $isFoldWidgetVisible ( index ) ) {
369
+ if ( this . $isFoldWidgetVisible ( index ) || this . $isCustomWidgetVisible ( index ) ) {
331
370
this . $blurFoldWidget ( this . activeRowIndex ) ;
371
+ this . $blurCustomWidget ( this . activeRowIndex ) ;
332
372
this . activeRowIndex = index ;
333
- this . $focusFoldWidget ( this . activeRowIndex ) ;
373
+ if ( this . $isFoldWidgetVisible ( index ) ) {
374
+ this . $focusFoldWidget ( this . activeRowIndex ) ;
375
+ }
376
+ else {
377
+ this . $focusCustomWidget ( this . activeRowIndex ) ;
378
+ }
334
379
return ;
335
380
}
336
381
}
@@ -343,10 +388,16 @@ class GutterKeyboardHandler {
343
388
while ( index < this . lines . getLength ( ) - 1 ) {
344
389
index ++ ;
345
390
346
- if ( this . $isFoldWidgetVisible ( index ) ) {
391
+ if ( this . $isFoldWidgetVisible ( index ) || this . $isCustomWidgetVisible ( index ) ) {
347
392
this . $blurFoldWidget ( this . activeRowIndex ) ;
393
+ this . $blurCustomWidget ( this . activeRowIndex ) ;
348
394
this . activeRowIndex = index ;
349
- this . $focusFoldWidget ( this . activeRowIndex ) ;
395
+ if ( this . $isFoldWidgetVisible ( index ) ) {
396
+ this . $focusFoldWidget ( this . activeRowIndex ) ;
397
+ }
398
+ else {
399
+ this . $focusCustomWidget ( this . activeRowIndex ) ;
400
+ }
350
401
return ;
351
402
}
352
403
}
@@ -385,6 +436,13 @@ class GutterKeyboardHandler {
385
436
return ;
386
437
}
387
438
439
+ $findClosestNumber ( num1 , num2 , target ) {
440
+ if ( num1 === null ) return num2 ;
441
+ if ( num2 === null ) return num1 ;
442
+
443
+ return ( Math . abs ( target - num1 ) <= Math . abs ( target - num2 ) ) ? num1 : num2 ;
444
+ }
445
+
388
446
$switchLane ( desinationLane ) {
389
447
switch ( desinationLane ) {
390
448
case "annotation" :
@@ -395,22 +453,29 @@ class GutterKeyboardHandler {
395
453
this . activeLane = "annotation" ;
396
454
397
455
this . $blurFoldWidget ( this . activeRowIndex ) ;
456
+ this . $blurCustomWidget ( this . activeRowIndex ) ;
398
457
this . activeRowIndex = annotationIndex ;
399
458
this . $focusAnnotation ( this . activeRowIndex ) ;
400
459
401
460
break ;
402
461
403
462
case "fold" :
404
- if ( this . activeLane === "fold" ) { break ; }
405
- var foldWidgetIndex = this . $findNearestFoldWidget ( this . activeRowIndex ) ;
406
- if ( foldWidgetIndex == null ) { break ; }
463
+ if ( this . activeLane === "fold" ) { break ; }
464
+ var foldLaneWidgetIndex = this . $findNearestFoldLaneWidget ( this . activeRowIndex ) ;
465
+ if ( foldLaneWidgetIndex = == null ) { break ; }
407
466
408
- this . activeLane = "fold" ;
467
+ this . activeLane = "fold" ;
409
468
410
- this . $blurAnnotation ( this . activeRowIndex ) ;
411
- this . activeRowIndex = foldWidgetIndex ;
469
+ this . $blurAnnotation ( this . activeRowIndex ) ;
470
+
471
+ this . activeRowIndex = foldLaneWidgetIndex ;
472
+
473
+ if ( this . $isCustomWidgetVisible ( foldLaneWidgetIndex ) ) {
474
+ this . $focusCustomWidget ( this . activeRowIndex ) ;
475
+ }
476
+ else {
412
477
this . $focusFoldWidget ( this . activeRowIndex ) ;
413
-
478
+ }
414
479
break ;
415
480
}
416
481
return ;
0 commit comments