@re-cinq/floor-graph 0.0.0-stage → 0.6.1

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.
Files changed (131) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +18 -2
  3. package/dist/ArrowMarkerDefs.d.ts +3 -0
  4. package/dist/ArrowMarkerDefs.js +16 -0
  5. package/dist/ControlIcon.d.ts +3 -0
  6. package/dist/ControlIcon.js +5 -0
  7. package/dist/DanglingStub.d.ts +7 -0
  8. package/dist/DanglingStub.js +10 -0
  9. package/dist/ExitDot.d.ts +8 -0
  10. package/dist/ExitDot.js +7 -0
  11. package/dist/GraphEdge.d.ts +27 -0
  12. package/dist/GraphEdge.js +32 -0
  13. package/dist/GraphNode.d.ts +43 -0
  14. package/dist/GraphNode.js +138 -0
  15. package/dist/GraphSvg.d.ts +50 -0
  16. package/dist/GraphSvg.js +77 -0
  17. package/dist/GraphViewport.d.ts +16 -0
  18. package/dist/GraphViewport.js +27 -0
  19. package/dist/NodeBadges.d.ts +8 -0
  20. package/dist/NodeBadges.js +19 -0
  21. package/dist/NodeBox.d.ts +8 -0
  22. package/dist/NodeBox.js +8 -0
  23. package/dist/NodeTitle.d.ts +8 -0
  24. package/dist/NodeTitle.js +7 -0
  25. package/dist/NodeTooltip.d.ts +9 -0
  26. package/dist/NodeTooltip.js +30 -0
  27. package/dist/RunGraphLegend.d.ts +4 -0
  28. package/dist/RunGraphLegend.js +20 -0
  29. package/dist/RunGraphView.d.ts +27 -0
  30. package/dist/RunGraphView.js +151 -0
  31. package/dist/ToggleButton.d.ts +8 -0
  32. package/dist/ToggleButton.js +6 -0
  33. package/dist/TypeGlyph.d.ts +9 -0
  34. package/dist/TypeGlyph.js +23 -0
  35. package/dist/ViewportControls.d.ts +13 -0
  36. package/dist/ViewportControls.js +11 -0
  37. package/dist/assembly-line-definition.d.ts +35 -0
  38. package/dist/assembly-line-definition.js +2 -0
  39. package/dist/elk-graph.d.ts +16 -0
  40. package/dist/elk-graph.js +131 -0
  41. package/dist/elk-layout.d.ts +46 -0
  42. package/dist/elk-layout.js +67 -0
  43. package/dist/graph-markers.d.ts +2 -0
  44. package/dist/graph-markers.js +10 -0
  45. package/dist/graph-view-model.d.ts +33 -0
  46. package/dist/graph-view-model.js +55 -0
  47. package/dist/index.d.ts +9 -0
  48. package/dist/index.js +6 -0
  49. package/dist/layout-geometry.d.ts +16 -0
  50. package/dist/layout-geometry.js +56 -0
  51. package/dist/metro/declared-ids.d.ts +3 -0
  52. package/dist/metro/declared-ids.js +5 -0
  53. package/dist/metro/edge-key.d.ts +3 -0
  54. package/dist/metro/edge-key.js +5 -0
  55. package/dist/metro/layout-diff.d.ts +12 -0
  56. package/dist/metro/layout-diff.js +32 -0
  57. package/dist/metro/main-path.d.ts +8 -0
  58. package/dist/metro/main-path.js +26 -0
  59. package/dist/metro/neighbourhood.d.ts +7 -0
  60. package/dist/metro/neighbourhood.js +28 -0
  61. package/dist/metro/node-edges.d.ts +5 -0
  62. package/dist/metro/node-edges.js +9 -0
  63. package/dist/metro/pass-through.d.ts +3 -0
  64. package/dist/metro/pass-through.js +16 -0
  65. package/dist/metro/reachability.d.ts +8 -0
  66. package/dist/metro/reachability.js +31 -0
  67. package/dist/metro/removed-stations.d.ts +13 -0
  68. package/dist/metro/removed-stations.js +11 -0
  69. package/dist/metro/station-visits.d.ts +7 -0
  70. package/dist/metro/station-visits.js +13 -0
  71. package/dist/metro/step-along.d.ts +8 -0
  72. package/dist/metro/step-along.js +28 -0
  73. package/dist/metro/taken-count.d.ts +6 -0
  74. package/dist/metro/taken-count.js +11 -0
  75. package/dist/metro/taken-lines.d.ts +12 -0
  76. package/dist/metro/taken-lines.js +20 -0
  77. package/dist/metro/visited-part.d.ts +4 -0
  78. package/dist/metro/visited-part.js +24 -0
  79. package/dist/node-aria-label.d.ts +9 -0
  80. package/dist/node-aria-label.js +11 -0
  81. package/dist/node-badges.d.ts +8 -0
  82. package/dist/node-badges.js +20 -0
  83. package/dist/node-tooltip.d.ts +12 -0
  84. package/dist/node-tooltip.js +34 -0
  85. package/dist/node-type-family.d.ts +6 -0
  86. package/dist/node-type-family.js +18 -0
  87. package/dist/run-graph-drawing.d.ts +14 -0
  88. package/dist/run-graph-drawing.js +22 -0
  89. package/dist/run-graph-geometry.d.ts +18 -0
  90. package/dist/run-graph-geometry.js +38 -0
  91. package/dist/run-graph-tone-classes.d.ts +5 -0
  92. package/dist/run-graph-tone-classes.js +34 -0
  93. package/dist/run-graph.module.css +470 -0
  94. package/dist/run-graph.test-helpers.d.ts +4 -0
  95. package/dist/run-graph.test-helpers.js +11 -0
  96. package/dist/station-subtitle.d.ts +2 -0
  97. package/dist/station-subtitle.js +6 -0
  98. package/dist/use-element-size.d.ts +6 -0
  99. package/dist/use-element-size.js +17 -0
  100. package/dist/use-elk-layout.d.ts +5 -0
  101. package/dist/use-elk-layout.js +19 -0
  102. package/dist/use-keyboard-cursor.d.ts +10 -0
  103. package/dist/use-keyboard-cursor.js +39 -0
  104. package/dist/use-layout-changes.d.ts +11 -0
  105. package/dist/use-layout-changes.js +45 -0
  106. package/dist/use-measured-box.d.ts +3 -0
  107. package/dist/use-measured-box.js +13 -0
  108. package/dist/use-node-size.d.ts +7 -0
  109. package/dist/use-node-size.js +17 -0
  110. package/dist/use-node-tooltip.d.ts +20 -0
  111. package/dist/use-node-tooltip.js +74 -0
  112. package/dist/use-pointer-drag.d.ts +7 -0
  113. package/dist/use-pointer-drag.js +23 -0
  114. package/dist/use-redrawn.d.ts +4 -0
  115. package/dist/use-redrawn.js +8 -0
  116. package/dist/use-revealed.d.ts +4 -0
  117. package/dist/use-revealed.js +11 -0
  118. package/dist/use-run-overlay.d.ts +7 -0
  119. package/dist/use-run-overlay.js +11 -0
  120. package/dist/use-toggle.d.ts +4 -0
  121. package/dist/use-toggle.js +7 -0
  122. package/dist/use-wheel-navigation.d.ts +3 -0
  123. package/dist/use-wheel-navigation.js +19 -0
  124. package/dist/viewport-input.d.ts +16 -0
  125. package/dist/viewport-input.js +19 -0
  126. package/dist/viewport-state.d.ts +46 -0
  127. package/dist/viewport-state.js +48 -0
  128. package/dist/viewport.d.ts +27 -0
  129. package/dist/viewport.js +66 -0
  130. package/dist/viewport.module.css +67 -0
  131. package/package.json +69 -4
@@ -0,0 +1,17 @@
1
+ // The card size ELK lays out: each card as wide as the longer of its title and its run-aware subtitle.
2
+ import { useMemo } from "react";
3
+ import { nodeTitle } from "./node-tooltip";
4
+ import { typeFamilyOf } from "./node-type-family";
5
+ import { NODE_HEIGHT, NODE_WIDTH, cardWidthOf } from "./run-graph-geometry";
6
+ import { subtitleOf } from "./station-subtitle";
7
+ export function useNodeSize(definition, visits) {
8
+ return useMemo(() => {
9
+ const familyById = new Map(definition.nodes.map((node) => [node.id, typeFamilyOf(node.type)]));
10
+ return {
11
+ width: NODE_WIDTH,
12
+ height: NODE_HEIGHT,
13
+ widthOf: (nodeId) => cardWidthOf(nodeTitle(nodeId), subtitleOf(familyById.get(nodeId) ?? "marker", visits?.get(nodeId))),
14
+ };
15
+ }, [definition, visits]);
16
+ }
17
+ //# sourceMappingURL=use-node-size.js.map
@@ -0,0 +1,20 @@
1
+ import { type FocusEvent, type KeyboardEvent, type PointerEvent } from "react";
2
+ export interface TooltipAnchor {
3
+ nodeId: string;
4
+ left: number;
5
+ top: number;
6
+ bottom: number;
7
+ }
8
+ export interface NodeTooltipHandlers {
9
+ onPointerOver: (event: PointerEvent<HTMLElement>) => void;
10
+ onPointerOut: (event: PointerEvent<HTMLElement>) => void;
11
+ onFocus: (event: FocusEvent<HTMLElement>) => void;
12
+ onBlur: () => void;
13
+ onKeyDown: (event: KeyboardEvent<HTMLElement>) => void;
14
+ }
15
+ export interface NodeTooltipState {
16
+ id: string;
17
+ anchor: TooltipAnchor | null;
18
+ handlers: NodeTooltipHandlers;
19
+ }
20
+ export declare function useNodeTooltip(): NodeTooltipState;
@@ -0,0 +1,74 @@
1
+ // Which node's tooltip is up and where (run-viz FR4.1h). A hover waits a moment so a pointer crossing the graph does not flash every box; keyboard focus shows it at once; leaving, blurring, Escape and any scroll take it down.
2
+ import { useCallback, useEffect, useId, useState, } from "react";
3
+ const HOVER_DELAY_MS = 250;
4
+ export function useNodeTooltip() {
5
+ const id = useId();
6
+ const [hovered, setHovered] = useState(null);
7
+ const [anchor, setAnchor] = useState(null);
8
+ const hide = useCallback(() => {
9
+ setHovered(null);
10
+ setAnchor(null);
11
+ }, []);
12
+ useEffect(() => showAfterDelay(hovered, setAnchor), [hovered]);
13
+ useEffect(() => closeOnScroll(anchor, hide), [anchor, hide]);
14
+ return { id, anchor, handlers: handlersFor({ setHovered, setAnchor, hide }) };
15
+ }
16
+ function handlersFor({ setHovered, setAnchor, hide, }) {
17
+ return {
18
+ onPointerOver: (event) => setHovered(anchorOf(event.target)),
19
+ onPointerOut: (event) => {
20
+ if (nodeOf(event.relatedTarget) !== nodeOf(event.target)) {
21
+ hide();
22
+ }
23
+ },
24
+ onFocus: (event) => showNow(anchorOf(event.target), setAnchor),
25
+ onBlur: hide,
26
+ onKeyDown: closeOnEscape(hide),
27
+ };
28
+ }
29
+ function closeOnEscape(hide) {
30
+ return (event) => {
31
+ if (event.key === "Escape") {
32
+ hide();
33
+ }
34
+ };
35
+ }
36
+ function showNow(found, setAnchor) {
37
+ if (found) {
38
+ setAnchor(found);
39
+ }
40
+ }
41
+ function showAfterDelay(hovered, setAnchor) {
42
+ if (!hovered) {
43
+ return undefined;
44
+ }
45
+ const timer = setTimeout(() => setAnchor(hovered), HOVER_DELAY_MS);
46
+ return () => clearTimeout(timer);
47
+ }
48
+ function closeOnScroll(anchor, hide) {
49
+ if (!anchor) {
50
+ return undefined;
51
+ }
52
+ const options = { capture: true };
53
+ window.addEventListener("scroll", hide, options);
54
+ return () => window.removeEventListener("scroll", hide, options);
55
+ }
56
+ /** The graph node an event happened on: the nearest group carrying `data-node`. */
57
+ function nodeOf(target) {
58
+ return target instanceof Element ? target.closest("[data-node]") : null;
59
+ }
60
+ function anchorOf(target) {
61
+ const node = nodeOf(target);
62
+ const nodeId = node?.getAttribute("data-node");
63
+ if (!node || !nodeId) {
64
+ return null;
65
+ }
66
+ const box = node.getBoundingClientRect();
67
+ return {
68
+ nodeId,
69
+ left: box.left + box.width / 2,
70
+ top: box.top,
71
+ bottom: box.bottom,
72
+ };
73
+ }
74
+ //# sourceMappingURL=use-node-tooltip.js.map
@@ -0,0 +1,7 @@
1
+ import { type Dispatch, type PointerEvent } from "react";
2
+ import type { ViewportAction } from "./viewport-state";
3
+ export declare function usePointerDrag(dispatch: Dispatch<ViewportAction>): {
4
+ onPointerDown: (event: PointerEvent) => void;
5
+ onPointerMove: (event: PointerEvent) => void;
6
+ onPointerUp: () => void;
7
+ };
@@ -0,0 +1,23 @@
1
+ // Turns pointer presses and moves on the viewport box into drag actions.
2
+ import { useRef } from "react";
3
+ import { dragAction } from "./viewport-input";
4
+ export function usePointerDrag(dispatch) {
5
+ const lastPointer = useRef(null);
6
+ return {
7
+ onPointerDown: (event) => {
8
+ lastPointer.current = { x: event.clientX, y: event.clientY };
9
+ },
10
+ onPointerMove: (event) => {
11
+ const pointer = { x: event.clientX, y: event.clientY };
12
+ if (!lastPointer.current) {
13
+ return;
14
+ }
15
+ dispatch(dragAction(lastPointer.current, pointer));
16
+ lastPointer.current = pointer;
17
+ },
18
+ onPointerUp: () => {
19
+ lastPointer.current = null;
20
+ },
21
+ };
22
+ }
23
+ //# sourceMappingURL=use-pointer-drag.js.map
@@ -0,0 +1,4 @@
1
+ import { type Dispatch } from "react";
2
+ import type { Size } from "./viewport";
3
+ import type { ViewportAction } from "./viewport-state";
4
+ export declare function useRedrawn({ width, height }: Size, dispatch: Dispatch<ViewportAction>): void;
@@ -0,0 +1,8 @@
1
+ // Tells the viewport each new size of the drawing, so it can fit the new drawing.
2
+ import { useEffect } from "react";
3
+ export function useRedrawn({ width, height }, dispatch) {
4
+ useEffect(() => {
5
+ dispatch({ type: "redrawn", drawing: { width, height } });
6
+ }, [width, height, dispatch]);
7
+ }
8
+ //# sourceMappingURL=use-redrawn.js.map
@@ -0,0 +1,4 @@
1
+ import { type Dispatch } from "react";
2
+ import type { Box } from "./elk-layout";
3
+ import type { ViewportAction } from "./viewport-state";
4
+ export declare function useRevealed(area: Box | undefined, coveredBelow: number | undefined, dispatch: Dispatch<ViewportAction>): void;
@@ -0,0 +1,11 @@
1
+ // Tells the viewport each new area to bring into view, keyed by value so an equal fresh object is ignored.
2
+ import { useEffect } from "react";
3
+ export function useRevealed(area, coveredBelow, dispatch) {
4
+ const areaKey = area === undefined ? undefined : JSON.stringify(area);
5
+ useEffect(() => {
6
+ if (areaKey === undefined)
7
+ return;
8
+ dispatch({ type: "revealed", area: JSON.parse(areaKey), coveredBelow });
9
+ }, [areaKey, coveredBelow, dispatch]);
10
+ }
11
+ //# sourceMappingURL=use-revealed.js.map
@@ -0,0 +1,7 @@
1
+ import type { AssemblyLineDefinition } from "./assembly-line-definition";
2
+ import { type Visit } from "./metro/taken-lines";
3
+ export interface RunOverlay {
4
+ visits: readonly Visit[];
5
+ }
6
+ export declare function useTakenLines(definition: AssemblyLineDefinition, run: RunOverlay | undefined): ReadonlyMap<string, number> | undefined;
7
+ export declare function useStationVisits(run: RunOverlay | undefined): ReadonlyMap<string, import("./metro/station-visits").StationVisits> | undefined;
@@ -0,0 +1,11 @@
1
+ // The lines an assembly run took, for the graph to mark; undefined while no run is shown.
2
+ import { useMemo } from "react";
3
+ import { stationVisitsOf } from "./metro/station-visits";
4
+ import { takenLinesOf } from "./metro/taken-lines";
5
+ export function useTakenLines(definition, run) {
6
+ return useMemo(() => run && takenLinesOf(definition, run.visits), [definition, run]);
7
+ }
8
+ export function useStationVisits(run) {
9
+ return useMemo(() => run && stationVisitsOf(run.visits), [run]);
10
+ }
11
+ //# sourceMappingURL=use-run-overlay.js.map
@@ -0,0 +1,4 @@
1
+ /** An on/off state that starts where `initiallyOn` says and flips on each call to its toggle. */
2
+ export declare function useToggle({ initiallyOn }: {
3
+ initiallyOn?: boolean;
4
+ }): readonly [boolean, () => void];
@@ -0,0 +1,7 @@
1
+ import { useState } from "react";
2
+ /** An on/off state that starts where `initiallyOn` says and flips on each call to its toggle. */
3
+ export function useToggle({ initiallyOn }) {
4
+ const [isOn, setIsOn] = useState(!!initiallyOn);
5
+ return [isOn, () => setIsOn((on) => !on)];
6
+ }
7
+ //# sourceMappingURL=use-toggle.js.map
@@ -0,0 +1,3 @@
1
+ import { type Dispatch, type RefObject } from "react";
2
+ import type { ViewportAction } from "./viewport-state";
3
+ export declare function useWheelNavigation(boxRef: RefObject<SVGSVGElement | null>, dispatch: Dispatch<ViewportAction>): void;
@@ -0,0 +1,19 @@
1
+ // Turns wheel turns on the viewport box into pan and zoom actions, keeping the page from scrolling.
2
+ import { useEffect } from "react";
3
+ import { wheelAction } from "./viewport-input";
4
+ export function useWheelNavigation(boxRef, dispatch) {
5
+ useEffect(() => {
6
+ const box = boxRef.current;
7
+ if (!box) {
8
+ return;
9
+ }
10
+ const onWheel = (event) => {
11
+ event.preventDefault();
12
+ dispatch(wheelAction(event, box.getBoundingClientRect()));
13
+ };
14
+ // Not React's onWheel: that one is passive, so preventDefault could never stop the page scrolling.
15
+ box.addEventListener("wheel", onWheel, { passive: false });
16
+ return () => box.removeEventListener("wheel", onWheel);
17
+ }, [boxRef, dispatch]);
18
+ }
19
+ //# sourceMappingURL=use-wheel-navigation.js.map
@@ -0,0 +1,16 @@
1
+ import type { ViewportAction } from "./viewport-state";
2
+ export type ViewportWheel = Pick<WheelEvent, "ctrlKey" | "metaKey" | "deltaX" | "deltaY" | "clientX" | "clientY">;
3
+ export declare function wheelAction(wheel: ViewportWheel, boxOrigin: Pick<DOMRect, "left" | "top">): ViewportAction;
4
+ export interface PointerPosition {
5
+ x: number;
6
+ y: number;
7
+ }
8
+ export declare function dragAction(previous: PointerPosition, pointer: PointerPosition): ViewportAction;
9
+ export interface ViewportControl {
10
+ label: string;
11
+ icon: string;
12
+ key: string;
13
+ action: ViewportAction;
14
+ }
15
+ export declare const viewportControls: ReadonlyArray<ViewportControl>;
16
+ export declare function keyAction(key: string): ViewportAction | undefined;
@@ -0,0 +1,19 @@
1
+ // A Ctrl wheel is how browsers report a trackpad pinch, and a Cmd wheel zooms too; any other wheel scrolls.
2
+ export function wheelAction(wheel, boxOrigin) {
3
+ return wheel.ctrlKey || wheel.metaKey
4
+ ? { type: "pinched", anchor: { x: wheel.clientX - boxOrigin.left, y: wheel.clientY - boxOrigin.top }, delta: wheel.deltaY }
5
+ : { type: "scrolled", delta: { x: wheel.deltaX, y: wheel.deltaY } };
6
+ }
7
+ export function dragAction(previous, pointer) {
8
+ return { type: "dragged", drag: { x: pointer.x - previous.x, y: pointer.y - previous.y } };
9
+ }
10
+ // One list read by both the buttons and the keyboard, so a control and its key cannot drift apart.
11
+ export const viewportControls = [
12
+ { label: "Zoom in", icon: "M8 3v10M3 8h10", key: "+", action: { type: "zoomed", direction: "in" } },
13
+ { label: "Zoom out", icon: "M3 8h10", key: "-", action: { type: "zoomed", direction: "out" } },
14
+ { label: "Fit", icon: "M2 6V2h4M10 2h4v4M14 10v4h-4M6 14H2v-4", key: "0", action: { type: "fit" } },
15
+ ];
16
+ export function keyAction(key) {
17
+ return viewportControls.find((control) => control.key === key)?.action;
18
+ }
19
+ //# sourceMappingURL=viewport-input.js.map
@@ -0,0 +1,46 @@
1
+ import type { Box } from "./elk-layout";
2
+ import { type BoxOffset, type BoxPoint, type Size, type ViewportTransform } from "./viewport";
3
+ export interface ViewportState {
4
+ drawing: Size;
5
+ box: Size;
6
+ transform: ViewportTransform;
7
+ fitted: boolean;
8
+ }
9
+ export type ZoomDirection = "in" | "out";
10
+ type NoPayload = unknown;
11
+ interface ViewportEvents {
12
+ measured: {
13
+ box: Size;
14
+ };
15
+ redrawn: {
16
+ drawing: Size;
17
+ };
18
+ dragged: {
19
+ drag: BoxOffset;
20
+ };
21
+ scrolled: {
22
+ delta: BoxOffset;
23
+ };
24
+ pinched: {
25
+ anchor: BoxPoint;
26
+ delta: number;
27
+ };
28
+ zoomed: {
29
+ direction: ZoomDirection;
30
+ };
31
+ revealed: {
32
+ area: Box;
33
+ coveredBelow?: number;
34
+ };
35
+ fit: NoPayload;
36
+ }
37
+ type ViewportKind = keyof ViewportEvents;
38
+ type ActionOf<Kind extends ViewportKind> = {
39
+ [Each in Kind]: {
40
+ type: Each;
41
+ } & ViewportEvents[Each];
42
+ }[Kind];
43
+ export type ViewportAction = ActionOf<ViewportKind>;
44
+ export declare function initialViewport(drawing: Size): ViewportState;
45
+ export declare function viewportReducer(state: ViewportState, action: ViewportAction): ViewportState;
46
+ export {};
@@ -0,0 +1,48 @@
1
+ import { fitTransform, IDENTITY, isSameTransform, keepInView, panBy, revealArea, UNMEASURED, zoomAt } from "./viewport";
2
+ const WHEEL_DISTANCE_TO_DOUBLE = 100;
3
+ const ZOOM_STEP = 1.25;
4
+ const ZOOM_FACTORS = { in: ZOOM_STEP, out: 1 / ZOOM_STEP };
5
+ export function initialViewport(drawing) {
6
+ return { drawing, box: UNMEASURED, transform: IDENTITY, fitted: true };
7
+ }
8
+ export function viewportReducer(state, action) {
9
+ return applied(state, action);
10
+ }
11
+ // Generic so TypeScript pairs `RULES[action.type]` with its own action kind without a cast.
12
+ function applied(state, action) {
13
+ const rule = RULES[action.type];
14
+ return rule(state, action);
15
+ }
16
+ const RULES = {
17
+ measured: (state, { box }) => settled({ ...state, box }),
18
+ redrawn: (state, { drawing }) => settled({ ...state, drawing }),
19
+ dragged: (state, { drag }) => movedByPerson(state, panBy(state.transform, drag)),
20
+ scrolled: (state, { delta }) => movedByPerson(state, panBy(state.transform, scrollAsDrag(delta))),
21
+ pinched: (state, { anchor, delta }) => movedByPerson(state, zoomAt(state.transform, anchor, pinchAsZoom(delta))),
22
+ zoomed: (state, { direction }) => movedByPerson(state, zoomAt(state.transform, boxCenter(state.box), ZOOM_FACTORS[direction])),
23
+ revealed: (state, { area, coveredBelow }) => movedIfChanged(state, revealArea(state.transform, area, state.box, coveredBelow)),
24
+ fit: (state) => settled({ ...state, fitted: true }),
25
+ };
26
+ function movedIfChanged(state, transform) {
27
+ return isSameTransform(state.transform, transform) ? state : movedByPerson(state, transform);
28
+ }
29
+ function movedByPerson(state, transform) {
30
+ return settled({ ...state, transform, fitted: false });
31
+ }
32
+ // A fitted view follows the drawing and the box; a view the person moved is only held in the box.
33
+ function settled(state) {
34
+ const { drawing, box, transform, fitted } = state;
35
+ return { ...state, transform: fitted ? fitTransform(drawing, box) : keepInView(transform, drawing, box) };
36
+ }
37
+ function boxCenter(box) {
38
+ return { x: box.width / 2, y: box.height / 2 };
39
+ }
40
+ // Wheel up (negative delta) zooms in, wheel down zooms out.
41
+ function pinchAsZoom(pinch) {
42
+ return 2 ** (-pinch / WHEEL_DISTANCE_TO_DOUBLE);
43
+ }
44
+ // A wheel delta says where the view travels over the drawing, so the drawing itself travels the opposite way.
45
+ function scrollAsDrag(scroll) {
46
+ return { x: -scroll.x, y: -scroll.y };
47
+ }
48
+ //# sourceMappingURL=viewport-state.js.map
@@ -0,0 +1,27 @@
1
+ import type { Box } from "./elk-layout";
2
+ export interface Size {
3
+ width: number;
4
+ height: number;
5
+ }
6
+ export interface ViewportTransform {
7
+ scale: number;
8
+ x: number;
9
+ y: number;
10
+ }
11
+ export interface BoxPoint {
12
+ x: number;
13
+ y: number;
14
+ }
15
+ export interface BoxOffset {
16
+ x: number;
17
+ y: number;
18
+ }
19
+ export declare const IDENTITY: ViewportTransform;
20
+ export declare const UNMEASURED: Size;
21
+ export declare function fitTransform(drawing: Size, box: Size): ViewportTransform;
22
+ export declare function zoomAt(current: ViewportTransform, anchor: BoxPoint, factor: number): ViewportTransform;
23
+ export declare function panBy(current: ViewportTransform, drag: BoxOffset): ViewportTransform;
24
+ export declare function keepInView(current: ViewportTransform, drawing: Size, box: Size): ViewportTransform;
25
+ export declare function revealArea(current: ViewportTransform, area: Box, box: Size, coveredBelow?: number): ViewportTransform;
26
+ export declare function isSameTransform(left: ViewportTransform, right: ViewportTransform): boolean;
27
+ export declare function svgTransformOf(transform: ViewportTransform): string;
@@ -0,0 +1,66 @@
1
+ const NATURAL_SCALE = 1;
2
+ const MIN_SCALE = 0.1;
3
+ const MAX_SCALE = 2;
4
+ const VISIBLE_STRIP = 48;
5
+ const REVEAL_MARGIN = 24;
6
+ export const IDENTITY = { scale: NATURAL_SCALE, x: 0, y: 0 };
7
+ export const UNMEASURED = { width: 0, height: 0 };
8
+ export function fitTransform(drawing, box) {
9
+ if (isUnmeasured(box))
10
+ return IDENTITY;
11
+ const scale = Math.min(NATURAL_SCALE, box.width / drawing.width, box.height / drawing.height);
12
+ return {
13
+ scale,
14
+ x: (box.width - drawing.width * scale) / 2,
15
+ y: (box.height - drawing.height * scale) / 2,
16
+ };
17
+ }
18
+ export function zoomAt(current, anchor, factor) {
19
+ const scale = withinLimits(current.scale * factor, MIN_SCALE, MAX_SCALE);
20
+ const appliedFactor = scale / current.scale;
21
+ return {
22
+ scale,
23
+ x: anchor.x - (anchor.x - current.x) * appliedFactor,
24
+ y: anchor.y - (anchor.y - current.y) * appliedFactor,
25
+ };
26
+ }
27
+ export function panBy(current, drag) {
28
+ return { scale: current.scale, x: current.x + drag.x, y: current.y + drag.y };
29
+ }
30
+ export function keepInView(current, drawing, box) {
31
+ if (isUnmeasured(box))
32
+ return current;
33
+ return {
34
+ ...current,
35
+ x: offsetInView(current.x, drawing.width * current.scale, box.width),
36
+ y: offsetInView(current.y, drawing.height * current.scale, box.height),
37
+ };
38
+ }
39
+ export function revealArea(current, area, box, coveredBelow = 0) {
40
+ if (isUnmeasured(box))
41
+ return current;
42
+ return {
43
+ ...current,
44
+ x: offsetRevealing(current.x, area.minX * current.scale, area.maxX * current.scale, box.width),
45
+ y: offsetRevealing(current.y, area.minY * current.scale, area.maxY * current.scale, box.height - coveredBelow),
46
+ };
47
+ }
48
+ export function isSameTransform(left, right) {
49
+ return left.scale === right.scale && left.x === right.x && left.y === right.y;
50
+ }
51
+ function offsetInView(offset, lengthOnScreen, boxLength) {
52
+ return withinLimits(offset, VISIBLE_STRIP - lengthOnScreen, boxLength - VISIBLE_STRIP);
53
+ }
54
+ function offsetRevealing(offset, startOnScreen, endOnScreen, boxLength) {
55
+ return withinLimits(offset, REVEAL_MARGIN - startOnScreen, boxLength - REVEAL_MARGIN - endOnScreen);
56
+ }
57
+ function withinLimits(value, lower, upper) {
58
+ return Math.max(lower, Math.min(value, upper));
59
+ }
60
+ function isUnmeasured(box) {
61
+ return box.width === 0 || box.height === 0;
62
+ }
63
+ export function svgTransformOf(transform) {
64
+ return `translate(${transform.x} ${transform.y}) scale(${transform.scale})`;
65
+ }
66
+ //# sourceMappingURL=viewport.js.map
@@ -0,0 +1,67 @@
1
+ .viewport {
2
+ position: relative;
3
+ flex: 1;
4
+ min-height: 0;
5
+ border: 1px solid var(--border);
6
+ border-radius: var(--radius);
7
+ background: var(--bg-surface);
8
+ overflow: hidden;
9
+ }
10
+
11
+ .box {
12
+ display: block;
13
+ width: 100%;
14
+ height: 100%;
15
+ touch-action: none;
16
+ /* Dragging pans the graph; it must not select the labels it passes over. */
17
+ user-select: none;
18
+ -webkit-user-select: none;
19
+ cursor: grab;
20
+ }
21
+
22
+ .box:active {
23
+ cursor: grabbing;
24
+ }
25
+
26
+ .controls {
27
+ position: absolute;
28
+ right: var(--space-3);
29
+ bottom: var(--space-3);
30
+ display: flex;
31
+ gap: var(--space-2);
32
+ }
33
+
34
+ .controls button {
35
+ display: grid;
36
+ place-items: center;
37
+ width: 30px;
38
+ height: 30px;
39
+ min-height: 0;
40
+ padding: 0;
41
+ border: 1px solid var(--border);
42
+ border-radius: var(--radius);
43
+ background: var(--bg-surface);
44
+ color: inherit;
45
+ cursor: pointer;
46
+ transition: background 150ms, border-color 150ms, transform 150ms;
47
+ }
48
+
49
+ .controls button:hover {
50
+ border-color: var(--border-hover);
51
+ background: var(--bg-hover);
52
+ }
53
+
54
+ .controls button:active {
55
+ transform: scale(0.94);
56
+ }
57
+
58
+ .controls button:focus-visible {
59
+ outline: 2px solid var(--accent);
60
+ outline-offset: 2px;
61
+ }
62
+
63
+ .controls button[aria-pressed="true"] {
64
+ border-color: var(--accent);
65
+ background: color-mix(in srgb, var(--accent) 18%, var(--bg-surface));
66
+ transition: background 150ms, border-color 150ms, transform 150ms;
67
+ }
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@re-cinq/floor-graph",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.6.1",
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
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/re-cinq/floor.git",
9
+ "directory": "packages/graph"
10
+ },
11
+ "homepage": "https://github.com/re-cinq/floor/tree/main/packages/graph#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/re-cinq/floor/issues"
14
+ },
15
+ "keywords": [
16
+ "floor",
17
+ "assembly-line",
18
+ "graph",
19
+ "react"
20
+ ],
21
+ "type": "module",
22
+ "main": "dist/index.js",
23
+ "types": "dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "!dist/**/*.map"
33
+ ],
34
+ "engines": {
35
+ "node": ">=22"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "scripts": {
41
+ "build": "tsc -p tsconfig.build.json && node scripts/copy-css.mjs",
42
+ "dev": "tsc --watch -p tsconfig.build.json",
43
+ "test": "vitest run",
44
+ "test:watch": "vitest",
45
+ "typecheck": "tsc --noEmit"
46
+ },
47
+ "dependencies": {
48
+ "@re-cinq/floor-contracts": "0.6.1",
49
+ "elkjs": "0.9.3"
50
+ },
51
+ "peerDependencies": {
52
+ "react": ">=19",
53
+ "react-dom": ">=19"
54
+ },
55
+ "devDependencies": {
56
+ "@testing-library/dom": "^10.4.2",
57
+ "@testing-library/react": "^16.3.3",
58
+ "@testing-library/user-event": "^14.6.13",
59
+ "@types/node": "^22.20.1",
60
+ "@types/react": "^19.3.0",
61
+ "@types/react-dom": "^19.3.0",
62
+ "@vitejs/plugin-react": "^6.3.0",
63
+ "@vitest/coverage-v8": "^5.0.0",
64
+ "jsdom": "^31.0.1",
65
+ "react": "^19.3.1",
66
+ "react-dom": "^19.3.1",
67
+ "typescript": "^7.0.2",
68
+ "vite": "^8.3.8",
69
+ "vitest": "^5.0.0"
70
+ }
71
+ }