@weasel-js/diagram 1.4.4 → 1.5.1

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/index.js CHANGED
@@ -1,442 +1,9 @@
1
- import { AUTO_POSE_DESCRIPTOR, pointAlongPath, translatePoseViaDescriptor, polylineFromPoints, rotatePoint, rectPath, polygonFromPoints, PATH_M, PATH_L, PATH_C, flattenCubic, PATH_Q, PATH_Z, PathBuilder, effectivePose, composeAffordanceLayer, createSimulation, usePoseRun, RECT_POSE_DESCRIPTOR, registerNodeShape } from '@weasel-js/core';
1
+ import { portOf, portsOf, diagramNodeOf, force, tree, layered, forceRelaxation, center, outlinePath } from './chunk-M34HI5RG.js';
2
+ export { COMPASS, DEFAULT_NODE_GAP, DEFAULT_PORTS, DEFAULT_RANK_GAP, DIAGRAM_TRAIT_KEY, axesFor, backEdges, bodyOutline, bodyTrait, boxForContent, buildBody, canvasMeasure, contentBox, createDiagramNodes, dataKeyReader, diagramNodeOf, force, forceRelaxation, forestOf, graphOrder, isPinned, layered, layoutBody, layoutRowPorts, measureBody, outlinePath, outlinePolyline, packAcross, pinnedSet, portOf, portsOf, portsOnOutline, ranksOf, rayHit, seededOrder, settle, sizeToBody, translated, tree } from './chunk-M34HI5RG.js';
3
+ import { AUTO_POSE_DESCRIPTOR, pointAlongPath, translatePoseViaDescriptor, polylineFromPoints, effectivePose, composeAffordanceLayer, usePoseRun, RECT_POSE_DESCRIPTOR, registerNodeShape } from '@weasel-js/core';
4
+ import { AUTO_POSE_DESCRIPTOR as AUTO_POSE_DESCRIPTOR$1 } from '@weasel-js/core/math';
2
5
  import { useRef, useCallback, useEffect, useMemo } from 'react';
3
6
 
4
- // src/outline.ts
5
- var LEAN = 0.2;
6
- var KAPPA = 0.5522847498307936;
7
- function outlinePath(outline, bounds) {
8
- if (typeof outline !== "string") return outline;
9
- const { x, y, width: w, height: h } = bounds;
10
- if (w <= 0 || h <= 0) return rectPath(x, y, Math.max(w, 0), Math.max(h, 0));
11
- switch (outline) {
12
- case "rect":
13
- return rectPath(x, y, w, h);
14
- case "diamond":
15
- return polygonFromPoints([
16
- { x: x + w / 2, y },
17
- { x: x + w, y: y + h / 2 },
18
- { x: x + w / 2, y: y + h },
19
- { x, y: y + h / 2 }
20
- ]);
21
- case "parallelogram": {
22
- const lean = Math.min(w * LEAN, w / 2);
23
- return polygonFromPoints([
24
- { x: x + lean, y },
25
- { x: x + w, y },
26
- { x: x + w - lean, y: y + h },
27
- { x, y: y + h }
28
- ]);
29
- }
30
- case "stadium":
31
- return stadiumPath(x, y, w, h);
32
- }
33
- }
34
- function stadiumPath(x, y, w, h) {
35
- const r = Math.min(w, h) / 2;
36
- const k = r * KAPPA;
37
- const b = new PathBuilder();
38
- if (w >= h) {
39
- const left2 = x + r;
40
- const right2 = x + w - r;
41
- const top2 = y;
42
- const bottom2 = y + h;
43
- const midY = y + h / 2;
44
- return b.moveTo(left2, top2).lineTo(right2, top2).curveTo(right2 + k, top2, x + w, midY - k, x + w, midY).curveTo(x + w, midY + k, right2 + k, bottom2, right2, bottom2).lineTo(left2, bottom2).curveTo(left2 - k, bottom2, x, midY + k, x, midY).curveTo(x, midY - k, left2 - k, top2, left2, top2).close().build();
45
- }
46
- const top = y + r;
47
- const bottom = y + h - r;
48
- const left = x;
49
- const right = x + w;
50
- const midX = x + w / 2;
51
- return b.moveTo(right, top).lineTo(right, bottom).curveTo(right, bottom + k, midX + k, y + h, midX, y + h).curveTo(midX - k, y + h, left, bottom + k, left, bottom).lineTo(left, top).curveTo(left, top - k, midX - k, y, midX, y).curveTo(midX + k, y, right, top - k, right, top).close().build();
52
- }
53
- function contentBox(outline, bounds) {
54
- if (typeof outline !== "string") return bounds;
55
- const { x, y, width: w, height: h } = bounds;
56
- switch (outline) {
57
- case "rect":
58
- return bounds;
59
- case "diamond":
60
- return { x: x + w / 4, y: y + h / 4, width: w / 2, height: h / 2 };
61
- case "parallelogram": {
62
- const lean = Math.min(w * LEAN, w / 2);
63
- return { x: x + lean, y, width: Math.max(w - lean * 2, 0), height: h };
64
- }
65
- case "stadium": {
66
- const r = Math.min(w, h) / 2;
67
- return w >= h ? { x: x + r, y, width: Math.max(w - r * 2, 0), height: h } : { x, y: y + r, width: w, height: Math.max(h - r * 2, 0) };
68
- }
69
- }
70
- }
71
- function boxForContent(outline, content) {
72
- if (typeof outline !== "string") return content;
73
- switch (outline) {
74
- case "rect":
75
- return content;
76
- case "diamond":
77
- return { width: content.width * 2, height: content.height * 2 };
78
- case "parallelogram":
79
- return { width: content.width / (1 - LEAN * 2), height: content.height };
80
- case "stadium":
81
- return { width: content.width + content.height, height: content.height };
82
- }
83
- }
84
- var TOLERANCE = 0.25;
85
- function outlinePolyline(outline, bounds) {
86
- const path = outlinePath(outline, bounds);
87
- return flattenPath(path);
88
- }
89
- function flattenPath(path) {
90
- if (path.kind === "rect") {
91
- const { x, y, width: w, height: h } = path;
92
- return [x, y, x + w, y, x + w, y + h, x, y + h, x, y];
93
- }
94
- const out = [];
95
- const { commands, coords } = path;
96
- let ci = 0;
97
- let cx = 0;
98
- let cy = 0;
99
- let sx = 0;
100
- let sy = 0;
101
- for (const cmd of commands) {
102
- if (cmd === PATH_M) {
103
- cx = coords[ci];
104
- cy = coords[ci + 1];
105
- ci += 2;
106
- sx = cx;
107
- sy = cy;
108
- out.push(cx, cy);
109
- } else if (cmd === PATH_L) {
110
- cx = coords[ci];
111
- cy = coords[ci + 1];
112
- ci += 2;
113
- out.push(cx, cy);
114
- } else if (cmd === PATH_C) {
115
- const x1 = coords[ci], y1 = coords[ci + 1];
116
- const x2 = coords[ci + 2], y2 = coords[ci + 3];
117
- const x3 = coords[ci + 4], y3 = coords[ci + 5];
118
- ci += 6;
119
- flattenCubic(cx, cy, x1, y1, x2, y2, x3, y3, TOLERANCE, out);
120
- cx = x3;
121
- cy = y3;
122
- } else if (cmd === PATH_Q) {
123
- const qx = coords[ci], qy = coords[ci + 1];
124
- const x3 = coords[ci + 2], y3 = coords[ci + 3];
125
- ci += 4;
126
- flattenCubic(
127
- cx,
128
- cy,
129
- cx + 2 / 3 * (qx - cx),
130
- cy + 2 / 3 * (qy - cy),
131
- x3 + 2 / 3 * (qx - x3),
132
- y3 + 2 / 3 * (qy - y3),
133
- x3,
134
- y3,
135
- TOLERANCE,
136
- out
137
- );
138
- cx = x3;
139
- cy = y3;
140
- } else if (cmd === PATH_Z) {
141
- out.push(sx, sy);
142
- cx = sx;
143
- cy = sy;
144
- }
145
- }
146
- if (out.length >= 2 && (out[0] !== out[out.length - 2] || out[1] !== out[out.length - 1])) {
147
- out.push(out[0], out[1]);
148
- }
149
- return out;
150
- }
151
- function rayHit(poly, center2, point) {
152
- const dx = point.x - center2.x;
153
- const dy = point.y - center2.y;
154
- if (dx === 0 && dy === 0) return null;
155
- let bestT = -Infinity;
156
- let hit = null;
157
- for (let i = 0; i + 3 < poly.length; i += 2) {
158
- const ax = poly[i], ay = poly[i + 1];
159
- const bx = poly[i + 2], by = poly[i + 3];
160
- const ex = bx - ax;
161
- const ey = by - ay;
162
- const denom = dx * ey - dy * ex;
163
- if (denom === 0) continue;
164
- const t = ((ax - center2.x) * ey - (ay - center2.y) * ex) / denom;
165
- const u = ((ax - center2.x) * dy - (ay - center2.y) * dx) / denom;
166
- if (t < 0 || u < 0 || u > 1) continue;
167
- if (t > bestT) {
168
- bestT = t;
169
- hit = { x: center2.x + dx * t, y: center2.y + dy * t };
170
- }
171
- }
172
- return hit;
173
- }
174
- function portsOnOutline(ports, outline, bounds) {
175
- if (bounds.width <= 0 || bounds.height <= 0) return [...ports];
176
- const poly = outlinePolyline(outline, bounds);
177
- const center2 = { x: bounds.x + bounds.width / 2, y: bounds.y + bounds.height / 2 };
178
- return ports.map((port) => {
179
- const hit = rayHit(poly, center2, port.point);
180
- return hit === null ? port : { ...port, point: hit };
181
- });
182
- }
183
-
184
- // src/trait.ts
185
- var DIAGRAM_TRAIT_KEY = "diagram";
186
- var dataKeyReader = (node) => {
187
- const data = node.data;
188
- if (data === null || typeof data !== "object") return null;
189
- const trait = data[DIAGRAM_TRAIT_KEY];
190
- if (trait === null || typeof trait !== "object") return null;
191
- if ("from" in trait && "to" in trait) return null;
192
- return trait;
193
- };
194
- function createDiagramNodes(entries) {
195
- const seen = /* @__PURE__ */ new Set();
196
- for (const entry of entries) {
197
- if (seen.has(entry.name)) {
198
- throw new Error(`createDiagramNodes: duplicate entry name "${entry.name}"`);
199
- }
200
- seen.add(entry.name);
201
- }
202
- return (node) => {
203
- const own = dataKeyReader(node);
204
- if (own !== null) return own;
205
- for (const entry of entries) {
206
- if (!entry.matches(node.data)) continue;
207
- return typeof entry.trait === "function" ? entry.trait(node.data) : entry.trait;
208
- }
209
- return null;
210
- };
211
- }
212
- function diagramNodeOf(node, read = dataKeyReader) {
213
- return read(node);
214
- }
215
- function isPinned(node, read) {
216
- return diagramNodeOf(node, read)?.pinned === true;
217
- }
218
-
219
- // src/ports.ts
220
- var COMPASS = {
221
- nw: { u: 0, v: 0 },
222
- n: { u: 0.5, v: 0 },
223
- ne: { u: 1, v: 0 },
224
- e: { u: 1, v: 0.5 },
225
- se: { u: 1, v: 1 },
226
- s: { u: 0.5, v: 1 },
227
- sw: { u: 0, v: 1 },
228
- w: { u: 0, v: 0.5 }
229
- };
230
- var DEFAULT_PORTS = Object.freeze([
231
- { id: "n", at: COMPASS.n },
232
- { id: "e", at: COMPASS.e },
233
- { id: "s", at: COMPASS.s },
234
- { id: "w", at: COMPASS.w }
235
- ]);
236
- function normalOf(at) {
237
- const x = at.u * 2 - 1;
238
- const y = at.v * 2 - 1;
239
- const len = Math.hypot(x, y);
240
- if (len === 0) return null;
241
- return { x: x / len, y: y / len };
242
- }
243
- function portsOf(node, pose, opts = {}) {
244
- const trait = diagramNodeOf(node, opts.read);
245
- if (trait === null) return [];
246
- const geometry = opts.geometry ?? AUTO_POSE_DESCRIPTOR;
247
- const bounds = geometry.getBounds(pose);
248
- const rotation = geometry.getRotation?.(pose) ?? 0;
249
- const cx = bounds.x + bounds.width / 2;
250
- const cy = bounds.y + bounds.height / 2;
251
- const poly = trait.outline === void 0 || bounds.width <= 0 || bounds.height <= 0 ? null : outlinePolyline(trait.outline, bounds);
252
- return (trait.ports ?? DEFAULT_PORTS).map((spec) => {
253
- const anchored = {
254
- x: bounds.x + spec.at.u * bounds.width,
255
- y: bounds.y + spec.at.v * bounds.height
256
- };
257
- const flat = (poly === null ? null : rayHit(poly, { x: cx, y: cy }, anchored)) ?? anchored;
258
- const normal = spec.normal === void 0 ? normalOf(spec.at) : spec.normal;
259
- if (rotation === 0) {
260
- return { id: spec.id, nodeId: node.id, point: flat, normal, ...typeOf(spec) };
261
- }
262
- return {
263
- id: spec.id,
264
- nodeId: node.id,
265
- point: rotatePoint(flat.x, flat.y, cx, cy, rotation),
266
- // A direction rotates about the origin, not about the node's center.
267
- normal: normal === null ? null : rotatePoint(normal.x, normal.y, 0, 0, rotation),
268
- ...typeOf(spec)
269
- };
270
- });
271
- }
272
- function portOf(node, pose, portId, opts = {}) {
273
- return portsOf(node, pose, opts).find((p) => p.id === portId);
274
- }
275
- function typeOf(spec) {
276
- return spec.type === void 0 ? {} : { type: spec.type };
277
- }
278
-
279
- // src/body.ts
280
- var DEFAULT_PADDING = 8;
281
- var DEFAULT_GAP = 4;
282
- var FALLBACK_LINE = 16;
283
- function canvasMeasure(ctx) {
284
- return (text, style) => {
285
- const size = style.fontSize ?? FALLBACK_LINE;
286
- ctx.font = `${style.bold === true ? "bold " : ""}${size}px ${style.fontFamily ?? "sans-serif"}`;
287
- return { width: ctx.measureText(text).width, height: Math.ceil(size * 1.2) };
288
- };
289
- }
290
- function measureRow(row, measure) {
291
- const text = (s, style) => measure === void 0 ? { width: s.length * ((style?.fontSize ?? FALLBACK_LINE) * 0.6), height: FALLBACK_LINE } : measure(s, style ?? {});
292
- switch (row.kind) {
293
- case "label":
294
- return text(row.text, row.style);
295
- case "field": {
296
- const l = text(row.label, row.style);
297
- const v = text(row.value, row.style);
298
- return { width: l.width + DEFAULT_GAP * 2 + v.width, height: Math.max(l.height, v.height) };
299
- }
300
- case "ports": {
301
- const side = (ports) => (ports ?? []).reduce((w, p) => Math.max(w, p.label === void 0 ? 0 : text(p.label).width), 0);
302
- const rows = Math.max(row.left?.length ?? 0, row.right?.length ?? 0, 1);
303
- return {
304
- width: side(row.left) + DEFAULT_GAP * 2 + side(row.right),
305
- height: row.height ?? rows * FALLBACK_LINE
306
- };
307
- }
308
- case "slot":
309
- return { width: 0, height: row.height };
310
- }
311
- }
312
- function measureBody(spec, measure) {
313
- const padding = spec.padding ?? DEFAULT_PADDING;
314
- const gap = spec.gap ?? DEFAULT_GAP;
315
- let width = 0;
316
- let height = 0;
317
- for (const [i, row] of spec.rows.entries()) {
318
- const m = measureRow(row, measure);
319
- width = Math.max(width, m.width);
320
- height += m.height + (i > 0 ? gap : 0);
321
- }
322
- const box = boxForContent(spec.outline, {
323
- width: width + padding * 2,
324
- height: height + padding * 2
325
- });
326
- return { minWidth: box.width, minHeight: box.height };
327
- }
328
- function sizeToBody(pose, floor) {
329
- const width = Math.max(pose.width, floor.minWidth);
330
- const height = Math.max(pose.height, floor.minHeight);
331
- if (width === pose.width && height === pose.height) return pose;
332
- return { ...pose, width, height };
333
- }
334
- function layoutBody(spec, bounds, measure) {
335
- const padding = spec.padding ?? DEFAULT_PADDING;
336
- const gap = spec.gap ?? DEFAULT_GAP;
337
- const inner = contentBox(spec.outline, bounds);
338
- const width = Math.max(inner.width - padding * 2, 0);
339
- let y = inner.y + padding;
340
- return spec.rows.map((row, index) => {
341
- const h = measureRow(row, measure).height;
342
- const box = { row, index, x: inner.x + padding, y, width, height: h };
343
- y += h + gap;
344
- return box;
345
- });
346
- }
347
- function layoutRowPorts(row, box, measure) {
348
- const width = (label) => label === void 0 ? 0 : measureRow({ kind: "label", text: label }, measure).width;
349
- const slots = Math.max(row.left?.length ?? 0, row.right?.length ?? 0, 1);
350
- const slotHeight = box.height / slots;
351
- const out = [];
352
- const side = (ports, which) => {
353
- for (const [index, port] of (ports ?? []).entries()) {
354
- const w = width(port.label);
355
- out.push({
356
- port,
357
- side: which,
358
- index,
359
- x: which === "left" ? box.x : box.x + box.width - w,
360
- y: box.y + index * slotHeight,
361
- width: w,
362
- height: slotHeight
363
- });
364
- }
365
- };
366
- side(row.left, "left");
367
- side(row.right, "right");
368
- return out;
369
- }
370
- function bodyTrait(spec, bounds, measure) {
371
- const outline = spec.outline;
372
- const compass = [
373
- { id: "n", at: COMPASS.n },
374
- { id: "e", at: COMPASS.e },
375
- { id: "s", at: COMPASS.s },
376
- { id: "w", at: COMPASS.w }
377
- ];
378
- if (bounds.height <= 0) return { outline, ports: compass };
379
- const rowPorts = [];
380
- let hasLeft = false;
381
- let hasRight = false;
382
- for (const box of layoutBody(spec, bounds, measure)) {
383
- if (box.row.kind !== "ports") continue;
384
- for (const pb of layoutRowPorts(box.row, box, measure)) {
385
- if (pb.side === "left") hasLeft = true;
386
- else hasRight = true;
387
- const v = (pb.y + pb.height / 2 - bounds.y) / bounds.height;
388
- rowPorts.push(withType(
389
- { id: pb.port.id, at: { u: pb.side === "left" ? 0 : 1, v } },
390
- pb.port.type
391
- ));
392
- }
393
- }
394
- const kept = compass.filter((p) => !(p.id === "w" && hasLeft) && !(p.id === "e" && hasRight));
395
- return { outline, ports: [...kept, ...rowPorts] };
396
- }
397
- function withType(spec, type) {
398
- return type === void 0 ? spec : { ...spec, type };
399
- }
400
- function bodyOutline(spec, bounds) {
401
- return outlinePath(spec.outline, bounds);
402
- }
403
- function buildBody(spec, at, opts) {
404
- const pose = sizeToBody(at, measureBody(spec, opts.measure));
405
- const specs = [{
406
- kind: "container",
407
- layer: opts.layer,
408
- pose,
409
- data: opts.body(bodyTrait(spec, pose, opts.measure), pose),
410
- ...opts.id === void 0 ? {} : { id: opts.id }
411
- }];
412
- const leaf = (box, data) => {
413
- specs.push({
414
- kind: "leaf",
415
- layer: opts.layer,
416
- pose: { ...pose, x: box.x, y: box.y, width: box.width, height: box.height },
417
- data,
418
- pickable: false,
419
- ...opts.id === void 0 ? {} : { parent: opts.id }
420
- });
421
- };
422
- const portLabel = opts.portLabel;
423
- for (const box of layoutBody(spec, pose, opts.measure)) {
424
- const data = opts.row(rowText(box.row), box);
425
- if (data !== null) leaf(box, data);
426
- if (portLabel === void 0 || box.row.kind !== "ports") continue;
427
- for (const pb of layoutRowPorts(box.row, box, opts.measure)) {
428
- if (pb.port.label === void 0) continue;
429
- const labelData = portLabel(pb.port.label, pb);
430
- if (labelData !== null) leaf(pb, labelData);
431
- }
432
- }
433
- return { pose, specs };
434
- }
435
- function rowText(row) {
436
- if (row.kind === "label") return row.text;
437
- if (row.kind === "field") return `${row.label}: ${row.value}`;
438
- return "";
439
- }
440
7
  var NAMED = { start: 0, mid: 0.5, end: 1 };
441
8
  function diagramLabelOf(node) {
442
9
  const data = node.data;
@@ -676,7 +243,6 @@ var DEFAULT_EDGE_STROKE = {
676
243
  markerEnd: "arrow"
677
244
  };
678
245
  var DEFAULT_ROUTER = "straight";
679
- var PREVIEW_STROKE = { paint: { color: "#7ba7c7" }, width: 2, dash: [4, 4] };
680
246
  function connectBinding(actionId = CONNECT_ACTION_ID) {
681
247
  return {
682
248
  spec: { kind: "drag", target: `affordance:${PORT_AFFORDANCE_KIND}` },
@@ -730,8 +296,7 @@ function createConnectAction(opts) {
730
296
  canConnect = defaultCanConnect,
731
297
  snapRadius = DEFAULT_SNAP,
732
298
  router: routerKey = DEFAULT_ROUTER,
733
- commit = commitEdgeToScene,
734
- stroke = PREVIEW_STROKE
299
+ commit = commitEdgeToScene
735
300
  } = opts;
736
301
  const routers = opts.routers ?? ROUTERS;
737
302
  const portOpts = {
@@ -787,12 +352,7 @@ function createConnectAction(opts) {
787
352
  const { end } = endAt(scratch.from, scratch.current);
788
353
  const points = route({ from: scratch.from, to: end, waypoints: [] });
789
354
  if (points.length < 2) return null;
790
- const cmd = {
791
- kind: "path",
792
- path: polylineFromPoints(points),
793
- stroke
794
- };
795
- return { kind: "commands", commands: [cmd], space: "world" };
355
+ return { kind: "polyline", points, role: "connector" };
796
356
  },
797
357
  onEnd(endCtx, reason) {
798
358
  scratch.open = false;
@@ -845,7 +405,7 @@ function diagramPorts(opts) {
845
405
  };
846
406
  }
847
407
  function buildGraph(source, opts = {}) {
848
- const geometry = opts.geometry ?? AUTO_POSE_DESCRIPTOR;
408
+ const geometry = opts.geometry ?? AUTO_POSE_DESCRIPTOR$1;
849
409
  const nodes = [];
850
410
  const byId = /* @__PURE__ */ new Map();
851
411
  const candidates = [];
@@ -888,432 +448,6 @@ function push(index, key, edge) {
888
448
  if (list === void 0) index.set(key, [edge]);
889
449
  else list.push(edge);
890
450
  }
891
-
892
- // src/layout.ts
893
- var DEFAULT_NODE_GAP = 48;
894
- var DEFAULT_RANK_GAP = 96;
895
- var DEFAULT_TOLERANCE = 1e-6;
896
- function axesFor(direction = "down") {
897
- switch (direction) {
898
- case "up":
899
- return { rank: "y", cross: "x", sign: -1 };
900
- case "right":
901
- return { rank: "x", cross: "y", sign: 1 };
902
- case "left":
903
- return { rank: "x", cross: "y", sign: -1 };
904
- default:
905
- return { rank: "y", cross: "x", sign: 1 };
906
- }
907
- }
908
- function extent(bounds, axis) {
909
- return axis === "x" ? bounds.width : bounds.height;
910
- }
911
- function center(bounds, axis) {
912
- return axis === "x" ? bounds.x + bounds.width / 2 : bounds.y + bounds.height / 2;
913
- }
914
- function pinnedSet(graph, opts = {}) {
915
- const pinned = new Set(opts.pin ?? []);
916
- for (const node of graph.nodes) {
917
- if (node.pinned) pinned.add(node.id);
918
- }
919
- return pinned;
920
- }
921
- function settle(graph, slots, opts = {}) {
922
- const axes = axesFor(opts.direction);
923
- const pinned = pinnedSet(graph, opts);
924
- const tolerance = opts.tolerance ?? DEFAULT_TOLERANCE;
925
- const placed = /* @__PURE__ */ new Map();
926
- for (const node of graph.nodes) {
927
- const slot = slots.get(node.id);
928
- if (slot === void 0) continue;
929
- const along = axes.sign === 1 ? slot.rank : -(slot.rank + extent(node.bounds, axes.rank));
930
- placed.set(
931
- node.id,
932
- axes.rank === "y" ? { x: slot.cross, y: along } : { x: along, y: slot.cross }
933
- );
934
- }
935
- return translated(graph, placed, pinned, tolerance);
936
- }
937
- function translated(graph, placed, pinned, tolerance = DEFAULT_TOLERANCE) {
938
- const anchor = anchorDelta(graph, placed, pinned);
939
- const out = /* @__PURE__ */ new Map();
940
- for (const node of graph.nodes) {
941
- const at = placed.get(node.id);
942
- if (at === void 0 || pinned.has(node.id)) continue;
943
- const x = at.x + anchor.x;
944
- const y = at.y + anchor.y;
945
- if (Math.abs(x - node.bounds.x) <= tolerance && Math.abs(y - node.bounds.y) <= tolerance) {
946
- continue;
947
- }
948
- out.set(node.id, { x, y });
949
- }
950
- return out;
951
- }
952
- function anchorDelta(graph, placed, pinned) {
953
- for (const node of graph.nodes) {
954
- if (!pinned.has(node.id)) continue;
955
- const at = placed.get(node.id);
956
- if (at !== void 0) return { x: node.bounds.x - at.x, y: node.bounds.y - at.y };
957
- }
958
- let minCurX = Infinity;
959
- let minCurY = Infinity;
960
- let minNewX = Infinity;
961
- let minNewY = Infinity;
962
- for (const node of graph.nodes) {
963
- const at = placed.get(node.id);
964
- if (at === void 0) continue;
965
- minCurX = Math.min(minCurX, node.bounds.x);
966
- minCurY = Math.min(minCurY, node.bounds.y);
967
- minNewX = Math.min(minNewX, at.x);
968
- minNewY = Math.min(minNewY, at.y);
969
- }
970
- if (!Number.isFinite(minCurX)) return { x: 0, y: 0 };
971
- return { x: minCurX - minNewX, y: minCurY - minNewY };
972
- }
973
- function packAcross(nodes, axis, gap) {
974
- const at = /* @__PURE__ */ new Map();
975
- let cursor = 0;
976
- for (const node of nodes) {
977
- at.set(node.id, cursor);
978
- cursor += extent(node.bounds, axis) + gap;
979
- }
980
- return { at, span: nodes.length === 0 ? 0 : cursor - gap };
981
- }
982
- function seededOrder(nodes, axis, indexOf) {
983
- return [...nodes].sort((a, b) => {
984
- const d = center(a.bounds, axis) - center(b.bounds, axis);
985
- return d !== 0 ? d : indexOf(a.id) - indexOf(b.id);
986
- });
987
- }
988
- function graphOrder(graph) {
989
- const index = /* @__PURE__ */ new Map();
990
- graph.nodes.forEach((node, i) => index.set(node.id, i));
991
- return (id) => index.get(id) ?? Number.MAX_SAFE_INTEGER;
992
- }
993
-
994
- // src/force.ts
995
- var DEFAULTS = {
996
- iterations: 300,
997
- linkDistance: 160,
998
- linkStrength: 0.35,
999
- charge: -3e3,
1000
- gravity: 0.02,
1001
- padding: 24
1002
- };
1003
- var GOLDEN = Math.PI * (3 - Math.sqrt(5));
1004
- var NUDGE = 0.5;
1005
- function charge(bodies, strength) {
1006
- return (alpha) => {
1007
- for (let i = 0; i < bodies.length; i++) {
1008
- const a = bodies[i];
1009
- for (let j = i + 1; j < bodies.length; j++) {
1010
- const b = bodies[j];
1011
- const dx = b.x - a.x;
1012
- const dy = b.y - a.y;
1013
- const d2 = dx * dx + dy * dy;
1014
- if (d2 === 0) continue;
1015
- const w = strength * alpha / d2;
1016
- a.vx += dx * w;
1017
- a.vy += dy * w;
1018
- b.vx -= dx * w;
1019
- b.vy -= dy * w;
1020
- }
1021
- }
1022
- };
1023
- }
1024
- function links(bodies, graph, distance, strength) {
1025
- return (alpha) => {
1026
- for (const edge of graph.edges) {
1027
- const a = bodies.get(edge.from);
1028
- const b = bodies.get(edge.to);
1029
- if (a === void 0 || b === void 0 || a === b) continue;
1030
- const dx = b.x - a.x;
1031
- const dy = b.y - a.y;
1032
- const d = Math.hypot(dx, dy);
1033
- if (d === 0) continue;
1034
- const pull = (d - distance) / d * alpha * strength;
1035
- a.vx += dx * pull;
1036
- a.vy += dy * pull;
1037
- b.vx -= dx * pull;
1038
- b.vy -= dy * pull;
1039
- }
1040
- };
1041
- }
1042
- function separateBoxes(bodies, padding) {
1043
- return () => {
1044
- for (let i = 0; i < bodies.length; i++) {
1045
- const a = bodies[i];
1046
- for (let j = i + 1; j < bodies.length; j++) {
1047
- const b = bodies[j];
1048
- const dx = b.x - a.x;
1049
- const dy = b.y - a.y;
1050
- const gapX = a.hw + b.hw + padding - Math.abs(dx);
1051
- const gapY = a.hh + b.hh + padding - Math.abs(dy);
1052
- if (gapX <= 0 || gapY <= 0) continue;
1053
- if (gapX < gapY) {
1054
- const push2 = (Math.sign(dx) || 1) * gapX * 0.5;
1055
- a.vx -= push2;
1056
- b.vx += push2;
1057
- } else {
1058
- const push2 = (Math.sign(dy) || 1) * gapY * 0.5;
1059
- a.vy -= push2;
1060
- b.vy += push2;
1061
- }
1062
- }
1063
- }
1064
- };
1065
- }
1066
- function gravity(bodies, at, strength) {
1067
- return (alpha) => {
1068
- for (const body of bodies) {
1069
- body.vx += (at.x - body.x) * strength * alpha;
1070
- body.vy += (at.y - body.y) * strength * alpha;
1071
- }
1072
- };
1073
- }
1074
- function forceRelaxation(graph, opts = {}) {
1075
- const {
1076
- linkDistance = DEFAULTS.linkDistance,
1077
- linkStrength = DEFAULTS.linkStrength,
1078
- charge: chargeStrength = DEFAULTS.charge,
1079
- gravity: gravityStrength = DEFAULTS.gravity,
1080
- padding = DEFAULTS.padding
1081
- } = opts;
1082
- const pinned = pinnedSet(graph, opts);
1083
- const bodies = graph.nodes.map((node) => {
1084
- const body = {
1085
- id: node.id,
1086
- x: center(node.bounds, "x"),
1087
- y: center(node.bounds, "y"),
1088
- hw: node.bounds.width / 2,
1089
- hh: node.bounds.height / 2
1090
- };
1091
- if (pinned.has(node.id)) {
1092
- body.fx = body.x;
1093
- body.fy = body.y;
1094
- }
1095
- return body;
1096
- });
1097
- separate(bodies);
1098
- const byId = new Map(bodies.map((b) => [b.id, b]));
1099
- const centroid = {
1100
- x: bodies.reduce((sum, b) => sum + b.x, 0) / bodies.length,
1101
- y: bodies.reduce((sum, b) => sum + b.y, 0) / bodies.length
1102
- };
1103
- const sim = createSimulation({
1104
- nodes: bodies,
1105
- forces: [
1106
- charge(bodies, chargeStrength),
1107
- links(byId, graph, linkDistance, linkStrength),
1108
- gravity(bodies, centroid, gravityStrength),
1109
- separateBoxes(bodies, padding)
1110
- ]
1111
- });
1112
- return {
1113
- bodies,
1114
- byId,
1115
- pinned,
1116
- sim,
1117
- placed() {
1118
- const placed = /* @__PURE__ */ new Map();
1119
- for (const node of graph.nodes) {
1120
- const body = byId.get(node.id);
1121
- placed.set(node.id, {
1122
- x: body.x - node.bounds.width / 2,
1123
- y: body.y - node.bounds.height / 2
1124
- });
1125
- }
1126
- return placed;
1127
- }
1128
- };
1129
- }
1130
- var force = (graph, opts = {}) => {
1131
- if (graph.nodes.length === 0) return /* @__PURE__ */ new Map();
1132
- const relaxation = forceRelaxation(graph, opts);
1133
- relaxation.sim.tick(opts.iterations ?? DEFAULTS.iterations);
1134
- return translated(graph, relaxation.placed(), relaxation.pinned, opts.tolerance);
1135
- };
1136
- function separate(bodies) {
1137
- const seen = /* @__PURE__ */ new Map();
1138
- bodies.forEach((body, i) => {
1139
- const key = `${body.x},${body.y}`;
1140
- const nth = seen.get(key) ?? 0;
1141
- seen.set(key, nth + 1);
1142
- if (nth === 0 || body.fx != null || body.fy != null) return;
1143
- const angle = GOLDEN * i;
1144
- const radius = NUDGE * nth;
1145
- body.x += Math.cos(angle) * radius;
1146
- body.y += Math.sin(angle) * radius;
1147
- });
1148
- }
1149
-
1150
- // src/layered.ts
1151
- function backEdges(graph) {
1152
- const back = /* @__PURE__ */ new Set();
1153
- const done = /* @__PURE__ */ new Set();
1154
- const onStack = /* @__PURE__ */ new Set();
1155
- const walk = (id) => {
1156
- if (done.has(id)) return;
1157
- onStack.add(id);
1158
- for (const edge of graph.outgoing(id)) {
1159
- if (onStack.has(edge.to)) back.add(edge.id);
1160
- else walk(edge.to);
1161
- }
1162
- onStack.delete(id);
1163
- done.add(id);
1164
- };
1165
- for (const node of graph.nodes) walk(node.id);
1166
- return back;
1167
- }
1168
- function ranksOf(graph, back) {
1169
- const ranks = /* @__PURE__ */ new Map();
1170
- const resolving = /* @__PURE__ */ new Set();
1171
- const rankOf = (id) => {
1172
- const known = ranks.get(id);
1173
- if (known !== void 0) return known;
1174
- if (resolving.has(id)) return 0;
1175
- resolving.add(id);
1176
- let rank = 0;
1177
- for (const edge of graph.incoming(id)) {
1178
- if (back.has(edge.id)) continue;
1179
- rank = Math.max(rank, rankOf(edge.from) + 1);
1180
- }
1181
- resolving.delete(id);
1182
- ranks.set(id, rank);
1183
- return rank;
1184
- };
1185
- for (const node of graph.nodes) rankOf(node.id);
1186
- return ranks;
1187
- }
1188
- var layered = (graph, opts = {}) => {
1189
- const axes = axesFor(opts.direction);
1190
- const nodeGap = opts.nodeGap ?? DEFAULT_NODE_GAP;
1191
- const rankGap = opts.rankGap ?? DEFAULT_RANK_GAP;
1192
- const order = graphOrder(graph);
1193
- const ranks = ranksOf(graph, backEdges(graph));
1194
- const byRank = /* @__PURE__ */ new Map();
1195
- for (const node of graph.nodes) {
1196
- const rank = ranks.get(node.id) ?? 0;
1197
- const list = byRank.get(rank);
1198
- if (list === void 0) byRank.set(rank, [node]);
1199
- else list.push(node);
1200
- }
1201
- const rows = [...byRank.keys()].sort((a, b) => a - b);
1202
- const packed = /* @__PURE__ */ new Map();
1203
- let widest = 0;
1204
- for (const rank of rows) {
1205
- const row = seededOrder(byRank.get(rank), axes.cross, order);
1206
- const p = packAcross(row, axes.cross, nodeGap);
1207
- packed.set(rank, p);
1208
- widest = Math.max(widest, p.span);
1209
- }
1210
- const slots = /* @__PURE__ */ new Map();
1211
- let along = 0;
1212
- for (const rank of rows) {
1213
- const row = byRank.get(rank);
1214
- const p = packed.get(rank);
1215
- const indent = (widest - p.span) / 2;
1216
- const depth = Math.max(0, ...row.map((n) => extent(n.bounds, axes.rank)));
1217
- for (const node of row) {
1218
- slots.set(node.id, {
1219
- cross: indent + p.at.get(node.id),
1220
- // Centered in its rank's band, so a tall node does not push its
1221
- // neighbors' edges off the line they read along.
1222
- rank: along + (depth - extent(node.bounds, axes.rank)) / 2
1223
- });
1224
- }
1225
- along += depth + rankGap;
1226
- }
1227
- return settle(graph, slots, opts);
1228
- };
1229
-
1230
- // src/tree.ts
1231
- function forestOf(graph, crossAxis) {
1232
- const order = graphOrder(graph);
1233
- const children = /* @__PURE__ */ new Map();
1234
- const depth = /* @__PURE__ */ new Map();
1235
- const roots = [];
1236
- const claim = (node, at) => {
1237
- depth.set(node.id, at);
1238
- const kids = [];
1239
- for (const edge of graph.outgoing(node.id)) {
1240
- if (edge.to === node.id || depth.has(edge.to)) continue;
1241
- const child = graph.node(edge.to);
1242
- if (child === void 0) continue;
1243
- depth.set(child.id, at + 1);
1244
- kids.push(child);
1245
- }
1246
- const ordered = seededOrder(kids, crossAxis, order);
1247
- children.set(node.id, ordered);
1248
- for (const child of ordered) claim(child, at + 1);
1249
- };
1250
- const rootish = graph.nodes.filter((node) => graph.incoming(node.id).every((edge) => edge.from === node.id));
1251
- for (const node of seededOrder(rootish, crossAxis, order)) {
1252
- if (depth.has(node.id)) continue;
1253
- roots.push(node);
1254
- claim(node, 0);
1255
- }
1256
- for (const node of graph.nodes) {
1257
- if (depth.has(node.id)) continue;
1258
- roots.push(node);
1259
- claim(node, 0);
1260
- }
1261
- return { roots, children, depth };
1262
- }
1263
- var tree = (graph, opts = {}) => {
1264
- const axes = axesFor(opts.direction);
1265
- const nodeGap = opts.nodeGap ?? DEFAULT_NODE_GAP;
1266
- const rankGap = opts.rankGap ?? DEFAULT_RANK_GAP;
1267
- const { roots, children, depth } = forestOf(graph, axes.cross);
1268
- const bands = /* @__PURE__ */ new Map();
1269
- for (const node of graph.nodes) {
1270
- const at = depth.get(node.id) ?? 0;
1271
- bands.set(at, Math.max(bands.get(at) ?? 0, extent(node.bounds, axes.rank)));
1272
- }
1273
- const bandStart = /* @__PURE__ */ new Map();
1274
- let along = 0;
1275
- for (const at of [...bands.keys()].sort((a, b) => a - b)) {
1276
- bandStart.set(at, along);
1277
- along += bands.get(at) + rankGap;
1278
- }
1279
- const slots = /* @__PURE__ */ new Map();
1280
- const measured = /* @__PURE__ */ new Map();
1281
- const measure = (node) => {
1282
- const known = measured.get(node.id);
1283
- if (known !== void 0) return known;
1284
- const kids = children.get(node.id) ?? [];
1285
- let width = 0;
1286
- for (const child of kids) width += measure(child);
1287
- if (kids.length > 0) width += nodeGap * (kids.length - 1);
1288
- const block = Math.max(extent(node.bounds, axes.cross), width);
1289
- measured.set(node.id, block);
1290
- return block;
1291
- };
1292
- const place = (node, from) => {
1293
- const kids = children.get(node.id) ?? [];
1294
- const block = measure(node);
1295
- const width = kids.reduce((sum, k) => sum + measure(k), 0) + Math.max(0, kids.length - 1) * nodeGap;
1296
- let cursor2 = from + (block - width) / 2;
1297
- for (const child of kids) {
1298
- place(child, cursor2);
1299
- cursor2 += measure(child) + nodeGap;
1300
- }
1301
- const own = extent(node.bounds, axes.cross);
1302
- const at = depth.get(node.id) ?? 0;
1303
- slots.set(node.id, {
1304
- cross: from + (block - own) / 2,
1305
- rank: bandStart.get(at) + (bands.get(at) - extent(node.bounds, axes.rank)) / 2
1306
- });
1307
- };
1308
- let cursor = 0;
1309
- for (const root of roots) {
1310
- place(root, cursor);
1311
- cursor += measure(root) + nodeGap;
1312
- }
1313
- return settle(graph, slots, opts);
1314
- };
1315
-
1316
- // src/layoutAction.ts
1317
451
  var LAYOUTS = Object.freeze({
1318
452
  layered,
1319
453
  tree,
@@ -1560,6 +694,6 @@ function registerDiagramShape(opts = {}) {
1560
694
  return registerNodeShape(diagramShape(opts), { priority: "high" });
1561
695
  }
1562
696
 
1563
- export { COMPASS, CONNECT_ACTION_ID, DEFAULT_EDGE_STROKE, DEFAULT_NODE_GAP, DEFAULT_PORTS, DEFAULT_RANK_GAP, DIAGRAM_EDGE, DIAGRAM_LABEL, DIAGRAM_TRAIT_KEY, EASED_LAYOUTS, EDGE_DERIVE_PATH, GRAB_PORT_ACTION_ID, LABEL_DERIVE_POSE, LAYOUTS, LAYOUT_ACTION_ID, PORT_AFFORDANCE_KIND, PORT_LAYER_ID, ROUTERS, applyLayout, axesFor, backEdges, bezier, bodyOutline, bodyTrait, boxForContent, buildBody, buildGraph, canvasMeasure, commitEdgeToScene, connectBinding, contentBox, createConnectAction, createDiagramContribution, createDiagramNodes, createLayoutAction, createPortAffordance, dataKeyReader, defaultCanConnect, diagramEdgeOf, diagramLabelOf, diagramNodeOf, diagramPorts, diagramShape, easedProducer, edgeDerivePath, force, forceProducer, forceRelaxation, forestOf, grabPortAction, grabPortBindings, graphOrder, isPinned, labelDerivePose, layered, layoutBody, layoutPoses, layoutRowPorts, measureBody, orthogonal, outlinePath, outlinePolyline, packAcross, pinnedSet, portLayer, portOf, portScratchOf, portsOf, portsOnOutline, ranksOf, rayHit, registerDiagramShape, resolveEnd, sceneParticipants, seededOrder, settle, sizeToBody, straight, translated, tree, useLiveLayout, withDiagramRegistry };
697
+ export { CONNECT_ACTION_ID, DEFAULT_EDGE_STROKE, DIAGRAM_EDGE, DIAGRAM_LABEL, EASED_LAYOUTS, EDGE_DERIVE_PATH, GRAB_PORT_ACTION_ID, LABEL_DERIVE_POSE, LAYOUTS, LAYOUT_ACTION_ID, PORT_AFFORDANCE_KIND, PORT_LAYER_ID, ROUTERS, applyLayout, bezier, buildGraph, commitEdgeToScene, connectBinding, createConnectAction, createDiagramContribution, createLayoutAction, createPortAffordance, defaultCanConnect, diagramEdgeOf, diagramLabelOf, diagramPorts, diagramShape, easedProducer, edgeDerivePath, forceProducer, grabPortAction, grabPortBindings, labelDerivePose, layoutPoses, orthogonal, portLayer, portScratchOf, registerDiagramShape, resolveEnd, sceneParticipants, straight, useLiveLayout, withDiagramRegistry };
1564
698
  //# sourceMappingURL=index.js.map
1565
699
  //# sourceMappingURL=index.js.map