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?
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.