I can populate a collapsed subprocess as discussed in Programmatically populate collapsed subprocess (thank you @nikku for the help!)
As discussed in Import subprocesses I would like to populate the collapsed subprocess from a separate BPMN-model.
By combining the code from Programmatically populate collapsed subprocess - #2 by rajgoel and GitHub - nikku/bpmn-js-copy-paste-example: An example how to copy and paste between multiple instances of bpmn-js I managed to get below code running which can load an XML file and copy & paste elements when creating my custom subprocesses:
import inherits from 'inherits';
import { is } from 'bpmn-js/lib/util/ModelUtil';
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
import ResourceActivity from './ResourceActivity.bpmn';
import BpmnModeler from 'bpmn-js/lib/Modeler';
const subProcessModeler = new BpmnModeler({});
subProcessModeler.importXML(ResourceActivity);
function ifNewResourceActivity(fn) {
return function(event) {
var context = event.context,
element = context.shape;
if (is(element, 'bpmn:SubProcess') && element.businessObject.type == 'Resource') {
fn(event);
}
};
}
/**
* A handler responsible for creating children to a resource subprocess when this is created
*/
export default function ResourceUpdater(eventBus, modeling, elementFactory, elementRegistry) {
CommandInterceptor.call(this, eventBus);
function createResourceChildren(evt) {
const sourceClipboard = subProcessModeler.get('clipboard'),
sourceCopyPaste = subProcessModeler.get('copyPaste'),
sourceElementRegistry = subProcessModeler.get('elementRegistry');
const targetClipboard = modeler.get('clipboard'),
targetCopyPaste = modeler.get('copyPaste'),
targetElementRegistry = modeler.get('elementRegistry');
const elements = sourceElementRegistry.getAll().filter(function(element) {
// determine top-level elements to be copied
return element.id != 'Process_Template' && element.parent && element.parent.id == 'Process_Template';
});
// copy!
sourceCopyPaste.copy(elements);
// retrieve clipboard contents
var copied = sourceClipboard.get();
// put into clipboard
targetClipboard.set(copied);
var context = evt.context,
element = context.shape,
businessObject = element.businessObject;
// I want to paste into my collapsed subprocess plane:
const parent = targetElementRegistry.get(businessObject.id + '_plane');
// Unfortunately I only managed to paste into the main process:
var pasteContext = {
// element: parent,
element: targetElementRegistry.get('Process_1'),
point: {x:100, y:100}
};
// paste tree
targetCopyPaste.paste(pasteContext);
}
this.postExecute([
'shape.create'
], ifNewResourceActivity(createResourceChildren));
}
inherits(ResourceUpdater, CommandInterceptor);
ResourceUpdater.$inject = [ 'eventBus', 'modeling', 'elementFactory', 'elementRegistry' ];
Unfortunately, I only managed to paste into the main process ('Process_1'
), but actually I want to paste into the collapsed subprocess, i.e. the parent
in above code. I assume that the pasteContext
is not properly defined or that I have to switch to the parent
plane before pasting and back afterwards.
Any hints would be highly appreciated!