@ruckc/truss-graph 1.25.0 → 1.26.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/Graph.svelte +42 -50
- package/dist/routing.d.ts +7 -2
- package/dist/routing.js +25 -6
- package/package.json +1 -1
package/dist/Graph.svelte
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { onMount, tick, type Snippet } from 'svelte';
|
|
3
3
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
4
|
-
import { Button } from '@ruckc/truss-ui';
|
|
4
|
+
import { Button, startDrag, type DragOptions } from '@ruckc/truss-ui';
|
|
5
5
|
import { autoLayout, forceLayout, radialLayout } from './layout.js';
|
|
6
6
|
import { defaultPorts, opposite, portAnchor, routeEdge, spreadLabels, labelSize, type Anchor, type Rect } from './routing.js';
|
|
7
7
|
import { NODE_H, NODE_W, type EdgeType, type GraphEdge, type GraphLayout, type GraphLayoutOptions, type GraphLegend, type GraphNode, type GraphPort, type HighlightDirection, type NodeCtx, type NodeKind, type NodeRenderer } from './types.js';
|
|
@@ -128,20 +128,24 @@
|
|
|
128
128
|
const sc = Math.min(MW / (x1 - x0), MH / (y1 - y0));
|
|
129
129
|
return { x0, y0, sc, w: (x1 - x0) * sc, h: (y1 - y0) * sc, view: { x: (vx - x0) * sc, y: (vy - y0) * sc, w: vw * sc, h: vh * sc } };
|
|
130
130
|
});
|
|
131
|
+
// every drag goes through startDrag; the ones still running are abandoned when the graph unmounts
|
|
132
|
+
const running = new Set<() => void>();
|
|
133
|
+
function begin(e: PointerEvent, o: Required<Pick<DragOptions, 'move' | 'end'>> & DragOptions): boolean {
|
|
134
|
+
const stop = startDrag(e, { ...o, end: (ev, cancelled) => { running.delete(stop!); o.end(ev, cancelled); } });
|
|
135
|
+
if (stop) running.add(stop);
|
|
136
|
+
return !!stop;
|
|
137
|
+
}
|
|
138
|
+
$effect(() => () => { for (const stop of running) stop(); running.clear(); });
|
|
131
139
|
function mmPan(e: PointerEvent) {
|
|
132
140
|
if (!mm) return;
|
|
133
141
|
const svg = e.currentTarget as SVGSVGElement;
|
|
134
|
-
svg.setPointerCapture(e.pointerId);
|
|
135
142
|
const to = (ev: PointerEvent) => {
|
|
136
143
|
const r = svg.getBoundingClientRect();
|
|
137
144
|
const gx = mm.x0 + (ev.clientX - r.left) / mm.sc, gy = mm.y0 + (ev.clientY - r.top) / mm.sc;
|
|
138
145
|
tx = rw / 2 - gx * k;
|
|
139
146
|
ty = rh / 2 - gy * k;
|
|
140
147
|
};
|
|
141
|
-
to(e);
|
|
142
|
-
const up = () => { svg.removeEventListener('pointermove', to); svg.removeEventListener('pointerup', up); };
|
|
143
|
-
svg.addEventListener('pointermove', to);
|
|
144
|
-
svg.addEventListener('pointerup', up);
|
|
148
|
+
if (begin(e, { move: to, end: () => {}, cursor: 'grabbing' })) to(e);
|
|
145
149
|
}
|
|
146
150
|
|
|
147
151
|
const W = (n: GraphNode) => n.width ?? NODE_W;
|
|
@@ -245,9 +249,9 @@
|
|
|
245
249
|
if (big && (Math.max(a.x + W(a), b.x + W(b)) < vx0 || Math.min(a.x, b.x) > vx1 || Math.max(a.y + H(a), b.y + H(b)) < vy0 || Math.min(a.y, b.y) > vy1)) return [];
|
|
246
250
|
const pa = findPort(a, e.fromPort, 'out'), pb = findPort(b, e.toPort, 'in');
|
|
247
251
|
if (!pa || !pb) return [];
|
|
248
|
-
const r = routeEdge(routeType(e.type ?? edgeType), portAnchor(a, pa, portsOf(a)), portAnchor(b, pb, portsOf(b)), needsObstacles ? rects : []);
|
|
252
|
+
const r = routeEdge(routeType(e.type ?? edgeType), portAnchor(a, pa, portsOf(a)), portAnchor(b, pb, portsOf(b)), needsObstacles ? rects : [], labelSize(e, !!edgeLabel));
|
|
249
253
|
return [{ e, d: r.d, m: r.mid }];
|
|
250
|
-
}), (g) => labelSize(g.e, !!edgeLabel))
|
|
254
|
+
}), (g) => labelSize(g.e, !!edgeLabel), needsObstacles ? rects : [])
|
|
251
255
|
);
|
|
252
256
|
const toGraph = (cx: number, cy: number) => {
|
|
253
257
|
const r = root.getBoundingClientRect();
|
|
@@ -387,12 +391,8 @@
|
|
|
387
391
|
selected = []; selectedEdge = undefined;
|
|
388
392
|
root.focus({ preventScroll: !interactive || !!widthFit }); // a tall static or width-fitted graph must not jump the page when tapped
|
|
389
393
|
if (!interactive) return; // no pan: the browser scrolls the page
|
|
390
|
-
const
|
|
391
|
-
panning =
|
|
392
|
-
const move = (ev: PointerEvent) => { tx = tx0 + ev.clientX - sx; ty = ty0 + ev.clientY - sy; };
|
|
393
|
-
const up = () => { panning = false; window.removeEventListener('pointermove', move); window.removeEventListener('pointerup', up); };
|
|
394
|
-
window.addEventListener('pointermove', move);
|
|
395
|
-
window.addEventListener('pointerup', up);
|
|
394
|
+
const tx0 = tx, ty0 = ty;
|
|
395
|
+
panning = begin(e, { move: (_, dx, dy) => { tx = tx0 + dx; ty = ty0 + dy; }, end: () => (panning = false), cursor: 'grabbing' });
|
|
396
396
|
}
|
|
397
397
|
|
|
398
398
|
// ---- nodes: select + drag. Controls inside custom nodes opt out of dragging.
|
|
@@ -406,28 +406,21 @@
|
|
|
406
406
|
if (e.shiftKey || e.ctrlKey || e.metaKey) selected = selected.includes(n.id) ? selected.filter((i) => i !== n.id) : [...selected, n.id];
|
|
407
407
|
else if (!selected.includes(n.id)) selected = [n.id];
|
|
408
408
|
if (!edit || t.closest(INTERACTIVE)) return;
|
|
409
|
-
const ids = [...selected]
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
drag = { ids, dx, dy };
|
|
416
|
-
};
|
|
417
|
-
const up = () => {
|
|
418
|
-
window.removeEventListener('pointermove', move);
|
|
419
|
-
window.removeEventListener('pointerup', up);
|
|
420
|
-
if (drag) {
|
|
409
|
+
const ids = [...selected];
|
|
410
|
+
begin(e, {
|
|
411
|
+
cursor: 'grabbing',
|
|
412
|
+
threshold: 3, // a click that barely moves is not a drag, and is not captured
|
|
413
|
+
move: (_, sx, sy) => { drag = { ids, dx: sx / k, dy: sy / k }; },
|
|
414
|
+
end: (_, cancelled) => {
|
|
421
415
|
const d = drag;
|
|
416
|
+
drag = null;
|
|
417
|
+
if (!d || cancelled) return;
|
|
422
418
|
suppressClick = true; // the click that ends a drag is not a node click
|
|
423
419
|
setTimeout(() => (suppressClick = false));
|
|
424
420
|
nodes = nodes.map((m) => (d.ids.includes(m.id) ? { ...m, x: Math.round(m.x + d.dx), y: Math.round(m.y + d.dy) } : m));
|
|
425
|
-
drag = null;
|
|
426
421
|
onchange?.();
|
|
427
422
|
}
|
|
428
|
-
};
|
|
429
|
-
window.addEventListener('pointermove', move);
|
|
430
|
-
window.addEventListener('pointerup', up);
|
|
423
|
+
});
|
|
431
424
|
}
|
|
432
425
|
let suppressClick = false, keyMenuAt = -Infinity;
|
|
433
426
|
// controls inside custom nodes and the node's own badges handle their own clicks
|
|
@@ -464,27 +457,26 @@
|
|
|
464
457
|
e.stopPropagation();
|
|
465
458
|
e.preventDefault();
|
|
466
459
|
const g0 = toGraph(e.clientX, e.clientY);
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
460
|
+
const started = begin(e, {
|
|
461
|
+
cursor: 'crosshair',
|
|
462
|
+
move: (ev) => {
|
|
463
|
+
const g = toGraph(ev.clientX, ev.clientY);
|
|
464
|
+
const el = document.elementFromPoint(ev.clientX, ev.clientY)?.closest<HTMLElement>('[data-port-in]');
|
|
465
|
+
const over = el ? { node: el.dataset.portIn!, port: el.dataset.portId! } : undefined;
|
|
466
|
+
link = { from: n.id, port: p.id, x: g.x, y: g.y, over: over && valid({ node: n.id, port: p.id }, over) ? over : undefined };
|
|
467
|
+
},
|
|
468
|
+
end: (_, cancelled) => {
|
|
469
|
+
const to = cancelled ? undefined : link?.over;
|
|
470
|
+
if (to && !edges.some((x) => x.from === n.id && x.to === to.node && (x.fromPort ?? p.id) === p.id && (x.toPort ?? to.port) === to.port)) {
|
|
471
|
+
const edge: GraphEdge = { id: `${n.id}.${p.id}->${to.node}.${to.port}`, from: n.id, to: to.node, fromPort: p.id, toPort: to.port };
|
|
472
|
+
edges = [...edges, edge];
|
|
473
|
+
onconnect?.(edge);
|
|
474
|
+
onchange?.();
|
|
475
|
+
}
|
|
476
|
+
link = null;
|
|
483
477
|
}
|
|
484
|
-
|
|
485
|
-
};
|
|
486
|
-
window.addEventListener('pointermove', move);
|
|
487
|
-
window.addEventListener('pointerup', up);
|
|
478
|
+
});
|
|
479
|
+
if (started) link = { from: n.id, port: p.id, x: g0.x, y: g0.y };
|
|
488
480
|
}
|
|
489
481
|
const linkRoute = $derived.by(() => {
|
|
490
482
|
if (!link) return null;
|
package/dist/routing.d.ts
CHANGED
|
@@ -21,6 +21,10 @@ export declare const opposite: (s: Side) => Side;
|
|
|
21
21
|
export declare function defaultPorts(n: GraphNode, direction?: 'LR' | 'TB'): GraphPort[];
|
|
22
22
|
/** Where a port sits on its node; several ports on one side are spread evenly. */
|
|
23
23
|
export declare function portAnchor(n: GraphNode, port: GraphPort, ports: GraphPort[]): Anchor;
|
|
24
|
+
type Box = {
|
|
25
|
+
w: number;
|
|
26
|
+
h: number;
|
|
27
|
+
};
|
|
24
28
|
/**
|
|
25
29
|
* Edge labels sit at the midpoint of their edge, and edges that fan out share their first segments, so the
|
|
26
30
|
* midpoints crowd together. Walks the items in order and slides each label along y (0, +1, -1, +2, ... steps of
|
|
@@ -32,7 +36,7 @@ export declare function spreadLabels<T extends {
|
|
|
32
36
|
}>(items: T[], size: (item: T) => {
|
|
33
37
|
w: number;
|
|
34
38
|
h: number;
|
|
35
|
-
} | null): T[];
|
|
39
|
+
} | null, obstacles?: Rect[]): T[];
|
|
36
40
|
/**
|
|
37
41
|
* Estimated size of an edge's label box: 180 x 32 for an `edgeLabel` snippet (any edge), else the text's width
|
|
38
42
|
* (`len * 6.8 + 16`) by 20 for a plain `label`, or null when the edge has none. Shared by label spreading and the
|
|
@@ -45,4 +49,5 @@ export declare function labelSize(edge: {
|
|
|
45
49
|
h: number;
|
|
46
50
|
} | null;
|
|
47
51
|
/** Route an edge between two anchors with the chosen method. `obstacles` is used by `avoid`, and by `step` / `smoothstep` to go around nodes. */
|
|
48
|
-
export declare function routeEdge(type: EdgeType, a: Anchor, b: Anchor, obstacles?: Rect[]): Routed;
|
|
52
|
+
export declare function routeEdge(type: EdgeType, a: Anchor, b: Anchor, obstacles?: Rect[], label?: Box | null): Routed;
|
|
53
|
+
export {};
|
package/dist/routing.js
CHANGED
|
@@ -69,6 +69,23 @@ function midOf(pts) {
|
|
|
69
69
|
}
|
|
70
70
|
return pts.at(-1);
|
|
71
71
|
}
|
|
72
|
+
const overlaps = (c, s, o) => Math.abs(c.x - (o.x + o.w / 2)) < (o.w + s.w) / 2 && Math.abs(c.y - (o.y + o.h / 2)) < (o.h + s.h) / 2;
|
|
73
|
+
/**
|
|
74
|
+
* Where an orthogonal route's label goes: its midpoint, unless the label box there would cover a node (a self-loop or an
|
|
75
|
+
* aligned back-edge hugs its card). Then the label moves off the line, perpendicular to the segment, to whichever side
|
|
76
|
+
* (the outer one first) clears every node; it keeps the midpoint when neither does.
|
|
77
|
+
*/
|
|
78
|
+
function labelPoint(pts, size, obstacles) {
|
|
79
|
+
const m = midOf(pts);
|
|
80
|
+
if (!size || !obstacles.some((o) => overlaps(m, size, o)))
|
|
81
|
+
return m;
|
|
82
|
+
// the segment holding the midpoint is vertical when its ends share x (the midpoint always lies on a segment)
|
|
83
|
+
const k = pts.findIndex((p, i) => i > 0 && dist(pts[i - 1], m) + dist(m, p) < dist(pts[i - 1], p) + 0.01);
|
|
84
|
+
const vertical = Math.abs(pts[k].x - pts[k - 1].x) < 0.01;
|
|
85
|
+
const off = (vertical ? size.w : size.h) / 2 + 6;
|
|
86
|
+
const spots = [off, -off].map((d) => (vertical ? { x: m.x + d, y: m.y } : { x: m.x, y: m.y + d }));
|
|
87
|
+
return spots.find((c) => !obstacles.some((o) => overlaps(c, size, o))) ?? m;
|
|
88
|
+
}
|
|
72
89
|
const stubOf = (a, s) => ({ x: a.x + DIR[a.side].x * s, y: a.y + DIR[a.side].y * s });
|
|
73
90
|
// ---------- routers
|
|
74
91
|
function bezier(a, b) {
|
|
@@ -243,13 +260,15 @@ function clearPoints(a, b, obstacles) {
|
|
|
243
260
|
* its own height) until its box clears every label placed before it. `size` is null for an item without a label.
|
|
244
261
|
* Pure and deterministic; an item that finds no free spot within 12 tries keeps its own midpoint.
|
|
245
262
|
*/
|
|
246
|
-
export function spreadLabels(items, size) {
|
|
263
|
+
export function spreadLabels(items, size, obstacles = []) {
|
|
247
264
|
const placed = [];
|
|
248
265
|
return items.map((item) => {
|
|
249
266
|
const s = size(item);
|
|
250
267
|
if (!s)
|
|
251
268
|
return item;
|
|
252
|
-
|
|
269
|
+
// a slid label also steers clear of the nodes (a label kept at its routed midpoint is left as routed)
|
|
270
|
+
const hit = (y) => placed.some((p) => Math.abs(p.x - item.m.x) < (p.w + s.w) / 2 && Math.abs(p.y - y) < (p.h + s.h) / 2)
|
|
271
|
+
|| (y !== item.m.y && obstacles.some((o) => overlaps({ x: item.m.x, y }, s, o)));
|
|
253
272
|
let y = item.m.y;
|
|
254
273
|
for (let k = 0; k < 12 && hit(y); k++)
|
|
255
274
|
y = item.m.y + (k % 2 ? -1 : 1) * Math.ceil((k + 1) / 2) * (s.h + 2);
|
|
@@ -268,21 +287,21 @@ export function labelSize(edge, snippet = false) {
|
|
|
268
287
|
return snippet ? { w: 180, h: 32 } : edge.label ? { w: edge.label.length * 6.8 + 16, h: 20 } : null;
|
|
269
288
|
}
|
|
270
289
|
/** Route an edge between two anchors with the chosen method. `obstacles` is used by `avoid`, and by `step` / `smoothstep` to go around nodes. */
|
|
271
|
-
export function routeEdge(type, a, b, obstacles = []) {
|
|
290
|
+
export function routeEdge(type, a, b, obstacles = [], label = null) {
|
|
272
291
|
switch (type) {
|
|
273
292
|
case 'straight':
|
|
274
293
|
return { d: `M${a.x},${a.y} L${b.x},${b.y}`, mid: { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 } };
|
|
275
294
|
case 'step': {
|
|
276
295
|
const p = clearPoints(a, b, obstacles);
|
|
277
|
-
return { d: pathOf(p, 0), mid:
|
|
296
|
+
return { d: pathOf(p, 0), mid: labelPoint(p, label, obstacles) };
|
|
278
297
|
}
|
|
279
298
|
case 'smoothstep': {
|
|
280
299
|
const p = clearPoints(a, b, obstacles);
|
|
281
|
-
return { d: pathOf(p, 12), mid:
|
|
300
|
+
return { d: pathOf(p, 12), mid: labelPoint(p, label, obstacles) };
|
|
282
301
|
}
|
|
283
302
|
case 'avoid': {
|
|
284
303
|
const p = avoidPoints(a, b, obstacles) ?? orthogonalPoints(a, b);
|
|
285
|
-
return { d: pathOf(p, 10), mid:
|
|
304
|
+
return { d: pathOf(p, 10), mid: labelPoint(p, label, obstacles) };
|
|
286
305
|
}
|
|
287
306
|
default:
|
|
288
307
|
return bezier(a, b);
|