Skip to content

Polygon with 2 vertices disappears on click #1446

@hugomau

Description

@hugomau

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] });
        }
    }
};

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions