Limit number of element from specific type to 1

I have create a custom element based on “bpmn:Participant” with the following exemple
I’d like to limit the number of this element on the diagram. Is there a way to get the list of all the elements in my CustomRenderer to stop the rendering of my custom element if there is already one on the diagram ?

A renderer is not the place to limit the number of certain elements. You shouldn’t allow them to be created in the first place.

Yeah, that’s true I suppose that I should limit it in my CustomPalette. So is there a way to get the list of all the element from my CustomPalette ?

elementRegistry.getAll() retrieves you all current elements on the canvas.


I tried to call elementRegistry.getAll() from my custom palette like that :

import ElementRegistry from 'diagram-js/lib/core/ElementRegistry';
var elements = ElementRegistry.getAll();

But I get this error :

Uncaught TypeError: diagram_js_lib_core_ElementRegistry__WEBPACK_IMPORTED_MODULE_3__.default.getAll is not a function

Am I doing the call to elementRegistry.getAll() the correct way ?

You cannot call a method that is not static without an having an instance. What you want is an instance of ElementRegistry. The toolkit uses dependency injection for that purpose. As you can see the palette has a lot of dependencies that get injected. You can inject the element registry in the same way.


Thanks it’s working !