@re-cinq/floor-graph 0.12.0 → 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 +1 -1
- package/dist/RunGraphView.d.ts +2 -0
- package/dist/RunGraphView.js +17 -7
- package/dist/elk-graph.d.ts +4 -0
- package/dist/elk-layout.js +2 -3
- package/dist/elk-snake-graph.d.ts +17 -0
- package/dist/elk-snake-graph.js +37 -0
- package/dist/run-graph.module.css +6 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ The controls hold zoom, fit and two toggles. The `mainPathOnly` and `compactStop
|
|
|
21
21
|
|
|
22
22
|
## Long lines
|
|
23
23
|
|
|
24
|
-
|
|
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
25
|
|
|
26
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
27
|
|
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
|
@@ -16,6 +16,10 @@ export interface ElkGraphOptions {
|
|
|
16
16
|
compactStops?: boolean;
|
|
17
17
|
/** How many times a run took each line by edge key; a line taken twice or more reserves room for its count. */
|
|
18
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;
|
|
19
23
|
}
|
|
20
24
|
export declare function elkGraphOf(definition: AssemblyLineDefinition, size?: NodeSize, options?: ElkGraphOptions): ElkNode;
|
|
21
25
|
/** Whether a laid-out child is an exit dot, or the shared end that merges a row's exit dots. */
|
package/dist/elk-layout.js
CHANGED
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
import ELK from "elkjs/lib/elk.bundled.js";
|
|
3
3
|
import { elkGraphOf, isExitId } from "./elk-graph";
|
|
4
4
|
import { unwrapSnake } from "./elk-snake-layout";
|
|
5
|
-
import { anchoredRowsOf, columnsOf, sharedEndsOf, snakeGraphOf, wrappedRowsOf } from "./elk-snake-graph";
|
|
5
|
+
import { anchoredRowsOf, columnsOf, columnsPerRowFor, rowShapeOf, sharedEndsOf, snakeGraphOf, wrappedRowsOf } from "./elk-snake-graph";
|
|
6
6
|
import { edgeKeyOf } from "./metro/edge-key";
|
|
7
|
-
const LAYERS_PER_ROW = 5;
|
|
8
7
|
/** Lays out a line with ELK: node centres, one SVG path and label centre per edge, and the canvas box. */
|
|
9
8
|
export async function layoutWithElk(definition, size, options) {
|
|
10
9
|
const flat = elkGraphOf(definition, size, options);
|
|
11
10
|
const oneLine = await new ELK().layout(structuredClone(flat));
|
|
12
11
|
const columns = columnsOf(oneLine);
|
|
13
|
-
const wrappedRows = wrappedRowsOf(columns,
|
|
12
|
+
const wrappedRows = wrappedRowsOf(columns, columnsPerRowFor(rowShapeOf(oneLine, columns, options)));
|
|
14
13
|
const snake = wrappedRows && sharedEndsOf(flat, anchoredRowsOf(flat, wrappedRows));
|
|
15
14
|
const laidOut = snake ? unwrapSnake(await new ELK().layout(snakeGraphOf(snake.flat, snake.rowOf)), definition.edges.map(edgeKeyOf)) : await withSharedEnd(flat, oneLine);
|
|
16
15
|
const nodes = nodesOf(laidOut);
|
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import type { ElkNode } from "elkjs/lib/elk-api";
|
|
2
|
+
import type { Size } from "./viewport";
|
|
2
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. */
|
|
3
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;
|
|
4
21
|
/** The row index of each column of a laid-out line. */
|
|
5
22
|
export declare function rowsOf(columnCount: number, columnsPerRow: number): number[];
|
|
6
23
|
/** The row of each child when the columns outnumber a row's share, otherwise undefined: the line stays on one row. */
|
package/dist/elk-snake-graph.js
CHANGED
|
@@ -17,6 +17,43 @@ function spanOf(child) {
|
|
|
17
17
|
const left = child.x ?? 0;
|
|
18
18
|
return { id: child.id, left, right: left + (child.width ?? 0) };
|
|
19
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
|
+
}
|
|
20
57
|
/** The row index of each column of a laid-out line. */
|
|
21
58
|
export function rowsOf(columnCount, columnsPerRow) {
|
|
22
59
|
const rowCount = Math.ceil(columnCount / columnsPerRow);
|
|
@@ -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);
|
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": {
|