@ruckc/truss-graph 1.26.0 → 1.27.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/Graph.svelte +40 -48
- 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;
|
|
@@ -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;
|