@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.
- package/dist/cjs/graphLayout/GraphDots.cjs +60 -0
- package/dist/cjs/graphLayout/GraphDots.native.js +66 -0
- package/dist/cjs/graphLayout/GraphDots.native.js.map +1 -0
- package/dist/cjs/graphLayout/GraphLayout.cjs +153 -0
- package/dist/cjs/graphLayout/GraphLayout.native.js +163 -0
- package/dist/cjs/graphLayout/GraphLayout.native.js.map +1 -0
- package/dist/cjs/graphLayout/GraphSvg.cjs +68 -0
- package/dist/cjs/graphLayout/GraphSvg.native.js +73 -0
- package/dist/cjs/graphLayout/GraphSvg.native.js.map +1 -0
- package/dist/cjs/graphLayout/GraphViewport.cjs +263 -0
- package/dist/cjs/graphLayout/GraphViewport.native.js +279 -0
- package/dist/cjs/graphLayout/GraphViewport.native.js.map +1 -0
- package/dist/cjs/graphLayout/index.cjs +51 -0
- package/dist/cjs/graphLayout/index.native.js +54 -0
- package/dist/cjs/graphLayout/index.native.js.map +1 -0
- package/dist/cjs/graphLayout/ogden.fixture.cjs +30 -0
- package/dist/cjs/graphLayout/ogden.fixture.native.js +33 -0
- package/dist/cjs/graphLayout/ogden.fixture.native.js.map +1 -0
- package/dist/cjs/graphLayout/plan.cjs +263 -0
- package/dist/cjs/graphLayout/plan.native.js +372 -0
- package/dist/cjs/graphLayout/plan.native.js.map +1 -0
- package/dist/cjs/graphLayout/viewMath.cjs +142 -0
- package/dist/cjs/graphLayout/viewMath.native.js +147 -0
- package/dist/cjs/graphLayout/viewMath.native.js.map +1 -0
- package/dist/esm/graphLayout/GraphDots.mjs +35 -0
- package/dist/esm/graphLayout/GraphDots.mjs.map +1 -0
- package/dist/esm/graphLayout/GraphDots.native.js +38 -0
- package/dist/esm/graphLayout/GraphDots.native.js.map +1 -0
- package/dist/esm/graphLayout/GraphLayout.mjs +127 -0
- package/dist/esm/graphLayout/GraphLayout.mjs.map +1 -0
- package/dist/esm/graphLayout/GraphLayout.native.js +134 -0
- package/dist/esm/graphLayout/GraphLayout.native.js.map +1 -0
- package/dist/esm/graphLayout/GraphSvg.mjs +42 -0
- package/dist/esm/graphLayout/GraphSvg.mjs.map +1 -0
- package/dist/esm/graphLayout/GraphSvg.native.js +44 -0
- package/dist/esm/graphLayout/GraphSvg.native.js.map +1 -0
- package/dist/esm/graphLayout/GraphViewport.mjs +238 -0
- package/dist/esm/graphLayout/GraphViewport.mjs.map +1 -0
- package/dist/esm/graphLayout/GraphViewport.native.js +251 -0
- package/dist/esm/graphLayout/GraphViewport.native.js.map +1 -0
- package/dist/esm/graphLayout/index.mjs +7 -0
- package/dist/esm/graphLayout/index.mjs.map +1 -0
- package/dist/esm/graphLayout/index.native.js +7 -0
- package/dist/esm/graphLayout/index.native.js.map +1 -0
- package/dist/esm/graphLayout/ogden.fixture.mjs +4 -0
- package/dist/esm/graphLayout/ogden.fixture.mjs.map +1 -0
- package/dist/esm/graphLayout/ogden.fixture.native.js +4 -0
- package/dist/esm/graphLayout/ogden.fixture.native.js.map +1 -0
- package/dist/esm/graphLayout/plan.mjs +235 -0
- package/dist/esm/graphLayout/plan.mjs.map +1 -0
- package/dist/esm/graphLayout/plan.native.js +341 -0
- package/dist/esm/graphLayout/plan.native.js.map +1 -0
- package/dist/esm/graphLayout/viewMath.mjs +105 -0
- package/dist/esm/graphLayout/viewMath.mjs.map +1 -0
- package/dist/esm/graphLayout/viewMath.native.js +107 -0
- package/dist/esm/graphLayout/viewMath.native.js.map +1 -0
- package/dist/jsx/graphLayout/GraphDots.mjs +35 -0
- package/dist/jsx/graphLayout/GraphDots.mjs.map +1 -0
- package/dist/jsx/graphLayout/GraphDots.native.js +66 -0
- package/dist/jsx/graphLayout/GraphDots.native.js.map +1 -0
- package/dist/jsx/graphLayout/GraphLayout.mjs +127 -0
- package/dist/jsx/graphLayout/GraphLayout.mjs.map +1 -0
- package/dist/jsx/graphLayout/GraphLayout.native.js +163 -0
- package/dist/jsx/graphLayout/GraphLayout.native.js.map +1 -0
- package/dist/jsx/graphLayout/GraphSvg.mjs +42 -0
- package/dist/jsx/graphLayout/GraphSvg.mjs.map +1 -0
- package/dist/jsx/graphLayout/GraphSvg.native.js +73 -0
- package/dist/jsx/graphLayout/GraphSvg.native.js.map +1 -0
- package/dist/jsx/graphLayout/GraphViewport.mjs +238 -0
- package/dist/jsx/graphLayout/GraphViewport.mjs.map +1 -0
- package/dist/jsx/graphLayout/GraphViewport.native.js +279 -0
- package/dist/jsx/graphLayout/GraphViewport.native.js.map +1 -0
- package/dist/jsx/graphLayout/index.mjs +7 -0
- package/dist/jsx/graphLayout/index.mjs.map +1 -0
- package/dist/jsx/graphLayout/index.native.js +54 -0
- package/dist/jsx/graphLayout/index.native.js.map +1 -0
- package/dist/jsx/graphLayout/ogden.fixture.mjs +4 -0
- package/dist/jsx/graphLayout/ogden.fixture.mjs.map +1 -0
- package/dist/jsx/graphLayout/ogden.fixture.native.js +33 -0
- package/dist/jsx/graphLayout/ogden.fixture.native.js.map +1 -0
- package/dist/jsx/graphLayout/plan.mjs +235 -0
- package/dist/jsx/graphLayout/plan.mjs.map +1 -0
- package/dist/jsx/graphLayout/plan.native.js +372 -0
- package/dist/jsx/graphLayout/plan.native.js.map +1 -0
- package/dist/jsx/graphLayout/viewMath.mjs +105 -0
- package/dist/jsx/graphLayout/viewMath.mjs.map +1 -0
- package/dist/jsx/graphLayout/viewMath.native.js +147 -0
- package/dist/jsx/graphLayout/viewMath.native.js.map +1 -0
- package/graph-layout.ts +1 -0
- package/package.json +21 -6
- package/src/graphLayout/GraphDots.tsx +32 -0
- package/src/graphLayout/GraphLayout.stories.tsx +86 -0
- package/src/graphLayout/GraphLayout.tsx +152 -0
- package/src/graphLayout/GraphSvg.native.tsx +31 -0
- package/src/graphLayout/GraphSvg.tsx +45 -0
- package/src/graphLayout/GraphViewport.native.tsx +231 -0
- package/src/graphLayout/GraphViewport.tsx +246 -0
- package/src/graphLayout/index.ts +31 -0
- package/src/graphLayout/ogden.fixture.ts +120 -0
- package/src/graphLayout/plan.spec.ts +202 -0
- package/src/graphLayout/plan.ts +223 -0
- package/src/graphLayout/purity.spec.ts +67 -0
- package/src/graphLayout/viewMath.spec.ts +185 -0
- package/src/graphLayout/viewMath.ts +151 -0
- package/types/graphLayout/GraphDots.d.ts +9 -0
- package/types/graphLayout/GraphDots.d.ts.map +1 -0
- package/types/graphLayout/GraphLayout.d.ts +22 -0
- package/types/graphLayout/GraphLayout.d.ts.map +1 -0
- package/types/graphLayout/GraphSvg.d.ts +17 -0
- package/types/graphLayout/GraphSvg.d.ts.map +1 -0
- package/types/graphLayout/GraphSvg.native.d.ts +5 -0
- package/types/graphLayout/GraphSvg.native.d.ts.map +1 -0
- package/types/graphLayout/GraphViewport.d.ts +11 -0
- package/types/graphLayout/GraphViewport.d.ts.map +1 -0
- package/types/graphLayout/GraphViewport.native.d.ts +4 -0
- package/types/graphLayout/GraphViewport.native.d.ts.map +1 -0
- package/types/graphLayout/index.d.ts +11 -0
- package/types/graphLayout/index.d.ts.map +1 -0
- package/types/graphLayout/ogden.fixture.d.ts +3 -0
- package/types/graphLayout/ogden.fixture.d.ts.map +1 -0
- package/types/graphLayout/plan.d.ts +52 -0
- package/types/graphLayout/plan.d.ts.map +1 -0
- package/types/graphLayout/viewMath.d.ts +47 -0
- package/types/graphLayout/viewMath.d.ts.map +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@multiplatform.one/node-graph",
|
|
3
|
-
"version": "9.0
|
|
3
|
+
"version": "9.1.0",
|
|
4
4
|
"description": "Node graph editor — React Flow on web, Tamagui knob-aware nodes, an Expo DOM host on native",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"multiplatform",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"demo.ts",
|
|
23
23
|
"dist",
|
|
24
24
|
"dom-inner.ts",
|
|
25
|
+
"graph-layout.ts",
|
|
25
26
|
"metro",
|
|
26
27
|
"model.ts",
|
|
27
28
|
"src",
|
|
@@ -53,6 +54,14 @@
|
|
|
53
54
|
"import": "./dist/esm/model/index.mjs",
|
|
54
55
|
"require": "./dist/cjs/model/index.cjs"
|
|
55
56
|
},
|
|
57
|
+
"./graph-layout": {
|
|
58
|
+
"source": "./src/graphLayout/index.ts",
|
|
59
|
+
"react-native": "./src/graphLayout/index.ts",
|
|
60
|
+
"types": "./types/graphLayout/index.d.ts",
|
|
61
|
+
"import": "./dist/esm/graphLayout/index.mjs",
|
|
62
|
+
"require": "./dist/cjs/graphLayout/index.cjs",
|
|
63
|
+
"default": "./dist/esm/graphLayout/index.mjs"
|
|
64
|
+
},
|
|
56
65
|
"./dom": {
|
|
57
66
|
"source": "./src/dom/NodeGraphDom.ts",
|
|
58
67
|
"react-native": "./src/dom/NodeGraphDom.ts",
|
|
@@ -82,16 +91,16 @@
|
|
|
82
91
|
},
|
|
83
92
|
"dependencies": {
|
|
84
93
|
"@dagrejs/dagre": "^3.1.1",
|
|
85
|
-
"@multiplatform.one/forms": "^9.0
|
|
86
|
-
"@multiplatform.one/theme": "^9.0
|
|
94
|
+
"@multiplatform.one/forms": "^9.1.0",
|
|
95
|
+
"@multiplatform.one/theme": "^9.1.0",
|
|
87
96
|
"@tamagui/web": "2.7.6",
|
|
88
97
|
"@xyflow/react": "^12.12.0",
|
|
89
98
|
"@xyflow/system": "0.0.83",
|
|
90
99
|
"tamagui": "2.7.6"
|
|
91
100
|
},
|
|
92
101
|
"devDependencies": {
|
|
93
|
-
"@multiplatform.one/config": "9.0
|
|
94
|
-
"@multiplatform.one/test-utils": "9.0
|
|
102
|
+
"@multiplatform.one/config": "9.1.0",
|
|
103
|
+
"@multiplatform.one/test-utils": "9.1.0",
|
|
95
104
|
"@tamagui/build": "2.7.6",
|
|
96
105
|
"@testing-library/react": "^16.3.3",
|
|
97
106
|
"@types/react-dom": "^19.2.3",
|
|
@@ -104,12 +113,15 @@
|
|
|
104
113
|
"vitest": "^4.1.12"
|
|
105
114
|
},
|
|
106
115
|
"peerDependencies": {
|
|
107
|
-
"@multiplatform.one/components": "^9.0
|
|
116
|
+
"@multiplatform.one/components": "^9.1.0",
|
|
108
117
|
"expo": ">=55",
|
|
109
118
|
"expo-haptics": "*",
|
|
110
119
|
"react": "^19.1.0",
|
|
111
120
|
"react-dom": "^19.1.0",
|
|
112
121
|
"react-native": ">=0.83.0",
|
|
122
|
+
"react-native-gesture-handler": ">=2.0.0",
|
|
123
|
+
"react-native-reanimated": "^4.2.0",
|
|
124
|
+
"react-native-svg": "^15.15.0",
|
|
113
125
|
"react-native-webview": ">=13.16.0"
|
|
114
126
|
},
|
|
115
127
|
"peerDependenciesMeta": {
|
|
@@ -125,6 +137,9 @@
|
|
|
125
137
|
"react-native": {
|
|
126
138
|
"optional": true
|
|
127
139
|
},
|
|
140
|
+
"react-native-gesture-handler": {
|
|
141
|
+
"optional": true
|
|
142
|
+
},
|
|
128
143
|
"react-native-webview": {
|
|
129
144
|
"optional": true
|
|
130
145
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { View } from "tamagui";
|
|
3
|
+
import { GraphPath, GraphSvg } from "./GraphSvg";
|
|
4
|
+
import { dotPath } from "./viewMath";
|
|
5
|
+
import type { DotArea } from "./viewMath";
|
|
6
|
+
|
|
7
|
+
export interface GraphDotsProps {
|
|
8
|
+
readonly area: DotArea;
|
|
9
|
+
readonly pitch: number;
|
|
10
|
+
readonly dot: number;
|
|
11
|
+
readonly color: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function GraphDots({ area, pitch, dot, color }: GraphDotsProps) {
|
|
15
|
+
const path = useMemo(() => dotPath(area, pitch), [area, pitch]);
|
|
16
|
+
return (
|
|
17
|
+
<View
|
|
18
|
+
position="absolute"
|
|
19
|
+
left={area.x}
|
|
20
|
+
top={area.y}
|
|
21
|
+
width={area.width}
|
|
22
|
+
height={area.height}
|
|
23
|
+
pointerEvents="none"
|
|
24
|
+
aria-hidden
|
|
25
|
+
testID="graph-dots"
|
|
26
|
+
>
|
|
27
|
+
<GraphSvg width={area.width} height={area.height}>
|
|
28
|
+
<GraphPath d={path} stroke={color} strokeWidth={dot} round />
|
|
29
|
+
</GraphSvg>
|
|
30
|
+
</View>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Card, SizableText } from "@multiplatform.one/components";
|
|
2
|
+
import type { Meta, StoryObj } from "@storybook/react-native-web-vite";
|
|
3
|
+
import { YStack } from "tamagui";
|
|
4
|
+
import { GraphLayout } from "./GraphLayout";
|
|
5
|
+
import type { GraphLayoutEdge, GraphLayoutNode } from "./plan";
|
|
6
|
+
import { ogdenApps, ogdenLinks } from "./ogden.fixture";
|
|
7
|
+
|
|
8
|
+
const apps = [...ogdenApps, "sandbox-a", "sandbox-b", "sandbox-c"];
|
|
9
|
+
const nodes: GraphLayoutNode[] = apps.map((id) => ({ id, width: 96, height: 72 }));
|
|
10
|
+
const edges: GraphLayoutEdge[] = ogdenLinks.map(([source, target], index) => ({
|
|
11
|
+
source,
|
|
12
|
+
target,
|
|
13
|
+
emphasis: index % 13 === 0,
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
function Tile({ id }: { id: string }) {
|
|
17
|
+
return (
|
|
18
|
+
<Card flex={1} alignItems="center" justifyContent="center" testID={`graph-tile-${id}`}>
|
|
19
|
+
<SizableText size="$2" numberOfLines={2} textAlign="center">
|
|
20
|
+
{id}
|
|
21
|
+
</SizableText>
|
|
22
|
+
</Card>
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const renderNode = (id: string) => <Tile id={id} />;
|
|
27
|
+
|
|
28
|
+
const meta: Meta<typeof GraphLayout> = {
|
|
29
|
+
title: "NodeGraph/GraphLayout",
|
|
30
|
+
component: GraphLayout,
|
|
31
|
+
parameters: {
|
|
32
|
+
status: { type: "beta" },
|
|
33
|
+
layout: "fullscreen",
|
|
34
|
+
nativeGallery: { scroll: false },
|
|
35
|
+
docs: {
|
|
36
|
+
description: {
|
|
37
|
+
component:
|
|
38
|
+
"A read-only graph of host views (LC-294). Nodes are whatever the host renders at the size it gives; GraphLayout places them with NodeGraph's dagre engine, draws every edge through NodeGraph's own edge path and router in theme tokens, and pans and zooms the canvas over a $color6 dot grid. Edges end on box edges and never run under a node. Emphasis is the theme accent. An editable graph is NodeGraph.",
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
export default meta;
|
|
44
|
+
|
|
45
|
+
type Story = StoryObj<typeof GraphLayout>;
|
|
46
|
+
|
|
47
|
+
function Frame(props: Parameters<typeof GraphLayout>[0]) {
|
|
48
|
+
return (
|
|
49
|
+
<YStack height="100vh" width="100%">
|
|
50
|
+
<GraphLayout {...props} />
|
|
51
|
+
</YStack>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const Apps: Story = {
|
|
56
|
+
render: () => <Frame nodes={nodes} edges={edges} renderNode={renderNode} />,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const LeftToRight: Story = {
|
|
60
|
+
render: () => <Frame nodes={nodes} edges={edges} renderNode={renderNode} direction="LR" />,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const TopToBottom: Story = {
|
|
64
|
+
render: () => <Frame nodes={nodes} edges={edges} renderNode={renderNode} direction="TB" />,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const EdgeKinds: Story = {
|
|
68
|
+
render: () => (
|
|
69
|
+
<Frame
|
|
70
|
+
nodes={["source", "flow", "data", "error", "conditional"].map((id) => ({
|
|
71
|
+
id,
|
|
72
|
+
width: 96,
|
|
73
|
+
height: 72,
|
|
74
|
+
}))}
|
|
75
|
+
edges={[
|
|
76
|
+
{ source: "source", target: "flow", kind: "flow" },
|
|
77
|
+
{ source: "source", target: "data", kind: "data" },
|
|
78
|
+
{ source: "source", target: "error", kind: "error" },
|
|
79
|
+
{ source: "source", target: "conditional", kind: "conditional" },
|
|
80
|
+
]}
|
|
81
|
+
renderNode={renderNode}
|
|
82
|
+
direction="LR"
|
|
83
|
+
dots={false}
|
|
84
|
+
/>
|
|
85
|
+
),
|
|
86
|
+
};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { defaultKnobs, usePresetContext } from "@multiplatform.one/theme";
|
|
2
|
+
import { Fragment, useMemo, useState } from "react";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import type { LayoutChangeEvent } from "react-native";
|
|
5
|
+
import { getTokenValue, useTheme, View } from "tamagui";
|
|
6
|
+
import { edgeStrokeWidth, themeTokenName } from "../edges/edgeStyle";
|
|
7
|
+
import type { XY } from "../model/document";
|
|
8
|
+
import { GraphPath, GraphSvg } from "./GraphSvg";
|
|
9
|
+
import { GraphViewport } from "./GraphViewport";
|
|
10
|
+
import { planGraphLayout } from "./plan";
|
|
11
|
+
import type { GraphDirection, GraphLayoutEdge, GraphLayoutNode } from "./plan";
|
|
12
|
+
import type { Extent } from "./viewMath";
|
|
13
|
+
|
|
14
|
+
export type GraphSpaceToken = `$${number}`;
|
|
15
|
+
|
|
16
|
+
export interface GraphLayoutProps {
|
|
17
|
+
readonly nodes: readonly GraphLayoutNode[];
|
|
18
|
+
readonly edges: readonly GraphLayoutEdge[];
|
|
19
|
+
readonly renderNode: (id: string) => ReactNode;
|
|
20
|
+
readonly direction?: GraphDirection | "auto";
|
|
21
|
+
readonly positions?: Readonly<Record<string, XY>>;
|
|
22
|
+
readonly spacing?: { readonly node?: GraphSpaceToken; readonly rank?: GraphSpaceToken };
|
|
23
|
+
readonly dots?: boolean;
|
|
24
|
+
readonly frozen?: boolean;
|
|
25
|
+
readonly testID?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function resolveGraphDirection(
|
|
29
|
+
direction: GraphDirection | "auto",
|
|
30
|
+
viewport: Extent,
|
|
31
|
+
): GraphDirection {
|
|
32
|
+
if (direction !== "auto") return direction;
|
|
33
|
+
return viewport.height > viewport.width ? "LR" : "TB";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function space(token: GraphSpaceToken): number {
|
|
37
|
+
const value = getTokenValue(token as Parameters<typeof getTokenValue>[0], "space");
|
|
38
|
+
return typeof value === "number" ? value : 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function useStrokeWidth(): number {
|
|
42
|
+
const preset = usePresetContext();
|
|
43
|
+
const knobs = preset ? { ...preset.preset.knobs, ...preset.overrides } : defaultKnobs;
|
|
44
|
+
return edgeStrokeWidth(knobs.borderWidth);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function useColour(): (token: string) => string {
|
|
48
|
+
const theme = useTheme() as unknown as Record<string, { get?: () => unknown } | undefined>;
|
|
49
|
+
return (token) => {
|
|
50
|
+
const value = theme[themeTokenName(token)]?.get?.();
|
|
51
|
+
return typeof value === "string" ? value : "currentColor";
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function GraphLayout({
|
|
56
|
+
nodes,
|
|
57
|
+
edges,
|
|
58
|
+
renderNode,
|
|
59
|
+
direction = "auto",
|
|
60
|
+
positions,
|
|
61
|
+
spacing,
|
|
62
|
+
dots = true,
|
|
63
|
+
frozen,
|
|
64
|
+
testID = "graph-layout",
|
|
65
|
+
}: GraphLayoutProps) {
|
|
66
|
+
const [viewport, setViewport] = useState<Extent>({ width: 0, height: 0 });
|
|
67
|
+
const strokeWidth = useStrokeWidth();
|
|
68
|
+
const colour = useColour();
|
|
69
|
+
const resolved = resolveGraphDirection(direction, viewport);
|
|
70
|
+
const nodeSpacing = space(spacing?.node ?? "$4");
|
|
71
|
+
const rankSpacing = space(spacing?.rank ?? "$8");
|
|
72
|
+
const margin = space("$4");
|
|
73
|
+
const pitch = space("$6");
|
|
74
|
+
const plan = useMemo(
|
|
75
|
+
() =>
|
|
76
|
+
planGraphLayout({
|
|
77
|
+
nodes,
|
|
78
|
+
edges,
|
|
79
|
+
direction: resolved,
|
|
80
|
+
nodeSpacing,
|
|
81
|
+
rankSpacing,
|
|
82
|
+
margin,
|
|
83
|
+
strokeWidth,
|
|
84
|
+
wrapWidth: viewport.width > 0 ? viewport.width - margin * 2 : undefined,
|
|
85
|
+
positions,
|
|
86
|
+
}),
|
|
87
|
+
[
|
|
88
|
+
nodes,
|
|
89
|
+
edges,
|
|
90
|
+
resolved,
|
|
91
|
+
nodeSpacing,
|
|
92
|
+
rankSpacing,
|
|
93
|
+
margin,
|
|
94
|
+
strokeWidth,
|
|
95
|
+
viewport.width,
|
|
96
|
+
positions,
|
|
97
|
+
],
|
|
98
|
+
);
|
|
99
|
+
const onLayout = (event: LayoutChangeEvent) => {
|
|
100
|
+
const { width, height } = event.nativeEvent.layout;
|
|
101
|
+
if (width !== viewport.width || height !== viewport.height) setViewport({ width, height });
|
|
102
|
+
};
|
|
103
|
+
const content = {
|
|
104
|
+
width: Math.max(plan.width, viewport.width),
|
|
105
|
+
height: Math.max(plan.height, viewport.height),
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<View flex={1} onLayout={onLayout} testID={testID} data-graph-direction={resolved}>
|
|
110
|
+
{viewport.width > 0 && viewport.height > 0 ? (
|
|
111
|
+
<GraphViewport
|
|
112
|
+
content={content}
|
|
113
|
+
viewport={viewport}
|
|
114
|
+
dots={dots ? { pitch, dot: strokeWidth, color: colour("$color6") } : undefined}
|
|
115
|
+
frozen={frozen}
|
|
116
|
+
>
|
|
117
|
+
<View width={content.width} height={content.height}>
|
|
118
|
+
<GraphSvg width={plan.width} height={plan.height}>
|
|
119
|
+
{plan.edges.map((edge) => {
|
|
120
|
+
const paint = colour(edge.stroke);
|
|
121
|
+
return (
|
|
122
|
+
<Fragment key={edge.id}>
|
|
123
|
+
<GraphPath
|
|
124
|
+
d={edge.d}
|
|
125
|
+
stroke={paint}
|
|
126
|
+
strokeWidth={edge.emphasis ? strokeWidth * 2 : strokeWidth}
|
|
127
|
+
dash={edge.dash}
|
|
128
|
+
/>
|
|
129
|
+
<GraphPath d={edge.head} fill={paint} />
|
|
130
|
+
</Fragment>
|
|
131
|
+
);
|
|
132
|
+
})}
|
|
133
|
+
</GraphSvg>
|
|
134
|
+
{plan.nodes.map((node) => (
|
|
135
|
+
<View
|
|
136
|
+
key={node.id}
|
|
137
|
+
position="absolute"
|
|
138
|
+
left={node.x}
|
|
139
|
+
top={node.y}
|
|
140
|
+
width={node.width}
|
|
141
|
+
height={node.height}
|
|
142
|
+
testID={`graph-node-${node.id}`}
|
|
143
|
+
>
|
|
144
|
+
{renderNode(node.id)}
|
|
145
|
+
</View>
|
|
146
|
+
))}
|
|
147
|
+
</View>
|
|
148
|
+
</GraphViewport>
|
|
149
|
+
) : null}
|
|
150
|
+
</View>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Path, Svg } from "react-native-svg";
|
|
2
|
+
import type { GraphPathProps, GraphSvgProps } from "./GraphSvg";
|
|
3
|
+
|
|
4
|
+
export type { GraphPathProps, GraphSvgProps } from "./GraphSvg";
|
|
5
|
+
|
|
6
|
+
export function GraphSvg({ width, height, children }: GraphSvgProps) {
|
|
7
|
+
return (
|
|
8
|
+
<Svg
|
|
9
|
+
width={width}
|
|
10
|
+
height={height}
|
|
11
|
+
pointerEvents="none"
|
|
12
|
+
style={{ position: "absolute", left: 0, top: 0, overflow: "visible" }}
|
|
13
|
+
>
|
|
14
|
+
{children}
|
|
15
|
+
</Svg>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function GraphPath({ d, stroke, strokeWidth, dash, fill, round }: GraphPathProps) {
|
|
20
|
+
return (
|
|
21
|
+
<Path
|
|
22
|
+
d={d}
|
|
23
|
+
fill={fill ?? "none"}
|
|
24
|
+
stroke={stroke ?? "none"}
|
|
25
|
+
strokeWidth={strokeWidth}
|
|
26
|
+
strokeDasharray={dash}
|
|
27
|
+
strokeLinecap={round ? "round" : "butt"}
|
|
28
|
+
strokeLinejoin="round"
|
|
29
|
+
/>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
export interface GraphSvgProps {
|
|
4
|
+
readonly width: number;
|
|
5
|
+
readonly height: number;
|
|
6
|
+
readonly children: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface GraphPathProps {
|
|
10
|
+
readonly d: string;
|
|
11
|
+
readonly stroke?: string;
|
|
12
|
+
readonly strokeWidth?: number;
|
|
13
|
+
readonly dash?: string;
|
|
14
|
+
readonly fill?: string;
|
|
15
|
+
readonly round?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function GraphSvg({ width, height, children }: GraphSvgProps) {
|
|
19
|
+
return (
|
|
20
|
+
<svg
|
|
21
|
+
width={width}
|
|
22
|
+
height={height}
|
|
23
|
+
aria-hidden
|
|
24
|
+
style={{ position: "absolute", left: 0, top: 0, overflow: "visible", pointerEvents: "none" }}
|
|
25
|
+
>
|
|
26
|
+
{children}
|
|
27
|
+
</svg>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function GraphPath({ d, stroke, strokeWidth, dash, fill, round }: GraphPathProps) {
|
|
32
|
+
return (
|
|
33
|
+
<path
|
|
34
|
+
d={d}
|
|
35
|
+
style={{
|
|
36
|
+
fill: fill ?? "none",
|
|
37
|
+
stroke: stroke ?? "none",
|
|
38
|
+
strokeWidth,
|
|
39
|
+
strokeDasharray: dash,
|
|
40
|
+
strokeLinecap: round ? "round" : "butt",
|
|
41
|
+
strokeLinejoin: "round",
|
|
42
|
+
}}
|
|
43
|
+
/>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { StyleSheet } from "react-native";
|
|
3
|
+
import { Gesture, GestureDetector } from "react-native-gesture-handler";
|
|
4
|
+
import Animated, {
|
|
5
|
+
cancelAnimation,
|
|
6
|
+
runOnJS,
|
|
7
|
+
useAnimatedReaction,
|
|
8
|
+
useAnimatedStyle,
|
|
9
|
+
useSharedValue,
|
|
10
|
+
withDecay,
|
|
11
|
+
withTiming,
|
|
12
|
+
} from "react-native-reanimated";
|
|
13
|
+
import type { SharedValue } from "react-native-reanimated";
|
|
14
|
+
import { View } from "tamagui";
|
|
15
|
+
import { GraphDots } from "./GraphDots";
|
|
16
|
+
import type { GraphViewportProps } from "./GraphViewport";
|
|
17
|
+
import {
|
|
18
|
+
clampOffset,
|
|
19
|
+
clampView,
|
|
20
|
+
dotPhase,
|
|
21
|
+
dotPitch,
|
|
22
|
+
graphZoom,
|
|
23
|
+
toggledView,
|
|
24
|
+
zoomAt,
|
|
25
|
+
} from "./viewMath";
|
|
26
|
+
import type { Extent, GraphDots as GraphDotsSpec } from "./viewMath";
|
|
27
|
+
|
|
28
|
+
export type { GraphViewportProps } from "./GraphViewport";
|
|
29
|
+
|
|
30
|
+
export function GraphViewport({ content, viewport, dots, frozen, children }: GraphViewportProps) {
|
|
31
|
+
const start = clampView({ scale: 1, x: 0, y: 0 }, content, viewport);
|
|
32
|
+
const scale = useSharedValue(start.scale);
|
|
33
|
+
const x = useSharedValue(start.x);
|
|
34
|
+
const y = useSharedValue(start.y);
|
|
35
|
+
const pinching = useSharedValue(0);
|
|
36
|
+
const focalX = useSharedValue(0);
|
|
37
|
+
const focalY = useSharedValue(0);
|
|
38
|
+
const held = useSharedValue(frozen ? 1 : 0);
|
|
39
|
+
const { width: cw, height: ch } = content;
|
|
40
|
+
const { width: vw, height: vh } = viewport;
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
const next = clampView(
|
|
44
|
+
{ scale: scale.value, x: x.value, y: y.value },
|
|
45
|
+
{ width: cw, height: ch },
|
|
46
|
+
{ width: vw, height: vh },
|
|
47
|
+
);
|
|
48
|
+
scale.value = next.scale;
|
|
49
|
+
x.value = next.x;
|
|
50
|
+
y.value = next.y;
|
|
51
|
+
}, [cw, ch, vw, vh, scale, x, y]);
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
held.value = frozen ? 1 : 0;
|
|
55
|
+
}, [frozen, held]);
|
|
56
|
+
|
|
57
|
+
const gesture = useMemo(() => {
|
|
58
|
+
const size = { width: cw, height: ch };
|
|
59
|
+
const view = { width: vw, height: vh };
|
|
60
|
+
const pan = Gesture.Pan()
|
|
61
|
+
.maxPointers(1)
|
|
62
|
+
.onStart(() => {
|
|
63
|
+
"worklet";
|
|
64
|
+
if (held.value) return;
|
|
65
|
+
cancelAnimation(x);
|
|
66
|
+
cancelAnimation(y);
|
|
67
|
+
})
|
|
68
|
+
.onChange((event) => {
|
|
69
|
+
"worklet";
|
|
70
|
+
if (pinching.value || held.value) return;
|
|
71
|
+
x.value = clampOffset(x.value + event.changeX, cw * scale.value, vw);
|
|
72
|
+
y.value = clampOffset(y.value + event.changeY, ch * scale.value, vh);
|
|
73
|
+
})
|
|
74
|
+
.onEnd((event) => {
|
|
75
|
+
"worklet";
|
|
76
|
+
if (pinching.value || held.value) return;
|
|
77
|
+
const wide = cw * scale.value;
|
|
78
|
+
const tall = ch * scale.value;
|
|
79
|
+
x.value = withDecay({
|
|
80
|
+
velocity: event.velocityX,
|
|
81
|
+
clamp: [clampOffset(-Infinity, wide, vw), clampOffset(Infinity, wide, vw)],
|
|
82
|
+
});
|
|
83
|
+
y.value = withDecay({
|
|
84
|
+
velocity: event.velocityY,
|
|
85
|
+
clamp: [clampOffset(-Infinity, tall, vh), clampOffset(Infinity, tall, vh)],
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
const pinch = Gesture.Pinch()
|
|
89
|
+
.onStart((event) => {
|
|
90
|
+
"worklet";
|
|
91
|
+
pinching.value = 1;
|
|
92
|
+
if (held.value) return;
|
|
93
|
+
cancelAnimation(x);
|
|
94
|
+
cancelAnimation(y);
|
|
95
|
+
cancelAnimation(scale);
|
|
96
|
+
focalX.value = event.focalX;
|
|
97
|
+
focalY.value = event.focalY;
|
|
98
|
+
})
|
|
99
|
+
.onChange((event) => {
|
|
100
|
+
"worklet";
|
|
101
|
+
if (held.value) return;
|
|
102
|
+
const next = zoomAt(
|
|
103
|
+
{
|
|
104
|
+
scale: scale.value,
|
|
105
|
+
x: x.value + event.focalX - focalX.value,
|
|
106
|
+
y: y.value + event.focalY - focalY.value,
|
|
107
|
+
},
|
|
108
|
+
scale.value * event.scaleChange,
|
|
109
|
+
{ x: event.focalX, y: event.focalY },
|
|
110
|
+
size,
|
|
111
|
+
view,
|
|
112
|
+
);
|
|
113
|
+
focalX.value = event.focalX;
|
|
114
|
+
focalY.value = event.focalY;
|
|
115
|
+
scale.value = next.scale;
|
|
116
|
+
x.value = next.x;
|
|
117
|
+
y.value = next.y;
|
|
118
|
+
})
|
|
119
|
+
.onFinalize(() => {
|
|
120
|
+
"worklet";
|
|
121
|
+
pinching.value = 0;
|
|
122
|
+
});
|
|
123
|
+
const reset = Gesture.Tap()
|
|
124
|
+
.numberOfTaps(2)
|
|
125
|
+
.onEnd((event, success) => {
|
|
126
|
+
"worklet";
|
|
127
|
+
if (!success || held.value) return;
|
|
128
|
+
const next = toggledView(
|
|
129
|
+
{ scale: scale.value, x: x.value, y: y.value },
|
|
130
|
+
{ x: event.x, y: event.y },
|
|
131
|
+
size,
|
|
132
|
+
view,
|
|
133
|
+
);
|
|
134
|
+
scale.value = withTiming(next.scale, { duration: graphZoom.settleMs });
|
|
135
|
+
x.value = withTiming(next.x, { duration: graphZoom.settleMs });
|
|
136
|
+
y.value = withTiming(next.y, { duration: graphZoom.settleMs });
|
|
137
|
+
});
|
|
138
|
+
return Gesture.Race(reset, Gesture.Simultaneous(pinch, pan));
|
|
139
|
+
}, [cw, ch, vw, vh, scale, x, y, pinching, focalX, focalY, held]);
|
|
140
|
+
|
|
141
|
+
const placed = useAnimatedStyle(() => ({
|
|
142
|
+
transform: [
|
|
143
|
+
{ translateX: x.value - ((1 - scale.value) * cw) / 2 },
|
|
144
|
+
{ translateY: y.value - ((1 - scale.value) * ch) / 2 },
|
|
145
|
+
{ scale: scale.value },
|
|
146
|
+
],
|
|
147
|
+
}));
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<GestureDetector gesture={gesture}>
|
|
151
|
+
<Animated.View style={styles.viewport} collapsable={false} testID="graph-viewport">
|
|
152
|
+
{dots ? <DotLayer viewport={viewport} dots={dots} scale={scale} x={x} y={y} /> : null}
|
|
153
|
+
<Animated.View style={[styles.content, placed]}>
|
|
154
|
+
<View width={cw} height={ch}>
|
|
155
|
+
{children}
|
|
156
|
+
</View>
|
|
157
|
+
</Animated.View>
|
|
158
|
+
</Animated.View>
|
|
159
|
+
</GestureDetector>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function DotLayer({
|
|
164
|
+
viewport,
|
|
165
|
+
dots,
|
|
166
|
+
scale,
|
|
167
|
+
x,
|
|
168
|
+
y,
|
|
169
|
+
}: {
|
|
170
|
+
viewport: Extent;
|
|
171
|
+
dots: GraphDotsSpec;
|
|
172
|
+
scale: SharedValue<number>;
|
|
173
|
+
x: SharedValue<number>;
|
|
174
|
+
y: SharedValue<number>;
|
|
175
|
+
}) {
|
|
176
|
+
const [drawn, setDrawn] = useState(() => scale.value);
|
|
177
|
+
const spaced = dotPitch(dots.pitch, drawn);
|
|
178
|
+
const step = spaced * drawn;
|
|
179
|
+
const width = viewport.width / 0.85 + step * 2;
|
|
180
|
+
const height = viewport.height / 0.85 + step * 2;
|
|
181
|
+
const pitch = dots.pitch;
|
|
182
|
+
useAnimatedReaction(
|
|
183
|
+
() => scale.value,
|
|
184
|
+
(now) => {
|
|
185
|
+
if (Math.abs(now / drawn - 1) > 0.1 || dotPitch(pitch, now) !== spaced)
|
|
186
|
+
runOnJS(setDrawn)(now);
|
|
187
|
+
},
|
|
188
|
+
[drawn, spaced, pitch],
|
|
189
|
+
);
|
|
190
|
+
const placed = useAnimatedStyle(() => {
|
|
191
|
+
const ratio = scale.value / drawn;
|
|
192
|
+
const shift = (axis: number, offset: number) =>
|
|
193
|
+
dotPhase(offset, scale.value, pitch, spaced) - (ratio * step) / 2 - (axis / 2) * (1 - ratio);
|
|
194
|
+
return {
|
|
195
|
+
transform: [
|
|
196
|
+
{ translateX: shift(width, x.value) },
|
|
197
|
+
{ translateY: shift(height, y.value) },
|
|
198
|
+
{ scale: ratio },
|
|
199
|
+
],
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
return (
|
|
203
|
+
<Animated.View pointerEvents="none" style={[styles.dots, placed]}>
|
|
204
|
+
<View width={width} height={height}>
|
|
205
|
+
<GraphDots
|
|
206
|
+
area={{ x: 0, y: 0, width, height }}
|
|
207
|
+
pitch={step}
|
|
208
|
+
dot={dots.dot}
|
|
209
|
+
color={dots.color}
|
|
210
|
+
/>
|
|
211
|
+
</View>
|
|
212
|
+
</Animated.View>
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const styles = StyleSheet.create({
|
|
217
|
+
viewport: {
|
|
218
|
+
flex: 1,
|
|
219
|
+
overflow: "hidden",
|
|
220
|
+
},
|
|
221
|
+
content: {
|
|
222
|
+
position: "absolute",
|
|
223
|
+
left: 0,
|
|
224
|
+
top: 0,
|
|
225
|
+
},
|
|
226
|
+
dots: {
|
|
227
|
+
position: "absolute",
|
|
228
|
+
left: 0,
|
|
229
|
+
top: 0,
|
|
230
|
+
},
|
|
231
|
+
});
|