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

Key events should be delivered when control.[trash|line|etc] are false

Open texels opened this issue 5 years ago • 1 comments

I am trying to make my own draw control modes/behaviors and it seems as though events.js consumes the delete/backspace/ keys if control.trash is set to false. If trash is false, I cannot get the key event to do my own thing with it. Unfortunately, when trash is true, a trash can button is placed on the map - which i don't want.

It does this for the the controls for line, point, and polygon too. If the controls are false, the event should be delivered for handling by the mode

In at least the keydown, it seems like

 else if (isKeyModeValid(event.keyCode)) {
      currentMode.keydown(event);
    } 

should be removed and

  else { currentMode.keydown(event);}

should be added at the end. Full code attached below

// 8 - Backspace
// 46 - Delete
events.keydown = function(event) {
  if ((event.srcElement || event.target).classList[0] !== 'mapboxgl-canvas') return; // we only handle events on the map

  if ((event.keyCode === 8 || event.keyCode === 46) && ctx.options.controls.trash) {
    event.preventDefault();
    currentMode.trash();
  } else if (isKeyModeValid(event.keyCode)) {
    currentMode.keydown(event);
  } else if (event.keyCode === 49 && ctx.options.controls.point) {
    changeMode(Constants.modes.DRAW_POINT);
  } else if (event.keyCode === 50 && ctx.options.controls.line_string) {
    changeMode(Constants.modes.DRAW_LINE_STRING);
  } else if (event.keyCode === 51 && ctx.options.controls.polygon) {
    changeMode(Constants.modes.DRAW_POLYGON);
  }
};

events.keyup = function(event) {
  if (isKeyModeValid(event.keyCode)) {
    currentMode.keyup(event);
  }
};

texels avatar Dec 18 '20 05:12 texels

Linking similar issue #1047

avpeery avatar Sep 21 '21 20:09 avpeery