Cancel creation of element

Hi,

I’m selecting an element from the palette and then creating it on the Canvas. I’ve created a CommandInterceptor on preExecute to prompt the user for a name, that will be assigned to this element.

If the user enters the name, element will be created. If the user presses "cancel’ on the dialog box, how do I abort the creation of element?

Thanks,
Jay.

Are you able to share your approach via CodeSandbox? This way it will be easier for us to help you.

I’m quite new to this - I’ve created the sandbox environment but it doesn’t run on it. However, the code is all there.

If you see the code at line 19 - how do I stop the creation of element on canvas if the user hasn’t entered a name in the dialog box?

Thanks.

Hey,

you can hook into the create.end event with high priority hook in before the element is created by Create.

eventBus.on("create.end", HIGH_PRIORITY, ({ context }) => {
  const { elements, hints, shape } = context;

  if (elements && elements.length > 1) {
    // more than one element created
    return;
  }

  const name = prompt("Please enter a name");

  if (name) {
    shape.businessObject.set("name", name);

    // disable direct editing
    hints.createElementsBehavior = false;

    return;
  }

  // do not create element
  return false;
});

Returning false from the event listener will prevent any other event listeners to be called afterwards. This effectively cancels creating the element.

Working example: Set Name on Create Example - CodeSandbox