@ruckc/truss-graph 1.25.0 → 1.26.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 CHANGED
@@ -245,9 +245,9 @@
245
245
  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
246
  const pa = findPort(a, e.fromPort, 'out'), pb = findPort(b, e.toPort, 'in');
247
247
  if (!pa || !pb) return [];
248
- const r = routeEdge(routeType(e.type ?? edgeType), portAnchor(a, pa, portsOf(a)), portAnchor(b, pb, portsOf(b)), needsObstacles ? rects : []);
248
+ const r = routeEdge(routeType(e.type ?? edgeType), portAnchor(a, pa, portsOf(a)), portAnchor(b, pb, portsOf(b)), needsObstacles ? rects : [], labelSize(e, !!edgeLabel));
249
249
  return [{ e, d: r.d, m: r.mid }];
250
- }), (g) => labelSize(g.e, !!edgeLabel))
250
+ }), (g) => labelSize(g.e, !!edgeLabel), needsObstacles ? rects : [])
251
251
  );
252
252
  const toGraph = (cx: number, cy: number) => {
253
253
  const r = root.getBoundingClientRect();
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
- 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);
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: midOf(p) };
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: midOf(p) };
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: midOf(p) };
304
+ return { d: pathOf(p, 10), mid: labelPoint(p, label, obstacles) };
286
305
  }
287
306
  default:
288
307
  return bezier(a, b);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruckc/truss-graph",
3
- "version": "1.25.0",
3
+ "version": "1.26.0",
4
4
  "description": "Zero-dependency node/edge graph editor for Truss UI",
5
5
  "type": "module",
6
6
  "license": "MIT",