@slashsuite/ui-react 0.13.1 → 0.13.2

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.
@@ -82,6 +82,6 @@ Left out, the part takes the nearest loading layout's or part's; `false` opts it
82
82
  - FlowDirection: `"down" | "right"`. `down` reads from the top, `right` from the start edge.
83
83
  - FlowLayout: `{ width: number; height: number; nodes: readonly FlowNodeBox[]; groups: readonly (FlowBox & { name: string })[]; edges: readonly FlowEdgePath[] }`. The whole diagram as laid out, in one coordinate space: `width` and `height`, a box per node and per group, and a path per arrow. `FlowDiagram` hands it to `overlay`.
84
84
  - FlowNodeBox: `{ x: number; y: number; width: number; height: number; id: string }`. A node as laid out: its `id` and its box. A group's box (`group`) is the frame round its nodes.
85
- - FlowEdgePath: `{ from: string; to: string; index: number; points: readonly FlowPoint[]; path: string; labelAt: FlowPoint | null }`. An arrow as laid out: the `from` and `to` ids, its place in `edges` (`index`), the points dagre routed it through, the SVG path drawn through them (it ends on the target's edge, at the arrowhead's tip) and its label's centre when it has one.
85
+ - FlowEdgePath: `{ from: string; to: string; index: number; points: readonly FlowPoint[]; path: string; labelAt: FlowPoint | null }`. An arrow as laid out: the `from` and `to` ids, its place in `edges` (`index`), the corners of its square route (it leaves the source's side facing the way it goes, turns only between ranks, and ends on the target's edge, at the arrowhead's tip), the SVG path through them and its label's centre when it has one.
86
86
  - FlowBox: `{ x: number; y: number; width: number; height: number }`. Where a node or a group sits, its top left corner and its size, in CSS pixels.
87
87
  - FlowPoint: `{ x: number; y: number }`. A point in the diagram's own space, in CSS pixels from its top left corner.
@@ -17,9 +17,10 @@ export type FlowNodeBox = FlowBox & {
17
17
  id: string;
18
18
  };
19
19
  /**
20
- * An arrow as laid out: the `from` and `to` ids, its place in `edges` (`index`), the points dagre
21
- * routed it through, the SVG path drawn through them (it ends on the target's edge, at the
22
- * arrowhead's tip) and its label's centre when it has one.
20
+ * An arrow as laid out: the `from` and `to` ids, its place in `edges` (`index`), the corners of its
21
+ * square route (it leaves the source's side facing the way it goes, turns only between ranks, and
22
+ * ends on the target's edge, at the arrowhead's tip), the SVG path through them and its label's
23
+ * centre when it has one.
23
24
  */
24
25
  export type FlowEdgePath = {
25
26
  from: string;
@@ -65,4 +66,3 @@ export type FlowDirection = "down" | "right";
65
66
  * typo draws no stray box. Pure: the same input gives the same layout.
66
67
  */
67
68
  export declare function layoutFlow(nodes: readonly LayoutNode[], edges: readonly LayoutEdge[], direction: FlowDirection): FlowLayout;
68
- export declare function stepPath(points: readonly FlowPoint[], direction: FlowDirection, lane?: number): string;
@@ -1,9 +1,11 @@
1
1
  /**
2
- * The layout behind `FlowDiagram`: dagre places the boxes and routes the arrows, then this file
3
- * turns its output into what the component draws. Internal: only the geometry types are exported
2
+ * The layout behind `FlowDiagram`: dagre places the boxes and gives each arrow a route,
3
+ * `flowRoute` draws the arrows round the boxes along it, and this file turns the result into what
4
+ * the component draws. Internal: only the geometry types are exported
4
5
  * from the package, for `FlowDiagram`'s `overlay`.
5
6
  */
6
7
  import { Graph, layout } from "@dagrejs/dagre";
8
+ import { routeEdges, squarePath } from "./flowRoute.js";
7
9
  /** Space round the drawing, between boxes in a rank, and between ranks. */
8
10
  const MARGIN = 16;
9
11
  const NODE_GAP = 32;
@@ -45,61 +47,78 @@ export function layoutFlow(nodes, edges, direction) {
45
47
  g.setEdge(e.from, e.to, e.label ? { width: e.label.width, height: e.label.height, labelpos: "c" } : {}, String(index));
46
48
  }
47
49
  layout(g);
48
- const box = (key) => {
50
+ return routeLayout(g, { ids, groups, kept }, direction === "right");
51
+ }
52
+ /**
53
+ * Routes dagre's laid-out graph round its boxes and labels (`flowRoute`) and reads it back in the
54
+ * diagram's own space, rounded. `across` is true when the diagram is ranked right.
55
+ */
56
+ function routeLayout(g, { ids, groups, kept }, across) {
57
+ /** Turns a point or box into rank-down space, and back: ranked right, x and y swap. */
58
+ const turn = (p) => (across ? { x: p.y, y: p.x } : p);
59
+ const turnBox = (b) => across ? { x: b.y, y: b.x, width: b.height, height: b.width } : b;
60
+ const raw = (key) => {
49
61
  const { x = 0, y = 0, width = 0, height = 0 } = g.node(key);
50
- return {
51
- x: round(x - width / 2),
52
- y: round(y - height / 2),
53
- width: round(width),
54
- height: round(height),
55
- };
62
+ return turnBox({ x: x - width / 2, y: y - height / 2, width, height });
63
+ };
64
+ const routes = kept.map(({ e, index }) => ({
65
+ e,
66
+ index,
67
+ routed: g.edge(e.from, e.to, String(index)),
68
+ }));
69
+ const labelAt = routes.map(({ e, routed }) => e.label && routed.x !== undefined && routed.y !== undefined
70
+ ? turn({ x: routed.x, y: routed.y })
71
+ : null);
72
+ const labels = routes.flatMap(({ e }, i) => {
73
+ const at = labelAt[i];
74
+ if (!e.label || !at)
75
+ return [];
76
+ const size = turnBox({ x: 0, y: 0, width: e.label.width, height: e.label.height });
77
+ return [
78
+ {
79
+ x: at.x - size.width / 2,
80
+ y: at.y - size.height / 2,
81
+ width: size.width,
82
+ height: size.height,
83
+ },
84
+ ];
85
+ });
86
+ const { lines, shift, grow } = routeEdges(new Map([...ids].map((id) => [id, raw(id)])), labels, routes.map(({ e, routed }) => ({
87
+ from: e.from,
88
+ to: e.to,
89
+ points: (routed.points ?? []).map(turn),
90
+ })));
91
+ /** A box from rank-down space, moved past any widened gap, back in the diagram's space. */
92
+ const place = (b) => {
93
+ const top = shift(b.y);
94
+ const bottom = shift(b.y + b.height);
95
+ const out = turnBox({ x: b.x, y: top, width: b.width, height: bottom - top });
96
+ return { x: round(out.x), y: round(out.y), width: round(out.width), height: round(out.height) };
56
97
  };
57
98
  const graph = g.graph();
99
+ const size = turn({ x: graph.width ?? 0, y: graph.height ?? 0 });
100
+ const grown = turn({ x: size.x, y: size.y + grow });
58
101
  return {
59
- width: Math.ceil(graph.width ?? 0),
60
- height: Math.ceil(graph.height ?? 0),
61
- nodes: [...ids].map((id) => ({ id, ...box(id) })),
62
- groups: groups.map((name) => ({ name, ...box(groupKey(name)) })),
63
- edges: kept.map(({ e, index }) => {
64
- const routed = g.edge(e.from, e.to, String(index));
65
- const points = (routed.points ?? []).map((p) => ({ x: round(p.x), y: round(p.y) }));
102
+ width: Math.ceil(grown.x),
103
+ height: Math.ceil(grown.y),
104
+ nodes: [...ids].map((id) => ({ id, ...place(raw(id)) })),
105
+ groups: groups.map((name) => ({ name, ...place(raw(groupKey(name))) })),
106
+ edges: routes.map(({ e, index }, i) => {
107
+ const points = (lines[i] ?? []).map((p) => {
108
+ const out = turn(p);
109
+ return { x: round(out.x), y: round(out.y) };
110
+ });
111
+ const at = labelAt[i];
66
112
  return {
67
113
  from: e.from,
68
114
  to: e.to,
69
115
  index,
70
116
  points,
71
- path: stepPath(points, direction, LANES[index % LANES.length]),
72
- labelAt: e.label && routed.x !== undefined && routed.y !== undefined
73
- ? { x: round(routed.x), y: round(routed.y) }
117
+ path: squarePath(points),
118
+ labelAt: at
119
+ ? (({ x, y }) => ({ x: round(x), y: round(y) }))(turn({ x: at.x, y: shift(at.y) }))
74
120
  : null,
75
121
  };
76
122
  }),
77
123
  };
78
124
  }
79
- /**
80
- * A square path through dagre's route points: each step leaves along the rank `direction`, turns
81
- * once at `lane` of the way across (0.5 is halfway), and arrives along it again, so every arrow is
82
- * made of straight runs at right angles and ends exactly on the last point, where its arrowhead
83
- * sits. Arrows between the same two ranks take different lanes, so their cross runs do not overlap.
84
- */
85
- /** Where each arrow turns between two ranks, by its index: spread so parallel runs stay apart. */
86
- const LANES = [0.5, 0.3, 0.7, 0.4, 0.6];
87
- export function stepPath(points, direction, lane = 0.5) {
88
- const [first, ...rest] = points;
89
- if (!first)
90
- return "";
91
- let d = `M${first.x},${first.y}`;
92
- let from = first;
93
- for (const to of rest) {
94
- if (direction === "right") {
95
- const mid = round(from.x + (to.x - from.x) * lane);
96
- d += `H${mid}V${to.y}H${to.x}`;
97
- }
98
- else {
99
- const mid = round(from.y + (to.y - from.y) * lane);
100
- d += `V${mid}H${to.x}V${to.y}`;
101
- }
102
- from = to;
103
- }
104
- return d;
105
- }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * How `FlowDiagram`'s arrows are drawn round its boxes, after dagre has placed them. Internal.
3
+ *
4
+ * dagre ranks the boxes and gives each arrow a route: a point on the source, one point in every
5
+ * rank it passes (each clear of the boxes in that rank) and a point on the target. Drawn as square
6
+ * steps that turn halfway between two points, an arrow can turn inside a rank of tall boxes and run
7
+ * through one, and an arrow that leaves a box's side runs along its border. So this routes every
8
+ * arrow again, in the rank direction:
9
+ *
10
+ * - it leaves its source through the side facing its next point and enters its target through the
11
+ * side facing its last one, at ports spread along that side, so no two arrows share a port and
12
+ * none runs along a border;
13
+ * - it runs along the rank direction through each of dagre's points, and turns only in a gap
14
+ * between ranks, where no box and no arrow label is;
15
+ * - the turns in one gap are spread over tracks, so two runs across the same gap never lie on one
16
+ * line, and a gap too narrow for its tracks is widened, moving everything past it along.
17
+ *
18
+ * Every function here works in rank-down space (ranks stacked along y); a diagram ranked right is
19
+ * turned into it and back.
20
+ */
21
+ import type { FlowBox as Rect, FlowPoint as Point } from "./flowLayout.js";
22
+ /** One arrow to route: its ends, and dagre's points from the source to the target. */
23
+ type RouteEdge = {
24
+ from: string;
25
+ to: string;
26
+ points: readonly Point[];
27
+ };
28
+ /** The routed diagram: every arrow as a square path's corners, and how to move a y past widened gaps. */
29
+ type Routed = {
30
+ lines: Point[][];
31
+ shift: (y: number) => number;
32
+ grow: number;
33
+ };
34
+ /**
35
+ * Routes every arrow round the boxes and labels (rank-down space): see the file's comment. The
36
+ * lines come back in `edges`' order, in the space after the gaps are widened; `shift` moves a y
37
+ * from before into it, and `grow` is how much taller the diagram became.
38
+ */
39
+ export declare function routeEdges(boxes: ReadonlyMap<string, Rect>, labels: readonly Rect[], edges: readonly RouteEdge[]): Routed;
40
+ /** The SVG path through square corners: a move, then one straight run to each next corner. */
41
+ export declare function squarePath(points: readonly Point[]): string;
42
+ export {};
@@ -0,0 +1,238 @@
1
+ /**
2
+ * How `FlowDiagram`'s arrows are drawn round its boxes, after dagre has placed them. Internal.
3
+ *
4
+ * dagre ranks the boxes and gives each arrow a route: a point on the source, one point in every
5
+ * rank it passes (each clear of the boxes in that rank) and a point on the target. Drawn as square
6
+ * steps that turn halfway between two points, an arrow can turn inside a rank of tall boxes and run
7
+ * through one, and an arrow that leaves a box's side runs along its border. So this routes every
8
+ * arrow again, in the rank direction:
9
+ *
10
+ * - it leaves its source through the side facing its next point and enters its target through the
11
+ * side facing its last one, at ports spread along that side, so no two arrows share a port and
12
+ * none runs along a border;
13
+ * - it runs along the rank direction through each of dagre's points, and turns only in a gap
14
+ * between ranks, where no box and no arrow label is;
15
+ * - the turns in one gap are spread over tracks, so two runs across the same gap never lie on one
16
+ * line, and a gap too narrow for its tracks is widened, moving everything past it along.
17
+ *
18
+ * Every function here works in rank-down space (ranks stacked along y); a diagram ranked right is
19
+ * turned into it and back.
20
+ */
21
+ /** The space between two runs across one gap, and between a run and the gap's edges. */
22
+ const TRACK = 8;
23
+ /** How far a port keeps from a box's corner, and the least space between two ports. */
24
+ const PORT_INSET = 12;
25
+ /** How far a self loop reaches past its box. */
26
+ const LOOP_REACH = 16;
27
+ const round = (n) => Math.round(n * 10) / 10;
28
+ /** The y ranges no box or label covers, between the first and the last. */
29
+ function gapsBetween(obstacles) {
30
+ const spans = obstacles
31
+ .map((r) => ({ top: r.y, bottom: r.y + r.height }))
32
+ .toSorted((a, b) => a.top - b.top);
33
+ const gaps = [];
34
+ let reach = Number.NEGATIVE_INFINITY;
35
+ for (const s of spans) {
36
+ if (s.top > reach && reach > Number.NEGATIVE_INFINITY)
37
+ gaps.push({ top: reach, bottom: s.top });
38
+ reach = Math.max(reach, s.bottom);
39
+ }
40
+ return gaps;
41
+ }
42
+ /** Whether a point lies past a box's middle, in the rank direction. */
43
+ const below = (box, p) => p.y > box.y + box.height / 2;
44
+ /**
45
+ * Where each arrow leaves and enters its boxes: on the side facing the next point along it,
46
+ * spread evenly over that side in the order of where the arrows head, so they do not cross there.
47
+ */
48
+ function placePorts(boxes, edges) {
49
+ const ports = [];
50
+ edges.forEach((e, edge) => {
51
+ const first = e.points[1] ?? e.points[0];
52
+ const last = e.points.at(-2) ?? e.points.at(-1);
53
+ const from = boxes.get(e.from);
54
+ const to = boxes.get(e.to);
55
+ if (e.from === e.to || !first || !last || !from || !to)
56
+ return;
57
+ ports.push({
58
+ edge,
59
+ end: "from",
60
+ box: e.from,
61
+ side: below(from, first) ? "bottom" : "top",
62
+ toward: first.x,
63
+ }, { edge, end: "to", box: e.to, side: below(to, last) ? "bottom" : "top", toward: last.x });
64
+ });
65
+ const ends = edges.map(() => []);
66
+ const bySide = new Map();
67
+ for (const p of ports) {
68
+ const key = `${p.box}\u0000${p.side}`;
69
+ bySide.set(key, [...(bySide.get(key) ?? []), p]);
70
+ }
71
+ for (const group of bySide.values()) {
72
+ const [head] = group;
73
+ const box = head && boxes.get(head.box);
74
+ if (!head || !box)
75
+ continue;
76
+ const sorted = group.toSorted((a, b) => a.toward - b.toward || a.edge - b.edge);
77
+ const room = Math.max(0, box.width - 2 * PORT_INSET);
78
+ const step = sorted.length > 1 ? room / (sorted.length - 1) : 0;
79
+ sorted.forEach((p, i) => {
80
+ const x = sorted.length === 1
81
+ ? Math.min(Math.max(p.toward, box.x + PORT_INSET), box.x + box.width - PORT_INSET)
82
+ : box.x + PORT_INSET + i * step;
83
+ const y = p.side === "top" ? box.y : box.y + box.height;
84
+ const at = ends[p.edge];
85
+ if (at)
86
+ at[p.end === "from" ? 0 : 1] = { x: round(x), y };
87
+ });
88
+ }
89
+ return ends;
90
+ }
91
+ /** Gives each run in a gap the lowest track whose runs it does not overlap, and counts the tracks. */
92
+ function assignTracks(runs, gaps) {
93
+ const counts = Array.from({ length: gaps }, () => 0);
94
+ const ends = Array.from({ length: gaps }, () => []);
95
+ for (const run of runs.toSorted((a, b) => a.lo - b.lo)) {
96
+ const lanes = ends[run.gap] ?? [];
97
+ let track = lanes.findIndex((end) => end + TRACK <= run.lo);
98
+ if (track === -1)
99
+ track = lanes.length;
100
+ lanes[track] = run.hi;
101
+ ends[run.gap] = lanes;
102
+ run.track = track;
103
+ counts[run.gap] = Math.max(counts[run.gap] ?? 0, track + 1);
104
+ }
105
+ return counts;
106
+ }
107
+ /** The corners of a square loop out of a box's far side and back. */
108
+ function loop(box, points) {
109
+ const ys = points.map((p) => p.y);
110
+ const reach = Math.max(box.x + box.width + LOOP_REACH, ...points.map((p) => p.x));
111
+ const cy = box.y + box.height / 2;
112
+ const half = Math.min(box.height / 2 - 4, Math.max(8, (Math.max(...ys) - Math.min(...ys)) / 2));
113
+ const side = box.x + box.width;
114
+ return [
115
+ { x: side, y: cy - half },
116
+ { x: reach, y: cy - half },
117
+ { x: reach, y: cy + half },
118
+ { x: side, y: cy + half },
119
+ ];
120
+ }
121
+ /** Whether three corners lie on one straight line across or along. */
122
+ const inLine = (a, b, c) => (a.x === b.x && b.x === c.x) || (a.y === b.y && b.y === c.y);
123
+ /** Drops a corner that does not turn: a repeat, or one in line with its neighbours. */
124
+ function tidy(points) {
125
+ const out = [];
126
+ for (const p of points) {
127
+ const last = out.at(-1);
128
+ const before = out.at(-2);
129
+ if (last && last.x === p.x && last.y === p.y)
130
+ continue;
131
+ if (last && before && inLine(before, last, p))
132
+ out[out.length - 1] = p;
133
+ else
134
+ out.push(p);
135
+ }
136
+ return out;
137
+ }
138
+ /** The gap a step between two points turns in: the one it overlaps most, if any. */
139
+ function gapFor(gaps, a, b) {
140
+ const lo = Math.min(a.y, b.y);
141
+ const hi = Math.max(a.y, b.y);
142
+ return gaps
143
+ .map((g, index) => ({ index, size: Math.min(g.bottom, hi) - Math.max(g.top, lo) }))
144
+ .filter((g) => g.size > 0)
145
+ .toSorted((p, q) => q.size - p.size)[0]?.index;
146
+ }
147
+ /**
148
+ * An arrow's draft corners through `via`: a step that moves across turns on a run in the gap it
149
+ * overlaps most (added to `runs`), or halfway along when no gap lies between its points.
150
+ */
151
+ function draftSteps(via, gaps, runs) {
152
+ const [start] = via;
153
+ const out = start ? [start] : [];
154
+ for (let k = 1; k < via.length; k++) {
155
+ const a = via[k - 1];
156
+ const b = via[k];
157
+ if (!a || !b)
158
+ continue;
159
+ const gap = a.x === b.x ? undefined : gapFor(gaps, a, b);
160
+ if (a.x === b.x)
161
+ out.push(b);
162
+ else if (gap === undefined)
163
+ out.push({ x: a.x, y: (a.y + b.y) / 2 }, { x: b.x, y: (a.y + b.y) / 2 }, b);
164
+ else {
165
+ runs.push({ gap, lo: Math.min(a.x, b.x), hi: Math.max(a.x, b.x), track: 0 });
166
+ out.push({ run: runs.length - 1, a, b }, b);
167
+ }
168
+ }
169
+ return out;
170
+ }
171
+ /**
172
+ * Routes every arrow round the boxes and labels (rank-down space): see the file's comment. The
173
+ * lines come back in `edges`' order, in the space after the gaps are widened; `shift` moves a y
174
+ * from before into it, and `grow` is how much taller the diagram became.
175
+ */
176
+ export function routeEdges(boxes, labels, edges) {
177
+ const gaps = gapsBetween([...boxes.values(), ...labels]);
178
+ const ends = placePorts(boxes, edges);
179
+ const runs = [];
180
+ /**
181
+ * Whether one of dagre's points lies beside a box or a label rather than in a gap. A point in a
182
+ * gap (a group's border rank) has nothing to clear, and passing through it would make the arrow
183
+ * turn on a track elsewhere in that gap and double back.
184
+ */
185
+ const inRank = (p) => !gaps.some((g) => p.y > g.top && p.y < g.bottom);
186
+ /** Each arrow's corners, with a run's index in place of the y it turns at. */
187
+ const drafts = edges.map((e, i) => {
188
+ const box = boxes.get(e.from);
189
+ if (e.from === e.to)
190
+ return box ? loop(box, e.points) : [];
191
+ const [start, end] = ends[i] ?? [];
192
+ if (!start || !end)
193
+ return [...e.points];
194
+ return draftSteps([start, ...e.points.slice(1, -1).filter(inRank), end], gaps, runs);
195
+ });
196
+ const tracks = assignTracks(runs, gaps.length);
197
+ const widen = gaps.map((g, i) => Math.max(0, ((tracks[i] ?? 0) + 1) * TRACK - (g.bottom - g.top)));
198
+ const shift = (y) => y + gaps.reduce((sum, g, i) => sum + (y > g.top + 0.01 ? (widen[i] ?? 0) : 0), 0);
199
+ const trackY = (run) => {
200
+ const g = gaps[run.gap];
201
+ if (!g)
202
+ return 0;
203
+ const top = shift(g.top);
204
+ const height = g.bottom - g.top + (widen[run.gap] ?? 0);
205
+ return round(top + ((run.track + 1) * height) / ((tracks[run.gap] ?? 0) + 1));
206
+ };
207
+ /** A draft corner's points: a turn becomes its two corners on its run's track. */
208
+ const corners = (c) => {
209
+ if (!("run" in c))
210
+ return [{ x: c.x, y: round(shift(c.y)) }];
211
+ const run = runs[c.run];
212
+ if (!run)
213
+ return [];
214
+ const y = trackY(run);
215
+ return [
216
+ { x: c.a.x, y },
217
+ { x: c.b.x, y },
218
+ ];
219
+ };
220
+ const lines = drafts.map((draft) => tidy(draft.flatMap(corners)));
221
+ return { lines, shift, grow: widen.reduce((a, b) => a + b, 0) };
222
+ }
223
+ /** The SVG path through square corners: a move, then one straight run to each next corner. */
224
+ export function squarePath(points) {
225
+ const [first, ...rest] = points;
226
+ if (!first)
227
+ return "";
228
+ let d = `M${first.x},${first.y}`;
229
+ let at = first;
230
+ for (const p of rest) {
231
+ if (p.x !== at.x && p.y !== at.y)
232
+ d += `V${p.y}H${p.x}`;
233
+ else
234
+ d += p.x === at.x ? `V${p.y}` : `H${p.x}`;
235
+ at = p;
236
+ }
237
+ return d;
238
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slashsuite/ui-react",
3
- "version": "0.13.1",
3
+ "version": "0.13.2",
4
4
  "description": "UISlash React components: primitives, typography roles and a safe Markdown renderer, written against Tailwind v4 utilities. Pair with a theme from @slashsuite/themes.",
5
5
  "keywords": [
6
6
  "components",