Change type of element but keep already set properties

Hey,
I’m using a custom metamodel, in which different types with overlapping properties exist. In a custom property panel, it is possible to set values for those properties (modeling.updateProperties).
In a next step, I added a select field to change the type of the element using the solution provided in https://forum.bpmn.io/t/changing-task-to-usertask-by-code/1865/2:

changedType (type) {
    const newElementData = {
        type
    }
    const replace = this.bpmnModeler.get('replace')
    this.element = replace.replaceElement(this.element, newElementData)
}

It is possible to keep the already set properties during replacement (e.g., by sending them within newElementData?) or do I have to add them manually after replacement using modeling.updateProperties?
My goal is to end up with just one undo | redo event when the type is changed.

Some properties (for example extension elements) are not copied by default. To copy your properties do:

eventBus.on('moddleCopy.canCopyProperty', HIGH_PRIORITY, function(context) {
  var property = context.property;

  if (is(property, 'myCustomProperty')) {

    // return copied property
    return copyMyCustomProperty(property);
  }
});
1 Like

Thank you for your response.
This works for copy and paste elements but seems not to be triggered when replace.replaceElement is called. For the moment, I wonder where replace.replaceElement is called at all. It seems to be call the diagram-js replaceElement function instead of bpmn-js's function.

Make sure to use bpmnReplace instead of replace.

2 Likes

Thank you so much! Using modeler.get('bpmnReplace') solves the problem.