@multiplatform.one/node-graph 9.0.10 → 9.1.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.
Files changed (124) hide show
  1. package/dist/cjs/graphLayout/GraphDots.cjs +60 -0
  2. package/dist/cjs/graphLayout/GraphDots.native.js +66 -0
  3. package/dist/cjs/graphLayout/GraphDots.native.js.map +1 -0
  4. package/dist/cjs/graphLayout/GraphLayout.cjs +153 -0
  5. package/dist/cjs/graphLayout/GraphLayout.native.js +163 -0
  6. package/dist/cjs/graphLayout/GraphLayout.native.js.map +1 -0
  7. package/dist/cjs/graphLayout/GraphSvg.cjs +68 -0
  8. package/dist/cjs/graphLayout/GraphSvg.native.js +73 -0
  9. package/dist/cjs/graphLayout/GraphSvg.native.js.map +1 -0
  10. package/dist/cjs/graphLayout/GraphViewport.cjs +263 -0
  11. package/dist/cjs/graphLayout/GraphViewport.native.js +279 -0
  12. package/dist/cjs/graphLayout/GraphViewport.native.js.map +1 -0
  13. package/dist/cjs/graphLayout/index.cjs +51 -0
  14. package/dist/cjs/graphLayout/index.native.js +54 -0
  15. package/dist/cjs/graphLayout/index.native.js.map +1 -0
  16. package/dist/cjs/graphLayout/ogden.fixture.cjs +30 -0
  17. package/dist/cjs/graphLayout/ogden.fixture.native.js +33 -0
  18. package/dist/cjs/graphLayout/ogden.fixture.native.js.map +1 -0
  19. package/dist/cjs/graphLayout/plan.cjs +263 -0
  20. package/dist/cjs/graphLayout/plan.native.js +372 -0
  21. package/dist/cjs/graphLayout/plan.native.js.map +1 -0
  22. package/dist/cjs/graphLayout/viewMath.cjs +142 -0
  23. package/dist/cjs/graphLayout/viewMath.native.js +147 -0
  24. package/dist/cjs/graphLayout/viewMath.native.js.map +1 -0
  25. package/dist/esm/graphLayout/GraphDots.mjs +35 -0
  26. package/dist/esm/graphLayout/GraphDots.mjs.map +1 -0
  27. package/dist/esm/graphLayout/GraphDots.native.js +38 -0
  28. package/dist/esm/graphLayout/GraphDots.native.js.map +1 -0
  29. package/dist/esm/graphLayout/GraphLayout.mjs +127 -0
  30. package/dist/esm/graphLayout/GraphLayout.mjs.map +1 -0
  31. package/dist/esm/graphLayout/GraphLayout.native.js +134 -0
  32. package/dist/esm/graphLayout/GraphLayout.native.js.map +1 -0
  33. package/dist/esm/graphLayout/GraphSvg.mjs +42 -0
  34. package/dist/esm/graphLayout/GraphSvg.mjs.map +1 -0
  35. package/dist/esm/graphLayout/GraphSvg.native.js +44 -0
  36. package/dist/esm/graphLayout/GraphSvg.native.js.map +1 -0
  37. package/dist/esm/graphLayout/GraphViewport.mjs +238 -0
  38. package/dist/esm/graphLayout/GraphViewport.mjs.map +1 -0
  39. package/dist/esm/graphLayout/GraphViewport.native.js +251 -0
  40. package/dist/esm/graphLayout/GraphViewport.native.js.map +1 -0
  41. package/dist/esm/graphLayout/index.mjs +7 -0
  42. package/dist/esm/graphLayout/index.mjs.map +1 -0
  43. package/dist/esm/graphLayout/index.native.js +7 -0
  44. package/dist/esm/graphLayout/index.native.js.map +1 -0
  45. package/dist/esm/graphLayout/ogden.fixture.mjs +4 -0
  46. package/dist/esm/graphLayout/ogden.fixture.mjs.map +1 -0
  47. package/dist/esm/graphLayout/ogden.fixture.native.js +4 -0
  48. package/dist/esm/graphLayout/ogden.fixture.native.js.map +1 -0
  49. package/dist/esm/graphLayout/plan.mjs +235 -0
  50. package/dist/esm/graphLayout/plan.mjs.map +1 -0
  51. package/dist/esm/graphLayout/plan.native.js +341 -0
  52. package/dist/esm/graphLayout/plan.native.js.map +1 -0
  53. package/dist/esm/graphLayout/viewMath.mjs +105 -0
  54. package/dist/esm/graphLayout/viewMath.mjs.map +1 -0
  55. package/dist/esm/graphLayout/viewMath.native.js +107 -0
  56. package/dist/esm/graphLayout/viewMath.native.js.map +1 -0
  57. package/dist/jsx/graphLayout/GraphDots.mjs +35 -0
  58. package/dist/jsx/graphLayout/GraphDots.mjs.map +1 -0
  59. package/dist/jsx/graphLayout/GraphDots.native.js +66 -0
  60. package/dist/jsx/graphLayout/GraphDots.native.js.map +1 -0
  61. package/dist/jsx/graphLayout/GraphLayout.mjs +127 -0
  62. package/dist/jsx/graphLayout/GraphLayout.mjs.map +1 -0
  63. package/dist/jsx/graphLayout/GraphLayout.native.js +163 -0
  64. package/dist/jsx/graphLayout/GraphLayout.native.js.map +1 -0
  65. package/dist/jsx/graphLayout/GraphSvg.mjs +42 -0
  66. package/dist/jsx/graphLayout/GraphSvg.mjs.map +1 -0
  67. package/dist/jsx/graphLayout/GraphSvg.native.js +73 -0
  68. package/dist/jsx/graphLayout/GraphSvg.native.js.map +1 -0
  69. package/dist/jsx/graphLayout/GraphViewport.mjs +238 -0
  70. package/dist/jsx/graphLayout/GraphViewport.mjs.map +1 -0
  71. package/dist/jsx/graphLayout/GraphViewport.native.js +279 -0
  72. package/dist/jsx/graphLayout/GraphViewport.native.js.map +1 -0
  73. package/dist/jsx/graphLayout/index.mjs +7 -0
  74. package/dist/jsx/graphLayout/index.mjs.map +1 -0
  75. package/dist/jsx/graphLayout/index.native.js +54 -0
  76. package/dist/jsx/graphLayout/index.native.js.map +1 -0
  77. package/dist/jsx/graphLayout/ogden.fixture.mjs +4 -0
  78. package/dist/jsx/graphLayout/ogden.fixture.mjs.map +1 -0
  79. package/dist/jsx/graphLayout/ogden.fixture.native.js +33 -0
  80. package/dist/jsx/graphLayout/ogden.fixture.native.js.map +1 -0
  81. package/dist/jsx/graphLayout/plan.mjs +235 -0
  82. package/dist/jsx/graphLayout/plan.mjs.map +1 -0
  83. package/dist/jsx/graphLayout/plan.native.js +372 -0
  84. package/dist/jsx/graphLayout/plan.native.js.map +1 -0
  85. package/dist/jsx/graphLayout/viewMath.mjs +105 -0
  86. package/dist/jsx/graphLayout/viewMath.mjs.map +1 -0
  87. package/dist/jsx/graphLayout/viewMath.native.js +147 -0
  88. package/dist/jsx/graphLayout/viewMath.native.js.map +1 -0
  89. package/graph-layout.ts +1 -0
  90. package/package.json +21 -6
  91. package/src/graphLayout/GraphDots.tsx +32 -0
  92. package/src/graphLayout/GraphLayout.stories.tsx +86 -0
  93. package/src/graphLayout/GraphLayout.tsx +152 -0
  94. package/src/graphLayout/GraphSvg.native.tsx +31 -0
  95. package/src/graphLayout/GraphSvg.tsx +45 -0
  96. package/src/graphLayout/GraphViewport.native.tsx +231 -0
  97. package/src/graphLayout/GraphViewport.tsx +246 -0
  98. package/src/graphLayout/index.ts +31 -0
  99. package/src/graphLayout/ogden.fixture.ts +120 -0
  100. package/src/graphLayout/plan.spec.ts +202 -0
  101. package/src/graphLayout/plan.ts +223 -0
  102. package/src/graphLayout/purity.spec.ts +67 -0
  103. package/src/graphLayout/viewMath.spec.ts +185 -0
  104. package/src/graphLayout/viewMath.ts +151 -0
  105. package/types/graphLayout/GraphDots.d.ts +9 -0
  106. package/types/graphLayout/GraphDots.d.ts.map +1 -0
  107. package/types/graphLayout/GraphLayout.d.ts +22 -0
  108. package/types/graphLayout/GraphLayout.d.ts.map +1 -0
  109. package/types/graphLayout/GraphSvg.d.ts +17 -0
  110. package/types/graphLayout/GraphSvg.d.ts.map +1 -0
  111. package/types/graphLayout/GraphSvg.native.d.ts +5 -0
  112. package/types/graphLayout/GraphSvg.native.d.ts.map +1 -0
  113. package/types/graphLayout/GraphViewport.d.ts +11 -0
  114. package/types/graphLayout/GraphViewport.d.ts.map +1 -0
  115. package/types/graphLayout/GraphViewport.native.d.ts +4 -0
  116. package/types/graphLayout/GraphViewport.native.d.ts.map +1 -0
  117. package/types/graphLayout/index.d.ts +11 -0
  118. package/types/graphLayout/index.d.ts.map +1 -0
  119. package/types/graphLayout/ogden.fixture.d.ts +3 -0
  120. package/types/graphLayout/ogden.fixture.d.ts.map +1 -0
  121. package/types/graphLayout/plan.d.ts +52 -0
  122. package/types/graphLayout/plan.d.ts.map +1 -0
  123. package/types/graphLayout/viewMath.d.ts +47 -0
  124. package/types/graphLayout/viewMath.d.ts.map +1 -0
@@ -0,0 +1,246 @@
1
+ import { useEffect, useLayoutEffect, useRef, useState } from "react";
2
+ import type { ReactNode } from "react";
3
+ import type { NativeScrollEvent, NativeSyntheticEvent } from "react-native";
4
+ import { ScrollView, View } from "tamagui";
5
+ import { GraphDots } from "./GraphDots";
6
+ import {
7
+ dotArea,
8
+ dotPitch,
9
+ keyedView,
10
+ leastScale,
11
+ sameArea,
12
+ toggledView,
13
+ zoomAt,
14
+ } from "./viewMath";
15
+ import type { Extent, GraphDots as GraphDotsSpec, GraphView } from "./viewMath";
16
+
17
+ export interface GraphViewportProps {
18
+ readonly content: Extent;
19
+ readonly viewport: Extent;
20
+ readonly dots?: GraphDotsSpec;
21
+ readonly frozen?: boolean;
22
+ readonly children: ReactNode;
23
+ }
24
+
25
+ type Point = { x: number; y: number };
26
+
27
+ interface Scrollable {
28
+ getScrollableNode?: () => HTMLElement | null;
29
+ }
30
+
31
+ function editable(target: EventTarget | null): boolean {
32
+ const element = target as { isContentEditable?: boolean; tagName?: string } | null;
33
+ return (
34
+ Boolean(element?.isContentEditable) ||
35
+ ["INPUT", "TEXTAREA", "SELECT"].includes(element?.tagName ?? "")
36
+ );
37
+ }
38
+
39
+ function onNode(target: EventTarget | null): boolean {
40
+ const element = target as { closest?: (selector: string) => unknown } | null;
41
+ return Boolean(element?.closest?.("[data-testid^='graph-node-']"));
42
+ }
43
+
44
+ export function GraphViewport({ content, viewport, dots, frozen, children }: GraphViewportProps) {
45
+ const scroller = useRef<Scrollable>(null);
46
+ const [scale, setScale] = useState(1);
47
+ const [scroll, setScroll] = useState<Point>({ x: 0, y: 0 });
48
+ const offset = useRef<Point>({ x: 0, y: 0 });
49
+ const pending = useRef<Point | undefined>(undefined);
50
+ const shown = Math.min(1, Math.max(leastScale(content, viewport), scale));
51
+ const wide = content.width * shown;
52
+ const tall = content.height * shown;
53
+ const pad = {
54
+ x: Math.max(0, (viewport.width - wide) / 2),
55
+ y: Math.max(0, (viewport.height - tall) / 2),
56
+ };
57
+ const live = useRef({ shown, pad, content, viewport, frozen });
58
+ live.current = { shown, pad, content, viewport, frozen };
59
+
60
+ const node = () => scroller.current?.getScrollableNode?.() ?? undefined;
61
+ const now = (): GraphView => ({
62
+ scale: live.current.shown,
63
+ x: live.current.pad.x - offset.current.x,
64
+ y: live.current.pad.y - offset.current.y,
65
+ });
66
+ const apply = (next: GraphView) => {
67
+ const { content: size, viewport: view } = live.current;
68
+ const padX = Math.max(0, (view.width - size.width * next.scale) / 2);
69
+ const padY = Math.max(0, (view.height - size.height * next.scale) / 2);
70
+ pending.current = { x: Math.max(0, padX - next.x), y: Math.max(0, padY - next.y) };
71
+ setScale(next.scale);
72
+ };
73
+ const applyRef = useRef(apply);
74
+ applyRef.current = apply;
75
+ const nowRef = useRef(now);
76
+ nowRef.current = now;
77
+
78
+ useLayoutEffect(() => {
79
+ const target = pending.current;
80
+ const element = node();
81
+ if (!target) return;
82
+ pending.current = undefined;
83
+ offset.current = target;
84
+ setScroll(target);
85
+ if (element) {
86
+ element.scrollLeft = target.x;
87
+ element.scrollTop = target.y;
88
+ }
89
+ }, [shown]);
90
+
91
+ useEffect(() => {
92
+ const element = node();
93
+ if (!element || typeof element.addEventListener !== "function") return;
94
+ element.style.touchAction = "pan-x pan-y";
95
+ element.tabIndex = 0;
96
+ const at = (clientX: number, clientY: number): Point => {
97
+ const box = element.getBoundingClientRect();
98
+ return { x: clientX - box.left, y: clientY - box.top };
99
+ };
100
+ const onWheel = (event: WheelEvent) => {
101
+ if (live.current.frozen || (!event.ctrlKey && !event.metaKey)) return;
102
+ event.preventDefault();
103
+ const current = nowRef.current();
104
+ const factor = 1.002 ** -event.deltaY;
105
+ applyRef.current(
106
+ zoomAt(
107
+ current,
108
+ current.scale * factor,
109
+ at(event.clientX, event.clientY),
110
+ live.current.content,
111
+ live.current.viewport,
112
+ ),
113
+ );
114
+ };
115
+ const onDouble = (event: MouseEvent) => {
116
+ if (live.current.frozen || onNode(event.target)) return;
117
+ applyRef.current(
118
+ toggledView(
119
+ nowRef.current(),
120
+ at(event.clientX, event.clientY),
121
+ live.current.content,
122
+ live.current.viewport,
123
+ ),
124
+ );
125
+ };
126
+ const onKey = (event: KeyboardEvent) => {
127
+ if (
128
+ live.current.frozen ||
129
+ event.ctrlKey ||
130
+ event.metaKey ||
131
+ event.altKey ||
132
+ editable(event.target)
133
+ )
134
+ return;
135
+ const next = keyedView(
136
+ event.key,
137
+ nowRef.current(),
138
+ live.current.content,
139
+ live.current.viewport,
140
+ );
141
+ if (!next) return;
142
+ event.preventDefault();
143
+ applyRef.current(next);
144
+ };
145
+ let pinch: { distance: number; scale: number } | undefined;
146
+ const spread = (touches: TouchList) =>
147
+ Math.hypot(
148
+ touches[0]!.clientX - touches[1]!.clientX,
149
+ touches[0]!.clientY - touches[1]!.clientY,
150
+ );
151
+ const middle = (touches: TouchList) =>
152
+ at(
153
+ (touches[0]!.clientX + touches[1]!.clientX) / 2,
154
+ (touches[0]!.clientY + touches[1]!.clientY) / 2,
155
+ );
156
+ const onTouchStart = (event: TouchEvent) => {
157
+ pinch =
158
+ event.touches.length === 2
159
+ ? { distance: spread(event.touches), scale: nowRef.current().scale }
160
+ : undefined;
161
+ };
162
+ const onTouchMove = (event: TouchEvent) => {
163
+ if (live.current.frozen || !pinch || event.touches.length !== 2) return;
164
+ event.preventDefault();
165
+ const ratio = spread(event.touches) / Math.max(1, pinch.distance);
166
+ applyRef.current(
167
+ zoomAt(
168
+ nowRef.current(),
169
+ pinch.scale * ratio,
170
+ middle(event.touches),
171
+ live.current.content,
172
+ live.current.viewport,
173
+ ),
174
+ );
175
+ };
176
+ const onTouchEnd = (event: TouchEvent) => {
177
+ if (event.touches.length < 2) pinch = undefined;
178
+ };
179
+ element.addEventListener("wheel", onWheel, { passive: false });
180
+ element.addEventListener("dblclick", onDouble);
181
+ element.addEventListener("keydown", onKey);
182
+ element.addEventListener("touchstart", onTouchStart, { passive: true });
183
+ element.addEventListener("touchmove", onTouchMove, { passive: false });
184
+ element.addEventListener("touchend", onTouchEnd);
185
+ return () => {
186
+ element.removeEventListener("wheel", onWheel);
187
+ element.removeEventListener("dblclick", onDouble);
188
+ element.removeEventListener("keydown", onKey);
189
+ element.removeEventListener("touchstart", onTouchStart);
190
+ element.removeEventListener("touchmove", onTouchMove);
191
+ element.removeEventListener("touchend", onTouchEnd);
192
+ };
193
+ }, []);
194
+
195
+ const spaced = dots ? dotPitch(dots.pitch, shown) : 1;
196
+ const chunk = spaced * 4;
197
+ const reach = { width: viewport.width / shown, height: viewport.height / shown };
198
+ const area = dotArea({ x: scroll.x / shown, y: scroll.y / shown }, reach, content, chunk);
199
+ const follow = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
200
+ const { x, y } = event.nativeEvent.contentOffset;
201
+ offset.current = { x, y };
202
+ const next = dotArea({ x: x / shown, y: y / shown }, reach, content, chunk);
203
+ if (!sameArea(next, area)) setScroll({ x, y });
204
+ };
205
+
206
+ return (
207
+ <ScrollView
208
+ ref={scroller as never}
209
+ flex={1}
210
+ testID="graph-viewport"
211
+ scrollEnabled={!frozen}
212
+ showsVerticalScrollIndicator={false}
213
+ showsHorizontalScrollIndicator={false}
214
+ scrollEventThrottle={16}
215
+ onScroll={follow}
216
+ contentContainerStyle={{ alignSelf: "flex-start" }}
217
+ {...({ style: { overflowX: frozen ? "hidden" : "auto" } } as Record<string, unknown>)}
218
+ >
219
+ <View
220
+ minWidth={viewport.width}
221
+ minHeight={viewport.height}
222
+ alignItems="center"
223
+ justifyContent="center"
224
+ >
225
+ <View width={wide} height={tall} overflow="hidden" testID="graph-zoom">
226
+ <View
227
+ position="absolute"
228
+ left={0}
229
+ top={0}
230
+ width={content.width}
231
+ height={content.height}
232
+ {...({ style: { transform: [{ scale: shown }], transformOrigin: "0 0" } } as Record<
233
+ string,
234
+ unknown
235
+ >)}
236
+ >
237
+ {dots ? (
238
+ <GraphDots area={area} pitch={spaced} dot={dots.dot / shown} color={dots.color} />
239
+ ) : null}
240
+ {children}
241
+ </View>
242
+ </View>
243
+ </View>
244
+ </ScrollView>
245
+ );
246
+ }
@@ -0,0 +1,31 @@
1
+ export { GraphLayout, resolveGraphDirection } from "./GraphLayout";
2
+ export type { GraphLayoutProps, GraphSpaceToken } from "./GraphLayout";
3
+ export { GraphViewport } from "./GraphViewport";
4
+ export type { GraphViewportProps } from "./GraphViewport";
5
+ export { GraphDots } from "./GraphDots";
6
+ export type { GraphDotsProps } from "./GraphDots";
7
+ export { arrowHead, graphEdgeId, planGraphLayout, usableEdges } from "./plan";
8
+ export type {
9
+ GraphDirection,
10
+ GraphLayoutEdge,
11
+ GraphLayoutNode,
12
+ GraphPlan,
13
+ GraphPlanInput,
14
+ PlannedEdge,
15
+ PlannedNode,
16
+ } from "./plan";
17
+ export {
18
+ clampOffset,
19
+ clampView,
20
+ dotArea,
21
+ dotPath,
22
+ dotPhase,
23
+ dotPitch,
24
+ fittedView,
25
+ graphZoom,
26
+ keyedView,
27
+ leastScale,
28
+ toggledView,
29
+ zoomAt,
30
+ } from "./viewMath";
31
+ export type { DotArea, Extent, GraphDots as GraphDotsSpec, GraphView } from "./viewMath";
@@ -0,0 +1,120 @@
1
+ export const ogdenApps: readonly string[] = [
2
+ "affine",
3
+ "bit",
4
+ "blog",
5
+ "ciso",
6
+ "crm",
7
+ "crowdsec",
8
+ "docuseal",
9
+ "dtrack",
10
+ "ffx",
11
+ "fizzy",
12
+ "fleet",
13
+ "gitlab",
14
+ "gitlab-fizzy",
15
+ "glitchtip",
16
+ "hookshot",
17
+ "identity-broker",
18
+ "ivatar",
19
+ "librechat",
20
+ "lookout",
21
+ "mailbox-reconciler",
22
+ "mailbox-reconciler-ffx",
23
+ "mailcow",
24
+ "mariadb",
25
+ "matrix",
26
+ "mautic",
27
+ "mongodb",
28
+ "mpo-storybook",
29
+ "mysql",
30
+ "netbird",
31
+ "nextcloud",
32
+ "office",
33
+ "ollama",
34
+ "oncall",
35
+ "openreplay",
36
+ "openreplay-ch",
37
+ "pbx",
38
+ "pbx-db",
39
+ "pdf",
40
+ "peertube",
41
+ "penpot",
42
+ "photos",
43
+ "pokemon-bench",
44
+ "pokemon-kc",
45
+ "pokemon-web",
46
+ "postgres",
47
+ "postiz",
48
+ "pulp",
49
+ "redis",
50
+ "relay",
51
+ "ses-relay",
52
+ "sonarqube",
53
+ "supabase",
54
+ "superset",
55
+ "uptime",
56
+ "wazuh",
57
+ "wellknown",
58
+ "workflows",
59
+ ];
60
+ export const ogdenLinks: readonly (readonly [string, string])[] = [
61
+ ["affine", "postgres"],
62
+ ["bit", "mariadb"],
63
+ ["blog", "mysql"],
64
+ ["blog", "mailcow"],
65
+ ["ciso", "postgres"],
66
+ ["crm", "mysql"],
67
+ ["docuseal", "postgres"],
68
+ ["dtrack", "postgres"],
69
+ ["ffx", "mariadb"],
70
+ ["fizzy", "affine"],
71
+ ["fizzy", "nextcloud"],
72
+ ["fizzy", "mailcow"],
73
+ ["fleet", "mysql"],
74
+ ["gitlab", "postgres"],
75
+ ["gitlab", "mailcow"],
76
+ ["gitlab-fizzy", "fizzy"],
77
+ ["gitlab-fizzy", "gitlab"],
78
+ ["glitchtip", "postgres"],
79
+ ["hookshot", "matrix"],
80
+ ["identity-broker", "matrix"],
81
+ ["identity-broker", "postgres"],
82
+ ["ivatar", "postgres"],
83
+ ["librechat", "ollama"],
84
+ ["librechat", "mongodb"],
85
+ ["mailbox-reconciler", "mailcow"],
86
+ ["mailbox-reconciler-ffx", "mailcow"],
87
+ ["mailcow", "ses-relay"],
88
+ ["matrix", "postgres"],
89
+ ["mautic", "mailcow"],
90
+ ["mautic", "mysql"],
91
+ ["netbird", "postgres"],
92
+ ["nextcloud", "mailcow"],
93
+ ["nextcloud", "office"],
94
+ ["nextcloud", "postgres"],
95
+ ["nextcloud", "pdf"],
96
+ ["oncall", "postgres"],
97
+ ["openreplay", "openreplay-ch"],
98
+ ["openreplay", "postgres"],
99
+ ["pbx", "pbx-db"],
100
+ ["pbx", "mailcow"],
101
+ ["peertube", "postgres"],
102
+ ["peertube", "mailcow"],
103
+ ["penpot", "postgres"],
104
+ ["photos", "postgres"],
105
+ ["photos", "mailcow"],
106
+ ["pokemon-bench", "mariadb"],
107
+ ["pokemon-bench", "pokemon-kc"],
108
+ ["pokemon-kc", "postgres"],
109
+ ["pokemon-web", "pokemon-bench"],
110
+ ["pokemon-web", "pokemon-kc"],
111
+ ["postiz", "postgres"],
112
+ ["postiz", "mailcow"],
113
+ ["pulp", "postgres"],
114
+ ["relay", "postgres"],
115
+ ["sonarqube", "postgres"],
116
+ ["superset", "postgres"],
117
+ ["uptime", "matrix"],
118
+ ["workflows", "postgres"],
119
+ ["workflows", "redis"],
120
+ ];
@@ -0,0 +1,202 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { samplePath } from "../edges/edgeRoute";
3
+ import { ogdenApps, ogdenLinks } from "./ogden.fixture";
4
+ import { arrowHead, planGraphLayout, usableEdges } from "./plan";
5
+ import type {
6
+ GraphDirection,
7
+ GraphLayoutEdge,
8
+ GraphLayoutNode,
9
+ GraphPlan,
10
+ PlannedNode,
11
+ } from "./plan";
12
+
13
+ const spacing = { nodeSpacing: 16, rankSpacing: 32, margin: 16, strokeWidth: 1.5 };
14
+
15
+ function ogden(): { nodes: GraphLayoutNode[]; edges: GraphLayoutEdge[] } {
16
+ return {
17
+ nodes: ogdenApps.map((id) => ({ id, width: 90, height: 110 })),
18
+ edges: ogdenLinks.map(([source, target]) => ({ source, target })),
19
+ };
20
+ }
21
+
22
+ function randomDag(
23
+ count: number,
24
+ seed: number,
25
+ ): { nodes: GraphLayoutNode[]; edges: GraphLayoutEdge[] } {
26
+ let state = seed;
27
+ const next = () => {
28
+ state = (state * 1103515245 + 12345) % 2147483648;
29
+ return state / 2147483648;
30
+ };
31
+ const nodes = Array.from({ length: count }, (_, index) => ({
32
+ id: `n${index}`,
33
+ width: 60 + Math.floor(next() * 60),
34
+ height: 40 + Math.floor(next() * 40),
35
+ }));
36
+ const edges: GraphLayoutEdge[] = [];
37
+ for (let index = 1; index < count; index++) {
38
+ if (next() < 0.15) continue;
39
+ const links = 1 + Math.floor(next() * 2);
40
+ for (let link = 0; link < links; link++) {
41
+ edges.push({
42
+ source: `n${Math.floor(next() * index)}`,
43
+ target: `n${index}`,
44
+ emphasis: next() < 0.1,
45
+ });
46
+ }
47
+ }
48
+ return { nodes, edges };
49
+ }
50
+
51
+ function overlaps(a: PlannedNode, b: PlannedNode): boolean {
52
+ return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
53
+ }
54
+
55
+ function onEdge(point: { x: number; y: number }, node: PlannedNode): boolean {
56
+ const within = (value: number, from: number, to: number) =>
57
+ value >= from - 0.5 && value <= to + 0.5;
58
+ const onX = within(point.x, node.x, node.x + node.width);
59
+ const onY = within(point.y, node.y, node.y + node.height);
60
+ const nearSide =
61
+ Math.abs(point.x - node.x) <= 0.5 ||
62
+ Math.abs(point.x - node.x - node.width) <= 0.5 ||
63
+ Math.abs(point.y - node.y) <= 0.5 ||
64
+ Math.abs(point.y - node.y - node.height) <= 0.5;
65
+ return onX && onY && nearSide;
66
+ }
67
+
68
+ function strictlyInside(point: { x: number; y: number }, node: PlannedNode): boolean {
69
+ return (
70
+ point.x > node.x + 0.5 &&
71
+ point.x < node.x + node.width - 0.5 &&
72
+ point.y > node.y + 0.5 &&
73
+ point.y < node.y + node.height - 0.5
74
+ );
75
+ }
76
+
77
+ function audit(plan: GraphPlan, direction: GraphDirection) {
78
+ const byId = new Map(plan.nodes.map((node) => [node.id, node]));
79
+ let overlapCount = 0;
80
+ for (let a = 0; a < plan.nodes.length; a++) {
81
+ for (let b = a + 1; b < plan.nodes.length; b++)
82
+ if (overlaps(plan.nodes[a]!, plan.nodes[b]!)) overlapCount++;
83
+ }
84
+ let offEdge = 0;
85
+ let headsUnder = 0;
86
+ let inversions = 0;
87
+ for (const edge of plan.edges) {
88
+ const source = byId.get(edge.source)!;
89
+ const target = byId.get(edge.target)!;
90
+ const points = samplePath(edge.d);
91
+ if (
92
+ !onEdge(points[0]!, source) ||
93
+ !onEdge(points[points.length - 1]!, target) ||
94
+ !onEdge(edge.tip, target)
95
+ )
96
+ offEdge++;
97
+ const head = samplePath(edge.head);
98
+ if (plan.nodes.some((node) => head.some((point) => strictlyInside(point, node)))) headsUnder++;
99
+ if (
100
+ direction === "LR" ? source.x + source.width > target.x : source.y + source.height > target.y
101
+ )
102
+ inversions++;
103
+ }
104
+ return { overlapCount, offEdge, headsUnder, inversions };
105
+ }
106
+
107
+ describe("planGraphLayout (FZ-8149, LC-294)", () => {
108
+ for (const direction of ["LR", "TB"] as const) {
109
+ it(`lays Ogden corp's 57 apps and 59 links out ${direction} with no overlap, inversion or stray head`, () => {
110
+ const { nodes, edges } = ogden();
111
+ const plan = planGraphLayout({ nodes, edges, direction, ...spacing, wrapWidth: 390 });
112
+ expect(plan.nodes).toHaveLength(57);
113
+ expect(plan.edges).toHaveLength(usableEdges(nodes, edges).length);
114
+ expect(audit(plan, direction)).toEqual({
115
+ overlapCount: 0,
116
+ offEdge: 0,
117
+ headsUnder: 0,
118
+ inversions: 0,
119
+ });
120
+ });
121
+
122
+ it(`holds the same on a 120-node DAG ${direction}`, () => {
123
+ const { nodes, edges } = randomDag(120, 7);
124
+ const plan = planGraphLayout({ nodes, edges, direction, ...spacing });
125
+ expect(plan.edges.length).toBeGreaterThan(100);
126
+ expect(audit(plan, direction)).toEqual({
127
+ overlapCount: 0,
128
+ offEdge: 0,
129
+ headsUnder: 0,
130
+ inversions: 0,
131
+ });
132
+ });
133
+ }
134
+
135
+ it("packs isolated nodes in rows under the graph, wrapping at the wider of the graph and wrapWidth", () => {
136
+ const { nodes, edges } = ogden();
137
+ const plan = planGraphLayout({ nodes, edges, direction: "LR", ...spacing, wrapWidth: 390 });
138
+ const linked = new Set(usableEdges(nodes, edges).flatMap((edge) => [edge.source, edge.target]));
139
+ const graph = plan.nodes.filter((node) => linked.has(node.id));
140
+ const loose = plan.nodes.filter((node) => !linked.has(node.id));
141
+ expect(loose.length).toBeGreaterThan(0);
142
+ const graphBottom = Math.max(...graph.map((node) => node.y + node.height));
143
+ const graphRight = Math.max(...graph.map((node) => node.x + node.width));
144
+ for (const node of loose) {
145
+ expect(node.y).toBeGreaterThanOrEqual(graphBottom);
146
+ expect(node.x + node.width).toBeLessThanOrEqual(
147
+ Math.max(graphRight, 390 + spacing.margin) + 0.5,
148
+ );
149
+ }
150
+ });
151
+
152
+ it("drops self-loops, unknown ends and repeats, and keeps a host's own positions", () => {
153
+ const nodes = [
154
+ { id: "a", width: 40, height: 20 },
155
+ { id: "b", width: 40, height: 20 },
156
+ ];
157
+ const edges = [
158
+ { source: "a", target: "a" },
159
+ { source: "a", target: "zz" },
160
+ { source: "a", target: "b" },
161
+ { source: "a", target: "b" },
162
+ ];
163
+ const plan = planGraphLayout({
164
+ nodes,
165
+ edges,
166
+ direction: "LR",
167
+ ...spacing,
168
+ positions: { a: { x: 0, y: 0 }, b: { x: 200, y: 50 } },
169
+ });
170
+ expect(plan.edges.map((edge) => edge.id)).toEqual(["a\nb"]);
171
+ expect(plan.nodes.map(({ id, x, y }) => ({ id, x, y }))).toEqual([
172
+ { id: "a", x: 16, y: 16 },
173
+ { id: "b", x: 216, y: 66 },
174
+ ]);
175
+ expect(plan).toMatchObject({ width: 272, height: 102 });
176
+ });
177
+
178
+ it("paints emphasis in the theme accent and a kind in its token, never a literal", () => {
179
+ const nodes = [
180
+ { id: "a", width: 40, height: 20 },
181
+ { id: "b", width: 40, height: 20 },
182
+ { id: "c", width: 40, height: 20 },
183
+ ];
184
+ const plan = planGraphLayout({
185
+ nodes,
186
+ edges: [
187
+ { source: "a", target: "b", emphasis: true },
188
+ { source: "a", target: "c", kind: "error" },
189
+ ],
190
+ direction: "LR",
191
+ ...spacing,
192
+ });
193
+ expect(plan.edges.map((edge) => edge.stroke)).toEqual(["$accentColor", "$red9"]);
194
+ expect(plan.edges[1]!.dash).toBe("6 4");
195
+ for (const edge of plan.edges) expect(edge.stroke.startsWith("$")).toBe(true);
196
+ });
197
+
198
+ it("draws the head as a closed triangle whose tip is the end point, four strokes long", () => {
199
+ expect(arrowHead({ x: 100, y: 50 }, "left" as never, 2)).toBe("M 100 50 L 92 53 L 92 47 Z");
200
+ expect(arrowHead({ x: 100, y: 50 }, "top" as never, 2)).toBe("M 100 50 L 97 42 L 103 42 Z");
201
+ });
202
+ });