Skip to content

Commit 5eb32b9

Browse files
committed
fix: improve cursor behavior on pointer down and up in modify control
1 parent c336dd7 commit 5eb32b9

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/control/modify.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Modify, Interaction } from 'ol/interaction';
22
import { singleClick } from 'ol/events/condition';
3+
// eslint-disable-next-line import/no-extraneous-dependencies
34
import throttle from 'lodash.throttle';
5+
import { unByKey } from 'ol/Observable';
46
import Control from './control';
57
import image from '../../img/modify_geometry2.svg';
68
import SelectMove from '../interaction/selectmove';
@@ -69,7 +71,7 @@ class ModifyControl extends Control {
6971
/* Cursor management */
7072
this.previousCursor = null;
7173
this.cursorTimeout = null;
72-
this.cursorHandler = throttle(this.cursorHandler.bind(this), 150, {
74+
this.cursorHandlerThrottled = throttle(this.cursorHandler.bind(this), 150, {
7375
leading: true,
7476
});
7577
this.cursorStyleHandler =
@@ -377,8 +379,12 @@ class ModifyControl extends Control {
377379
this.map.removeInteraction(this.selectModify);
378380
this.map.removeInteraction(this.deleteInteraction);
379381
this.map.removeInteraction(this.deselectInteraction);
382+
this.removeListeners();
380383
}
381384
super.setMap(map);
385+
if (this.getActive()) {
386+
this.addListeners();
387+
}
382388
this.map?.addInteraction(this.deselectInteraction);
383389
this.map?.addInteraction(this.deleteInteraction);
384390
this.map?.addInteraction(this.selectModify);
@@ -396,9 +402,21 @@ class ModifyControl extends Control {
396402
*/
397403
addListeners() {
398404
this.removeListeners();
399-
this.map?.on('pointerdown', this.cursorHandler);
400-
this.map?.on('pointermove', this.cursorHandler);
401-
this.map?.on('pointerup', this.cursorHandler);
405+
this.cursorListenerKeys = [
406+
this.map?.on('pointerdown', () => {
407+
const element = this.map.getTargetElement();
408+
if (element?.style?.cursor === 'grab') {
409+
this.changeCursor('grabbing');
410+
}
411+
}),
412+
this.map?.on('pointermove', this.cursorHandlerThrottled),
413+
this.map?.on('pointerup', () => {
414+
const element = this.map.getTargetElement();
415+
if (element?.style?.cursor === 'grabbing') {
416+
this.changeCursor('grab');
417+
}
418+
}),
419+
];
402420
}
403421

404422
/**
@@ -407,9 +425,7 @@ class ModifyControl extends Control {
407425
* @private
408426
*/
409427
removeListeners() {
410-
this.map?.un('pointerdown', this.cursorHandler);
411-
this.map?.un('pointermove', this.cursorHandler);
412-
this.map?.un('pointerup', this.cursorHandler);
428+
unByKey(this.cursorListenerKeys);
413429
}
414430

415431
/**

0 commit comments

Comments
 (0)