@dxos/react-ui-graph 0.8.4-main.422d1c7879 → 0.8.4-main.43cb759274
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/LICENSE +102 -5
- package/dist/lib/browser/{chunk-LKC455GK.mjs → chunk-EASNZZ7B.mjs} +1 -1
- package/dist/lib/browser/{chunk-LKC455GK.mjs.map → chunk-EASNZZ7B.mjs.map} +2 -2
- package/dist/lib/browser/index.mjs +1783 -496
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +1 -1
- package/dist/lib/browser/testing/index.mjs.map +2 -2
- package/dist/lib/node-esm/{chunk-JE4KZCUF.mjs → chunk-JSE2X4ML.mjs} +1 -1
- package/dist/lib/node-esm/{chunk-JE4KZCUF.mjs.map → chunk-JSE2X4ML.mjs.map} +2 -2
- package/dist/lib/node-esm/index.mjs +1783 -496
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +1 -1
- package/dist/lib/node-esm/testing/index.mjs.map +2 -2
- package/dist/types/src/components/Graph/Graph.d.ts +20 -2
- package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
- package/dist/types/src/components/Graph/Graph.stories.d.ts +3 -3
- package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
- package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -1
- package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
- package/dist/types/src/components/SVG/FPS.d.ts +20 -0
- package/dist/types/src/components/SVG/FPS.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Markers.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Root.d.ts +5 -1
- package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
- package/dist/types/src/components/SVG/SVG.d.ts +3 -1
- package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
- package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
- package/dist/types/src/fx/pulsar.d.ts.map +1 -1
- package/dist/types/src/graph/markers.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts +47 -0
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts +65 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts +23 -0
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.d.ts +67 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.test.d.ts +2 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.test.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts +8 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +6 -0
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-swarm-projector.d.ts +72 -0
- package/dist/types/src/graph/projector/graph-swarm-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/index.d.ts +5 -0
- package/dist/types/src/graph/projector/index.d.ts.map +1 -1
- package/dist/types/src/graph/projector/projector.d.ts +10 -1
- package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts +41 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/index.d.ts +1 -0
- package/dist/types/src/graph/renderer/index.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/labels.d.ts +33 -0
- package/dist/types/src/graph/renderer/labels.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/renderer.d.ts +12 -1
- package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
- package/dist/types/src/graph/types.d.ts +14 -0
- package/dist/types/src/graph/types.d.ts.map +1 -1
- package/dist/types/src/hooks/useGrid.d.ts.map +1 -1
- package/dist/types/src/hooks/useStateRef.d.ts.map +1 -1
- package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -1
- package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
- package/dist/types/src/stories/experimental.stories.d.ts +7 -0
- package/dist/types/src/stories/experimental.stories.d.ts.map +1 -0
- package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
- package/dist/types/src/testing/data.d.ts +1 -1
- package/dist/types/src/testing/data.d.ts.map +1 -1
- package/dist/types/src/testing/hooks.d.ts.map +1 -1
- package/dist/types/src/testing/model.d.ts.map +1 -1
- package/dist/types/src/util/events.d.ts.map +1 -1
- package/dist/types/src/util/fraction.d.ts.map +1 -1
- package/dist/types/src/util/points.d.ts.map +1 -1
- package/dist/types/src/util/scale.d.ts.map +1 -1
- package/dist/types/src/util/screen.d.ts.map +1 -1
- package/dist/types/src/util/vector.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -19
- package/src/components/Graph/Graph.stories.tsx +5 -5
- package/src/components/Graph/Graph.tsx +116 -21
- package/src/components/Mesh/Mesh.stories.tsx +6 -4
- package/src/components/Mesh/Mesh.tsx +43 -35
- package/src/components/SVG/FPS.tsx +91 -0
- package/src/components/SVG/Root.tsx +30 -4
- package/src/components/SVG/SVG.tsx +4 -1
- package/src/graph/projector/graph-bundle-projector.ts +210 -0
- package/src/graph/projector/graph-cluster-projector.ts +337 -0
- package/src/graph/projector/graph-force-projector.ts +6 -1
- package/src/graph/projector/graph-hierarchical-projector.ts +1 -0
- package/src/graph/projector/graph-lattice-projector.ts +86 -0
- package/src/graph/projector/graph-plexus-projector.test.ts +102 -0
- package/src/graph/projector/graph-plexus-projector.ts +309 -0
- package/src/graph/projector/graph-projector.ts +22 -1
- package/src/graph/projector/graph-radial-projector.ts +30 -2
- package/src/graph/projector/graph-relational-projector.ts +1 -0
- package/src/graph/projector/graph-swarm-projector.ts +373 -0
- package/src/graph/projector/index.ts +5 -0
- package/src/graph/projector/projector.ts +12 -3
- package/src/graph/renderer/graph-renderer.ts +289 -30
- package/src/graph/renderer/index.ts +1 -0
- package/src/graph/renderer/labels.ts +138 -0
- package/src/graph/renderer/renderer.ts +15 -4
- package/src/graph/types.ts +14 -0
- package/src/stories/experimental.stories.tsx +446 -0
- package/src/stories/hooks.stories.tsx +6 -1
- package/styles/graph.css +51 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-graph",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.43cb759274",
|
|
4
4
|
"description": "GEM Graph.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "https://github.com/dxos/dxos"
|
|
10
10
|
},
|
|
11
|
-
"license": "
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
12
12
|
"author": "DXOS.org",
|
|
13
13
|
"sideEffects": [
|
|
14
14
|
"*.css"
|
|
@@ -44,13 +44,13 @@
|
|
|
44
44
|
"lodash.defaultsdeep": "^4.6.1",
|
|
45
45
|
"react-resize-detector": "^11.0.1",
|
|
46
46
|
"topojson-client": "^3.1.0",
|
|
47
|
-
"@dxos/
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/invariant": "0.8.4-main.
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/node-std": "0.8.4-main.
|
|
52
|
-
"@dxos/
|
|
53
|
-
"@dxos/
|
|
47
|
+
"@dxos/graph": "0.8.4-main.43cb759274",
|
|
48
|
+
"@dxos/async": "0.8.4-main.43cb759274",
|
|
49
|
+
"@dxos/invariant": "0.8.4-main.43cb759274",
|
|
50
|
+
"@dxos/log": "0.8.4-main.43cb759274",
|
|
51
|
+
"@dxos/node-std": "0.8.4-main.43cb759274",
|
|
52
|
+
"@dxos/react-ui-mosaic": "0.8.4-main.43cb759274",
|
|
53
|
+
"@dxos/debug": "0.8.4-main.43cb759274"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@babel/core": "^7.18.13",
|
|
@@ -60,24 +60,24 @@
|
|
|
60
60
|
"@types/react-dom": "~19.2.3",
|
|
61
61
|
"@types/topojson-client": "^3.1.4",
|
|
62
62
|
"@types/topojson-specification": "^1.0.5",
|
|
63
|
-
"postcss": "^8.
|
|
63
|
+
"postcss": "^8.5.12",
|
|
64
64
|
"react": "~19.2.3",
|
|
65
65
|
"react-dom": "~19.2.3",
|
|
66
|
-
"@dxos/random": "0.8.4-main.
|
|
67
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
68
|
-
"@dxos/react-ui-
|
|
69
|
-
"@dxos/
|
|
70
|
-
"@dxos/
|
|
71
|
-
"@dxos/
|
|
66
|
+
"@dxos/random": "0.8.4-main.43cb759274",
|
|
67
|
+
"@dxos/react-ui": "0.8.4-main.43cb759274",
|
|
68
|
+
"@dxos/react-ui-stack": "0.8.4-main.43cb759274",
|
|
69
|
+
"@dxos/react-ui-syntax-highlighter": "0.8.4-main.43cb759274",
|
|
70
|
+
"@dxos/storybook-utils": "0.8.4-main.43cb759274",
|
|
71
|
+
"@dxos/ui-theme": "0.8.4-main.43cb759274"
|
|
72
72
|
},
|
|
73
73
|
"peerDependencies": {
|
|
74
74
|
"react": "~19.2.3",
|
|
75
75
|
"react-dom": "~19.2.3",
|
|
76
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
77
|
-
"@dxos/ui-theme": "0.8.4-main.
|
|
76
|
+
"@dxos/react-ui": "0.8.4-main.43cb759274",
|
|
77
|
+
"@dxos/ui-theme": "0.8.4-main.43cb759274"
|
|
78
78
|
},
|
|
79
79
|
"optionalDependencies": {
|
|
80
|
-
"@dxos/random": "0.8.4-main.
|
|
80
|
+
"@dxos/random": "0.8.4-main.43cb759274"
|
|
81
81
|
},
|
|
82
82
|
"publishConfig": {
|
|
83
83
|
"access": "public"
|
|
@@ -9,7 +9,7 @@ import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
|
9
9
|
import { select } from 'd3';
|
|
10
10
|
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
11
11
|
|
|
12
|
-
import { type Graph, type GraphModel, SelectionModel } from '@dxos/graph';
|
|
12
|
+
import { type Graph, type GraphModel, type SelectionMode, SelectionModel } from '@dxos/graph';
|
|
13
13
|
import { IconButton, Popover, Toolbar } from '@dxos/react-ui';
|
|
14
14
|
import { Card } from '@dxos/react-ui';
|
|
15
15
|
import { JsonHighlighter, Syntax } from '@dxos/react-ui-syntax-highlighter';
|
|
@@ -53,8 +53,8 @@ type DefaultStoryProps = GraphProps & {
|
|
|
53
53
|
debug?: boolean;
|
|
54
54
|
grid?: boolean | SVGGridProps;
|
|
55
55
|
inspect?: boolean;
|
|
56
|
-
singleSelect?: boolean;
|
|
57
56
|
graph: () => Graph.Any;
|
|
57
|
+
selectionMode?: SelectionMode;
|
|
58
58
|
projectorType?: ProjectorType;
|
|
59
59
|
projectorOptions?:
|
|
60
60
|
| GraphForceProjectorOptions
|
|
@@ -67,7 +67,7 @@ const DefaultStory = ({
|
|
|
67
67
|
debug,
|
|
68
68
|
grid,
|
|
69
69
|
inspect,
|
|
70
|
-
|
|
70
|
+
selectionMode,
|
|
71
71
|
graph: _graph,
|
|
72
72
|
projectorType: _projectorType = 'force',
|
|
73
73
|
projectorOptions,
|
|
@@ -78,11 +78,11 @@ const DefaultStory = ({
|
|
|
78
78
|
const registry = useContext(RegistryContext);
|
|
79
79
|
|
|
80
80
|
// Models.
|
|
81
|
+
const selection = useMemo(() => new SelectionModel({ mode: selectionMode }), [selectionMode]);
|
|
81
82
|
const [model, setModel] = useState<GraphModel.ReactiveGraphModel | undefined>(() => {
|
|
82
83
|
const graph = _graph?.();
|
|
83
84
|
return graph ? new TestGraphModel(registry, graph) : undefined;
|
|
84
85
|
});
|
|
85
|
-
const selection = useMemo(() => new SelectionModel(singleSelect), [singleSelect]);
|
|
86
86
|
|
|
87
87
|
// Projector.
|
|
88
88
|
const [projectorType, setProjectorType] = useState<ProjectorType>(_projectorType);
|
|
@@ -359,7 +359,7 @@ export const Projector: Story = {
|
|
|
359
359
|
debug: true,
|
|
360
360
|
drag: true,
|
|
361
361
|
arrows: true,
|
|
362
|
-
|
|
362
|
+
selectionMode: 'single',
|
|
363
363
|
projectorType: 'hierarchical',
|
|
364
364
|
projectorOptions: {
|
|
365
365
|
duration: 500,
|
|
@@ -26,15 +26,34 @@ export type GraphController = {
|
|
|
26
26
|
findNode: (id: string) => SVGGElement | null;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
+
/** Cursor position expressed in the same SVG model coordinates that node `x/y` live in (centered origin, post-zoom). */
|
|
30
|
+
export type ModelPoint = { x: number; y: number };
|
|
31
|
+
|
|
29
32
|
export type GraphProps<Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge.Any = any> = ThemedClassName<
|
|
30
|
-
Pick<
|
|
33
|
+
Pick<
|
|
34
|
+
GraphRendererOptions<Node>,
|
|
35
|
+
'labels' | 'subgraphs' | 'attributes' | 'renderNode' | 'highlightOnHover' | 'applyNode' | 'edgeOpacity'
|
|
36
|
+
> & {
|
|
31
37
|
model?: GraphModel.ReactiveGraphModel<Node, Edge>;
|
|
32
38
|
projector?: GraphProjector<Node>;
|
|
33
39
|
renderer?: GraphRenderer<Node>;
|
|
34
40
|
drag?: boolean;
|
|
35
41
|
arrows?: boolean;
|
|
36
42
|
onSelect?: (node: GraphLayoutNode<Node>, event: MouseEvent) => void;
|
|
37
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Fires on pointerenter with the hovered node, and on pointerleave with `null`.
|
|
45
|
+
* Use the `null` signal to clear hover-driven previews / highlights.
|
|
46
|
+
*/
|
|
47
|
+
onInspect?: (node: GraphLayoutNode<Node> | null, event: MouseEvent) => void;
|
|
48
|
+
/**
|
|
49
|
+
* Pointer-move in SVG model coordinates (the same space `node.x/y` live in — centered
|
|
50
|
+
* origin, post-zoom). The CTM lookup is done against the dx-graph group, so zoom + viewBox
|
|
51
|
+
* are both undone in one step. Pair with `onPointerLeave` to clear cursor-driven state.
|
|
52
|
+
* When either handler is set the renderer's `<svg>` is given `pointer-events: all` so
|
|
53
|
+
* events fire across empty canvas, not just over painted nodes.
|
|
54
|
+
*/
|
|
55
|
+
onPointerMove?: (point: ModelPoint, event: PointerEvent) => void;
|
|
56
|
+
onPointerLeave?: (event: PointerEvent) => void;
|
|
38
57
|
}
|
|
39
58
|
>;
|
|
40
59
|
|
|
@@ -48,14 +67,14 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
48
67
|
arrows,
|
|
49
68
|
onSelect,
|
|
50
69
|
onInspect,
|
|
70
|
+
onPointerMove,
|
|
71
|
+
onPointerLeave,
|
|
51
72
|
...props
|
|
52
73
|
}: GraphProps<Node, Edge>,
|
|
53
74
|
forwardedRef: Ref<GraphController>,
|
|
54
75
|
) => {
|
|
55
76
|
const context = useSvgContext();
|
|
56
77
|
const graphRef = useRef<SVGGElement>(null);
|
|
57
|
-
// TODO(wittjosiah): This doesn't work for some reason.
|
|
58
|
-
// const graph = useAtomValue(model?.graphAtom ?? EMPTY_ATOM);
|
|
59
78
|
|
|
60
79
|
const { projector, renderer } = useMemo(() => {
|
|
61
80
|
let projector = projectorProp;
|
|
@@ -70,13 +89,40 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
70
89
|
// TODO(burdon): Replace drag when projector is updated.
|
|
71
90
|
drag: drag ? createGraphDrag(context, projector) : undefined,
|
|
72
91
|
arrows: { end: arrows },
|
|
73
|
-
|
|
74
|
-
|
|
92
|
+
// Projector intercept first so built-in interactions (e.g. cluster collapse) can
|
|
93
|
+
// run without consumers wiring them. Returning true suppresses the user handler.
|
|
94
|
+
onNodeClick: (node, event) => {
|
|
95
|
+
if (projector.handleNodeClick(node, event)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
onSelect?.(node, event);
|
|
99
|
+
},
|
|
100
|
+
onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : undefined,
|
|
101
|
+
// Pair pointerenter with leave so consumers can clear hover-driven previews.
|
|
102
|
+
// The callback receives `null` on leave (event still carries the source).
|
|
103
|
+
onNodePointerLeave: onInspect ? (_node, event) => onInspect(null, event) : undefined,
|
|
75
104
|
});
|
|
76
105
|
}
|
|
77
106
|
|
|
78
107
|
return { projector, renderer };
|
|
79
|
-
|
|
108
|
+
// Renderer options are baked into the instance — re-memo when any of them change
|
|
109
|
+
// so a swapped `renderNode` / handler is actually applied.
|
|
110
|
+
}, [
|
|
111
|
+
context,
|
|
112
|
+
projectorProp,
|
|
113
|
+
rendererProp,
|
|
114
|
+
drag,
|
|
115
|
+
arrows,
|
|
116
|
+
onSelect,
|
|
117
|
+
onInspect,
|
|
118
|
+
props.labels,
|
|
119
|
+
props.subgraphs,
|
|
120
|
+
props.attributes,
|
|
121
|
+
props.renderNode,
|
|
122
|
+
props.highlightOnHover,
|
|
123
|
+
props.applyNode,
|
|
124
|
+
props.edgeOpacity,
|
|
125
|
+
]);
|
|
80
126
|
|
|
81
127
|
// External API.
|
|
82
128
|
useImperativeHandle(
|
|
@@ -97,26 +143,34 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
97
143
|
|
|
98
144
|
// Subscriptions.
|
|
99
145
|
useEffect(() => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
projector.updated.on(({ layout }) => {
|
|
146
|
+
// Wire the renderer listener BEFORE calling updateData so the synchronous
|
|
147
|
+
// 'topology' emit (fired from inside onUpdate) reaches the renderer.
|
|
148
|
+
const cleanup = combine(
|
|
149
|
+
// Subscribe to model changes if reactive model.
|
|
150
|
+
model
|
|
151
|
+
? model.subscribe(() => {
|
|
152
|
+
projector.updateData(model?.graph);
|
|
153
|
+
})
|
|
154
|
+
: undefined,
|
|
155
|
+
|
|
156
|
+
projector.updated.on(({ layout, kind }) => {
|
|
112
157
|
try {
|
|
113
|
-
|
|
158
|
+
// 'positions' is the per-tick fast path; 'topology' (and unset, for back-compat) is the full render.
|
|
159
|
+
if (kind === 'positions') {
|
|
160
|
+
renderer.applyPositions(layout);
|
|
161
|
+
} else {
|
|
162
|
+
renderer.render(layout);
|
|
163
|
+
}
|
|
114
164
|
} catch (error) {
|
|
115
|
-
void projector.stop();
|
|
116
165
|
log.catch(error);
|
|
166
|
+
void projector.stop();
|
|
117
167
|
}
|
|
118
168
|
}),
|
|
119
169
|
);
|
|
170
|
+
|
|
171
|
+
projector.updateData(model?.graph);
|
|
172
|
+
|
|
173
|
+
return cleanup;
|
|
120
174
|
}, [model, projector, renderer]);
|
|
121
175
|
|
|
122
176
|
// Start.
|
|
@@ -127,6 +181,47 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
127
181
|
};
|
|
128
182
|
}, [projector]);
|
|
129
183
|
|
|
184
|
+
// Model-space pointer events. Attached to the SVG element so they fire across the entire
|
|
185
|
+
// canvas (with pointer-events: all), not just over painted nodes. The dx-graph group's CTM
|
|
186
|
+
// undoes both the centered viewBox and any active zoom in one step, landing the cursor in
|
|
187
|
+
// the same coordinate space the projector reads.
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
if (!onPointerMove && !onPointerLeave) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const svg = context.svg;
|
|
193
|
+
if (!svg) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const previousPointerEvents = svg.style.pointerEvents;
|
|
197
|
+
svg.style.pointerEvents = 'all';
|
|
198
|
+
const handleMove = (event: PointerEvent) => {
|
|
199
|
+
if (!onPointerMove) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const target = graphRef.current;
|
|
203
|
+
const ctm = target?.getScreenCTM();
|
|
204
|
+
if (!target || !ctm) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const point = svg.createSVGPoint();
|
|
208
|
+
point.x = event.clientX;
|
|
209
|
+
point.y = event.clientY;
|
|
210
|
+
const modelPoint = point.matrixTransform(ctm.inverse());
|
|
211
|
+
onPointerMove({ x: modelPoint.x, y: modelPoint.y }, event);
|
|
212
|
+
};
|
|
213
|
+
const handleLeave = (event: PointerEvent) => {
|
|
214
|
+
onPointerLeave?.(event);
|
|
215
|
+
};
|
|
216
|
+
svg.addEventListener('pointermove', handleMove);
|
|
217
|
+
svg.addEventListener('pointerleave', handleLeave);
|
|
218
|
+
return () => {
|
|
219
|
+
svg.removeEventListener('pointermove', handleMove);
|
|
220
|
+
svg.removeEventListener('pointerleave', handleLeave);
|
|
221
|
+
svg.style.pointerEvents = previousPointerEvents;
|
|
222
|
+
};
|
|
223
|
+
}, [context, onPointerMove, onPointerLeave]);
|
|
224
|
+
|
|
130
225
|
return <g ref={graphRef} className={mx('dx-graph', classNames)} />;
|
|
131
226
|
};
|
|
132
227
|
|
|
@@ -46,18 +46,20 @@ export default meta;
|
|
|
46
46
|
|
|
47
47
|
type Story = StoryObj<typeof meta>;
|
|
48
48
|
|
|
49
|
+
// Selectors use the descendant combinator (`[&_X]`) — the mesh / hexagon / border elements
|
|
50
|
+
// live inside the SVG inside the div, so direct-child (`>`) selectors don't match.
|
|
49
51
|
export const Default: Story = {
|
|
50
52
|
args: {
|
|
51
53
|
classNames: [
|
|
52
|
-
'[
|
|
53
|
-
'[
|
|
54
|
-
'[
|
|
54
|
+
'[&_.mesh]:fill-none',
|
|
55
|
+
'[&_.hexagon>path.fill]:fill-blue-800 [&_.hexagon]:stroke-blue-700',
|
|
56
|
+
'[&_.border]:fill-none [&_.border]:stroke-blue-500 [&_.border]:stroke-2',
|
|
55
57
|
],
|
|
56
58
|
},
|
|
57
59
|
};
|
|
58
60
|
|
|
59
61
|
export const Outline: Story = {
|
|
60
62
|
args: {
|
|
61
|
-
classNames: ['[
|
|
63
|
+
classNames: ['[&_.mesh]:fill-none', '[&_.border]:fill-none [&_.border]:stroke-red-500 [&_.border]:stroke-2'],
|
|
62
64
|
},
|
|
63
65
|
};
|
|
@@ -20,43 +20,51 @@ export const Mesh = ({ radius = 16, value = 0.5 }: MeshProps) => {
|
|
|
20
20
|
const { svg, size } = useSvgContext();
|
|
21
21
|
|
|
22
22
|
useEffect(() => {
|
|
23
|
-
if (size) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
23
|
+
if (!size) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const topology = hexTopology(size, radius, value);
|
|
28
|
+
const projection = hexProjection(radius);
|
|
29
|
+
const path = geoPath().projection(projection);
|
|
30
|
+
|
|
31
|
+
// Replace the previous render: idempotent join on a single keyed root group, then
|
|
32
|
+
// recreate the inner layers. Without this the effect re-runs (e.g. animating `value`
|
|
33
|
+
// or on resize) appended new groups on top of the old ones instead of replacing them.
|
|
34
|
+
const root = select(svg)
|
|
35
|
+
.selectAll<SVGGElement, null>('g.dx-mesh-root')
|
|
36
|
+
.data([null])
|
|
37
|
+
.join('g')
|
|
38
|
+
.classed('dx-mesh-root', true);
|
|
39
|
+
root.selectAll('*').remove();
|
|
40
|
+
|
|
41
|
+
root
|
|
42
|
+
.append('g')
|
|
43
|
+
.attr('class', 'hexagon')
|
|
44
|
+
.selectAll('path')
|
|
45
|
+
.data(topology.objects.hexagons.geometries)
|
|
46
|
+
.enter()
|
|
47
|
+
.append('path')
|
|
48
|
+
.attr('d', (d) => path(feature(topology, d)))
|
|
49
|
+
.attr('class', (d) => ((d.properties as Custom)?.fill ? 'fill' : null));
|
|
50
|
+
|
|
51
|
+
root.append('path').datum(mesh(topology, topology.objects.hexagons)).attr('class', 'mesh').attr('d', path);
|
|
52
|
+
|
|
53
|
+
const redraw = (border: any) => {
|
|
54
|
+
border.attr(
|
|
55
|
+
'd',
|
|
56
|
+
path(
|
|
57
|
+
mesh(
|
|
58
|
+
topology,
|
|
59
|
+
topology.objects.hexagons,
|
|
60
|
+
(a, b) => (a.properties as Custom).fill !== (b.properties as Custom).fill,
|
|
53
61
|
),
|
|
54
|
-
)
|
|
55
|
-
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
};
|
|
56
65
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}, [svg, size, value]);
|
|
66
|
+
root.append('path').attr('class', 'border').call(redraw);
|
|
67
|
+
}, [svg, size, value, radius]);
|
|
60
68
|
|
|
61
69
|
return null;
|
|
62
70
|
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { useEffect, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { type ThemedClassName } from '@dxos/react-ui';
|
|
8
|
+
import { mx } from '@dxos/ui-theme';
|
|
9
|
+
|
|
10
|
+
import { useSvgContext } from '../../hooks';
|
|
11
|
+
|
|
12
|
+
export type FPSCorner = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
|
|
13
|
+
|
|
14
|
+
export type FPSProps = ThemedClassName<{
|
|
15
|
+
/** Which corner of the SVG viewport to anchor the readout to. */
|
|
16
|
+
corner?: FPSCorner;
|
|
17
|
+
/** Pixels of padding from the chosen corner. */
|
|
18
|
+
margin?: number;
|
|
19
|
+
}>;
|
|
20
|
+
|
|
21
|
+
const PADDING = 8;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Frame-rate readout — drops into an `<SVG.Root>` alongside `<SVG.Graph>` to display a
|
|
25
|
+
* rolling 1-second average of the browser's repaint rate. Self-contained: runs its own
|
|
26
|
+
* `requestAnimationFrame` loop, so the number reflects actual render rate independent of
|
|
27
|
+
* whichever projector is driving the graph.
|
|
28
|
+
*
|
|
29
|
+
* Anchored to a corner of the SVG viewBox via the `<text>`'s `text-anchor` and `dy`
|
|
30
|
+
* attributes — re-renders on `context.resized` so the position tracks container resizes.
|
|
31
|
+
*/
|
|
32
|
+
export const FPS = ({ classNames, corner = 'top-right', margin = PADDING }: FPSProps) => {
|
|
33
|
+
const context = useSvgContext();
|
|
34
|
+
const [size, setSize] = useState(context.size);
|
|
35
|
+
const [fps, setFps] = useState<number | null>(null);
|
|
36
|
+
|
|
37
|
+
// Keep `size` in sync with the SVG context so the corner anchor follows container resizes.
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
setSize(context.size);
|
|
40
|
+
return context.resized.on((ctx) => setSize(ctx.size));
|
|
41
|
+
}, [context]);
|
|
42
|
+
|
|
43
|
+
// Independent rAF loop — measures the browser's actual repaint rate, not any specific
|
|
44
|
+
// projector's tick rate. Rolling 1-second window so the number is responsive but stable.
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
let frames = 0;
|
|
47
|
+
let windowStart = performance.now();
|
|
48
|
+
let rafId = 0;
|
|
49
|
+
const loop = (now: number) => {
|
|
50
|
+
frames++;
|
|
51
|
+
const elapsed = now - windowStart;
|
|
52
|
+
if (elapsed >= 1000) {
|
|
53
|
+
setFps((frames * 1000) / elapsed);
|
|
54
|
+
frames = 0;
|
|
55
|
+
windowStart = now;
|
|
56
|
+
}
|
|
57
|
+
rafId = requestAnimationFrame(loop);
|
|
58
|
+
};
|
|
59
|
+
rafId = requestAnimationFrame(loop);
|
|
60
|
+
return () => cancelAnimationFrame(rafId);
|
|
61
|
+
}, []);
|
|
62
|
+
|
|
63
|
+
if (!size) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The SVG.Root viewBox is centered ([-w/2, w/2] × [-h/2, h/2]); offset from the chosen
|
|
68
|
+
// corner inward by `margin` and pick the corresponding text-anchor / dominant-baseline.
|
|
69
|
+
const halfW = size.width / 2;
|
|
70
|
+
const halfH = size.height / 2;
|
|
71
|
+
const left = corner === 'top-left' || corner === 'bottom-left';
|
|
72
|
+
const top = corner === 'top-left' || corner === 'top-right';
|
|
73
|
+
const x = left ? -halfW + margin : halfW - margin;
|
|
74
|
+
const y = top ? -halfH + margin : halfH - margin;
|
|
75
|
+
const textAnchor = left ? 'start' : 'end';
|
|
76
|
+
const dominantBaseline = top ? 'hanging' : 'alphabetic';
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<g className='dx-fps' style={{ pointerEvents: 'none' }}>
|
|
80
|
+
<text
|
|
81
|
+
x={x}
|
|
82
|
+
y={y}
|
|
83
|
+
textAnchor={textAnchor}
|
|
84
|
+
dominantBaseline={dominantBaseline}
|
|
85
|
+
className={mx('font-mono text-xs fill-neutral-500 tabular-nums', classNames)}
|
|
86
|
+
>
|
|
87
|
+
{fps == null ? '— fps' : `${fps.toFixed(0)} fps`}
|
|
88
|
+
</text>
|
|
89
|
+
</g>
|
|
90
|
+
);
|
|
91
|
+
};
|
|
@@ -10,11 +10,12 @@ import React, {
|
|
|
10
10
|
useEffect,
|
|
11
11
|
useImperativeHandle,
|
|
12
12
|
useMemo,
|
|
13
|
+
useState,
|
|
13
14
|
} from 'react';
|
|
14
|
-
import { useResizeDetector } from 'react-resize-detector';
|
|
15
15
|
|
|
16
16
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
17
|
-
import { composableProps
|
|
17
|
+
import { composableProps } from '@dxos/react-ui';
|
|
18
|
+
import { mx } from '@dxos/ui-theme';
|
|
18
19
|
|
|
19
20
|
import { SVGContext, type SVGContextOptions, SVGContextProvider } from '../../hooks';
|
|
20
21
|
|
|
@@ -23,9 +24,34 @@ export type RootProps = ThemedClassName<PropsWithChildren<SVGContextOptions & Co
|
|
|
23
24
|
/**
|
|
24
25
|
* Makes the SVG context available to child nodes.
|
|
25
26
|
* Automatically resizes the SVG element, which expands to fit the container.
|
|
27
|
+
*
|
|
28
|
+
* NOTE: Uses `ResizeObserver` directly rather than `react-resize-detector` — the latter's
|
|
29
|
+
* `useResizeDetector` was flaky in our Storybook environment (sometimes never fired its
|
|
30
|
+
* initial measurement), leaving the SVG stuck at `opacity-0` with no rendered content.
|
|
26
31
|
*/
|
|
27
32
|
export const Root = forwardRef<SVGContext, RootProps>(({ children, scale, centered, ...props }, ref) => {
|
|
28
|
-
const
|
|
33
|
+
const [container, setContainer] = useState<HTMLDivElement | null>(null);
|
|
34
|
+
const [size, setSize] = useState<{ width: number; height: number }>({ width: 0, height: 0 });
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
if (!container) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const rect = container.getBoundingClientRect();
|
|
41
|
+
setSize({ width: rect.width, height: rect.height });
|
|
42
|
+
const observer = new ResizeObserver((entries) => {
|
|
43
|
+
const entry = entries[0];
|
|
44
|
+
if (!entry) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const { width, height } = entry.contentRect;
|
|
48
|
+
setSize((prev) => (prev.width === width && prev.height === height ? prev : { width, height }));
|
|
49
|
+
});
|
|
50
|
+
observer.observe(container);
|
|
51
|
+
return () => observer.disconnect();
|
|
52
|
+
}, [container]);
|
|
53
|
+
|
|
54
|
+
const { width, height } = size;
|
|
29
55
|
|
|
30
56
|
const context = useMemo<SVGContext>(() => {
|
|
31
57
|
return new SVGContext({ scale, centered });
|
|
@@ -48,7 +74,7 @@ export const Root = forwardRef<SVGContext, RootProps>(({ children, scale, center
|
|
|
48
74
|
|
|
49
75
|
return (
|
|
50
76
|
<SVGContextProvider value={context}>
|
|
51
|
-
<div {...composableProps(props, { classNames: 'dx-expander' })} ref={
|
|
77
|
+
<div {...composableProps(props, { classNames: 'dx-expander' })} ref={setContainer}>
|
|
52
78
|
<svg
|
|
53
79
|
xmlns='http://www.w3.org/2000/svg'
|
|
54
80
|
ref={context.ref}
|
|
@@ -8,6 +8,7 @@ import { type ThemedClassName } from '@dxos/react-ui';
|
|
|
8
8
|
|
|
9
9
|
import { Graph, type GraphProps as SVGGraphProps } from '../Graph';
|
|
10
10
|
import { Mesh, type MeshProps as SVGMeshProps } from '../Mesh';
|
|
11
|
+
import { FPS, type FPSProps as SVGFPSProps } from './FPS';
|
|
11
12
|
import { Grid, type GridProps as SVGGridProps } from './Grid';
|
|
12
13
|
import { Markers, type MarkersProps as SVGMarkersProps } from './Markers';
|
|
13
14
|
import { Root, type RootProps as SVGRootProps } from './Root';
|
|
@@ -22,6 +23,7 @@ type SVG = {
|
|
|
22
23
|
Mesh: typeof Mesh;
|
|
23
24
|
Zoom: typeof Zoom;
|
|
24
25
|
Graph: typeof Graph;
|
|
26
|
+
FPS: typeof FPS;
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
export const SVG: SVG = {
|
|
@@ -31,6 +33,7 @@ export const SVG: SVG = {
|
|
|
31
33
|
Mesh,
|
|
32
34
|
Zoom,
|
|
33
35
|
Graph,
|
|
36
|
+
FPS,
|
|
34
37
|
};
|
|
35
38
|
|
|
36
|
-
export type { SVGRootProps, SVGGridProps, SVGMarkersProps, SVGZoomProps, SVGGraphProps, SVGMeshProps };
|
|
39
|
+
export type { SVGRootProps, SVGGridProps, SVGMarkersProps, SVGZoomProps, SVGGraphProps, SVGMeshProps, SVGFPSProps };
|