@re-cinq/floor-graph 0.11.4 → 0.13.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/README.md +7 -1
- package/dist/DanglingStub.d.ts +3 -2
- package/dist/DanglingStub.js +6 -2
- package/dist/ExitDot.d.ts +1 -1
- package/dist/ExitDot.js +3 -2
- package/dist/GraphNode.js +3 -1
- package/dist/RunGraphView.d.ts +2 -0
- package/dist/RunGraphView.js +17 -7
- package/dist/elk-graph.d.ts +10 -0
- package/dist/elk-graph.js +23 -5
- package/dist/elk-layout.d.ts +1 -0
- package/dist/elk-layout.js +36 -13
- package/dist/elk-snake-graph.d.ts +41 -0
- package/dist/elk-snake-graph.js +179 -0
- package/dist/elk-snake-layout.d.ts +3 -0
- package/dist/elk-snake-layout.js +48 -0
- package/dist/graph-markers.js +2 -1
- package/dist/layout-geometry.d.ts +12 -0
- package/dist/layout-geometry.js +39 -1
- package/dist/run-graph.module.css +10 -0
- package/dist/run-graph.test-helpers.d.ts +3 -0
- package/dist/run-graph.test-helpers.js +3 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @re-cinq/floor-graph
|
|
2
2
|
|
|
3
|
-
A React view of an assembly line as a graph: layout, pan and zoom, nodes and edges. [elkjs](https://github.com/kieler/elkjs) lays it out with stations left to right,
|
|
3
|
+
A React view of an assembly line as a graph: layout, pan and zoom, nodes and edges. [elkjs](https://github.com/kieler/elkjs) lays it out with stations left to right, and a longer line wraps into rows that alternate direction. Every edge gets its own ports and is routed at right angles around stations, labels and marker titles. The package ships compiled JS and its CSS modules, so use it from a bundler such as Vite.
|
|
4
4
|
|
|
5
5
|
```
|
|
6
6
|
npm install @re-cinq/floor-graph
|
|
@@ -19,6 +19,12 @@ The controls hold zoom, fit and two toggles. The `mainPathOnly` and `compactStop
|
|
|
19
19
|
- "Main path" fades every station and edge off the path from the entry along `success`, or along the first `always` when nothing succeeds.
|
|
20
20
|
- "Compact stops" draws a station with one edge in and one `always` edge out as a stop on the track, unless it is the entry or is started from outside.
|
|
21
21
|
|
|
22
|
+
## Long lines
|
|
23
|
+
|
|
24
|
+
With `fit="width"` a long line wraps into as many columns as fit the canvas's width at natural size (the width rounded down to 40 px, so a resize lays it out again only past a step), and the canvas is as tall as the drawing; the page caps the height, for example with a `max-height` on the element holding the view, and the viewport fits the drawing into what is left. With the default, `fit="box"`, a long line wraps into rows chosen from the shape of the box it is drawn in: the view measures its canvas and picks the row count whose drawing it can fit largest, the fewest rows on a tie, so a wide box gets one or two long rows and a tall one many short ones. A box that changes shape lays the line out again, in steps of a quarter of a doubling of its width over its height so a small resize does not. Until the box is measured, and wherever it cannot be (on the server, without `ResizeObserver`), a row holds 5 columns. The columns split evenly, so 13 in 3 rows become 5 + 4 + 4. The first row runs left to right, the next right to left, and so on. An edge between rows leaves its row at the far end and enters the next row at its start.
|
|
25
|
+
|
|
26
|
+
Within a row, every edge that leaves the main path for the line's exit meets one shared end: a vertical bar with the end ring in its middle, 60 px tall for each line, so each line arrives in its own lane. An end dot or stub stays in its station's row.
|
|
27
|
+
|
|
22
28
|
## Selection and keyboard
|
|
23
29
|
|
|
24
30
|
`onSelectLine(key)` and `selectedLineKey` select an edge by its `from|to|event` key (`edgeKeyOf`).
|
package/dist/DanglingStub.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { ElkLayout } from "./elk-layout";
|
|
2
|
+
type StubNode = ElkLayout["nodes"][number];
|
|
2
3
|
interface DanglingStubProps {
|
|
3
|
-
node:
|
|
4
|
+
node: StubNode;
|
|
4
5
|
}
|
|
5
|
-
/** Where an outcome leads to a line nobody starts: a short end bar with a warning beside it. */
|
|
6
|
+
/** Where an outcome leads to a line nobody starts: a short end bar with a warning beside it, on the side the line does not arrive from. */
|
|
6
7
|
export default function DanglingStub({ node }: DanglingStubProps): import("react").JSX.Element;
|
|
7
8
|
export {};
|
package/dist/DanglingStub.js
CHANGED
|
@@ -3,8 +3,12 @@ import styles from "./run-graph.module.css";
|
|
|
3
3
|
const BAR_HALF_LENGTH = 7;
|
|
4
4
|
const LABEL_GAP = 8;
|
|
5
5
|
const LABEL_DROP = 12;
|
|
6
|
-
/** Where an outcome leads to a line nobody starts: a short end bar with a warning beside it. */
|
|
6
|
+
/** Where an outcome leads to a line nobody starts: a short end bar with a warning beside it, on the side the line does not arrive from. */
|
|
7
7
|
export default function DanglingStub({ node }) {
|
|
8
|
-
|
|
8
|
+
const isLeftward = arrivesFromEast(node);
|
|
9
|
+
return (_jsxs("g", { "data-stub": node.id.replace(/:stub$/, ""), children: [_jsx("line", { className: styles.stubBar, x1: node.x, x2: node.x, y1: node.y - BAR_HALF_LENGTH, y2: node.y + BAR_HALF_LENGTH }), _jsx("text", { className: styles.stubText, x: isLeftward ? node.x - LABEL_GAP : node.x + LABEL_GAP, y: node.y + LABEL_DROP, textAnchor: isLeftward ? "end" : undefined, children: "\u26A0 nobody starts on it" })] }));
|
|
10
|
+
}
|
|
11
|
+
function arrivesFromEast({ x: centreX, ports }) {
|
|
12
|
+
return ports.some((port) => port.x > centreX);
|
|
9
13
|
}
|
|
10
14
|
//# sourceMappingURL=DanglingStub.js.map
|
package/dist/ExitDot.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ interface ExitDotProps {
|
|
|
3
3
|
node: ElkLayout["nodes"][number];
|
|
4
4
|
exit: string;
|
|
5
5
|
}
|
|
6
|
-
/**
|
|
6
|
+
/** The shared end of a row's off-path lines: a ring with a dot, like the exit terminal, and a bar joining the lines' ports. */
|
|
7
7
|
export default function ExitDot({ node, exit }: ExitDotProps): import("react").JSX.Element;
|
|
8
8
|
export {};
|
package/dist/ExitDot.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import styles from "./run-graph.module.css";
|
|
3
|
-
/**
|
|
3
|
+
/** The shared end of a row's off-path lines: a ring with a dot, like the exit terminal, and a bar joining the lines' ports. */
|
|
4
4
|
export default function ExitDot({ node, exit }) {
|
|
5
|
-
|
|
5
|
+
const portYs = node.ports.map((port) => port.y);
|
|
6
|
+
return (_jsxs("g", { "data-exit-dot": node.id.replace(/:exit$/, ""), children: [_jsx("title", { children: `ends the run at ${exit}` }), _jsx("line", { className: styles.exitBar, x1: node.x, x2: node.x, y1: Math.min(...portYs), y2: Math.max(...portYs) }), _jsx("circle", { className: styles.exitRing, cx: node.x, cy: node.y, r: 6 }), _jsx("circle", { className: styles.exitCore, cx: node.x, cy: node.y, r: 2.5 })] }));
|
|
6
7
|
}
|
|
7
8
|
//# sourceMappingURL=ExitDot.js.map
|
package/dist/GraphNode.js
CHANGED
|
@@ -71,8 +71,10 @@ function CardBody({ node, family, subtitle, height, isUnreachable, }) {
|
|
|
71
71
|
const { top, leftEdge } = box;
|
|
72
72
|
return (_jsxs(_Fragment, { children: [_jsx(NodeBox, { ...box, height: height, isUnreachable: isUnreachable }), _jsx(TypeGlyph, { family: family, cx: leftEdge + GLYPH_INSET, cy: top + GLYPH_INSET }), _jsx(NodeTitle, { title: nodeTitle(node.id), subtitle: subtitle, isUnreachable: isUnreachable, centerX: node.x, centerY: node.y })] }));
|
|
73
73
|
}
|
|
74
|
+
const TITLE_BASELINE_INSET = 4;
|
|
74
75
|
function TerminalBody({ node }) {
|
|
75
|
-
|
|
76
|
+
const titleY = node.titleBox ? node.titleBox.maxY - TITLE_BASELINE_INSET : node.y + 24;
|
|
77
|
+
return (_jsxs(_Fragment, { children: [_jsx("circle", { className: styles.terminalRing, cx: node.x, cy: node.y, r: 11 }), _jsx("circle", { className: styles.terminalCore, cx: node.x, cy: node.y, r: 5 }), _jsx("text", { className: styles.nodeId, x: node.x, y: titleY, textAnchor: "middle", children: nodeTitle(node.id) })] }));
|
|
76
78
|
}
|
|
77
79
|
// The stop's circle centre from the box's top, and the title's drop below it.
|
|
78
80
|
const STOP_RADIUS = 12;
|
package/dist/RunGraphView.d.ts
CHANGED
|
@@ -25,5 +25,7 @@ export interface RunGraphViewProps {
|
|
|
25
25
|
waiting?: Readonly<Record<string, string>>;
|
|
26
26
|
/** Draws the whole line or only the part the run walked; defaults to the walked part when a run is given. */
|
|
27
27
|
show?: "all" | "visited";
|
|
28
|
+
/** "box" (the default) wraps a long line into the rows that fit the box's shape; "width" wraps it into as many columns as fit the box's width and makes the canvas as tall as the drawing, for a page that caps the height itself. */
|
|
29
|
+
fit?: "box" | "width";
|
|
28
30
|
}
|
|
29
31
|
export default function RunGraphView(givenProps: RunGraphViewProps): import("react").JSX.Element;
|
package/dist/RunGraphView.js
CHANGED
|
@@ -3,6 +3,8 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
|
|
|
3
3
|
import { useCallback, useMemo, useState } from "react";
|
|
4
4
|
import { useToggle } from "./use-toggle";
|
|
5
5
|
import { useElkLayout } from "./use-elk-layout";
|
|
6
|
+
import { useElementSize } from "./use-element-size";
|
|
7
|
+
import { aspectBucketOf, widthBucketOf } from "./elk-snake-graph";
|
|
6
8
|
import { drawingOf } from "./run-graph-drawing";
|
|
7
9
|
import { mainPathOf } from "./metro/main-path";
|
|
8
10
|
import { reachabilityOf } from "./metro/reachability";
|
|
@@ -32,7 +34,7 @@ export default function RunGraphView(givenProps) {
|
|
|
32
34
|
if (graph.nodes.length === 0) {
|
|
33
35
|
return _jsx(EmptyGraph, {});
|
|
34
36
|
}
|
|
35
|
-
return (_jsxs("section", { className: styles.panel, "aria-label": `Graph of ${props.definition.name}`, children: [_jsx(GraphCanvas, { view: props, graph: graph }), _jsx(RunGraphLegend, { families: familiesOf(graph) })] }));
|
|
37
|
+
return (_jsxs("section", { className: props.fit === "width" ? `${styles.panel} ${styles.fitWidth}` : styles.panel, "aria-label": `Graph of ${props.definition.name}`, children: [_jsx(GraphCanvas, { view: props, graph: graph }), _jsx(RunGraphLegend, { families: familiesOf(graph) })] }));
|
|
36
38
|
}
|
|
37
39
|
/** The whole definition or the part the run visited, and the toggle between them, offered only with a run. Until it is pressed, the default follows the props, so a run that arrives after the first draw still opens on its visited part. */
|
|
38
40
|
function useWholeLine({ definition, run, show }) {
|
|
@@ -48,8 +50,8 @@ function useWholeLine({ definition, run, show }) {
|
|
|
48
50
|
}
|
|
49
51
|
/** The drawing in its viewport, and the tooltip of the node under the pointer or the focus (run-viz FR4.1h). */
|
|
50
52
|
function GraphCanvas({ view, graph }) {
|
|
51
|
-
const { laid, tooltip, marks, toggles, setHoveredId } = useGraphCanvasState(view, graph);
|
|
52
|
-
return (_jsxs(_Fragment, { children: [_jsx("div", { className: styles.canvas, ...tooltip.handlers, children: laid && (_jsx(GraphViewport, { drawing: laid.view, reveal: revealOf(laid, view.selectedNodeId), coveredBelow: view.coveredBelow, ...toggles, children: _jsx(GraphSvg, { laid: laid, ...drawnFrom(view), describedNode: describedNodeOf(tooltip), onHover: setHoveredId, marks: marks }) })) }), _jsx(GraphTooltip, { ...tooltip, definition: view.definition })] }));
|
|
53
|
+
const { canvas, laid, tooltip, marks, toggles, setHoveredId } = useGraphCanvasState(view, graph);
|
|
54
|
+
return (_jsxs(_Fragment, { children: [_jsx("div", { "data-graph-canvas": true, className: styles.canvas, ...canvas, ...tooltip.handlers, children: laid && (_jsx(GraphViewport, { drawing: laid.view, reveal: revealOf(laid, view.selectedNodeId), coveredBelow: view.coveredBelow, ...toggles, children: _jsx(GraphSvg, { laid: laid, ...drawnFrom(view), describedNode: describedNodeOf(tooltip), onHover: setHoveredId, marks: marks }) })) }), _jsx(GraphTooltip, { ...tooltip, definition: view.definition })] }));
|
|
53
55
|
}
|
|
54
56
|
/** What the drawing needs from the view: the line's name and exit, and what is selected and how to select. */
|
|
55
57
|
function drawnFrom(view) {
|
|
@@ -64,12 +66,13 @@ function drawnFrom(view) {
|
|
|
64
66
|
selectedNodeId,
|
|
65
67
|
};
|
|
66
68
|
}
|
|
67
|
-
/** Everything the canvas draws from: the laid-out drawing, the tooltip, the marks on its elements and the viewport's two toggles. */
|
|
69
|
+
/** Everything the canvas draws from: the laid-out drawing, wrapped to the canvas's shape, the tooltip, the marks on its elements and the viewport's two toggles. */
|
|
68
70
|
function useGraphCanvasState(view, graph) {
|
|
71
|
+
const { canvasRef, box } = useCanvasBox(view.fit);
|
|
69
72
|
const { unreachable, mainPath, mainPathOnly, toggleMainPath, taken, visits } = useLineMarkings(view);
|
|
70
73
|
const { lit, setHoveredId } = useHoverNeighbourhood(view.definition);
|
|
71
74
|
const keyboard = useKeyboardCursor(view.definition);
|
|
72
|
-
const { elkLayout, changes, stops, compactStops, toggleCompactStops } = useStopsLayout(view, graph, { visits, taken });
|
|
75
|
+
const { elkLayout, changes, stops, compactStops, toggleCompactStops } = useStopsLayout(view, graph, { visits, taken, box });
|
|
73
76
|
const laid = useMemo(() => elkLayout && drawingOf(elkLayout, graph, view.definition.name, stops), [elkLayout, graph, view.definition.name, stops]);
|
|
74
77
|
const toggles = {
|
|
75
78
|
mainPathOn: mainPathOnly,
|
|
@@ -80,6 +83,7 @@ function useGraphCanvasState(view, graph) {
|
|
|
80
83
|
onToggleWholeLine: view.onToggleWholeLine,
|
|
81
84
|
};
|
|
82
85
|
return {
|
|
86
|
+
canvas: { ref: canvasRef, style: view.fit === "width" && laid ? { height: laid.view.height } : undefined },
|
|
83
87
|
laid,
|
|
84
88
|
tooltip: useNodeTooltip(),
|
|
85
89
|
marks: { lit, mainPath, unreachable, keyboard, ...changes, taken, visits, takenEdgeKey: view.takenEdge && edgeKeyOf(view.takenEdge) },
|
|
@@ -87,6 +91,12 @@ function useGraphCanvasState(view, graph) {
|
|
|
87
91
|
setHoveredId,
|
|
88
92
|
};
|
|
89
93
|
}
|
|
94
|
+
/** The canvas, measured, and what the layout wraps to from its size: the width alone when the drawing sets the height, else the shape. */
|
|
95
|
+
function useCanvasBox(fit) {
|
|
96
|
+
const [canvasRef, canvasSize] = useElementSize();
|
|
97
|
+
const box = fit === "width" ? { boxWidth: widthBucketOf(canvasSize) } : { boxAspect: aspectBucketOf(canvasSize) };
|
|
98
|
+
return { canvasRef, box };
|
|
99
|
+
}
|
|
90
100
|
/** The stations no start leads to, the lines a run took, and the main line of the definition while `mainPathOnly` is set (else null). */
|
|
91
101
|
function useLineMarkings({ definition, run, waiting, mainPathOnly: initialMainPathOnly, }) {
|
|
92
102
|
const [mainPathOnly, toggleMainPath] = useToggle({
|
|
@@ -104,11 +114,11 @@ function useLineMarkings({ definition, run, waiting, mainPathOnly: initialMainPa
|
|
|
104
114
|
};
|
|
105
115
|
}
|
|
106
116
|
/** The layout, asked to compact its stops when `compactStops` is set, and the stations drawn as stops: the pass-through ones. */
|
|
107
|
-
function useStopsLayout({ definition, compactStops: initialCompactStops }, graph, { visits, taken }) {
|
|
117
|
+
function useStopsLayout({ definition, compactStops: initialCompactStops }, graph, { visits, taken, box }) {
|
|
108
118
|
const [compactStops, toggleCompactStops] = useToggle({
|
|
109
119
|
initiallyOn: initialCompactStops,
|
|
110
120
|
});
|
|
111
|
-
const layoutOptions = useMemo(() => ({ compactStops, takenCounts: taken }), [compactStops, taken]);
|
|
121
|
+
const layoutOptions = useMemo(() => ({ compactStops, takenCounts: taken, ...box }), [compactStops, taken, box.boxAspect, box.boxWidth]);
|
|
112
122
|
const stops = useMemo(() => stopsOf({ definition, compactStops }, graph), [definition, compactStops, graph]);
|
|
113
123
|
const elkLayout = useElkLayout(definition, useNodeSize(definition, visits), undefined, layoutOptions);
|
|
114
124
|
return {
|
package/dist/elk-graph.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { ElkNode } from "elkjs/lib/elk-api";
|
|
2
2
|
import type { AssemblyLineDefinition } from "./assembly-line-definition";
|
|
3
|
+
export declare const NODE_SIZE: {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
};
|
|
3
7
|
/** Builds the ELK input graph for a line: one child per node, markers 22x22, others 140x60 unless sized. */
|
|
4
8
|
export interface NodeSize {
|
|
5
9
|
width: number;
|
|
@@ -12,5 +16,11 @@ export interface ElkGraphOptions {
|
|
|
12
16
|
compactStops?: boolean;
|
|
13
17
|
/** How many times a run took each line by edge key; a line taken twice or more reserves room for its count. */
|
|
14
18
|
takenCounts?: ReadonlyMap<string, number>;
|
|
19
|
+
/** The width over the height of the box the line is drawn in; a long line wraps into the rows that fill it best. */
|
|
20
|
+
boxAspect?: number;
|
|
21
|
+
/** The width of the box when the drawing sets its height: a long line wraps into as many columns as fit it at natural size, before boxAspect. */
|
|
22
|
+
boxWidth?: number;
|
|
15
23
|
}
|
|
16
24
|
export declare function elkGraphOf(definition: AssemblyLineDefinition, size?: NodeSize, options?: ElkGraphOptions): ElkNode;
|
|
25
|
+
/** Whether a laid-out child is an exit dot, or the shared end that merges a row's exit dots. */
|
|
26
|
+
export declare const isExitId: (id: string) => boolean;
|
package/dist/elk-graph.js
CHANGED
|
@@ -7,7 +7,7 @@ import { isPassThrough } from "./metro/pass-through";
|
|
|
7
7
|
import { countWidthOf } from "./metro/taken-count";
|
|
8
8
|
import { nodeTitle } from "./node-tooltip";
|
|
9
9
|
import { typeFamilyOf } from "./node-type-family";
|
|
10
|
-
const NODE_SIZE = { width: 140, height: 60 };
|
|
10
|
+
export const NODE_SIZE = { width: 140, height: 60 };
|
|
11
11
|
// Card height keeps ELK from centring stations on a shorter marker, which made sub-16px turns.
|
|
12
12
|
const MARKER_SIZE = { width: 22, height: NODE_SIZE.height };
|
|
13
13
|
const PORT_SIZE = 8;
|
|
@@ -23,6 +23,11 @@ const SOURCE_STUB = { idSuffix: "source-stub", direction: "out", side: "EAST" };
|
|
|
23
23
|
const LABEL_HEIGHT = 16;
|
|
24
24
|
const LABEL_CHAR_WIDTH = 7.3;
|
|
25
25
|
const LABEL_PADDING = 6;
|
|
26
|
+
// Match the 14 px bold title font under a marker.
|
|
27
|
+
const TITLE_CHAR_WIDTH = 8;
|
|
28
|
+
const TITLE_PADDING = 8;
|
|
29
|
+
const TITLE_HEIGHT = 16;
|
|
30
|
+
const FIXED_SIDE = { "elk.portConstraints": "FIXED_SIDE" };
|
|
26
31
|
const LAYOUT_OPTIONS = {
|
|
27
32
|
"elk.algorithm": "layered",
|
|
28
33
|
"elk.direction": "RIGHT",
|
|
@@ -48,7 +53,8 @@ export function elkGraphOf(definition, size = NODE_SIZE, options = {}) {
|
|
|
48
53
|
...definition.nodes.map((node) => options.compactStops && isPassThrough(definition, node.id) ? stopOf(definition, node.id) : ({
|
|
49
54
|
id: node.id,
|
|
50
55
|
...(typeFamilyOf(node.type) === "marker" ? MARKER_SIZE : { width: size.widthOf?.(node.id) ?? size.width, height: size.height }),
|
|
51
|
-
layoutOptions:
|
|
56
|
+
layoutOptions: FIXED_SIDE,
|
|
57
|
+
...markerTitleOf(node.id, node.type),
|
|
52
58
|
ports: [
|
|
53
59
|
...incomingEdgesOf(definition, node.id)
|
|
54
60
|
.filter((edge) => !exitDotEdges.includes(edge))
|
|
@@ -67,6 +73,15 @@ export function elkGraphOf(definition, size = NODE_SIZE, options = {}) {
|
|
|
67
73
|
})),
|
|
68
74
|
};
|
|
69
75
|
}
|
|
76
|
+
function markerTitleOf(nodeId, type) {
|
|
77
|
+
if (typeFamilyOf(type) !== "marker")
|
|
78
|
+
return {};
|
|
79
|
+
const text = nodeTitle(nodeId);
|
|
80
|
+
return {
|
|
81
|
+
labels: [{ text, width: Math.ceil(text.length * TITLE_CHAR_WIDTH + TITLE_PADDING), height: TITLE_HEIGHT }],
|
|
82
|
+
layoutOptions: { ...FIXED_SIDE, "elk.nodeLabels.placement": "OUTSIDE V_BOTTOM H_CENTER" },
|
|
83
|
+
};
|
|
84
|
+
}
|
|
70
85
|
function stopOf(definition, nodeId) {
|
|
71
86
|
const width = Math.max(STOP_MIN_WIDTH, Math.ceil(nodeTitle(nodeId).length * STOP_CHAR_WIDTH + STOP_PADDING));
|
|
72
87
|
const [incoming] = incomingEdgesOf(definition, nodeId);
|
|
@@ -86,12 +101,15 @@ function exitDotEdgesOf(definition) {
|
|
|
86
101
|
const mainEdges = mainPathOf(definition).edges;
|
|
87
102
|
return definition.edges.filter((edge) => edge.to === definition.exit && !mainEdges.has(edgeKeyOf(edge)));
|
|
88
103
|
}
|
|
104
|
+
/** Whether a laid-out child is an exit dot, or the shared end that merges a row's exit dots. */
|
|
105
|
+
export const isExitId = (id) => id.endsWith(EXIT_SUFFIX);
|
|
106
|
+
const EXIT_SUFFIX = ":exit";
|
|
89
107
|
function exitDotOf(edge) {
|
|
90
108
|
return {
|
|
91
|
-
id: `${edgeKeyOf(edge)}
|
|
109
|
+
id: `${edgeKeyOf(edge)}${EXIT_SUFFIX}`,
|
|
92
110
|
width: EXIT_DOT_SIZE,
|
|
93
111
|
height: EXIT_DOT_SIZE,
|
|
94
|
-
layoutOptions:
|
|
112
|
+
layoutOptions: FIXED_SIDE,
|
|
95
113
|
ports: [portOf(edge, "in", "WEST")],
|
|
96
114
|
};
|
|
97
115
|
}
|
|
@@ -109,7 +127,7 @@ function stubOf(edge, { idSuffix, direction, side }) {
|
|
|
109
127
|
id: `${edgeKeyOf(edge)}:${idSuffix}`,
|
|
110
128
|
width: STUB_SIZE,
|
|
111
129
|
height: STUB_SIZE,
|
|
112
|
-
layoutOptions:
|
|
130
|
+
layoutOptions: FIXED_SIDE,
|
|
113
131
|
ports: [portOf(edge, direction, side)],
|
|
114
132
|
};
|
|
115
133
|
}
|
package/dist/elk-layout.d.ts
CHANGED
package/dist/elk-layout.js
CHANGED
|
@@ -1,26 +1,48 @@
|
|
|
1
1
|
// Runs ELK on a line's graph and maps the result into what the view draws.
|
|
2
2
|
import ELK from "elkjs/lib/elk.bundled.js";
|
|
3
|
-
import { elkGraphOf } from "./elk-graph";
|
|
3
|
+
import { elkGraphOf, isExitId } from "./elk-graph";
|
|
4
|
+
import { unwrapSnake } from "./elk-snake-layout";
|
|
5
|
+
import { anchoredRowsOf, columnsOf, columnsPerRowFor, rowShapeOf, sharedEndsOf, snakeGraphOf, wrappedRowsOf } from "./elk-snake-graph";
|
|
6
|
+
import { edgeKeyOf } from "./metro/edge-key";
|
|
4
7
|
/** Lays out a line with ELK: node centres, one SVG path and label centre per edge, and the canvas box. */
|
|
5
8
|
export async function layoutWithElk(definition, size, options) {
|
|
6
|
-
const
|
|
9
|
+
const flat = elkGraphOf(definition, size, options);
|
|
10
|
+
const oneLine = await new ELK().layout(structuredClone(flat));
|
|
11
|
+
const columns = columnsOf(oneLine);
|
|
12
|
+
const wrappedRows = wrappedRowsOf(columns, columnsPerRowFor(rowShapeOf(oneLine, columns, options)));
|
|
13
|
+
const snake = wrappedRows && sharedEndsOf(flat, anchoredRowsOf(flat, wrappedRows));
|
|
14
|
+
const laidOut = snake ? unwrapSnake(await new ELK().layout(snakeGraphOf(snake.flat, snake.rowOf)), definition.edges.map(edgeKeyOf)) : await withSharedEnd(flat, oneLine);
|
|
7
15
|
const nodes = nodesOf(laidOut);
|
|
8
16
|
return {
|
|
9
17
|
nodes,
|
|
10
|
-
edges: edgesOf(laidOut, definition,
|
|
18
|
+
edges: edgesOf(laidOut, definition, columns),
|
|
11
19
|
contentBox: { minX: 0, minY: 0, maxX: laidOut.width ?? 0, maxY: laidOut.height ?? 0 },
|
|
12
20
|
};
|
|
13
21
|
}
|
|
22
|
+
async function withSharedEnd(flat, oneLine) {
|
|
23
|
+
const children = flat.children ?? [];
|
|
24
|
+
if (!children.some((child) => isExitId(child.id)))
|
|
25
|
+
return oneLine;
|
|
26
|
+
const { flat: sharedFlat } = sharedEndsOf(flat, new Map(children.map((child) => [child.id, 0])));
|
|
27
|
+
return new ELK().layout(structuredClone(sharedFlat));
|
|
28
|
+
}
|
|
14
29
|
function nodesOf(laidOut) {
|
|
15
|
-
return (laidOut.children ?? []).map(({ id, x: left = 0, y: top = 0, width = 0, height = 0, ports = [] }) => ({
|
|
30
|
+
return (laidOut.children ?? []).map(({ id, x: left = 0, y: top = 0, width = 0, height = 0, ports = [], labels }) => ({
|
|
16
31
|
id,
|
|
17
32
|
x: left + width / 2,
|
|
18
33
|
y: top + height / 2,
|
|
19
34
|
width,
|
|
20
35
|
height,
|
|
21
36
|
ports: ports.map((port) => portOf(port, left, top)),
|
|
37
|
+
...titleBoxOf(labels, left, top),
|
|
22
38
|
}));
|
|
23
39
|
}
|
|
40
|
+
function titleBoxOf(labels, left, top) {
|
|
41
|
+
if (!labels?.length)
|
|
42
|
+
return {};
|
|
43
|
+
const { minX, minY, maxX, maxY } = labelBoxOf(labels[0]);
|
|
44
|
+
return { titleBox: { minX: left + minX, minY: top + minY, maxX: left + maxX, maxY: top + maxY } };
|
|
45
|
+
}
|
|
24
46
|
function portOf({ id, x: portLeft = 0, y: portTop = 0, width = 0, height = 0 }, left, top) {
|
|
25
47
|
const separator = id.lastIndexOf(":");
|
|
26
48
|
return {
|
|
@@ -30,17 +52,18 @@ function portOf({ id, x: portLeft = 0, y: portTop = 0, width = 0, height = 0 },
|
|
|
30
52
|
y: top + portTop + height / 2,
|
|
31
53
|
};
|
|
32
54
|
}
|
|
33
|
-
function edgesOf(laidOut, definition,
|
|
34
|
-
const
|
|
35
|
-
return
|
|
36
|
-
const { from, to, on } =
|
|
37
|
-
const
|
|
55
|
+
function edgesOf(laidOut, definition, columns) {
|
|
56
|
+
const laidOutEdges = new Map((laidOut.edges ?? []).map((laidOutEdge) => [laidOutEdge.id, laidOutEdge]));
|
|
57
|
+
return definition.edges.map((edge) => {
|
|
58
|
+
const { from, to, on } = edge;
|
|
59
|
+
const laidOutEdge = laidOutEdges.get(edgeKeyOf(edge));
|
|
60
|
+
const labelBox = labelBoxOf(laidOutEdge?.labels?.[0]);
|
|
38
61
|
return {
|
|
39
62
|
from,
|
|
40
63
|
to,
|
|
41
64
|
on,
|
|
42
|
-
kind: edgeKindOf(from, to,
|
|
43
|
-
d: pathOf(laidOutEdge
|
|
65
|
+
kind: edgeKindOf(from, to, columns),
|
|
66
|
+
d: pathOf(laidOutEdge?.sections?.[0]),
|
|
44
67
|
mid: centreOf(labelBox),
|
|
45
68
|
labelBox,
|
|
46
69
|
};
|
|
@@ -59,9 +82,9 @@ function labelBoxOf(label) {
|
|
|
59
82
|
function centreOf({ minX, minY, maxX, maxY }) {
|
|
60
83
|
return { x: (minX + maxX) / 2, y: (minY + maxY) / 2, vertical: false };
|
|
61
84
|
}
|
|
62
|
-
function edgeKindOf(from, to,
|
|
85
|
+
function edgeKindOf(from, to, columns) {
|
|
63
86
|
if (from === to)
|
|
64
87
|
return "self";
|
|
65
|
-
return (
|
|
88
|
+
return (columns.get(to) ?? 0) > (columns.get(from) ?? 0) ? "forward" : "back";
|
|
66
89
|
}
|
|
67
90
|
//# sourceMappingURL=elk-layout.js.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ElkNode } from "elkjs/lib/elk-api";
|
|
2
|
+
import type { Size } from "./viewport";
|
|
3
|
+
/** The column of each child of a laid-out line, keyed by child id: sorted by x, a child starting past the column's widest right edge opens the next column. */
|
|
4
|
+
export declare function columnsOf(laidOut: ElkNode): Map<string, number>;
|
|
5
|
+
/** What the columns per row are chosen from: the one-row line's column count, the width of a column and the height of its row, and the box: its width over its height, or its width alone when the drawing sets the box's height; undefined until the box is measured. */
|
|
6
|
+
export interface RowShapeInput {
|
|
7
|
+
columnCount: number;
|
|
8
|
+
columnWidth: number;
|
|
9
|
+
rowHeight: number;
|
|
10
|
+
boxAspect?: number;
|
|
11
|
+
boxWidth?: number;
|
|
12
|
+
}
|
|
13
|
+
/** The shape of a line laid out on one row: its column count, the mean width of a column and the row's height, with the box's aspect to fit them into. */
|
|
14
|
+
export declare function rowShapeOf(oneLine: ElkNode, columns: ReadonlyMap<string, number>, { boxAspect, boxWidth }?: Pick<RowShapeInput, "boxAspect" | "boxWidth">): RowShapeInput;
|
|
15
|
+
/** The columns per row, split evenly as rowsOf splits them: as many as fit the box's width at natural size when the drawing sets the box's height, else those of the row count the viewport fits largest into the box, the fewest rows on a tie; 5 while the box is not measured or the line has no columns. */
|
|
16
|
+
export declare function columnsPerRowFor({ columnCount, columnWidth, rowHeight, boxAspect, boxWidth }: RowShapeInput): number;
|
|
17
|
+
/** The box's width rounded down to 40 px, so a resize lays the line out again only past a step; undefined until it has a width. */
|
|
18
|
+
export declare function widthBucketOf(box: Size | null): number | undefined;
|
|
19
|
+
/** The box's width over its height, rounded to a quarter of a doubling so a resize lays the line out again only when the box's shape really changed; undefined until it has a size. */
|
|
20
|
+
export declare function aspectBucketOf(box: Size | null): number | undefined;
|
|
21
|
+
/** The row index of each column of a laid-out line. */
|
|
22
|
+
export declare function rowsOf(columnCount: number, columnsPerRow: number): number[];
|
|
23
|
+
/** The row of each child when the columns outnumber a row's share, otherwise undefined: the line stays on one row. */
|
|
24
|
+
export declare function wrappedRowsOf(columns: ReadonlyMap<string, number>, columnsPerRow: number): Map<string, number> | undefined;
|
|
25
|
+
/** The rows with each end dot and stub moved to the row of the station at the other end of its edge. */
|
|
26
|
+
export declare function anchoredRowsOf(flat: ElkNode, rowOf: ReadonlyMap<string, number>): Map<string, number>;
|
|
27
|
+
/** The flat line graph with each row's exit dots merged into one end, as tall as its lines need. */
|
|
28
|
+
export declare function sharedEndsOf(flat: ElkNode, rowOf: ReadonlyMap<string, number>): {
|
|
29
|
+
flat: ElkNode;
|
|
30
|
+
rowOf: Map<string, number>;
|
|
31
|
+
};
|
|
32
|
+
/** The flat line graph wrapped into one compound per row, under a root that runs down. */
|
|
33
|
+
export declare function snakeGraphOf(flat: ElkNode, rowOf: ReadonlyMap<string, number>): ElkNode;
|
|
34
|
+
/** The ids of the three parts a between-row edge is split into, and of its two row ports; the layout joins the parts back by these. */
|
|
35
|
+
export declare const rowId: (row: number) => string;
|
|
36
|
+
export declare const sharedEndId: (row: number) => string;
|
|
37
|
+
export declare const outPartId: (edgeId: string) => string;
|
|
38
|
+
export declare const betweenPartId: (edgeId: string) => string;
|
|
39
|
+
export declare const inPartId: (edgeId: string) => string;
|
|
40
|
+
export declare const rowOutPortId: (edgeId: string) => string;
|
|
41
|
+
export declare const rowInPortId: (edgeId: string) => string;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { isExitId, NODE_SIZE } from "./elk-graph";
|
|
2
|
+
/** The column of each child of a laid-out line, keyed by child id: sorted by x, a child starting past the column's widest right edge opens the next column. */
|
|
3
|
+
export function columnsOf(laidOut) {
|
|
4
|
+
const spans = (laidOut.children ?? []).map(spanOf).sort((first, second) => first.left - second.left);
|
|
5
|
+
const columns = new Map();
|
|
6
|
+
let column = -1;
|
|
7
|
+
let widestRight = 0;
|
|
8
|
+
for (const span of spans) {
|
|
9
|
+
if (span.left >= widestRight)
|
|
10
|
+
column += 1;
|
|
11
|
+
columns.set(span.id, column);
|
|
12
|
+
widestRight = Math.max(widestRight, span.right);
|
|
13
|
+
}
|
|
14
|
+
return columns;
|
|
15
|
+
}
|
|
16
|
+
function spanOf(child) {
|
|
17
|
+
const left = child.x ?? 0;
|
|
18
|
+
return { id: child.id, left, right: left + (child.width ?? 0) };
|
|
19
|
+
}
|
|
20
|
+
const DEFAULT_COLUMNS_PER_ROW = 5;
|
|
21
|
+
const ASPECT_STEPS_PER_DOUBLING = 4;
|
|
22
|
+
const WIDTH_STEP = 40;
|
|
23
|
+
/** The shape of a line laid out on one row: its column count, the mean width of a column and the row's height, with the box's aspect to fit them into. */
|
|
24
|
+
export function rowShapeOf(oneLine, columns, { boxAspect, boxWidth } = {}) {
|
|
25
|
+
const columnCount = new Set(columns.values()).size;
|
|
26
|
+
return { columnCount, columnWidth: (oneLine.width ?? 0) / columnCount, rowHeight: oneLine.height ?? 0, boxAspect, boxWidth };
|
|
27
|
+
}
|
|
28
|
+
/** The columns per row, split evenly as rowsOf splits them: as many as fit the box's width at natural size when the drawing sets the box's height, else those of the row count the viewport fits largest into the box, the fewest rows on a tie; 5 while the box is not measured or the line has no columns. */
|
|
29
|
+
export function columnsPerRowFor({ columnCount, columnWidth, rowHeight, boxAspect, boxWidth }) {
|
|
30
|
+
if (columnCount < 1)
|
|
31
|
+
return DEFAULT_COLUMNS_PER_ROW;
|
|
32
|
+
if (boxWidth !== undefined)
|
|
33
|
+
return evenlySplit(columnCount, Math.min(columnCount, Math.max(1, Math.floor(boxWidth / columnWidth))));
|
|
34
|
+
if (boxAspect === undefined)
|
|
35
|
+
return DEFAULT_COLUMNS_PER_ROW;
|
|
36
|
+
const fitOf = (rowCount) => Math.min(boxAspect / (Math.ceil(columnCount / rowCount) * columnWidth), 1 / (rowCount * rowHeight));
|
|
37
|
+
const rowCounts = Array.from({ length: columnCount }, (unused, index) => index + 1);
|
|
38
|
+
const bestRowCount = rowCounts.reduce((best, candidate) => (fitOf(candidate) > fitOf(best) ? candidate : best));
|
|
39
|
+
return Math.ceil(columnCount / bestRowCount);
|
|
40
|
+
}
|
|
41
|
+
/** The columns per row once at most `columnsPerRow` are split evenly over the rows they need. */
|
|
42
|
+
function evenlySplit(columnCount, columnsPerRow) {
|
|
43
|
+
return Math.ceil(columnCount / Math.ceil(columnCount / columnsPerRow));
|
|
44
|
+
}
|
|
45
|
+
/** The box's width rounded down to 40 px, so a resize lays the line out again only past a step; undefined until it has a width. */
|
|
46
|
+
export function widthBucketOf(box) {
|
|
47
|
+
if (!box || box.width <= 0)
|
|
48
|
+
return undefined;
|
|
49
|
+
return Math.floor(box.width / WIDTH_STEP) * WIDTH_STEP;
|
|
50
|
+
}
|
|
51
|
+
/** The box's width over its height, rounded to a quarter of a doubling so a resize lays the line out again only when the box's shape really changed; undefined until it has a size. */
|
|
52
|
+
export function aspectBucketOf(box) {
|
|
53
|
+
if (!box || box.width <= 0 || box.height <= 0)
|
|
54
|
+
return undefined;
|
|
55
|
+
return 2 ** (Math.round(Math.log2(box.width / box.height) * ASPECT_STEPS_PER_DOUBLING) / ASPECT_STEPS_PER_DOUBLING);
|
|
56
|
+
}
|
|
57
|
+
/** The row index of each column of a laid-out line. */
|
|
58
|
+
export function rowsOf(columnCount, columnsPerRow) {
|
|
59
|
+
const rowCount = Math.ceil(columnCount / columnsPerRow);
|
|
60
|
+
return Array.from({ length: columnCount }, (unused, column) => Math.floor((column * rowCount) / columnCount));
|
|
61
|
+
}
|
|
62
|
+
/** The row of each child when the columns outnumber a row's share, otherwise undefined: the line stays on one row. */
|
|
63
|
+
export function wrappedRowsOf(columns, columnsPerRow) {
|
|
64
|
+
const columnCount = new Set(columns.values()).size;
|
|
65
|
+
if (columnCount <= columnsPerRow)
|
|
66
|
+
return undefined;
|
|
67
|
+
const rowOfColumn = rowsOf(columnCount, columnsPerRow);
|
|
68
|
+
return new Map([...columns].map(([id, column]) => [id, rowOfColumn[column]]));
|
|
69
|
+
}
|
|
70
|
+
/** The rows with each end dot and stub moved to the row of the station at the other end of its edge. */
|
|
71
|
+
export function anchoredRowsOf(flat, rowOf) {
|
|
72
|
+
const owners = ownerOfPorts(flat);
|
|
73
|
+
const ends = (flat.edges ?? []).flatMap((edge) => endAnchorsOf(owners.get(edge.sources[0]) ?? "", owners.get(edge.targets[0]) ?? "", rowOf));
|
|
74
|
+
return new Map([...rowOf, ...ends]);
|
|
75
|
+
}
|
|
76
|
+
function endAnchorsOf(source, target, rowOf) {
|
|
77
|
+
if (END_CHILD_ID.test(source))
|
|
78
|
+
return [[source, rowOf.get(target) ?? 0]];
|
|
79
|
+
if (END_CHILD_ID.test(target))
|
|
80
|
+
return [[target, rowOf.get(source) ?? 0]];
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
const END_CHILD_ID = /:(exit|stub|source-stub)$/;
|
|
84
|
+
/** The flat line graph with each row's exit dots merged into one end, as tall as its lines need. */
|
|
85
|
+
export function sharedEndsOf(flat, rowOf) {
|
|
86
|
+
const children = flat.children ?? [];
|
|
87
|
+
const dots = children.filter((child) => isExitId(child.id));
|
|
88
|
+
const dotRows = [...new Set(dots.map((dot) => rowOf.get(dot.id) ?? 0))];
|
|
89
|
+
const ends = dotRows.map((row) => ({ row, end: sharedEndOf(row, dots.filter((dot) => rowOf.get(dot.id) === row)) }));
|
|
90
|
+
const dotIds = new Set(dots.map((dot) => dot.id));
|
|
91
|
+
const keptRows = [...rowOf].filter(([id]) => !dotIds.has(id));
|
|
92
|
+
return {
|
|
93
|
+
flat: { ...flat, children: [...children.filter((child) => !dotIds.has(child.id)), ...ends.map(({ end }) => end)] },
|
|
94
|
+
rowOf: new Map([...keptRows, ...ends.map(({ row, end }) => [end.id, row])]),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function sharedEndOf(row, dots) {
|
|
98
|
+
const ports = dots.flatMap((dot) => dot.ports ?? []);
|
|
99
|
+
return { ...dots[0], id: sharedEndId(row), height: ports.length * NODE_SIZE.height, ports };
|
|
100
|
+
}
|
|
101
|
+
/** The flat line graph wrapped into one compound per row, under a root that runs down. */
|
|
102
|
+
export function snakeGraphOf(flat, rowOf) {
|
|
103
|
+
const rowCount = Math.max(...rowOf.values()) + 1;
|
|
104
|
+
const owners = ownerOfPorts(flat);
|
|
105
|
+
const edges = flat.edges ?? [];
|
|
106
|
+
const edgeRows = new Map(edges.map((edge) => [edge.id, sharedRowOf(edge, rowOf, owners)]));
|
|
107
|
+
const crossings = edges.filter((edge) => edgeRows.get(edge.id) === undefined).map((edge) => crossingOf(edge, rowOf, owners));
|
|
108
|
+
const rows = Array.from({ length: rowCount }, (unused, row) => ({
|
|
109
|
+
id: rowId(row),
|
|
110
|
+
layoutOptions: { ...flat.layoutOptions, "elk.direction": directionOf(row), "elk.portConstraints": "FIXED_SIDE", "elk.layered.nodePlacement.bk.fixedAlignment": "LEFTUP" },
|
|
111
|
+
children: (flat.children ?? []).filter((child) => rowOf.get(child.id) === row).map((child) => (isRightRow(row) ? child : mirroredPortsOf(child))),
|
|
112
|
+
ports: crossings.flatMap((crossing) => rowPortsOf(crossing, row)),
|
|
113
|
+
edges: [...edges.filter((edge) => edgeRows.get(edge.id) === row), ...crossings.flatMap((crossing) => rowPartsOf(crossing, row))],
|
|
114
|
+
}));
|
|
115
|
+
return {
|
|
116
|
+
...flat,
|
|
117
|
+
edges: crossings.map(betweenEdgeOf),
|
|
118
|
+
layoutOptions: { ...flat.layoutOptions, "elk.direction": "DOWN", "elk.hierarchyHandling": "SEPARATE_CHILDREN", "elk.layered.nodePlacement.strategy": "SIMPLE" },
|
|
119
|
+
children: rows,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** The ids of the three parts a between-row edge is split into, and of its two row ports; the layout joins the parts back by these. */
|
|
123
|
+
export const rowId = (row) => `row:${row}`;
|
|
124
|
+
export const sharedEndId = (row) => `${rowId(row)}:exit`;
|
|
125
|
+
export const outPartId = (edgeId) => `${edgeId}#out`;
|
|
126
|
+
export const betweenPartId = (edgeId) => `${edgeId}#between`;
|
|
127
|
+
export const inPartId = (edgeId) => `${edgeId}#in`;
|
|
128
|
+
export const rowOutPortId = (edgeId) => `${edgeId}:rowout`;
|
|
129
|
+
export const rowInPortId = (edgeId) => `${edgeId}:rowin`;
|
|
130
|
+
function crossingOf(edge, rowOf, owners) {
|
|
131
|
+
return { edge, fromRow: endpointRowOf(edge.sources[0], rowOf, owners) ?? 0, toRow: endpointRowOf(edge.targets[0], rowOf, owners) ?? 0 };
|
|
132
|
+
}
|
|
133
|
+
function rowPortsOf({ edge, fromRow, toRow }, row) {
|
|
134
|
+
const rowPort = (id, side) => ({ id, width: 0, height: 0, layoutOptions: { "elk.port.side": side } });
|
|
135
|
+
const outPorts = fromRow === row ? [rowPort(rowOutPortId(edge.id), flowEndSideOf(row))] : [];
|
|
136
|
+
const inPorts = toRow === row ? [rowPort(rowInPortId(edge.id), MIRRORED_SIDE[flowEndSideOf(row)])] : [];
|
|
137
|
+
return [...outPorts, ...inPorts];
|
|
138
|
+
}
|
|
139
|
+
function rowPartsOf({ edge, fromRow, toRow }, row) {
|
|
140
|
+
const outParts = fromRow === row ? [{ id: outPartId(edge.id), sources: edge.sources, targets: [rowOutPortId(edge.id)] }] : [];
|
|
141
|
+
const inParts = toRow === row ? [{ id: inPartId(edge.id), sources: [rowInPortId(edge.id)], targets: edge.targets }] : [];
|
|
142
|
+
return [...outParts, ...inParts];
|
|
143
|
+
}
|
|
144
|
+
function betweenEdgeOf({ edge }) {
|
|
145
|
+
return { id: betweenPartId(edge.id), sources: [rowOutPortId(edge.id)], targets: [rowInPortId(edge.id)], labels: edge.labels };
|
|
146
|
+
}
|
|
147
|
+
function directionOf(row) {
|
|
148
|
+
return isRightRow(row) ? "RIGHT" : "LEFT";
|
|
149
|
+
}
|
|
150
|
+
function isRightRow(row) {
|
|
151
|
+
return row % 2 === 0;
|
|
152
|
+
}
|
|
153
|
+
function flowEndSideOf(row) {
|
|
154
|
+
return directionOf(row) === "RIGHT" ? "EAST" : "WEST";
|
|
155
|
+
}
|
|
156
|
+
function ownerOfPorts(flat) {
|
|
157
|
+
const portOwners = (flat.children ?? []).flatMap((child) => (child.ports ?? []).map((port) => [port.id, child.id]));
|
|
158
|
+
return new Map(portOwners);
|
|
159
|
+
}
|
|
160
|
+
function endpointRowOf(portId, rowOf, owners) {
|
|
161
|
+
return rowOf.get(owners.get(portId) ?? "");
|
|
162
|
+
}
|
|
163
|
+
function sharedRowOf(edge, rowOf, owners) {
|
|
164
|
+
const sourceRow = endpointRowOf(edge.sources[0], rowOf, owners);
|
|
165
|
+
const targetRow = endpointRowOf(edge.targets[0], rowOf, owners);
|
|
166
|
+
return sourceRow === targetRow ? sourceRow : undefined;
|
|
167
|
+
}
|
|
168
|
+
const MIRRORED_SIDE = { WEST: "EAST", EAST: "WEST" };
|
|
169
|
+
function mirroredPortsOf(child) {
|
|
170
|
+
const ports = (child.ports ?? []).map((port) => mirroredPortOf(port, child.width ?? 0));
|
|
171
|
+
return { ...child, ports };
|
|
172
|
+
}
|
|
173
|
+
function mirroredPortOf(port, nodeWidth) {
|
|
174
|
+
const side = port.layoutOptions?.["elk.port.side"] ?? "";
|
|
175
|
+
if (side in MIRRORED_SIDE)
|
|
176
|
+
return { ...port, layoutOptions: { ...port.layoutOptions, "elk.port.side": MIRRORED_SIDE[side] } };
|
|
177
|
+
return port.x === undefined ? port : { ...port, x: nodeWidth - port.x };
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=elk-snake-graph.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { betweenPartId, inPartId, outPartId } from "./elk-snake-graph";
|
|
2
|
+
/** The laid-out snake as one graph: children and edge paths in absolute coordinates, one edge per key, with its label. */
|
|
3
|
+
export function unwrapSnake(snake, edgeKeys) {
|
|
4
|
+
const rows = snake.children ?? [];
|
|
5
|
+
const placed = new Map([...(snake.edges ?? []).map((edge) => placedOn(edge, snake)), ...rows.flatMap((row) => (row.edges ?? []).map((edge) => placedOn(edge, row)))]);
|
|
6
|
+
return { ...snake, children: rows.flatMap(movedChildrenOf), edges: edgeKeys.map((key) => joinedEdgeOf(key, placed)) };
|
|
7
|
+
}
|
|
8
|
+
function placedOn(edge, container) {
|
|
9
|
+
return [edge.id, { edge, origin: originOf(container) }];
|
|
10
|
+
}
|
|
11
|
+
function movedChildrenOf(row) {
|
|
12
|
+
return (row.children ?? []).map((child) => movedBy(child, originOf(row)));
|
|
13
|
+
}
|
|
14
|
+
function originOf({ x: left = 0, y: top = 0 }) {
|
|
15
|
+
return { x: left, y: top };
|
|
16
|
+
}
|
|
17
|
+
function movedBy(placed, origin) {
|
|
18
|
+
return { ...placed, x: (placed.x ?? 0) + origin.x, y: (placed.y ?? 0) + origin.y };
|
|
19
|
+
}
|
|
20
|
+
function joinedEdgeOf(key, placed) {
|
|
21
|
+
const parts = [key, outPartId(key), betweenPartId(key), inPartId(key)].flatMap((id) => placed.get(id) ?? []);
|
|
22
|
+
const [startPoint, ...rest] = withoutStraightMiddles(withoutRepeats(parts.flatMap(pointsOf)));
|
|
23
|
+
const endPoint = rest.pop() ?? startPoint;
|
|
24
|
+
const section = { id: key, startPoint, bendPoints: rest, endPoint };
|
|
25
|
+
return { id: key, sources: [], targets: [], sections: [section], labels: parts.flatMap(labelOf).slice(0, 1) };
|
|
26
|
+
}
|
|
27
|
+
function pointsOf({ edge, origin }) {
|
|
28
|
+
const sections = edge.sections ?? [];
|
|
29
|
+
return sections.flatMap(({ startPoint, bendPoints = [], endPoint }) => [startPoint, ...bendPoints, endPoint]).map((point) => movedBy(point, origin));
|
|
30
|
+
}
|
|
31
|
+
function labelOf({ edge, origin }) {
|
|
32
|
+
return (edge.labels ?? []).map((label) => movedBy(label, origin));
|
|
33
|
+
}
|
|
34
|
+
function withoutRepeats(points) {
|
|
35
|
+
return points.filter((point, index) => index === 0 || !isNear(point.x, points[index - 1].x) || !isNear(point.y, points[index - 1].y));
|
|
36
|
+
}
|
|
37
|
+
function withoutStraightMiddles(points) {
|
|
38
|
+
return points.filter((point, index) => index === 0 || index === points.length - 1 || !isStraightRun(points[index - 1], point, points[index + 1]));
|
|
39
|
+
}
|
|
40
|
+
function isStraightRun(before, point, after) {
|
|
41
|
+
const sameColumn = isNear(before.x, point.x) && isNear(point.x, after.x);
|
|
42
|
+
const sameRow = isNear(before.y, point.y) && isNear(point.y, after.y);
|
|
43
|
+
return sameColumn || sameRow;
|
|
44
|
+
}
|
|
45
|
+
function isNear(first, second) {
|
|
46
|
+
return Math.abs(first - second) < 0.01;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=elk-snake-layout.js.map
|
package/dist/graph-markers.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { isExitId } from "./elk-graph";
|
|
2
3
|
import DanglingStub from "./DanglingStub";
|
|
3
4
|
import ExitDot from "./ExitDot";
|
|
4
5
|
export function markerOf(node, exit) {
|
|
5
|
-
if (node.id
|
|
6
|
+
if (isExitId(node.id)) {
|
|
6
7
|
return _jsx(ExitDot, { node: node, exit: exit }, node.id);
|
|
7
8
|
}
|
|
8
9
|
return node.id.endsWith(":stub") ? _jsx(DanglingStub, { node: node }, node.id) : null;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import type { ElkLayout } from "./elk-layout";
|
|
2
|
+
type EdgePair = {
|
|
3
|
+
edge: string;
|
|
4
|
+
beside: string;
|
|
5
|
+
};
|
|
2
6
|
/** Lists each edge segment that crosses a station box other than the edge's own endpoints. */
|
|
3
7
|
export declare function linesThroughStations(layout: ElkLayout): {
|
|
4
8
|
edge: string;
|
|
@@ -9,8 +13,16 @@ export declare function linesOverLabels(layout: ElkLayout): {
|
|
|
9
13
|
edge: string;
|
|
10
14
|
label: string;
|
|
11
15
|
}[];
|
|
16
|
+
/** Lists each edge whose path runs through the title box of any node, its own endpoints included. */
|
|
17
|
+
export declare function linesThroughTitles(layout: ElkLayout): {
|
|
18
|
+
edge: string;
|
|
19
|
+
title: string;
|
|
20
|
+
}[];
|
|
12
21
|
/** Lists each interior segment of an edge path, never the first or last, shorter than the minimum. */
|
|
13
22
|
export declare function tinyTurns(layout: ElkLayout, minimum: number): {
|
|
14
23
|
edge: string;
|
|
15
24
|
length: number;
|
|
16
25
|
}[];
|
|
26
|
+
/** Lists each pair of different edges whose parallel stretches run closer than the minimum gap. */
|
|
27
|
+
export declare function linesAlongside(layout: ElkLayout, minGap: number): EdgePair[];
|
|
28
|
+
export {};
|
package/dist/layout-geometry.js
CHANGED
|
@@ -8,6 +8,10 @@ export function linesThroughStations(layout) {
|
|
|
8
8
|
export function linesOverLabels(layout) {
|
|
9
9
|
return layout.edges.flatMap((edge) => labelsCrossedBy(edge, layout.edges).map((labelled) => ({ edge: edgeKeyOf(edge), label: edgeKeyOf(labelled) })));
|
|
10
10
|
}
|
|
11
|
+
/** Lists each edge whose path runs through the title box of any node, its own endpoints included. */
|
|
12
|
+
export function linesThroughTitles(layout) {
|
|
13
|
+
return layout.edges.flatMap((edge) => titlesCrossedBy(edge, layout.nodes).map((titled) => ({ edge: edgeKeyOf(edge), title: titled.id })));
|
|
14
|
+
}
|
|
11
15
|
/** Lists each interior segment of an edge path, never the first or last, shorter than the minimum. */
|
|
12
16
|
export function tinyTurns(layout, minimum) {
|
|
13
17
|
return layout.edges.flatMap((edge) => segmentsOf(edge)
|
|
@@ -16,6 +20,31 @@ export function tinyTurns(layout, minimum) {
|
|
|
16
20
|
.filter((length) => length < minimum)
|
|
17
21
|
.map((length) => ({ edge: edgeKeyOf(edge), length })));
|
|
18
22
|
}
|
|
23
|
+
/** Lists each pair of different edges whose parallel stretches run closer than the minimum gap. */
|
|
24
|
+
export function linesAlongside(layout, minGap) {
|
|
25
|
+
return layout.edges.flatMap((edge, index) => besideLaterEdges(edge, layout.edges.slice(index + 1), minGap));
|
|
26
|
+
}
|
|
27
|
+
/** The pairs of the edge with each later edge that runs alongside it. */
|
|
28
|
+
function besideLaterEdges(edge, laterEdges, minGap) {
|
|
29
|
+
const neighbours = laterEdges.filter((later) => runsAlongside(segmentsOf(edge), segmentsOf(later), minGap));
|
|
30
|
+
return neighbours.map((later) => ({ edge: edgeKeyOf(edge), beside: edgeKeyOf(later) }));
|
|
31
|
+
}
|
|
32
|
+
/** True when a horizontal or a vertical stretch of one path runs closer than the gap to a parallel stretch of the other, over a shared span. */
|
|
33
|
+
function runsAlongside(first, second, minGap) {
|
|
34
|
+
return first.some((one) => second.some((other) => horizontalAlongside(one, other, minGap) || horizontalAlongside(transposed(one), transposed(other), minGap)));
|
|
35
|
+
}
|
|
36
|
+
/** Two flat stretches whose heights differ by less than the gap while their spans overlap; a zero-length point never overlaps. */
|
|
37
|
+
function horizontalAlongside(one, other, minGap) {
|
|
38
|
+
return isHorizontal(one) && isHorizontal(other) && Math.abs(one.top - other.top) < minGap && one.left < other.right && other.left < one.right;
|
|
39
|
+
}
|
|
40
|
+
/** The box mirrored across the diagonal, so a vertical stretch reads as a horizontal one. */
|
|
41
|
+
function transposed(box) {
|
|
42
|
+
return { left: box.top, right: box.bottom, top: box.left, bottom: box.right };
|
|
43
|
+
}
|
|
44
|
+
/** A stretch with no height, running left to right. */
|
|
45
|
+
function isHorizontal(box) {
|
|
46
|
+
return box.top === box.bottom;
|
|
47
|
+
}
|
|
19
48
|
/** Paths are axis-aligned, so one of the two spans is always zero. */
|
|
20
49
|
function lengthOf(segment) {
|
|
21
50
|
return segment.right - segment.left + (segment.bottom - segment.top);
|
|
@@ -26,6 +55,12 @@ function labelsCrossedBy(edge, edges) {
|
|
|
26
55
|
const others = edges.filter((other) => other !== edge);
|
|
27
56
|
return others.filter((other) => segments.some((segment) => overlaps(segment, labelBoxOf(other))));
|
|
28
57
|
}
|
|
58
|
+
/** The stations whose title box the edge's path runs through. */
|
|
59
|
+
function titlesCrossedBy(edge, stations) {
|
|
60
|
+
const segments = segmentsOf(edge);
|
|
61
|
+
const titles = stations.flatMap((station) => (station.titleBox ? [{ station, bounds: boundsOf(station.titleBox) }] : []));
|
|
62
|
+
return titles.filter(({ bounds }) => segments.some((segment) => overlaps(segment, bounds))).map(({ station }) => station);
|
|
63
|
+
}
|
|
29
64
|
/** The stations, other than the edge's own endpoints, whose box the edge's path runs through. */
|
|
30
65
|
function stationsCrossedBy(edge, stations) {
|
|
31
66
|
const segments = segmentsOf(edge);
|
|
@@ -42,7 +77,10 @@ function segmentsOf({ d }) {
|
|
|
42
77
|
});
|
|
43
78
|
}
|
|
44
79
|
function labelBoxOf({ labelBox }) {
|
|
45
|
-
return
|
|
80
|
+
return boundsOf(labelBox);
|
|
81
|
+
}
|
|
82
|
+
function boundsOf({ minX, maxX, minY, maxY }) {
|
|
83
|
+
return { left: minX, right: maxX, top: minY, bottom: maxY };
|
|
46
84
|
}
|
|
47
85
|
/** The station box shrunk by a margin, so a path grazing the border does not count as crossing. */
|
|
48
86
|
function stationBoxOf(station) {
|
|
@@ -5,6 +5,12 @@
|
|
|
5
5
|
flex: 1;
|
|
6
6
|
min-height: 0;
|
|
7
7
|
}
|
|
8
|
+
/* Fitted to its width, the canvas is as tall as its drawing, and the panel as tall as the canvas: a page that holds it caps the height and the viewport fits the drawing into what is left. */
|
|
9
|
+
.fitWidth,
|
|
10
|
+
.fitWidth .canvas {
|
|
11
|
+
flex: 0 1 auto;
|
|
12
|
+
}
|
|
13
|
+
|
|
8
14
|
.heading {
|
|
9
15
|
font-size: var(--fs-md);
|
|
10
16
|
font-weight: var(--fw-semibold);
|
|
@@ -366,6 +372,10 @@
|
|
|
366
372
|
stroke: var(--text);
|
|
367
373
|
stroke-width: 1.5;
|
|
368
374
|
}
|
|
375
|
+
.exitBar {
|
|
376
|
+
stroke: var(--text);
|
|
377
|
+
stroke-width: 1.5;
|
|
378
|
+
}
|
|
369
379
|
.exitCore {
|
|
370
380
|
fill: var(--text);
|
|
371
381
|
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export declare const LAYOUT_WAIT: {
|
|
2
|
+
timeout: number;
|
|
3
|
+
};
|
|
1
4
|
export declare function renderGraph(ui: React.ReactElement): Promise<import("@testing-library/react").RenderResult<typeof import("@testing-library/dom/types/queries"), HTMLElement, HTMLElement>>;
|
|
2
5
|
export declare const nodeEl: (container: HTMLElement, id: string) => HTMLElement;
|
|
3
6
|
export declare const edgeEl: (container: HTMLElement, key: string) => HTMLElement;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { render, waitFor } from "@testing-library/react";
|
|
2
2
|
import { expect } from "vitest";
|
|
3
|
+
// ELK lays out asynchronously; its cold start under a parallel test run outlasts waitFor's default 1000 ms.
|
|
4
|
+
export const LAYOUT_WAIT = { timeout: 5000 };
|
|
3
5
|
export async function renderGraph(ui) {
|
|
4
6
|
const rendered = render(ui);
|
|
5
|
-
await waitFor(() => expect(rendered.container.querySelector("[data-node]")).not.toBeNull());
|
|
7
|
+
await waitFor(() => expect(rendered.container.querySelector("[data-node]")).not.toBeNull(), LAYOUT_WAIT);
|
|
6
8
|
return rendered;
|
|
7
9
|
}
|
|
8
10
|
export const nodeEl = (container, id) => container.querySelector(`[data-node="${id}"]`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@re-cinq/floor-graph",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "A React view of an assembly line as a graph: the layout of its nodes and edges, the viewport that fits and pans it, and the node and edge drawing, with the stylesheets it needs.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"typecheck": "tsc --noEmit"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@re-cinq/floor-contracts": "0.
|
|
48
|
+
"@re-cinq/floor-contracts": "0.13.0",
|
|
49
49
|
"elkjs": "0.9.3"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|