-
Notifications
You must be signed in to change notification settings - Fork 15
/
BpmnModeler.tsx
255 lines (222 loc) · 7.38 KB
/
BpmnModeler.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { tss } from "tss-react";
import * as monaco from "monaco-editor";
import CustomBpmnJsModeler from "./bpmnio/bpmn/CustomBpmnJsModeler";
import SvgIcon from "./components/SvgIcon";
import ToggleGroup from "./components/ToggleGroup";
import BpmnEditor, {
BpmnModelerOptions,
BpmnPropertiesPanelOptions,
} from "./editor/BpmnEditor";
import XmlEditor, { MonacoOptions } from "./editor/XmlEditor";
import { Event } from "./events";
import {
ContentSavedReason,
createContentSavedEvent,
} from "./events/modeler/ContentSavedEvent";
const useStyles = tss.create(() => ({
root: {
height: "100%",
overflow: "hidden",
},
modeToggle: {
position: "absolute",
left: "97px",
bottom: "32px",
backgroundColor: "rgba(255, 255, 255, 0.87)",
},
icon: {
marginTop: "4px",
},
}));
export interface ModelerTabOptions {
/**
* This option disables the modeler tab.
*/
disabled?: boolean;
/**
* The options passed to the bpmn-js modeler.
*
* CAUTION: When this option object is changed, the old editor instance will be destroyed
* and a new one will be created without automatic saving!
*/
bpmnJsOptions?: any;
/**
* The options to control the appearance of the properties panel.
*/
propertiesPanelOptions?: BpmnPropertiesPanelOptions;
/**
* The options to control the appearance of the modeler.
*/
modelerOptions?: BpmnModelerOptions;
/**
* The class name to apply to the modeler tab root element.
*/
className?: string;
}
export interface XmlTabOptions {
/**
* This option disables the XML tab.
*/
disabled?: boolean;
/**
* The options to pass to the monaco editor.
*/
monacoOptions?: MonacoOptions;
/**
* The class name applied to the host of the modeler.
*/
className?: string;
}
export interface BpmnModelerProps {
/**
* The class name applied to the root element.
*/
className?: string;
/**
* The XML to display in the editor.
*/
xml: string;
/**
* Called whenever an event occurs.
*/
onEvent: (event: Event<any, any>) => void;
/**
* Options to customize the modeler tab.
*/
modelerTabOptions?: ModelerTabOptions;
/**
* Options to customize the XML tab.
*/
xmlTabOptions?: XmlTabOptions;
}
declare type BpmnViewMode = "bpmn" | "xml";
const BpmnModeler: React.FC<BpmnModelerProps> = props => {
const { classes, cx } = useStyles();
const { onEvent, xml, modelerTabOptions, xmlTabOptions, className } = props;
const monacoRef = useRef<monaco.editor.IStandaloneCodeEditor>(null);
const modelerRef = useRef<CustomBpmnJsModeler>();
const [mode, setMode] = useState<BpmnViewMode>("bpmn");
useEffect(() => {
if (modelerTabOptions?.disabled && !xmlTabOptions?.disabled) {
setMode("xml");
}
}, [modelerTabOptions, xmlTabOptions]);
const modelerOptions: BpmnModelerOptions = useMemo(() => {
if (!modelerTabOptions?.modelerOptions) {
return {
refs: [modelerRef],
};
}
return {
...modelerTabOptions.modelerOptions,
refs: [...(modelerTabOptions.modelerOptions.refs ?? []), modelerRef],
};
}, [modelerTabOptions]);
const monacoOptions: MonacoOptions = useMemo(() => {
if (!xmlTabOptions?.monacoOptions) {
return {
refs: [monacoRef],
};
}
return {
...xmlTabOptions.monacoOptions,
refs: [...(xmlTabOptions.monacoOptions.refs ?? []), monacoRef],
};
}, [xmlTabOptions]);
const saveFile = useCallback(
async (source: BpmnViewMode, reason: ContentSavedReason) => {
switch (source) {
case "bpmn": {
if (modelerRef.current) {
const saved = await modelerRef.current?.save();
onEvent(createContentSavedEvent(saved.xml, saved.svg, reason));
}
break;
}
case "xml": {
if (monacoRef.current) {
//const saved = await monacoRef.current?.editor?.getValue() || "";
const saved = monacoRef.current?.getValue() || "";
onEvent(createContentSavedEvent(saved, undefined, reason));
}
break;
}
}
},
[onEvent],
);
const changeMode = useCallback(
async (value: string) => {
const bpmnViewMode = value as BpmnViewMode;
if (bpmnViewMode !== null && bpmnViewMode !== mode) {
await saveFile(mode, "view.changed");
setMode(bpmnViewMode);
}
},
[saveFile, mode],
);
const onXmlChanged = useCallback(
(value: string) => {
onEvent(createContentSavedEvent(value, undefined, "xml.changed"));
},
[onEvent],
);
if (!xml) {
return null;
}
return (
<div className={cx(classes.root, className)}>
{!modelerTabOptions?.disabled && (
<BpmnEditor
xml={xml}
active={mode === "bpmn"}
onEvent={onEvent}
modelerOptions={modelerOptions}
propertiesPanelOptions={modelerTabOptions?.propertiesPanelOptions}
bpmnJsOptions={modelerTabOptions?.bpmnJsOptions}
className={modelerTabOptions?.className}
/>
)}
{!xmlTabOptions?.disabled && (
<XmlEditor
xml={xml}
active={mode === "xml"}
monacoOptions={monacoOptions}
onChanged={onXmlChanged}
/>
)}
{!xmlTabOptions?.disabled && !modelerTabOptions?.disabled && (
<ToggleGroup
className={classes.modeToggle}
options={[
{
id: "bpmn",
node: (
<SvgIcon
className={classes.icon}
path="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71
7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34a.9959.9959 0 00-1.41
0l-1.83 1.83 3.75 3.75 1.83-1.83z"
/>
),
},
{
id: "xml",
node: (
<SvgIcon
className={classes.icon}
path="M9.4 16.6L4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4zm5.2
0l4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4z"
/>
),
},
]}
onChange={changeMode}
active={mode}
/>
)}
</div>
);
};
export default BpmnModeler;