1
1
import { Modify , Interaction } from 'ol/interaction' ;
2
2
import { singleClick } from 'ol/events/condition' ;
3
+ import throttle from 'lodash.throttle' ;
3
4
import Control from './control' ;
4
5
import image from '../../img/modify_geometry2.svg' ;
5
6
import SelectMove from '../interaction/selectmove' ;
@@ -68,7 +69,9 @@ class ModifyControl extends Control {
68
69
/* Cursor management */
69
70
this . previousCursor = null ;
70
71
this . cursorTimeout = null ;
71
- this . cursorHandler = this . cursorHandler . bind ( this ) ;
72
+ this . cursorHandler = throttle ( this . cursorHandler . bind ( this ) , 150 , {
73
+ leading : true ,
74
+ } ) ;
72
75
this . cursorStyleHandler =
73
76
options ?. cursorStyleHandler || ( ( cursorStyle ) => cursorStyle ) ;
74
77
@@ -318,35 +321,30 @@ class ModifyControl extends Control {
318
321
* @private
319
322
*/
320
323
cursorHandler ( evt ) {
321
- if ( this . cursorTimeout ) {
322
- clearTimeout ( this . cursorTimeout ) ;
324
+ if ( evt . dragging || this . isMoving || this . isModifying ) {
325
+ this . changeCursor ( 'grabbing' ) ;
326
+ return ;
323
327
}
324
- this . cursorTimeout = setTimeout ( ( ) => {
325
- if ( evt . dragging || this . isMoving || this . isModifying ) {
326
- this . changeCursor ( 'grabbing' ) ;
327
- return ;
328
- }
329
328
330
- const feature = this . getFeatureAtPixel ( evt . pixel ) ;
331
- if ( ! feature ) {
332
- this . changeCursor ( this . previousCursor ) ;
333
- this . previousCursor = null ;
334
- return ;
335
- }
329
+ const feature = this . getFeatureAtPixel ( evt . pixel ) ;
330
+ if ( ! feature ) {
331
+ this . changeCursor ( this . previousCursor ) ;
332
+ this . previousCursor = null ;
333
+ return ;
334
+ }
336
335
337
- if ( this . isSelectedByMove ( feature ) ) {
336
+ if ( this . isSelectedByMove ( feature ) ) {
337
+ this . changeCursor ( 'grab' ) ;
338
+ } else if ( this . isSelectedByModify ( feature ) ) {
339
+ if ( this . isHoverVertexFeatureAtPixel ( evt . pixel ) ) {
338
340
this . changeCursor ( 'grab' ) ;
339
- } else if ( this . isSelectedByModify ( feature ) ) {
340
- if ( this . isHoverVertexFeatureAtPixel ( evt . pixel ) ) {
341
- this . changeCursor ( 'grab' ) ;
342
- } else {
343
- this . changeCursor ( this . previousCursor ) ;
344
- }
345
341
} else {
346
- // Feature available for selection.
347
- this . changeCursor ( 'pointer' ) ;
342
+ this . changeCursor ( this . previousCursor ) ;
348
343
}
349
- } , 100 ) ;
344
+ } else {
345
+ // Feature available for selection.
346
+ this . changeCursor ( 'pointer' ) ;
347
+ }
350
348
}
351
349
352
350
/**
@@ -379,10 +377,8 @@ class ModifyControl extends Control {
379
377
this . map . removeInteraction ( this . selectModify ) ;
380
378
this . map . removeInteraction ( this . deleteInteraction ) ;
381
379
this . map . removeInteraction ( this . deselectInteraction ) ;
382
- this . removeListeners ( ) ;
383
380
}
384
381
super . setMap ( map ) ;
385
- this . addListeners ( ) ;
386
382
this . map ?. addInteraction ( this . deselectInteraction ) ;
387
383
this . map ?. addInteraction ( this . deleteInteraction ) ;
388
384
this . map ?. addInteraction ( this . selectModify ) ;
@@ -400,7 +396,9 @@ class ModifyControl extends Control {
400
396
*/
401
397
addListeners ( ) {
402
398
this . removeListeners ( ) ;
399
+ this . map ?. on ( 'pointerdown' , this . cursorHandler ) ;
403
400
this . map ?. on ( 'pointermove' , this . cursorHandler ) ;
401
+ this . map ?. on ( 'pointerup' , this . cursorHandler ) ;
404
402
}
405
403
406
404
/**
@@ -409,8 +407,9 @@ class ModifyControl extends Control {
409
407
* @private
410
408
*/
411
409
removeListeners ( ) {
412
- clearTimeout ( this . cursorTimeout ) ;
410
+ this . map ?. un ( 'pointerdown' , this . cursorHandler ) ;
413
411
this . map ?. un ( 'pointermove' , this . cursorHandler ) ;
412
+ this . map ?. un ( 'pointerup' , this . cursorHandler ) ;
414
413
}
415
414
416
415
/**
@@ -424,13 +423,14 @@ class ModifyControl extends Control {
424
423
// For the default behavior it's very important to add selectMove after selectModify.
425
424
// It will avoid single/dbleclick mess.
426
425
this . selectMove . setActive ( true ) ;
426
+ this . addListeners ( ) ;
427
427
}
428
428
429
429
/**
430
430
* @inheritdoc
431
431
*/
432
432
deactivate ( silent ) {
433
- clearTimeout ( this . cursorTimeout ) ;
433
+ this . removeListeners ( ) ;
434
434
this . selectMove . getFeatures ( ) . clear ( ) ;
435
435
this . selectModify . getFeatures ( ) . clear ( ) ;
436
436
this . deselectInteraction . setActive ( false ) ;
0 commit comments