mapbox / mapbox/mapbox-gl-draw
Polygon with 2 vertices disappears on click
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 1.1k
- Forks
- 612
- Avg merge
- 8d 9h
- Merged PRs (30d)
- 5
Description
## How to reproduce?
- set draw polygon mode
- tap two vertices on the map
- click on each on the two vertices
## Expected result
- nothing happens
## Actual result
- the polygon disappears and the map enters in simple_select mode
## possible correction
```
import { MapboxDrawCustomMode } from '@mapbox/mapbox-gl-draw';
import MapboxDraw from '@mapbox/mapbox-gl-draw';
const DrawModes = {
SIMPLE_SELECT: 'simple_select',
DIRECT_SELECT: 'direct_select'
};
// Start with a copy of the original draw_polygon mode
const DrawPolygonMode = MapboxDraw.modes.draw_polygon;
// Override or extend the mode with custom functionality
export const DrawPolygonModeUndo: MapboxDrawCustomMode = {
...DrawPolygonMode, // Copy all existing methods
clickOnVertex(state) {
// If there are more than 2 vertices, finish the polygon on vertex click
// This corrects a bug, where if there are only two vertices and a vertex is clicked,
// the polygon is closed and disappears.
// Check if state has property 'polygon'
if (!('polygon' in state)) {
return;
}
if (state.currentVertexPosition > 2) {
// Only close the polygon if there are more than 2 vertices
return this.changeMode(DrawModes.SIMPLE_SELECT, { featureIds: [state.polygon.id] });
}
}
};
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the draw_polygon mode and reproduce the issue by creating a polygon with two vertices, then clicking either vertex. Check the vertex-click behavior and verify that two-vertex polygons remain visible and the map does not enter simple_select mode, while polygons with more than two vertices retain their expected behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100