Skip to content

Commit

Permalink
feat: allow custom limits of microdrags whilst selecting
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner committed Sep 1, 2023
1 parent a51d278 commit c02a067
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
32 changes: 23 additions & 9 deletions src/adapters/common/base.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ export abstract class TerraDrawBaseAdapter {
coordinatePrecision?: number;
minPixelDragDistanceDrawing?: number;
minPixelDragDistance?: number;
minPixelDragDistanceSelecting?: number;
}) {
this._minPixelDragDistance =
typeof config.minPixelDragDistance === "number"
? config.minPixelDragDistance
: 1;

this._minPixelDragDistanceSelecting =
typeof config.minPixelDragDistanceSelecting === "number"
? config.minPixelDragDistanceSelecting
: 8;

this._minPixelDragDistanceDrawing =
typeof config.minPixelDragDistanceDrawing === "number"
? config.minPixelDragDistanceDrawing
Expand Down Expand Up @@ -111,22 +117,29 @@ export abstract class TerraDrawBaseAdapter {
currentEventXY
);

// We start off assuming it is not a microdrag
let isMicroDrag = false;

if (modeState === "drawing") {
// We want to ignore very small pointer movements when holding
// the map down as these are normally done by accident when
// drawing and is not an intended drag
const isMicroDrag =
isMicroDrag =
pixelDistanceToCheck < this._minPixelDragDistanceDrawing;
if (isMicroDrag) {
return;
}
} else if (modeState === "selecting") {
// Simiarly when selecting, we want to ignore very small pointer
// movements when holding the map down as these are normally done
// by accident when drawing and is not an intended drag
isMicroDrag =
pixelDistanceToCheck < this._minPixelDragDistanceSelecting;
} else {
// Same as above, but when not drawing we generally want a much lower tolerance
const isMicroDrag =
pixelDistanceToCheck < this._minPixelDragDistance;
if (isMicroDrag) {
return;
}
isMicroDrag = pixelDistanceToCheck < this._minPixelDragDistance;
}

// If it is a microdrag we do not register it by returning early
if (isMicroDrag) {
return;
}

this._dragState = "dragging";
Expand Down Expand Up @@ -280,6 +293,7 @@ export abstract class TerraDrawBaseAdapter {

protected _minPixelDragDistance: number;
protected _minPixelDragDistanceDrawing: number;
protected _minPixelDragDistanceSelecting: number;
protected _lastDrawEvent: TerraDrawMouseEvent | undefined;
protected _coordinatePrecision: number;
protected _heldKeys: Set<string> = new Set();
Expand Down
2 changes: 1 addition & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type TerraDrawModeState =
| "registered"
| "started"
| "drawing"
| "selected"
| "selecting"
| "stopped";

export interface TerraDrawCallbacks {
Expand Down
2 changes: 0 additions & 2 deletions src/geometry/shape/create-bbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ describe("Geometry", () => {
pointerDistance: 30,
});

console.log(JSON.stringify(result));

expect(result.geometry.type).toBe("Polygon");
expect(result.geometry.coordinates).toEqual([
[
Expand Down
3 changes: 2 additions & 1 deletion src/modes/base.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
if (
this._state === "stopped" ||
this._state === "registered" ||
this._state === "drawing"
this._state === "drawing" ||
this._state === "selecting"
) {
this._state = "started";
this.setDoubleClickToZoom(false);
Expand Down
2 changes: 1 addition & 1 deletion src/modes/select/select.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe("TerraDrawSelectMode", () => {
selectMode.register(getMockModeConfig(selectMode.mode));
selectMode.start();

expect(selectMode.state).toBe("started");
expect(selectMode.state).toBe("selecting");
});

it("can stop correctly", () => {
Expand Down
10 changes: 10 additions & 0 deletions src/modes/select/select.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ export class TerraDrawSelectMode extends TerraDrawBaseDrawMode<SelectionStyling>
5;
}

setSelecting() {
if (this._state === "started") {
this._state = "selecting";
} else {
throw new Error("Mode must be started to move to selecting state");
}
}

registerBehaviors(config: BehaviorConfig) {
this.pixelDistance = new PixelDistanceBehavior(config);
this.clickBoundingBox = new ClickBoundingBoxBehavior(config);
Expand Down Expand Up @@ -431,11 +439,13 @@ export class TerraDrawSelectMode extends TerraDrawBaseDrawMode<SelectionStyling>
/** @internal */
start() {
this.setStarted();
this.setSelecting();
}

/** @internal */
stop() {
this.cleanUp();
this.setStarted();
this.setStopped();
}

Expand Down

0 comments on commit c02a067

Please sign in to comment.