An example how to integrate bpmn-js with an Angular application.
Assume you bootstrapped your application using the ng command:
ng new bpmn-js-app --defaults=true
cd bpmn-js-appInclude the style files for diagram-js and bpmn in your Angular.json file under projects > your project > architect > build > options > styles
"styles": [
"./node_modules/bpmn-js/dist/assets/diagram-js.css",
"./node_modules/bpmn-js/dist/assets/bpmn-js.css",
"./node_modules/bpmn-js/dist/assets/bpmn-font/css/bpmn.css",
"src/styles.css"
],Create a component similar to DiagramComponent:
import {
AfterContentInit,
Component,
ElementRef,
Input,
OnChanges,
OnDestroy,
Output,
ViewChild,
SimpleChanges,
EventEmitter
} from '@angular/core';
import type Canvas from 'diagram-js/lib/core/Canvas';
import type { ImportDoneEvent } from 'bpmn-js/lib/BaseViewer';
/**
* You may include a different variant of BpmnJS:
*
* bpmn-viewer - displays BPMN diagrams without the ability
* to navigate them
* bpmn-modeler - bootstraps a full-fledged BPMN editor
*/
import * as BpmnJS from 'bpmn-js/dist/bpmn-modeler.production.min.js';
@Component({
selector: 'app-diagram',
template: `
<div #ref class="diagram-container"></div>
`,
styles: [
`
.diagram-container {
height: 100%;
width: 100%;
}
`
]
})
export class DiagramComponent implements AfterContentInit, OnChanges, OnDestroy {
// instantiate BpmnJS with component
private bpmnJS: BpmnJS;
// retrieve DOM element reference
@ViewChild('ref', { static: true }) private el: ElementRef;
@Input() private url: string;
constructor() {
this.bpmnJS = new BpmnJS();
this.bpmnJS.on<ImportDoneEvent>('import.done', ({ error }) => {
if (!error) {
this.bpmnJS.get<Canvas>('canvas').zoom('fit-viewport');
}
});
}
ngAfterContentInit(): void {
// attach BpmnJS instance to DOM element
this.bpmnJS.attachTo(this.el.nativeElement);
}
ngOnChanges(changes: SimpleChanges) {
// re-import whenever the url changes
if (changes.url) {
this.loadUrl(changes.url.currentValue).then(xml => {
return this.bpmnJS.importXML(xml);
});
}
}
ngOnDestroy(): void {
// destroy BpmnJS instance
this.bpmnJS.destroy();
}
private loadUrl(url: string) : Promise<string> {
throw new Error('not implemented - return diagram XML from url');
}
}npm install
npm run all- bpmn-js Examples
- bpmn-js Viewer Documentation, Example
- bpmn-js Modeler Documentation, Example
- How to add Keyboard-Bindings (cf.
Keyboardservice)
MIT
