@uipath/maestro-builder-sdk 6.86.27 → 6.87.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bpmn/decompile-cli.js +4 -3
- package/dist/bpmn/decompile.js +4 -3
- package/dist/bpmn/merge-cli.js +19 -1
- package/dist/bpmn/merge-placement.d.ts +33 -0
- package/dist/bpmn/merge-placement.js +899 -0
- package/dist/bpmn/merge.d.ts +7 -0
- package/dist/bpmn/merge.js +44 -2
- package/dist/bpmn/serialize.js +2 -1
- package/dist/check.js +24 -54
- package/dist/core/step-ports.d.ts +37 -0
- package/dist/decompile.js +20 -38
- package/dist/flow-node-type.d.ts +30 -1
- package/dist/flow-step-exits.d.ts +10 -1
- package/dist/flow-step-exits.js +18 -6
- package/dist/nodes/hitl/flow/node-type.js +76 -1
- package/package.json +2 -2
|
@@ -0,0 +1,899 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* merge-placement — draw what a brownfield edit added to a `.bpmn`, and leave the rest
|
|
3
|
+
* of the diagram where the author put it. The BPMN counterpart of `node-placement.ts`.
|
|
4
|
+
*
|
|
5
|
+
* Compile writes no diagram (serialize.ts), so after a merge the elements the edit added
|
|
6
|
+
* have no `bpmndi:BPMNShape` and their flows no `bpmndi:BPMNEdge`, and
|
|
7
|
+
* `uip maestro bpmn validate` reports `MISSING_DI_SHAPE` / `MISSING_DI_EDGE`. Before this
|
|
8
|
+
* file the one fix was `uip maestro bpmn format`, which lays out the WHOLE diagram again.
|
|
9
|
+
* That is the greenfield tool. Here only the added elements are placed:
|
|
10
|
+
*
|
|
11
|
+
* - **A shape the original has keeps its bounds, byte for byte.** Placement never moves
|
|
12
|
+
* one to make room. The Flow merge does (it pushes the nodes downstream of an insert
|
|
13
|
+
* right); a BPMN diagram is often hand-arranged, in pools and lanes, so the new shape
|
|
14
|
+
* finds a free spot instead. The one exception is below.
|
|
15
|
+
* - **A new flow node goes right of its rightmost drawn predecessor**, at that
|
|
16
|
+
* predecessor's distance-free spot (`H_DIST` past its right edge, centred on its row),
|
|
17
|
+
* the spot bpmn-js's auto-place (and so the BPMN designer) picks when you append a
|
|
18
|
+
* shape. When that spot is taken, the next row down is tried, then the next row up,
|
|
19
|
+
* alternating, and then the next column right, the way auto-place steps to a free row.
|
|
20
|
+
* With no drawn predecessor it goes left of its leftmost drawn successor; with neither,
|
|
21
|
+
* below everything in its container. A free spot clears every shape, every shape and
|
|
22
|
+
* edge label, and every segment of an edge that keeps its route. A new node is drawn
|
|
23
|
+
* in every diagram that draws its process. Nodes are placed in waves, each once every
|
|
24
|
+
* predecessor the edit added is placed, so a chain of new nodes lays out as a row and
|
|
25
|
+
* a join lands right of its branches.
|
|
26
|
+
* - **A new boundary event sits on its host**, in the first free slot: bottom face first,
|
|
27
|
+
* then top, right and left.
|
|
28
|
+
* - **A kept node the edit moved into another scope** (steps grouped into a new
|
|
29
|
+
* sub-process) is drawn again inside its new parent, under its old DI id, and its flows
|
|
30
|
+
* are routed again; it is reported as `moved`.
|
|
31
|
+
* - **It stays in its container**: inside an expanded sub-process it is the parent of,
|
|
32
|
+
* inside the lane its predecessor (else successor) is in, inside the pool. A node
|
|
33
|
+
* placed in a lane is also listed in that lane and every lane around it
|
|
34
|
+
* (`bpmn:flowNodeRef`), so the lanes own what they draw.
|
|
35
|
+
* - **When a container has no free spot, it grows — the one way an existing shape
|
|
36
|
+
* changes.** The node takes the first row under the container's content, and the
|
|
37
|
+
* container, every container around it, and everything below its old bottom move down
|
|
38
|
+
* together by one offset (a lane grows; the lanes under it and their contents shift; the
|
|
39
|
+
* pool grows; a pool below moves). A boundary event on a growing face moves with it, and
|
|
40
|
+
* an edge follows its ends: it moves with them when both moved by the same offset, and
|
|
41
|
+
* is routed again when only one did. Every such id is reported (`resized`, `moved`),
|
|
42
|
+
* never done silently.
|
|
43
|
+
* - **A new sequence flow gets orthogonal waypoints**, docked on the faces that look at
|
|
44
|
+
* each other (Studio Web picks the dock side from the end waypoints): straight when the
|
|
45
|
+
* two ends share a row, a Z through the gap between them otherwise, out of a gateway's
|
|
46
|
+
* bottom or top face to a branch on another row, and vertical between shapes that share
|
|
47
|
+
* a column. A flow the edit kept by id but rewired to another source or target gets the
|
|
48
|
+
* same new route (`rerouted`): its old waypoints point at the wrong shape. Its label
|
|
49
|
+
* moves beside the middle of the new route.
|
|
50
|
+
*
|
|
51
|
+
* What it does not touch: an element of the original that was already missing its
|
|
52
|
+
* shape (a pre-existing gap is the author's), a document with no diagram at all (that is
|
|
53
|
+
* greenfield: `uip maestro bpmn format`), and a sub-process drawn collapsed with no plane
|
|
54
|
+
* of its own (nowhere to draw its children). New DI ids follow the file's own pattern
|
|
55
|
+
* (`S_<id>`, `<id>_di`, …), and the `bpmndi`/`dc`/`di` prefixes are the file's.
|
|
56
|
+
*/
|
|
57
|
+
import { parseXmlRanges } from './xml-ranges.js';
|
|
58
|
+
const NS = {
|
|
59
|
+
bpmndi: 'http://www.omg.org/spec/BPMN/20100524/DI',
|
|
60
|
+
dc: 'http://www.omg.org/spec/DD/20100524/DC',
|
|
61
|
+
di: 'http://www.omg.org/spec/DD/20100524/DI',
|
|
62
|
+
};
|
|
63
|
+
/** Gap between a predecessor's right edge and the new node (bpmn-js auto-place distance). */
|
|
64
|
+
const H_DIST = 50;
|
|
65
|
+
/** Vertical gap between two rows of shapes. */
|
|
66
|
+
const V_GAP = 30;
|
|
67
|
+
/** Clear space kept around every shape a new one must not touch. */
|
|
68
|
+
const CLEARANCE = 15;
|
|
69
|
+
/** Space a new node keeps from the edge of the container it is in. */
|
|
70
|
+
const INSET = 20;
|
|
71
|
+
/** Rows tried above and below the preferred spot, and columns right of it. */
|
|
72
|
+
const MAX_ROWS = 12;
|
|
73
|
+
const MAX_COLUMNS = 8;
|
|
74
|
+
/** Where the first shape goes on a diagram that has none (CLI bpmn-layout `X_START`/`Y_START`). */
|
|
75
|
+
const EMPTY_CANVAS_ORIGIN = { x: 160, y: 100 };
|
|
76
|
+
/** The name band down the left side of a horizontal pool, which no flow node sits in. */
|
|
77
|
+
const POOL_LABEL_BAND = 30;
|
|
78
|
+
/** An expanded sub-process's inner padding (CLI bpmn-layout `SUB_PROCESS_PADDING_*`). */
|
|
79
|
+
const SUB_PROCESS_PAD = { x: 40, top: 60, bottom: 40 };
|
|
80
|
+
const EVENTS = new Set(['startEvent', 'endEvent', 'intermediateCatchEvent', 'intermediateThrowEvent', 'boundaryEvent']);
|
|
81
|
+
const GATEWAYS = new Set(['exclusiveGateway', 'parallelGateway', 'inclusiveGateway', 'eventBasedGateway', 'complexGateway']);
|
|
82
|
+
const SUB_PROCESSES = new Set(['subProcess', 'transaction', 'adHocSubProcess']);
|
|
83
|
+
const ACTIVITIES = new Set([
|
|
84
|
+
'task', 'userTask', 'serviceTask', 'scriptTask', 'sendTask', 'receiveTask', 'manualTask', 'businessRuleTask',
|
|
85
|
+
'callActivity', ...SUB_PROCESSES,
|
|
86
|
+
]);
|
|
87
|
+
const isFlowNode = (local) => EVENTS.has(local) || GATEWAYS.has(local) || ACTIVITIES.has(local);
|
|
88
|
+
const right = (b) => b.x + b.w;
|
|
89
|
+
const bottom = (b) => b.y + b.h;
|
|
90
|
+
const cx = (b) => b.x + b.w / 2;
|
|
91
|
+
const cy = (b) => b.y + b.h / 2;
|
|
92
|
+
const overlaps = (a, b, pad) => a.x < right(b) + pad && b.x < right(a) + pad && a.y < bottom(b) + pad && b.y < bottom(a) + pad;
|
|
93
|
+
const inside = (a, region) => a.x >= region.x && a.y >= region.y && right(a) <= right(region) && bottom(a) <= bottom(region);
|
|
94
|
+
/** The size the designer draws a new element at (CLI bpmn-layout `nodeDims`). */
|
|
95
|
+
function sizeOf(local, rendering) {
|
|
96
|
+
if (EVENTS.has(local))
|
|
97
|
+
return { w: 36, h: 36 };
|
|
98
|
+
if (GATEWAYS.has(local))
|
|
99
|
+
return { w: 50, h: 50 };
|
|
100
|
+
if (rendering === 'expanded')
|
|
101
|
+
return { w: 350, h: 200 };
|
|
102
|
+
return { w: 100, h: 80 };
|
|
103
|
+
}
|
|
104
|
+
const num = (value) => Number(value ?? 0);
|
|
105
|
+
function boxOf(range) {
|
|
106
|
+
const a = range.attributes;
|
|
107
|
+
return { x: num(a.x), y: num(a.y), w: num(a.width), h: num(a.height) };
|
|
108
|
+
}
|
|
109
|
+
function geometry(range) {
|
|
110
|
+
const read = boxOf(range);
|
|
111
|
+
return { range, read, now: { ...read } };
|
|
112
|
+
}
|
|
113
|
+
const all = (nodes, local) => nodes.flatMap((n) => [...(n.local === local ? [n] : []), ...all(n.children, local)]);
|
|
114
|
+
/** The prefix the root declares for a namespace, or undefined. */
|
|
115
|
+
function prefixFor(root, uri) {
|
|
116
|
+
for (const [name, value] of Object.entries(root.attributes))
|
|
117
|
+
if (name.startsWith('xmlns:') && value === uri)
|
|
118
|
+
return name.slice(6);
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
/** The `prefix`/`suffix` the file's own DI ids put around their element id (`S_x`, `x_di`). */
|
|
122
|
+
function idPattern(pairs, fallback) {
|
|
123
|
+
for (const { id, element } of pairs) {
|
|
124
|
+
if (id === undefined || !element)
|
|
125
|
+
continue;
|
|
126
|
+
const at = id.indexOf(element);
|
|
127
|
+
if (at >= 0 && id !== element)
|
|
128
|
+
return { prefix: id.slice(0, at), suffix: id.slice(at + element.length) };
|
|
129
|
+
}
|
|
130
|
+
return fallback;
|
|
131
|
+
}
|
|
132
|
+
function readModel(xml, root) {
|
|
133
|
+
const nodes = new Map();
|
|
134
|
+
const flows = new Map();
|
|
135
|
+
const lanes = [];
|
|
136
|
+
const ids = new Set();
|
|
137
|
+
const collect = (r) => {
|
|
138
|
+
if (r.id !== undefined)
|
|
139
|
+
ids.add(r.id);
|
|
140
|
+
r.children.forEach(collect);
|
|
141
|
+
};
|
|
142
|
+
collect(root);
|
|
143
|
+
const readLanes = (laneSet, parent) => {
|
|
144
|
+
for (const lane of laneSet.children.filter((c) => c.local === 'lane' && c.id !== undefined)) {
|
|
145
|
+
const refs = new Set();
|
|
146
|
+
for (const ref of lane.children.filter((c) => c.local === 'flowNodeRef'))
|
|
147
|
+
refs.add(xml.slice(ref.innerStart, ref.innerEnd).trim());
|
|
148
|
+
lanes.push({ id: lane.id, range: lane, refs, ...(parent ? { parent } : {}) });
|
|
149
|
+
for (const child of lane.children.filter((c) => c.local === 'childLaneSet'))
|
|
150
|
+
readLanes(child, lane.id);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const visit = (scope) => {
|
|
154
|
+
for (const child of scope.children) {
|
|
155
|
+
if (child.local === 'laneSet')
|
|
156
|
+
readLanes(child);
|
|
157
|
+
if (child.id === undefined)
|
|
158
|
+
continue;
|
|
159
|
+
if (child.local === 'sequenceFlow') {
|
|
160
|
+
flows.set(child.id, { id: child.id, source: child.attributes.sourceRef ?? '', target: child.attributes.targetRef ?? '' });
|
|
161
|
+
}
|
|
162
|
+
else if (isFlowNode(child.local)) {
|
|
163
|
+
const hasChildren = SUB_PROCESSES.has(child.local) && child.children.some((c) => isFlowNode(c.local));
|
|
164
|
+
nodes.set(child.id, {
|
|
165
|
+
id: child.id,
|
|
166
|
+
local: child.local,
|
|
167
|
+
scope: scope.id ?? '',
|
|
168
|
+
...(child.attributes.attachedToRef !== undefined ? { attachedTo: child.attributes.attachedToRef } : {}),
|
|
169
|
+
hasChildren,
|
|
170
|
+
});
|
|
171
|
+
if (SUB_PROCESSES.has(child.local))
|
|
172
|
+
visit(child);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
const participantOf = new Map();
|
|
177
|
+
const collaborationOf = new Map();
|
|
178
|
+
for (const top of root.children) {
|
|
179
|
+
if (top.local === 'process')
|
|
180
|
+
visit(top);
|
|
181
|
+
if (top.local === 'collaboration' && top.id !== undefined) {
|
|
182
|
+
for (const participant of top.children.filter((c) => c.local === 'participant')) {
|
|
183
|
+
const ref = participant.attributes.processRef;
|
|
184
|
+
if (ref !== undefined && participant.id !== undefined) {
|
|
185
|
+
participantOf.set(ref, participant.id);
|
|
186
|
+
collaborationOf.set(ref, top.id);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const planes = all(root.children, 'BPMNPlane').map((plane) => ({
|
|
192
|
+
range: plane,
|
|
193
|
+
element: plane.attributes.bpmnElement ?? '',
|
|
194
|
+
shapes: plane.children.filter((c) => c.local === 'BPMNShape').flatMap((shape) => {
|
|
195
|
+
const bounds = shape.children.find((c) => c.local === 'Bounds');
|
|
196
|
+
if (!bounds)
|
|
197
|
+
return [];
|
|
198
|
+
const labels = shape.children.filter((c) => c.local === 'BPMNLabel').flatMap((l) => l.children.filter((c) => c.local === 'Bounds')).map(geometry);
|
|
199
|
+
return [{ ...(shape.id !== undefined ? { id: shape.id } : {}), range: shape, element: shape.attributes.bpmnElement ?? '', bounds: geometry(bounds), labels, expanded: shape.attributes.isExpanded === 'true' }];
|
|
200
|
+
}),
|
|
201
|
+
edges: plane.children.filter((c) => c.local === 'BPMNEdge').map((edge) => ({
|
|
202
|
+
...(edge.id !== undefined ? { id: edge.id } : {}),
|
|
203
|
+
element: edge.attributes.bpmnElement ?? '',
|
|
204
|
+
range: edge,
|
|
205
|
+
waypoints: edge.children.filter((c) => c.local === 'waypoint').map(geometry),
|
|
206
|
+
labels: edge.children.filter((c) => c.local === 'BPMNLabel').flatMap((l) => l.children.filter((c) => c.local === 'Bounds')).map(geometry),
|
|
207
|
+
})),
|
|
208
|
+
added: [],
|
|
209
|
+
addedEdges: [],
|
|
210
|
+
}));
|
|
211
|
+
return { nodes, flows, lanes, participantOf, collaborationOf, planes, ids };
|
|
212
|
+
}
|
|
213
|
+
class Placer {
|
|
214
|
+
model;
|
|
215
|
+
rewired;
|
|
216
|
+
rescoped;
|
|
217
|
+
report = { placed: [], routed: [], rerouted: [], resized: [], moved: [] };
|
|
218
|
+
laneRefs = [];
|
|
219
|
+
constructor(model, rewired, rescoped) {
|
|
220
|
+
this.model = model;
|
|
221
|
+
this.rewired = rewired;
|
|
222
|
+
this.rescoped = rescoped;
|
|
223
|
+
}
|
|
224
|
+
/** The box an element is drawn with in `plane`, if any. */
|
|
225
|
+
boxIn(plane, element) {
|
|
226
|
+
const added = plane.added.find((a) => a.element === element);
|
|
227
|
+
if (added)
|
|
228
|
+
return added.box;
|
|
229
|
+
return plane.shapes.find((s) => s.element === element)?.bounds.now;
|
|
230
|
+
}
|
|
231
|
+
shapeIn(plane, element) {
|
|
232
|
+
return plane.shapes.find((s) => s.element === element);
|
|
233
|
+
}
|
|
234
|
+
drawn(element) {
|
|
235
|
+
return this.model.planes.some((p) => p.shapes.some((s) => s.element === element) || p.added.some((a) => a.element === element));
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Every plane that draws a scope's children, each with the expanded sub-process box
|
|
239
|
+
* they sit in there. A document can draw one process in more than one diagram, and a
|
|
240
|
+
* new node is drawn in each of them.
|
|
241
|
+
*/
|
|
242
|
+
planesFor(scope) {
|
|
243
|
+
const collaboration = this.model.collaborationOf.get(scope);
|
|
244
|
+
const own = this.model.planes.filter((p) => p.element === scope || (collaboration !== undefined && p.element === collaboration));
|
|
245
|
+
if (own.length)
|
|
246
|
+
return own.map((plane) => ({ plane }));
|
|
247
|
+
if (!this.model.nodes.has(scope))
|
|
248
|
+
return [];
|
|
249
|
+
return this.model.planes
|
|
250
|
+
.filter((plane) => plane.added.some((a) => a.element === scope && a.rendering === 'expanded') || this.shapeIn(plane, scope)?.expanded)
|
|
251
|
+
.map((plane) => ({ plane, host: scope }));
|
|
252
|
+
}
|
|
253
|
+
/** The innermost lane holding `element`. */
|
|
254
|
+
laneOf(element) {
|
|
255
|
+
const holding = this.model.lanes.filter((l) => l.refs.has(element));
|
|
256
|
+
return holding.find((l) => !holding.some((other) => other.parent === l.id)) ?? holding[0];
|
|
257
|
+
}
|
|
258
|
+
/** The lane chain from `lane` out to the outermost one. */
|
|
259
|
+
laneChain(lane) {
|
|
260
|
+
const chain = [lane];
|
|
261
|
+
let at = lane;
|
|
262
|
+
while (at.parent !== undefined) {
|
|
263
|
+
const next = this.model.lanes.find((l) => l.id === at.parent);
|
|
264
|
+
if (!next)
|
|
265
|
+
break;
|
|
266
|
+
chain.push(next);
|
|
267
|
+
at = next;
|
|
268
|
+
}
|
|
269
|
+
return chain;
|
|
270
|
+
}
|
|
271
|
+
placementFor(node, neighbours, target) {
|
|
272
|
+
const { plane } = target;
|
|
273
|
+
if (target.host !== undefined) {
|
|
274
|
+
const box = this.boxIn(plane, target.host);
|
|
275
|
+
const hostShape = this.shapeIn(plane, target.host);
|
|
276
|
+
return { plane, region: subProcessInside(box), inner: box, containers: [...(hostShape ? [hostShape] : []), ...this.enclosing(plane, box)] };
|
|
277
|
+
}
|
|
278
|
+
const participant = this.model.participantOf.get(node.scope);
|
|
279
|
+
const pool = participant !== undefined ? this.shapeIn(plane, participant) : undefined;
|
|
280
|
+
const lane = neighbours.map((n) => this.laneOf(n)).find((l) => l !== undefined);
|
|
281
|
+
const laneShape = lane ? this.shapeIn(plane, lane.id) : undefined;
|
|
282
|
+
if (lane && laneShape) {
|
|
283
|
+
const chain = this.laneChain(lane).map((l) => this.shapeIn(plane, l.id)).filter((s) => s !== undefined);
|
|
284
|
+
return { plane, region: inset(laneShape.bounds.now), inner: laneShape.bounds.now, containers: [...chain, ...(pool ? [pool] : [])], lanes: this.laneChain(lane) };
|
|
285
|
+
}
|
|
286
|
+
if (pool)
|
|
287
|
+
return { plane, region: poolInside(pool.bounds.now), inner: pool.bounds.now, containers: [pool] };
|
|
288
|
+
return { plane, containers: [] };
|
|
289
|
+
}
|
|
290
|
+
/** The expanded sub-processes, lanes and pools around `box` in `plane`, innermost first: they grow with it. */
|
|
291
|
+
enclosing(plane, box) {
|
|
292
|
+
return plane.shapes
|
|
293
|
+
.filter((s) => (s.expanded || this.isContainerShape(s)) && inside(box, s.bounds.now) && !sameBox(s.bounds.now, box))
|
|
294
|
+
.sort((a, b) => a.bounds.now.w * a.bounds.now.h - b.bounds.now.w * b.bounds.now.h);
|
|
295
|
+
}
|
|
296
|
+
isContainerShape(shape) {
|
|
297
|
+
return this.model.lanes.some((l) => l.id === shape.element) || [...this.model.participantOf.values()].includes(shape.element);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* What a new node in `placement` must not overlap: every shape but its containers, every
|
|
301
|
+
* shape and edge label, and every segment of an edge that keeps its route (a rewired
|
|
302
|
+
* flow's old route is about to go).
|
|
303
|
+
*/
|
|
304
|
+
obstacles(placement, node) {
|
|
305
|
+
const skip = new Set(placement.containers.map((s) => s.element));
|
|
306
|
+
const ancestors = new Set();
|
|
307
|
+
for (let scope = node.scope; this.model.nodes.has(scope); scope = this.model.nodes.get(scope).scope)
|
|
308
|
+
ancestors.add(scope);
|
|
309
|
+
const { plane } = placement;
|
|
310
|
+
const keptEdges = plane.edges.filter((e) => !this.rewired.has(e.element));
|
|
311
|
+
return [
|
|
312
|
+
...plane.shapes.filter((s) => !skip.has(s.element) && !ancestors.has(s.element) && !this.isContainerShape(s)).map((s) => s.bounds.now),
|
|
313
|
+
...plane.added.filter((a) => !ancestors.has(a.element)).map((a) => a.box),
|
|
314
|
+
...plane.shapes.flatMap((s) => s.labels.map((l) => l.now)),
|
|
315
|
+
...plane.edges.flatMap((e) => e.labels.map((l) => l.now)),
|
|
316
|
+
...keptEdges.flatMap((e) => e.waypoints.slice(1).map((p, k) => segmentBox(e.waypoints[k].now, p.now))),
|
|
317
|
+
];
|
|
318
|
+
}
|
|
319
|
+
free(box, region, obstacles) {
|
|
320
|
+
if (region && !inside(box, region))
|
|
321
|
+
return false;
|
|
322
|
+
return !obstacles.some((o) => overlaps(box, o, CLEARANCE));
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* How far right of `p` its new successor goes: as far as its nearest successor already
|
|
326
|
+
* drawn to its right, so a new branch lines up with the existing ones (bpmn-js
|
|
327
|
+
* `getFlowNodeDistance`), up to three times `H_DIST`; `H_DIST` when it has none.
|
|
328
|
+
*/
|
|
329
|
+
distanceAfter(plane, owner, p, except) {
|
|
330
|
+
const gaps = [...this.model.flows.values()]
|
|
331
|
+
.filter((f) => f.source === owner && f.target !== except)
|
|
332
|
+
.map((f) => this.boxIn(plane, f.target))
|
|
333
|
+
.filter((b) => b !== undefined && b.x >= right(p))
|
|
334
|
+
.map((b) => b.x - right(p));
|
|
335
|
+
return gaps.length ? Math.min(...gaps, 3 * H_DIST) : H_DIST;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Where a new boundary event sits on `host`: the first slot, bottom face first, then
|
|
339
|
+
* top, right and left, that overlaps nothing but the host. Only when every face is
|
|
340
|
+
* full does it share a bottom slot, which is then the least crowded one.
|
|
341
|
+
*/
|
|
342
|
+
boundarySpot(placement, node, host, size) {
|
|
343
|
+
const { w, h } = size;
|
|
344
|
+
const others = this.obstacles(placement, node).filter((o) => !sameBox(o, host));
|
|
345
|
+
// The designer's usual slots first, then every twelfth of the face.
|
|
346
|
+
const fractions = [...new Set([2 / 3, 1 / 3, 1 / 2, 5 / 6, 1 / 6, ...Array.from({ length: 11 }, (_, k) => (k + 1) / 12)])];
|
|
347
|
+
const at = (p) => ({ x: Math.round(p.x - w / 2), y: Math.round(p.y - h / 2), w, h });
|
|
348
|
+
const bottomSlots = fractions.map((f) => at({ x: host.x + host.w * f, y: bottom(host) }));
|
|
349
|
+
const candidates = [
|
|
350
|
+
...bottomSlots,
|
|
351
|
+
...fractions.map((f) => at({ x: host.x + host.w * f, y: host.y })),
|
|
352
|
+
...fractions.map((f) => at({ x: right(host), y: host.y + host.h * f })),
|
|
353
|
+
...fractions.map((f) => at({ x: host.x, y: host.y + host.h * f })),
|
|
354
|
+
];
|
|
355
|
+
const free = candidates.find((c) => !others.some((o) => overlaps(c, o, 2)));
|
|
356
|
+
if (free)
|
|
357
|
+
return free;
|
|
358
|
+
const crowding = (c) => others.filter((o) => overlaps(c, o, 2)).length;
|
|
359
|
+
return bottomSlots.reduce((a, b) => (crowding(b) < crowding(a) ? b : a));
|
|
360
|
+
}
|
|
361
|
+
placeNode(node) {
|
|
362
|
+
const incoming = [...this.model.flows.values()].filter((f) => f.target === node.id).map((f) => f.source);
|
|
363
|
+
const outgoing = [...this.model.flows.values()].filter((f) => f.source === node.id).map((f) => f.target);
|
|
364
|
+
const neighbours = [...incoming, ...outgoing, ...(node.attachedTo !== undefined ? [node.attachedTo] : [])].filter((n) => this.drawn(n));
|
|
365
|
+
for (const target of this.planesFor(node.scope))
|
|
366
|
+
this.placeIn(this.placementFor(node, neighbours, target), node, incoming, outgoing);
|
|
367
|
+
}
|
|
368
|
+
placeIn(placement, node, incoming, outgoing) {
|
|
369
|
+
const { plane } = placement;
|
|
370
|
+
const rendering = node.hasChildren ? 'expanded' : 'collapsed';
|
|
371
|
+
const { w, h } = sizeOf(node.local, rendering);
|
|
372
|
+
if (node.attachedTo !== undefined) {
|
|
373
|
+
const host = this.boxIn(plane, node.attachedTo);
|
|
374
|
+
if (!host)
|
|
375
|
+
return;
|
|
376
|
+
this.add(placement, node, this.boundarySpot(placement, node, host, { w, h }), rendering);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const preds = incoming.flatMap((id) => {
|
|
380
|
+
const box = this.boxIn(plane, id);
|
|
381
|
+
return box ? [{ id, box }] : [];
|
|
382
|
+
});
|
|
383
|
+
const succs = outgoing.map((n) => this.boxIn(plane, n)).filter((b) => b !== undefined);
|
|
384
|
+
const obstacles = this.obstacles(placement, node);
|
|
385
|
+
const region = placement.region;
|
|
386
|
+
let anchor;
|
|
387
|
+
if (preds.length) {
|
|
388
|
+
const p = preds.reduce((a, b) => (right(b.box) > right(a.box) || (right(b.box) === right(a.box) && b.id < a.id) ? b : a));
|
|
389
|
+
anchor = { x: right(p.box) + this.distanceAfter(plane, p.id, p.box, node.id), y: cy(p.box) - h / 2 };
|
|
390
|
+
}
|
|
391
|
+
else if (succs.length) {
|
|
392
|
+
const s = succs.reduce((a, b) => (b.x < a.x ? b : a));
|
|
393
|
+
anchor = { x: s.x - H_DIST - w, y: cy(s) - h / 2 };
|
|
394
|
+
}
|
|
395
|
+
else if (region) {
|
|
396
|
+
const within = obstacles.filter((o) => inside(o, region));
|
|
397
|
+
anchor = within.length
|
|
398
|
+
? { x: Math.min(...within.map((o) => o.x)), y: Math.max(...within.map(bottom)) + V_GAP }
|
|
399
|
+
: { x: region.x, y: region.y + region.h / 2 - h / 2 };
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
anchor = obstacles.length
|
|
403
|
+
? { x: Math.min(...obstacles.map((o) => o.x)), y: Math.max(...obstacles.map(bottom)) + 2 * V_GAP }
|
|
404
|
+
: { ...EMPTY_CANVAS_ORIGIN };
|
|
405
|
+
}
|
|
406
|
+
const pitch = Math.max(h, 80) + V_GAP;
|
|
407
|
+
const rows = [0, ...Array.from({ length: MAX_ROWS }, (_, k) => [k + 1, -(k + 1)]).flat()];
|
|
408
|
+
for (let column = 0; column <= (region ? MAX_COLUMNS : 0); column++) {
|
|
409
|
+
for (const row of region || column > 0 ? rows : rows.filter((r) => r >= 0)) {
|
|
410
|
+
const box = { x: Math.round(anchor.x + column * (w + H_DIST)), y: Math.round(anchor.y + row * pitch), w, h };
|
|
411
|
+
if (this.free(box, region, obstacles)) {
|
|
412
|
+
this.add(placement, node, box, rendering);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (!region) {
|
|
418
|
+
// An open canvas always has room below.
|
|
419
|
+
const y = Math.max(...obstacles.map(bottom)) + V_GAP;
|
|
420
|
+
this.add(placement, node, { x: Math.round(anchor.x), y: Math.round(y), w, h }, rendering);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
this.growInto(placement, node, anchor, { w, h }, obstacles, rendering);
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* The container is full: put the node in the first row under the container's content,
|
|
427
|
+
* then grow the container (and those around it) and move everything under its old
|
|
428
|
+
* bottom down, together, by the same offset.
|
|
429
|
+
*/
|
|
430
|
+
growInto(placement, node, anchor, size, obstacles, rendering) {
|
|
431
|
+
const region = placement.region;
|
|
432
|
+
const within = obstacles.filter((o) => overlaps(o, region, 0));
|
|
433
|
+
const y = Math.max(region.y, ...within.map((o) => bottom(o) + V_GAP));
|
|
434
|
+
const x = Math.min(Math.max(Math.round(anchor.x), region.x), Math.max(region.x, right(region) - size.w));
|
|
435
|
+
const box = { x, y: Math.round(y), w: size.w, h: size.h };
|
|
436
|
+
const dy = Math.max(0, Math.ceil(bottom(box) - bottom(region)));
|
|
437
|
+
const dx = Math.max(0, Math.ceil(right(box) - right(region)));
|
|
438
|
+
if (placement.inner && (dy > 0 || dx > 0))
|
|
439
|
+
this.grow(placement, placement.inner, dy, dx);
|
|
440
|
+
this.add(placement, node, box, rendering);
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Grow `inner` by `dy` at the bottom and `dx` at the right, whether it is an existing
|
|
444
|
+
* container or a sub-process this edit added. The containers drawn around it grow with
|
|
445
|
+
* it. Every other shape, label and waypoint at or under its old bottom moves down by
|
|
446
|
+
* `dy`, and at or right of its old right edge moves right by `dx`, so the diagram
|
|
447
|
+
* around it shifts as one block; a pool's lanes widen together. A boundary event on a
|
|
448
|
+
* growing host's bottom or right face moves with that face.
|
|
449
|
+
*/
|
|
450
|
+
grow(placement, inner, dy, dx) {
|
|
451
|
+
const { plane } = placement;
|
|
452
|
+
const line = bottom(inner);
|
|
453
|
+
const edge = right(inner);
|
|
454
|
+
const innerAdded = plane.added.find((a) => a.box === inner);
|
|
455
|
+
const around = new Set([...placement.containers, ...this.enclosing(plane, inner)].filter((s) => s.bounds.now !== inner || !innerAdded));
|
|
456
|
+
const pool = [...around].find((s) => [...this.model.participantOf.values()].includes(s.element));
|
|
457
|
+
const isLane = (s) => this.model.lanes.some((l) => l.id === s.element);
|
|
458
|
+
// Which shapes grow, and the box each had: their boundary events follow the face.
|
|
459
|
+
const grows = new Map();
|
|
460
|
+
if (innerAdded)
|
|
461
|
+
grows.set(innerAdded.element, { ...inner });
|
|
462
|
+
for (const shape of plane.shapes) {
|
|
463
|
+
const b = shape.bounds.now;
|
|
464
|
+
const taller = dy > 0 && around.has(shape) && bottom(b) >= line;
|
|
465
|
+
const wider = dx > 0 && (around.has(shape) || (pool !== undefined && isLane(shape) && inside(b, pool.bounds.now))) && right(b) >= edge - 1;
|
|
466
|
+
if (taller || wider)
|
|
467
|
+
grows.set(shape.element, { ...b });
|
|
468
|
+
}
|
|
469
|
+
const face = new Map();
|
|
470
|
+
const boundaryShift = (element, b) => {
|
|
471
|
+
const host = this.model.nodes.get(element)?.attachedTo;
|
|
472
|
+
const was = host !== undefined ? grows.get(host) : undefined;
|
|
473
|
+
if (!was)
|
|
474
|
+
return undefined;
|
|
475
|
+
return { dy: Math.abs(cy(b) - bottom(was)) < 1 ? dy : 0, dx: Math.abs(cx(b) - right(was)) < 1 ? dx : 0 };
|
|
476
|
+
};
|
|
477
|
+
for (const shape of plane.shapes) {
|
|
478
|
+
const shift = boundaryShift(shape.element, shape.bounds.now);
|
|
479
|
+
if (shift)
|
|
480
|
+
face.set(shape.element, shift);
|
|
481
|
+
}
|
|
482
|
+
for (const added of plane.added) {
|
|
483
|
+
const shift = boundaryShift(added.element, added.box);
|
|
484
|
+
if (shift)
|
|
485
|
+
face.set(added.element, shift);
|
|
486
|
+
}
|
|
487
|
+
if (innerAdded) {
|
|
488
|
+
inner.h += dy;
|
|
489
|
+
inner.w += dx;
|
|
490
|
+
}
|
|
491
|
+
const shiftOf = (element, b) => face.get(element) ?? { dy: dy > 0 && b.y >= line ? dy : 0, dx: dx > 0 && b.x >= edge ? dx : 0 };
|
|
492
|
+
// How far each shape moved, so an edge can follow its two ends.
|
|
493
|
+
const shifted = new Map();
|
|
494
|
+
for (const shape of plane.shapes) {
|
|
495
|
+
const b = shape.bounds.now;
|
|
496
|
+
const was = grows.get(shape.element);
|
|
497
|
+
if (was) {
|
|
498
|
+
if (dy > 0 && around.has(shape) && bottom(was) >= line)
|
|
499
|
+
b.h += dy;
|
|
500
|
+
if (dx > 0 && right(was) >= edge - 1)
|
|
501
|
+
b.w += dx;
|
|
502
|
+
this.note(this.report.resized, shape.element);
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
const shift = shiftOf(shape.element, b);
|
|
506
|
+
if (!shift.dy && !shift.dx)
|
|
507
|
+
continue;
|
|
508
|
+
shifted.set(shape.element, shift);
|
|
509
|
+
for (const g of [shape.bounds, ...shape.labels]) {
|
|
510
|
+
g.now.y += shift.dy;
|
|
511
|
+
g.now.x += shift.dx;
|
|
512
|
+
}
|
|
513
|
+
this.note(this.report.moved, shape.element);
|
|
514
|
+
}
|
|
515
|
+
for (const added of plane.added) {
|
|
516
|
+
if (added.box === inner)
|
|
517
|
+
continue;
|
|
518
|
+
const shift = shiftOf(added.element, added.box);
|
|
519
|
+
if (shift.dy || shift.dx)
|
|
520
|
+
shifted.set(added.element, shift);
|
|
521
|
+
added.box.y += shift.dy;
|
|
522
|
+
added.box.x += shift.dx;
|
|
523
|
+
}
|
|
524
|
+
// A sequence flow follows its ends: moved together with both when they moved by the
|
|
525
|
+
// same offset, left alone when neither moved, and routed again between their new
|
|
526
|
+
// boxes when only one did, so no edge comes loose from a shape. An edge that is not a
|
|
527
|
+
// sequence flow shifts point by point, by the same rule as the shapes.
|
|
528
|
+
const still = { dy: 0, dx: 0 };
|
|
529
|
+
const same = (a, b) => a.dy === b.dy && a.dx === b.dx;
|
|
530
|
+
for (const e of plane.edges) {
|
|
531
|
+
const flow = this.model.flows.get(e.element);
|
|
532
|
+
const s = flow && this.boxIn(plane, flow.source);
|
|
533
|
+
const t = flow && this.boxIn(plane, flow.target);
|
|
534
|
+
if (flow && s && t) {
|
|
535
|
+
const sourceShift = shifted.get(flow.source) ?? still;
|
|
536
|
+
const targetShift = shifted.get(flow.target) ?? still;
|
|
537
|
+
if (same(sourceShift, targetShift)) {
|
|
538
|
+
if (same(sourceShift, still))
|
|
539
|
+
continue;
|
|
540
|
+
for (const g of [...e.waypoints, ...e.labels]) {
|
|
541
|
+
g.now.y += sourceShift.dy;
|
|
542
|
+
g.now.x += sourceShift.dx;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
e.route = orthogonal(s, t, this.model.nodes.get(flow.source)?.local ?? '', this.model.nodes.get(flow.target)?.local ?? '');
|
|
547
|
+
for (const label of e.labels)
|
|
548
|
+
label.now = besideMiddle(e.route, label.now);
|
|
549
|
+
}
|
|
550
|
+
this.note(this.report.moved, e.element);
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
let moved = false;
|
|
554
|
+
for (const point of e.waypoints) {
|
|
555
|
+
const shift = { dy: dy > 0 && point.now.y >= line ? dy : 0, dx: dx > 0 && point.now.x >= edge ? dx : 0 };
|
|
556
|
+
if (!shift.dy && !shift.dx)
|
|
557
|
+
continue;
|
|
558
|
+
point.now.y += shift.dy;
|
|
559
|
+
point.now.x += shift.dx;
|
|
560
|
+
moved = true;
|
|
561
|
+
}
|
|
562
|
+
for (const label of e.labels) {
|
|
563
|
+
if (dy > 0 && label.now.y >= line)
|
|
564
|
+
label.now.y += dy;
|
|
565
|
+
if (dx > 0 && label.now.x >= edge)
|
|
566
|
+
label.now.x += dx;
|
|
567
|
+
}
|
|
568
|
+
if (moved)
|
|
569
|
+
this.note(this.report.moved, e.element);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
note(list, id) {
|
|
573
|
+
if (!list.includes(id))
|
|
574
|
+
list.push(id);
|
|
575
|
+
}
|
|
576
|
+
add(placement, node, box, rendering) {
|
|
577
|
+
placement.plane.added.push({ element: node.id, box, rendering });
|
|
578
|
+
this.note(this.rescoped.has(node.id) ? this.report.moved : this.report.placed, node.id);
|
|
579
|
+
// Listed in its lane and every lane around it, as bpmn-js lists a node, unless the
|
|
580
|
+
// edit already put it in one.
|
|
581
|
+
if (placement.lanes && !this.model.lanes.some((l) => l.refs.has(node.id))) {
|
|
582
|
+
for (const lane of placement.lanes)
|
|
583
|
+
this.laneRefs.push({ lane, node: node.id });
|
|
584
|
+
for (const lane of placement.lanes)
|
|
585
|
+
lane.refs.add(node.id);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Route `flow` in every plane that draws both of its ends: a new route for each edge
|
|
590
|
+
* it already has there (a kept flow the edit rewired), a new edge where it has none.
|
|
591
|
+
* A rewired flow whose edge the merge dropped with a deleted endpoint is routed afresh
|
|
592
|
+
* and still reported as rerouted. A rerouted edge's label (a branch's `Yes`) moves
|
|
593
|
+
* beside the middle of its new route.
|
|
594
|
+
*/
|
|
595
|
+
routeFlow(flow, change) {
|
|
596
|
+
let drawn = false;
|
|
597
|
+
for (const plane of this.model.planes) {
|
|
598
|
+
const s = this.boxIn(plane, flow.source);
|
|
599
|
+
const t = this.boxIn(plane, flow.target);
|
|
600
|
+
if (!s || !t)
|
|
601
|
+
continue;
|
|
602
|
+
const route = orthogonal(s, t, this.model.nodes.get(flow.source)?.local ?? '', this.model.nodes.get(flow.target)?.local ?? '');
|
|
603
|
+
const existing = plane.edges.filter((e) => e.element === flow.id);
|
|
604
|
+
for (const edge of existing) {
|
|
605
|
+
edge.route = route;
|
|
606
|
+
for (const label of edge.labels)
|
|
607
|
+
label.now = besideMiddle(route, label.now);
|
|
608
|
+
}
|
|
609
|
+
if (!existing.length)
|
|
610
|
+
plane.addedEdges.push({ element: flow.id, route });
|
|
611
|
+
drawn = true;
|
|
612
|
+
}
|
|
613
|
+
if (drawn)
|
|
614
|
+
(change === 'rewired' ? this.report.rerouted : this.report.routed).push(flow.id);
|
|
615
|
+
}
|
|
616
|
+
get laneAdditions() {
|
|
617
|
+
return this.laneRefs;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
function inset(b) {
|
|
621
|
+
return { x: b.x + INSET, y: b.y + INSET, w: b.w - 2 * INSET, h: b.h - 2 * INSET };
|
|
622
|
+
}
|
|
623
|
+
/** Inside a horizontal pool: past its name band. */
|
|
624
|
+
function poolInside(b) {
|
|
625
|
+
return inset({ x: b.x + POOL_LABEL_BAND, y: b.y, w: b.w - POOL_LABEL_BAND, h: b.h });
|
|
626
|
+
}
|
|
627
|
+
function subProcessInside(b) {
|
|
628
|
+
return { x: b.x + SUB_PROCESS_PAD.x, y: b.y + SUB_PROCESS_PAD.top, w: b.w - 2 * SUB_PROCESS_PAD.x, h: b.h - SUB_PROCESS_PAD.top - SUB_PROCESS_PAD.bottom };
|
|
629
|
+
}
|
|
630
|
+
/** The zero-thickness box a straight edge segment from `a` to `b` covers. */
|
|
631
|
+
function segmentBox(a, b) {
|
|
632
|
+
return { x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), w: Math.abs(a.x - b.x), h: Math.abs(a.y - b.y) };
|
|
633
|
+
}
|
|
634
|
+
/** Where a flow's label goes on `route`: above the middle segment's midpoint, or right of it when that segment is vertical. */
|
|
635
|
+
function besideMiddle(route, label) {
|
|
636
|
+
const k = Math.max(0, Math.floor((route.length - 1) / 2));
|
|
637
|
+
const a = route[k];
|
|
638
|
+
const b = route[k + 1] ?? a;
|
|
639
|
+
const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
640
|
+
const vertical = a.x === b.x && a.y !== b.y;
|
|
641
|
+
return vertical
|
|
642
|
+
? { ...label, x: Math.round(mid.x + 6), y: Math.round(mid.y - label.h / 2) }
|
|
643
|
+
: { ...label, x: Math.round(mid.x - label.w / 2), y: Math.round(mid.y - label.h - 4) };
|
|
644
|
+
}
|
|
645
|
+
const sameBox = (a, b) => a.x === b.x && a.y === b.y && a.w === b.w && a.h === b.h;
|
|
646
|
+
/** Orthogonal waypoints from `s` to `t`, docked on the faces that look at each other. */
|
|
647
|
+
function orthogonal(s, t, sourceLocal, targetLocal) {
|
|
648
|
+
const r = (p) => ({ x: Math.round(p.x), y: Math.round(p.y) });
|
|
649
|
+
if (t.x >= right(s)) {
|
|
650
|
+
if (Math.abs(cy(s) - cy(t)) < 1)
|
|
651
|
+
return [r({ x: right(s), y: cy(s) }), r({ x: t.x, y: cy(t) })];
|
|
652
|
+
const down = cy(t) > cy(s);
|
|
653
|
+
if (GATEWAYS.has(sourceLocal) || sourceLocal === 'boundaryEvent') {
|
|
654
|
+
return [r({ x: cx(s), y: down ? bottom(s) : s.y }), r({ x: cx(s), y: cy(t) }), r({ x: t.x, y: cy(t) })];
|
|
655
|
+
}
|
|
656
|
+
if (GATEWAYS.has(targetLocal)) {
|
|
657
|
+
return [r({ x: right(s), y: cy(s) }), r({ x: cx(t), y: cy(s) }), r({ x: cx(t), y: down ? t.y : bottom(t) })];
|
|
658
|
+
}
|
|
659
|
+
const mx = (right(s) + t.x) / 2;
|
|
660
|
+
return [r({ x: right(s), y: cy(s) }), r({ x: mx, y: cy(s) }), r({ x: mx, y: cy(t) }), r({ x: t.x, y: cy(t) })];
|
|
661
|
+
}
|
|
662
|
+
if (right(t) <= s.x) {
|
|
663
|
+
// Back to an earlier column: under both shapes.
|
|
664
|
+
const y = Math.max(bottom(s), bottom(t)) + V_GAP;
|
|
665
|
+
return [r({ x: cx(s), y: bottom(s) }), r({ x: cx(s), y }), r({ x: cx(t), y }), r({ x: cx(t), y: bottom(t) })];
|
|
666
|
+
}
|
|
667
|
+
// The two share a column: one vertical segment, at an x both span.
|
|
668
|
+
const x = cx(t) >= s.x && cx(t) <= right(s) ? cx(t) : cx(s) >= t.x && cx(s) <= right(t) ? cx(s) : (Math.max(s.x, t.x) + Math.min(right(s), right(t))) / 2;
|
|
669
|
+
return cy(t) > cy(s)
|
|
670
|
+
? [r({ x, y: bottom(s) }), r({ x, y: t.y })]
|
|
671
|
+
: [r({ x, y: s.y }), r({ x, y: bottom(t) })];
|
|
672
|
+
}
|
|
673
|
+
/** The order to place added nodes in: each once the added predecessors it has are placed, by id. */
|
|
674
|
+
function waves(nodes, flows) {
|
|
675
|
+
const pending = new Map(nodes.map((n) => [n.id, n]));
|
|
676
|
+
const order = [];
|
|
677
|
+
const blockers = (n) => [
|
|
678
|
+
...flows.filter((f) => f.target === n.id && pending.has(f.source) && f.source !== n.id).map((f) => f.source),
|
|
679
|
+
...(n.attachedTo !== undefined && pending.has(n.attachedTo) ? [n.attachedTo] : []),
|
|
680
|
+
// A child of a new sub-process waits for its parent's box.
|
|
681
|
+
...(pending.has(n.scope) ? [n.scope] : []),
|
|
682
|
+
];
|
|
683
|
+
while (pending.size) {
|
|
684
|
+
const ready = [...pending.values()].filter((n) => blockers(n).length === 0).sort((a, b) => a.id.localeCompare(b.id));
|
|
685
|
+
// A cycle among added nodes: break it at the smallest id.
|
|
686
|
+
const wave = ready.length ? ready : [[...pending.values()].sort((a, b) => a.id.localeCompare(b.id))[0]];
|
|
687
|
+
for (const n of wave) {
|
|
688
|
+
order.push(n);
|
|
689
|
+
pending.delete(n.id);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
return order;
|
|
693
|
+
}
|
|
694
|
+
const fmt = (n) => String(Math.round(n * 100) / 100);
|
|
695
|
+
/** `<dc:Bounds …>` with its four numbers replaced, every other attribute kept as written. */
|
|
696
|
+
function rewriteBounds(xml, g, fields) {
|
|
697
|
+
let tag = xml.slice(g.range.start, g.range.end);
|
|
698
|
+
const names = { x: 'x', y: 'y', w: 'width', h: 'height' };
|
|
699
|
+
for (const field of fields) {
|
|
700
|
+
if (g.now[field] === g.read[field])
|
|
701
|
+
continue;
|
|
702
|
+
const name = names[field];
|
|
703
|
+
tag = tag.replace(new RegExp(`(\\s${name}\\s*=\\s*)("[^"]*"|'[^']*')`), (_all, lead) => `${lead}"${fmt(g.now[field])}"`);
|
|
704
|
+
}
|
|
705
|
+
return tag;
|
|
706
|
+
}
|
|
707
|
+
/** Where the whitespace before `at` starts, back to and including the line break. */
|
|
708
|
+
function lineStart(xml, at) {
|
|
709
|
+
let from = at;
|
|
710
|
+
while (from > 0 && /\s/.test(xml[from - 1]))
|
|
711
|
+
from--;
|
|
712
|
+
return from;
|
|
713
|
+
}
|
|
714
|
+
function lineIndent(xml, at) {
|
|
715
|
+
let from = at;
|
|
716
|
+
while (from > 0 && (xml[from - 1] === ' ' || xml[from - 1] === '\t'))
|
|
717
|
+
from--;
|
|
718
|
+
return xml.slice(from, at);
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Draw the flow nodes and sequence flows the edit added, and only those, into the
|
|
722
|
+
* document's existing diagram. See the file header for the rules.
|
|
723
|
+
*/
|
|
724
|
+
export function placeAddedElements(xml, options) {
|
|
725
|
+
const empty = { placed: [], routed: [], rerouted: [], resized: [], moved: [] };
|
|
726
|
+
const [root] = parseXmlRanges(xml);
|
|
727
|
+
if (!root)
|
|
728
|
+
return { xml, report: empty };
|
|
729
|
+
const model = readModel(xml, root);
|
|
730
|
+
if (model.planes.length === 0)
|
|
731
|
+
return { xml, report: empty };
|
|
732
|
+
// A node moved into another scope is drawn again inside it, when that scope is drawn
|
|
733
|
+
// open (an expanded or new sub-process, or a process with its own plane): its old shape
|
|
734
|
+
// goes, and with it the DI id, which the new shape takes back.
|
|
735
|
+
const opensIn = (scope) => model.planes.some((p) => p.element === scope || p.element === model.collaborationOf.get(scope) || p.shapes.some((s) => s.element === scope && s.expanded)) ||
|
|
736
|
+
(options.isAdded(scope) && model.nodes.get(scope)?.hasChildren === true);
|
|
737
|
+
const rescoped = new Set([...(options.rescoped ?? [])].filter((id) => {
|
|
738
|
+
const node = model.nodes.get(id);
|
|
739
|
+
return node !== undefined && !options.isAdded(id) && opensIn(node.scope);
|
|
740
|
+
}));
|
|
741
|
+
const dropped = [];
|
|
742
|
+
for (const plane of model.planes) {
|
|
743
|
+
for (const shape of plane.shapes.filter((s) => rescoped.has(s.element))) {
|
|
744
|
+
dropped.push(shape.range);
|
|
745
|
+
if (shape.id !== undefined)
|
|
746
|
+
model.ids.delete(shape.id);
|
|
747
|
+
plane.shapes.splice(plane.shapes.indexOf(shape), 1);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
const placer = new Placer(model, options.rewired ?? new Set(), rescoped);
|
|
751
|
+
const drawnAnywhere = (element) => model.planes.some((p) => p.shapes.some((s) => s.element === element));
|
|
752
|
+
const toPlace = [...model.nodes.values()].filter((n) => (options.isAdded(n.id) && !drawnAnywhere(n.id)) || rescoped.has(n.id));
|
|
753
|
+
for (const node of waves(toPlace, [...model.flows.values()]))
|
|
754
|
+
placer.placeNode(node);
|
|
755
|
+
for (const flow of model.flows.values()) {
|
|
756
|
+
const rewired = options.rewired?.has(flow.id) === true || rescoped.has(flow.source) || rescoped.has(flow.target);
|
|
757
|
+
const edged = model.planes.some((p) => p.edges.some((e) => e.element === flow.id));
|
|
758
|
+
if (rewired || (!edged && options.isAdded(flow.id)))
|
|
759
|
+
placer.routeFlow(flow, rewired ? 'rewired' : 'added');
|
|
760
|
+
}
|
|
761
|
+
const report = placer.report;
|
|
762
|
+
if (!report.placed.length && !report.routed.length && !report.rerouted.length && !dropped.length)
|
|
763
|
+
return { xml, report };
|
|
764
|
+
// Write: replace changed geometry in place, insert new DI and lane refs.
|
|
765
|
+
const bpmndi = prefixFor(root, NS.bpmndi) ?? 'bpmndi';
|
|
766
|
+
const dc = prefixFor(root, NS.dc) ?? 'dc';
|
|
767
|
+
let di = prefixFor(root, NS.di);
|
|
768
|
+
const declare = [];
|
|
769
|
+
if (prefixFor(root, NS.bpmndi) === undefined)
|
|
770
|
+
declare.push(`xmlns:bpmndi="${NS.bpmndi}"`);
|
|
771
|
+
if (prefixFor(root, NS.dc) === undefined)
|
|
772
|
+
declare.push(`xmlns:dc="${NS.dc}"`);
|
|
773
|
+
if (di === undefined && (report.routed.length || report.rerouted.length)) {
|
|
774
|
+
di = Object.hasOwn(root.attributes, 'xmlns:di') ? 'omgdi' : 'di';
|
|
775
|
+
declare.push(`xmlns:${di}="${NS.di}"`);
|
|
776
|
+
}
|
|
777
|
+
di ??= 'di';
|
|
778
|
+
const shapeIds = idPattern(model.planes.flatMap((p) => p.shapes), { prefix: '', suffix: '_di' });
|
|
779
|
+
const edgeIds = idPattern(model.planes.flatMap((p) => p.edges), shapeIds);
|
|
780
|
+
const fresh = (pattern, element) => {
|
|
781
|
+
let id = `${pattern.prefix}${element}${pattern.suffix}`;
|
|
782
|
+
for (let k = 2; model.ids.has(id); k++)
|
|
783
|
+
id = `${pattern.prefix}${element}${pattern.suffix}_${k}`;
|
|
784
|
+
model.ids.add(id);
|
|
785
|
+
return id;
|
|
786
|
+
};
|
|
787
|
+
const point = (p) => `<${di}:waypoint x="${fmt(p.x)}" y="${fmt(p.y)}" />`;
|
|
788
|
+
// DI ids for the shapes this run adds, minted up front so an edge can name its ends.
|
|
789
|
+
const addedIds = new Map(model.planes.map((plane) => [plane, new Map(plane.added.map((a) => [a.element, fresh(shapeIds, a.element)]))]));
|
|
790
|
+
const shapeIdIn = (plane, element) => addedIds.get(plane)?.get(element) ?? plane.shapes.find((s) => s.element === element)?.id;
|
|
791
|
+
const edits = [];
|
|
792
|
+
for (const range of dropped)
|
|
793
|
+
edits.push({ at: lineStart(xml, range.start), end: range.end, text: '' });
|
|
794
|
+
for (const plane of model.planes) {
|
|
795
|
+
for (const shape of plane.shapes) {
|
|
796
|
+
for (const g of [shape.bounds, ...shape.labels]) {
|
|
797
|
+
if (!sameBox(g.now, g.read))
|
|
798
|
+
edits.push({ at: g.range.start, end: g.range.end, text: rewriteBounds(xml, g, ['x', 'y', 'w', 'h']) });
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
for (const edge of plane.edges) {
|
|
802
|
+
if (edge.route) {
|
|
803
|
+
// A `sourceElement`/`targetElement` naming the old end's shape follows the new one.
|
|
804
|
+
const flow = model.flows.get(edge.element);
|
|
805
|
+
const ends = {
|
|
806
|
+
sourceElement: flow && shapeIdIn(plane, flow.source),
|
|
807
|
+
targetElement: flow && shapeIdIn(plane, flow.target),
|
|
808
|
+
};
|
|
809
|
+
const openTag = xml.slice(edge.range.start, edge.range.innerStart);
|
|
810
|
+
const retagged = openTag.replace(/(\s)(sourceElement|targetElement)(\s*=\s*)("[^"]*"|'[^']*')/g, (_all, space, name, eq) => {
|
|
811
|
+
const id = name === 'sourceElement' ? ends.sourceElement : ends.targetElement;
|
|
812
|
+
return id === undefined ? '' : `${space}${name}${eq}"${id}"`;
|
|
813
|
+
});
|
|
814
|
+
if (retagged !== openTag)
|
|
815
|
+
edits.push({ at: edge.range.start, end: edge.range.innerStart, text: retagged });
|
|
816
|
+
const first = edge.waypoints[0];
|
|
817
|
+
const last = edge.waypoints[edge.waypoints.length - 1];
|
|
818
|
+
const indent = first ? lineIndent(xml, first.range.start) : '';
|
|
819
|
+
const text = edge.route.map(point).join(indent ? `\n${indent}` : '');
|
|
820
|
+
if (first && last)
|
|
821
|
+
edits.push({ at: first.range.start, end: last.range.end, text });
|
|
822
|
+
else if (edge.range.innerStart !== edge.range.end)
|
|
823
|
+
edits.push({ at: edge.range.innerStart, end: edge.range.innerStart, text });
|
|
824
|
+
else {
|
|
825
|
+
// A self-closing edge: open it (its retagged open tag, when it has one, is replaced too).
|
|
826
|
+
const inner = lineIndent(xml, edge.range.start);
|
|
827
|
+
const open = (retagged !== openTag ? retagged : openTag).replace(/\s*\/>$/, '>');
|
|
828
|
+
const body = edge.route.map((p) => `\n${inner} ${point(p)}`).join('');
|
|
829
|
+
const at = edits.findIndex((e) => e.at === edge.range.start && e.end === edge.range.innerStart);
|
|
830
|
+
if (at >= 0)
|
|
831
|
+
edits.splice(at, 1);
|
|
832
|
+
edits.push({ at: edge.range.start, end: edge.range.end, text: `${open}${body}\n${inner}</${edge.range.name}>` });
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
for (const g of [...(edge.route ? [] : edge.waypoints), ...edge.labels]) {
|
|
836
|
+
if (!sameBox(g.now, g.read))
|
|
837
|
+
edits.push({ at: g.range.start, end: g.range.end, text: rewriteBounds(xml, g, ['x', 'y', 'w', 'h']) });
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
if (!plane.added.length && !plane.addedEdges.length)
|
|
841
|
+
continue;
|
|
842
|
+
const children = plane.range.children;
|
|
843
|
+
const indent = children.length ? lineIndent(xml, children[0].start) : `${lineIndent(xml, plane.range.start)} `;
|
|
844
|
+
const shapeXml = plane.added.map(({ element, box, rendering }) => `\n${indent}<${bpmndi}:BPMNShape id="${shapeIdIn(plane, element)}" bpmnElement="${element}"${rendering === 'expanded' ? ' isExpanded="true"' : ''}>` +
|
|
845
|
+
`\n${indent} <${dc}:Bounds x="${fmt(box.x)}" y="${fmt(box.y)}" width="${fmt(box.w)}" height="${fmt(box.h)}" />` +
|
|
846
|
+
`\n${indent}</${bpmndi}:BPMNShape>`).join('');
|
|
847
|
+
const edgeXml = plane.addedEdges.map(({ element, route }) => `\n${indent}<${bpmndi}:BPMNEdge id="${fresh(edgeIds, element)}" bpmnElement="${element}">` +
|
|
848
|
+
route.map((p) => `\n${indent} ${point(p)}`).join('') +
|
|
849
|
+
`\n${indent}</${bpmndi}:BPMNEdge>`).join('');
|
|
850
|
+
const lastShape = [...children].reverse().find((c) => c.local === 'BPMNShape');
|
|
851
|
+
const closeIndent = children.length ? xml.slice(children[children.length - 1].end, plane.range.innerEnd) : `\n${lineIndent(xml, plane.range.start)}`;
|
|
852
|
+
if (lastShape && lastShape !== children[children.length - 1]) {
|
|
853
|
+
if (shapeXml)
|
|
854
|
+
edits.push({ at: lastShape.end, end: lastShape.end, text: shapeXml });
|
|
855
|
+
if (edgeXml)
|
|
856
|
+
edits.push({ at: children[children.length - 1].end, end: children[children.length - 1].end, text: edgeXml });
|
|
857
|
+
}
|
|
858
|
+
else if (children.length) {
|
|
859
|
+
edits.push({ at: children[children.length - 1].end, end: children[children.length - 1].end, text: shapeXml + edgeXml });
|
|
860
|
+
}
|
|
861
|
+
else if (plane.range.innerStart !== plane.range.end) {
|
|
862
|
+
edits.push({ at: plane.range.innerStart, end: plane.range.innerEnd, text: shapeXml + edgeXml + closeIndent });
|
|
863
|
+
}
|
|
864
|
+
else {
|
|
865
|
+
// A self-closing plane: open it, as a self-closing lane is opened below.
|
|
866
|
+
const open = xml.slice(plane.range.start, plane.range.end).replace(/\s*\/>$/, '>');
|
|
867
|
+
edits.push({ at: plane.range.start, end: plane.range.end, text: `${open}${shapeXml}${edgeXml}${closeIndent}</${plane.range.name}>` });
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
for (const { lane, node } of placer.laneAdditions) {
|
|
871
|
+
const refs = lane.range.children.filter((c) => c.local === 'flowNodeRef');
|
|
872
|
+
const prefix = lane.range.name.includes(':') ? `${lane.range.name.split(':')[0]}:` : '';
|
|
873
|
+
const tag = `<${prefix}flowNodeRef>${node}</${prefix}flowNodeRef>`;
|
|
874
|
+
const last = refs[refs.length - 1];
|
|
875
|
+
if (last)
|
|
876
|
+
edits.push({ at: last.end, end: last.end, text: `\n${lineIndent(xml, last.start)}${tag}` });
|
|
877
|
+
else if (lane.range.innerStart !== lane.range.end) {
|
|
878
|
+
edits.push({ at: lane.range.innerStart, end: lane.range.innerStart, text: `\n${lineIndent(xml, lane.range.start)} ${tag}` });
|
|
879
|
+
}
|
|
880
|
+
else {
|
|
881
|
+
// A self-closing lane: open it.
|
|
882
|
+
const open = xml.slice(lane.range.start, lane.range.end).replace(/\s*\/>$/, '>');
|
|
883
|
+
edits.push({ at: lane.range.start, end: lane.range.end, text: `${open}\n${lineIndent(xml, lane.range.start)} ${tag}\n${lineIndent(xml, lane.range.start)}</${lane.range.name}>` });
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
if (declare.length) {
|
|
887
|
+
const tagEnd = root.innerStart;
|
|
888
|
+
const close = /\s*\/?>$/.exec(xml.slice(root.start, tagEnd))[0];
|
|
889
|
+
edits.push({ at: tagEnd - close.length, end: tagEnd - close.length, text: ` ${declare.join(' ')}` });
|
|
890
|
+
}
|
|
891
|
+
let out = '';
|
|
892
|
+
let at = 0;
|
|
893
|
+
for (const edit of edits.sort((a, b) => a.at - b.at || a.end - b.end)) {
|
|
894
|
+
out += xml.slice(at, edit.at) + edit.text;
|
|
895
|
+
at = Math.max(at, edit.end);
|
|
896
|
+
}
|
|
897
|
+
out += xml.slice(at);
|
|
898
|
+
return { xml: out, report };
|
|
899
|
+
}
|