Cancel Sketch Update in JS API 4

892
6
08-21-2019 01:52 PM
JeromeCauchon
New Contributor III

Is there a way to cancel a sketch during an update. I want to cancel update operation based on a condition.

const sketch = new Sketch({
   layer: layer,
   view: view
});

sketch.on('update', event => {

      // if something
      sketch.cancel();
})

Here's a CodePen of what i'm trying to achieve. Cancel Sketch Update

Thanks 

0 Kudos
6 Replies
Noah-Sager
Esri Regular Contributor

I don't think I understand the question. It seems that the codepen does exactly what your post is asking for; it cancels the sketch when the user attempts to update (reshape) the sketch.

0 Kudos
JeromeCauchon
New Contributor III

I have some graphics on my layer that i don't want to be editable. So when i click on one of them, i want to escape the sketch mode. Is it more clear?

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

I'd suggest adding attributes to your graphics to determine which graphics can be updated or which ones are not allowed to be updated. Then in the sketch's update event, check the selected graphic's attributes to see if the update is allowed. If not then call cancel. This test app should show the general concept.

Hope this helps,

-Undral  

JeromeCauchon
New Contributor III

Thanks Undral. I tried your test app but even if you cancel the sketch, the sketch box remains on the layer. I tried the reset function too and i have the same behavior.

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

So there is a timing issue with sketch update event. We will work on this for 4.13. In meantime, you can wrap the sketch.cancel in setTimeout function. You will see the graphic being selected before it is unselected.

setTimeout(function() {
  sketch.cancel();
},100);

If the approach above does not work then you can set SketchViewModel's updateOnGraphicClick property false, then add your own hittest logic for updating graphics. This sample shows how to use custom hittest logic for updating graphics.

JeromeCauchon
New Contributor III

Tanks Undral.