mapbox-gl-draw icon indicating copy to clipboard operation
mapbox-gl-draw copied to clipboard

draw.create fired before map click event

Open dtabarceazeni opened this issue 2 years ago • 1 comments

mapbox-gl-js 2.14.1: mapbox-gl-draw 1.4.1:

Steps to Trigger Behavior

when double clicking to complete a polygon, the draw.create event is fired before the second click event is fired.

  1. go to https://jsfiddle.net/dnltbrca/sdv4w16L/
  2. open the browser console
  3. draw a polygon and complete it by double clicking

Expected Behavior

the console shows the order of the events: clicked, clicked, drawing created

Actual Behavior

the order is clicked, drawing created, clicked

This is important because it can be architecturally undesirable to remove click handlers and then add them back after the drawing is finished. We used a flag to simply ignore these click events, but since the flag is disabled when the drawing is created the second click events does not get ignored.

dtabarceazeni avatar Apr 24 '23 12:04 dtabarceazeni

This is important because it can be architecturally undesirable to remove click handlers and then add them back after the drawing is finished. We used a flag to simply ignore these click events, but since the flag is disabled when the drawing is created the second click events does not get ignored.

I worked around this by:

  1. taking off the undesired click handler during the draw.create event
  2. in the draw.create event, add a one-time event handler to the map.once("click", ...) handler that adds the handler removed in 1 back to the map

It's hacky but it seems to have worked. It prevents the non-draw click handler from picking up the event from the click that finished the polygon

MattBrooks95 avatar Jan 30 '24 04:01 MattBrooks95