Skip to content

Commit 5175746

Browse files
committed
fix: throttle the cursor style change in modify control
1 parent ceb4bff commit 5175746

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dependencies": {},
88
"peerDependencies": {
99
"jsts": ">=2",
10+
"lodash.throttle": ">=4",
1011
"ol": ">=7"
1112
},
1213
"devDependencies": {
@@ -28,6 +29,7 @@
2829
"jsdoc-export-default-interop": "0.3.1",
2930
"jsts": "2.11.3",
3031
"lint-staged": "15.2.2",
32+
"lodash.throttle": "4.1.1",
3133
"ol": "8",
3234
"prettier": "3.2.5",
3335
"shx": "0.3.4",

src/control/modify.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Modify, Interaction } from 'ol/interaction';
22
import { singleClick } from 'ol/events/condition';
3+
import throttle from 'lodash.throttle';
34
import Control from './control';
45
import image from '../../img/modify_geometry2.svg';
56
import SelectMove from '../interaction/selectmove';
@@ -68,7 +69,9 @@ class ModifyControl extends Control {
6869
/* Cursor management */
6970
this.previousCursor = null;
7071
this.cursorTimeout = null;
71-
this.cursorHandler = this.cursorHandler.bind(this);
72+
this.cursorHandler = throttle(this.cursorHandler.bind(this), 150, {
73+
leading: true,
74+
});
7275
this.cursorStyleHandler =
7376
options?.cursorStyleHandler || ((cursorStyle) => cursorStyle);
7477

@@ -318,35 +321,30 @@ class ModifyControl extends Control {
318321
* @private
319322
*/
320323
cursorHandler(evt) {
321-
if (this.cursorTimeout) {
322-
clearTimeout(this.cursorTimeout);
324+
if (evt.dragging || this.isMoving || this.isModifying) {
325+
this.changeCursor('grabbing');
326+
return;
323327
}
324-
this.cursorTimeout = setTimeout(() => {
325-
if (evt.dragging || this.isMoving || this.isModifying) {
326-
this.changeCursor('grabbing');
327-
return;
328-
}
329328

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+
}
336335

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)) {
338340
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-
}
345341
} else {
346-
// Feature available for selection.
347-
this.changeCursor('pointer');
342+
this.changeCursor(this.previousCursor);
348343
}
349-
}, 100);
344+
} else {
345+
// Feature available for selection.
346+
this.changeCursor('pointer');
347+
}
350348
}
351349

352350
/**
@@ -379,10 +377,8 @@ class ModifyControl extends Control {
379377
this.map.removeInteraction(this.selectModify);
380378
this.map.removeInteraction(this.deleteInteraction);
381379
this.map.removeInteraction(this.deselectInteraction);
382-
this.removeListeners();
383380
}
384381
super.setMap(map);
385-
this.addListeners();
386382
this.map?.addInteraction(this.deselectInteraction);
387383
this.map?.addInteraction(this.deleteInteraction);
388384
this.map?.addInteraction(this.selectModify);
@@ -400,7 +396,9 @@ class ModifyControl extends Control {
400396
*/
401397
addListeners() {
402398
this.removeListeners();
399+
this.map?.on('pointerdown', this.cursorHandler);
403400
this.map?.on('pointermove', this.cursorHandler);
401+
this.map?.on('pointerup', this.cursorHandler);
404402
}
405403

406404
/**
@@ -409,8 +407,9 @@ class ModifyControl extends Control {
409407
* @private
410408
*/
411409
removeListeners() {
412-
clearTimeout(this.cursorTimeout);
410+
this.map?.un('pointerdown', this.cursorHandler);
413411
this.map?.un('pointermove', this.cursorHandler);
412+
this.map?.un('pointerup', this.cursorHandler);
414413
}
415414

416415
/**
@@ -424,13 +423,14 @@ class ModifyControl extends Control {
424423
// For the default behavior it's very important to add selectMove after selectModify.
425424
// It will avoid single/dbleclick mess.
426425
this.selectMove.setActive(true);
426+
this.addListeners();
427427
}
428428

429429
/**
430430
* @inheritdoc
431431
*/
432432
deactivate(silent) {
433-
clearTimeout(this.cursorTimeout);
433+
this.removeListeners();
434434
this.selectMove.getFeatures().clear();
435435
this.selectModify.getFeatures().clear();
436436
this.deselectInteraction.setActive(false);

vitest.config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable import/no-extraneous-dependencies */
2+
// eslint-disable-next-line import/no-unresolved
23
import { defineConfig } from 'vitest/config';
34

45
export default defineConfig({

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3882,6 +3882,11 @@ lodash.startcase@^4.4.0:
38823882
resolved "https://registry.yarnpkg.com/lodash.startcase/-/lodash.startcase-4.4.0.tgz#9436e34ed26093ed7ffae1936144350915d9add8"
38833883
integrity sha512-+WKqsK294HMSc2jEbNgpHpd0JfIBhp7rEV4aqXWqFr6AlXov+SlcgB1Fv01y2kGe3Gc8nMW7VA0SrGuSkRfIEg==
38843884

3885+
3886+
version "4.1.1"
3887+
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"
3888+
integrity sha512-wIkUCfVKpVsWo3JSZlc+8MB5it+2AN5W8J7YVMST30UrvcQNZ1Okbj+rbVniijTWE6FGYy4XJq/rHkas8qJMLQ==
3889+
38853890
lodash.truncate@^4.4.2:
38863891
version "4.4.2"
38873892
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"

0 commit comments

Comments
 (0)