/
Bpmn.tsx
91 lines (69 loc) · 1.85 KB
/
Bpmn.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* eslint-disable @typescript-eslint/no-explicit-any */
import BpmnViewer from 'bpmn-js/lib/NavigatedViewer';
import type eventBus from "bpmn-js/lib/NavigatedViewer"
import type InternalEvent from "bpmn-js/lib/NavigatedViewer"
import { forwardRef, useCallback, useEffect, useRef, useState, useImperativeHandle } from 'react';
import "bpmn-js/dist/assets/diagram-js.css";
import "bpmn-js/dist/assets/bpmn-font/css/bpmn-embedded.css";
type BpmnProps = {
xml: string
onEventclick?: (e: InternalEvent) => void
}
const Bpmn = forwardRef(({ xml, onEventclick }: BpmnProps, ref) => {
const bpmnRef = useRef(null)
const [diagram, setDiagram] = useState("");
const modeler = useRef<BpmnViewer | null>()
const handleEventClick = useCallback((e: InternalEvent) => {
if (onEventclick) {
onEventclick(e)
}
}, [onEventclick])
useImperativeHandle(ref, () => ({
handleReset() {
if (modeler.current) {
const selection = modeler.current.get("selection") as any
selection.select([])
}
},
}), [])
useEffect(() => {
setDiagram(xml)
}, [xml])
useEffect(() => {
const createModeler = async () => {
if (bpmnRef.current && diagram) {
modeler.current = new BpmnViewer({
container: bpmnRef.current,
keyboard: {
bindTo: bpmnRef.current
}
})
try {
const { warnings } = await modeler.current.importXML(diagram);
console.log(warnings)
const eventBus = modeler?.current?.get("eventBus") as eventBus
eventBus.on("element.click", handleEventClick)
} catch (error) {
console.log(error)
}
}
}
createModeler()
return () => { modeler.current?.destroy() }
}, [diagram, handleEventClick])
return (
<>
<div
ref={bpmnRef}
style={{
border: "1px solid #000000",
height: "90vh",
width: "90vw",
margin: "auto"
}}
>
</div>
</>
)
})
export default Bpmn