@sqlrooms/cosmos 0.4.2 → 0.5.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.
- package/dist/CosmosGraph.d.ts +3 -3
- package/dist/CosmosGraph.d.ts.map +1 -1
- package/dist/CosmosGraph.js +51 -13
- package/dist/CosmosGraph.js.map +1 -1
- package/dist/CosmosGraphControls.d.ts +1 -1
- package/dist/CosmosGraphControls.js +4 -4
- package/dist/CosmosGraphControls.js.map +1 -1
- package/dist/CosmosSimulationControls.d.ts +1 -5
- package/dist/CosmosSimulationControls.d.ts.map +1 -1
- package/dist/CosmosSimulationControls.js +7 -28
- package/dist/CosmosSimulationControls.js.map +1 -1
- package/dist/CosmosSlice.d.ts +75 -0
- package/dist/CosmosSlice.d.ts.map +1 -0
- package/dist/CosmosSlice.js +153 -0
- package/dist/CosmosSlice.js.map +1 -0
- package/dist/CosmosSliceConfig.d.ts +209 -0
- package/dist/CosmosSliceConfig.d.ts.map +1 -0
- package/dist/CosmosSliceConfig.js +187 -0
- package/dist/CosmosSliceConfig.js.map +1 -0
- package/dist/hooks/useHoverState.d.ts +11 -8
- package/dist/hooks/useHoverState.d.ts.map +1 -1
- package/dist/hooks/useHoverState.js +13 -9
- package/dist/hooks/useHoverState.js.map +1 -1
- package/dist/index.d.ts +61 -27
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +62 -26
- package/dist/index.js.map +1 -1
- package/dist/utils/coordinates.js.map +1 -1
- package/package.json +8 -4
- package/dist/CosmosGraphContext.d.ts +0 -76
- package/dist/CosmosGraphContext.d.ts.map +0 -1
- package/dist/CosmosGraphContext.js +0 -95
- package/dist/CosmosGraphContext.js.map +0 -1
- package/dist/components/CosmosGraph.d.ts +0 -14
- package/dist/components/CosmosGraph.d.ts.map +0 -1
- package/dist/components/CosmosGraph.js +0 -130
- package/dist/components/CosmosGraph.js.map +0 -1
- package/dist/config.d.ts +0 -24
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -18
- package/dist/config.js.map +0 -1
- package/dist/hooks/index.d.ts +0 -29
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/index.js +0 -29
- package/dist/hooks/index.js.map +0 -1
- package/dist/hooks/useGraph.d.ts +0 -49
- package/dist/hooks/useGraph.d.ts.map +0 -1
- package/dist/hooks/useGraph.js +0 -93
- package/dist/hooks/useGraph.js.map +0 -1
- package/dist/hooks/useGraphConfig.d.ts +0 -101
- package/dist/hooks/useGraphConfig.d.ts.map +0 -1
- package/dist/hooks/useGraphConfig.js +0 -53
- package/dist/hooks/useGraphConfig.js.map +0 -1
- package/dist/hooks/useRelativeCoordinates.d.ts +0 -2
- package/dist/hooks/useRelativeCoordinates.d.ts.map +0 -1
- package/dist/hooks/useRelativeCoordinates.js +0 -10
- package/dist/hooks/useRelativeCoordinates.js.map +0 -1
package/dist/CosmosGraph.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export type CosmosGraphProps = {
|
|
|
19
19
|
/** Optional index of the point to focus on */
|
|
20
20
|
focusedPointIndex?: number | undefined;
|
|
21
21
|
/** Optional function to render custom tooltip content for a point */
|
|
22
|
-
|
|
22
|
+
renderPointTooltip?: (index: number) => React.ReactNode;
|
|
23
23
|
/** Optional child elements to render inside the graph container */
|
|
24
24
|
children?: React.ReactNode;
|
|
25
25
|
};
|
|
@@ -30,7 +30,7 @@ export type CosmosGraphProps = {
|
|
|
30
30
|
* - Renders points and optional links in a WebGL canvas
|
|
31
31
|
* - Supports point hovering with customizable tooltips
|
|
32
32
|
* - Handles point focusing
|
|
33
|
-
* - Provides graph
|
|
33
|
+
* - Provides graph state to child components through zustand store
|
|
34
34
|
*
|
|
35
35
|
* @example
|
|
36
36
|
* ```tsx
|
|
@@ -51,7 +51,7 @@ export type CosmosGraphProps = {
|
|
|
51
51
|
* pointPositions={new Float32Array([0, 0, 1, 1])}
|
|
52
52
|
* pointColors={new Float32Array([1, 0, 0, 1, 0, 1, 0, 1])}
|
|
53
53
|
* pointSizes={new Float32Array([5, 5])}
|
|
54
|
-
*
|
|
54
|
+
* renderPointTooltip={(index) => `Point ${index}`}
|
|
55
55
|
* >
|
|
56
56
|
* <GraphControls />
|
|
57
57
|
* </CosmosGraph>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CosmosGraph.d.ts","sourceRoot":"","sources":["../src/CosmosGraph.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,oBAAoB,EAAC,MAAM,oBAAoB,CAAC;AAExD,OAAO,EAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"CosmosGraph.d.ts","sourceRoot":"","sources":["../src/CosmosGraph.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAC,oBAAoB,EAAC,MAAM,oBAAoB,CAAC;AAExD,OAAO,EAAC,EAAE,EAAoB,MAAM,OAAO,CAAC;AAI5C;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,4EAA4E;IAC5E,MAAM,EAAE,oBAAoB,CAAC;IAC7B,kFAAkF;IAClF,cAAc,EAAE,YAAY,CAAC;IAC7B,6EAA6E;IAC7E,UAAU,EAAE,YAAY,CAAC;IACzB,8EAA8E;IAC9E,WAAW,EAAE,YAAY,CAAC;IAC1B,6EAA6E;IAC7E,WAAW,CAAC,EAAE,YAAY,CAAC;IAC3B,qFAAqF;IACrF,UAAU,CAAC,EAAE,YAAY,CAAC;IAC1B,8CAA8C;IAC9C,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,qEAAqE;IACrE,kBAAkB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,KAAK,CAAC,SAAS,CAAC;IACxD,mEAAmE;IACnE,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,eAAO,MAAM,WAAW,EAAE,EAAE,CAAC,gBAAgB,CAoF5C,CAAC"}
|
package/dist/CosmosGraph.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { cn, useRelativeCoordinates } from '@sqlrooms/ui';
|
|
3
|
-
import { useRef } from 'react';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { useEffect, useRef } from 'react';
|
|
4
|
+
import { useStoreWithCosmos } from './CosmosSlice';
|
|
5
|
+
import { useHoverState } from './hooks/useHoverState';
|
|
6
6
|
/**
|
|
7
7
|
* A React component that renders an interactive graph visualization using Cosmos.
|
|
8
8
|
*
|
|
@@ -10,7 +10,7 @@ import { useGraph, useHoverState, useGraphConfig } from './hooks';
|
|
|
10
10
|
* - Renders points and optional links in a WebGL canvas
|
|
11
11
|
* - Supports point hovering with customizable tooltips
|
|
12
12
|
* - Handles point focusing
|
|
13
|
-
* - Provides graph
|
|
13
|
+
* - Provides graph state to child components through zustand store
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
16
16
|
* ```tsx
|
|
@@ -31,7 +31,7 @@ import { useGraph, useHoverState, useGraphConfig } from './hooks';
|
|
|
31
31
|
* pointPositions={new Float32Array([0, 0, 1, 1])}
|
|
32
32
|
* pointColors={new Float32Array([1, 0, 0, 1, 0, 1, 0, 1])}
|
|
33
33
|
* pointSizes={new Float32Array([5, 5])}
|
|
34
|
-
*
|
|
34
|
+
* renderPointTooltip={(index) => `Point ${index}`}
|
|
35
35
|
* >
|
|
36
36
|
* <GraphControls />
|
|
37
37
|
* </CosmosGraph>
|
|
@@ -40,15 +40,53 @@ import { useGraph, useHoverState, useGraphConfig } from './hooks';
|
|
|
40
40
|
* };
|
|
41
41
|
* ```
|
|
42
42
|
*/
|
|
43
|
-
export const CosmosGraph = ({ config, pointPositions, pointSizes, pointColors, linkIndexes, linkColors, focusedPointIndex,
|
|
43
|
+
export const CosmosGraph = ({ config, pointPositions, pointSizes, pointColors, linkIndexes, linkColors, focusedPointIndex, renderPointTooltip, children, }) => {
|
|
44
44
|
const containerRef = useRef(null);
|
|
45
45
|
const calcRelativeCoordinates = useRelativeCoordinates(containerRef);
|
|
46
|
-
const { hoveredPoint,
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
const { hoveredPoint, eventHandlers } = useHoverState(calcRelativeCoordinates);
|
|
47
|
+
const createGraph = useStoreWithCosmos((s) => s.cosmos.createGraph);
|
|
48
|
+
const destroyGraph = useStoreWithCosmos((s) => s.cosmos.destroyGraph);
|
|
49
|
+
const updateGraphConfig = useStoreWithCosmos((s) => s.cosmos.updateGraphConfig);
|
|
50
|
+
const updateGraphData = useStoreWithCosmos((s) => s.cosmos.updateGraphData);
|
|
51
|
+
const setFocusedPoint = useStoreWithCosmos((s) => s.cosmos.setFocusedPoint);
|
|
52
|
+
// Create graph instance and clean up on unmount
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!containerRef.current)
|
|
55
|
+
return;
|
|
56
|
+
createGraph(containerRef.current);
|
|
57
|
+
return () => destroyGraph();
|
|
58
|
+
}, [createGraph, destroyGraph]);
|
|
59
|
+
// Update graph config when it changes
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
updateGraphConfig({
|
|
62
|
+
...config,
|
|
63
|
+
...eventHandlers,
|
|
64
|
+
});
|
|
65
|
+
}, [config, eventHandlers, updateGraphConfig]);
|
|
66
|
+
// Update graph data when props change
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
updateGraphData({
|
|
69
|
+
pointPositions,
|
|
70
|
+
pointColors,
|
|
71
|
+
pointSizes,
|
|
72
|
+
linkIndexes,
|
|
73
|
+
linkColors,
|
|
74
|
+
});
|
|
75
|
+
}, [
|
|
76
|
+
updateGraphData,
|
|
77
|
+
pointPositions,
|
|
78
|
+
pointColors,
|
|
79
|
+
pointSizes,
|
|
80
|
+
linkIndexes,
|
|
81
|
+
linkColors,
|
|
82
|
+
]);
|
|
83
|
+
// Update focus when it changes
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
setFocusedPoint(focusedPointIndex);
|
|
86
|
+
}, [setFocusedPoint, focusedPointIndex]);
|
|
87
|
+
return (_jsxs("div", { className: "relative w-full h-full", children: [_jsx("div", { ref: containerRef, className: "absolute w-full h-full" }), renderPointTooltip ? (_jsx("div", { className: cn('absolute z-50 max-w-xs', 'bg-white/90 dark:bg-gray-800/90 rounded-md shadow-lg p-2', 'text-xs flex gap-2 items-center pointer-events-none transition-opacity duration-150', hoveredPoint ? 'opacity-100' : 'opacity-0'), style: {
|
|
88
|
+
transform: `translate(${hoveredPoint?.position?.[0] ?? 0}px, ${hoveredPoint?.position?.[1] ?? 0}px) translate(-50%, 5px)`,
|
|
89
|
+
visibility: hoveredPoint ? 'visible' : 'hidden',
|
|
90
|
+
}, children: _jsx("div", { children: renderPointTooltip?.(hoveredPoint?.index ?? 0) }) })) : null, children] }));
|
|
53
91
|
};
|
|
54
92
|
//# sourceMappingURL=CosmosGraph.js.map
|
package/dist/CosmosGraph.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CosmosGraph.js","sourceRoot":"","sources":["../src/CosmosGraph.tsx"],"names":[],"mappings":";AACA,OAAO,EAAC,EAAE,EAAE,sBAAsB,EAAC,MAAM,cAAc,CAAC;AACxD,OAAO,EAAK,MAAM,EAAC,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"CosmosGraph.js","sourceRoot":"","sources":["../src/CosmosGraph.tsx"],"names":[],"mappings":";AACA,OAAO,EAAC,EAAE,EAAE,sBAAsB,EAAC,MAAM,cAAc,CAAC;AACxD,OAAO,EAAK,SAAS,EAAE,MAAM,EAAC,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAC,kBAAkB,EAAC,MAAM,eAAe,CAAC;AACjD,OAAO,EAAC,aAAa,EAAC,MAAM,uBAAuB,CAAC;AA0BpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoC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mos';\nimport {cn, useRelativeCoordinates} from '@sqlrooms/ui';\nimport {FC, useEffect, useRef} from 'react';\nimport {useStoreWithCosmos} from './CosmosSlice';\nimport {useHoverState} from './hooks/useHoverState';\n\n/**\n * Props for the CosmosGraph component.\n */\nexport type CosmosGraphProps = {\n /** Configuration object for the graph's visual and behavioral properties */\n config: GraphConfigInterface;\n /** Float32Array containing x,y coordinates for each point (2 values per point) */\n pointPositions: Float32Array;\n /** Float32Array containing size values for each point (1 value per point) */\n pointSizes: Float32Array;\n /** Float32Array containing RGBA values for each point (4 values per point) */\n pointColors: Float32Array;\n /** Optional Float32Array containing pairs of point indices defining links */\n linkIndexes?: Float32Array;\n /** Optional Float32Array containing RGBA values for each link (4 values per link) */\n linkColors?: Float32Array;\n /** Optional index of the point to focus on */\n focusedPointIndex?: number | undefined;\n /** Optional function to render custom tooltip content for a point */\n renderPointTooltip?: (index: number) => React.ReactNode;\n /** Optional child elements to render inside the graph container */\n children?: React.ReactNode;\n};\n\n/**\n * A React component that renders an interactive graph visualization using Cosmos.\n *\n * Features:\n * - Renders points and optional links in a WebGL canvas\n * - Supports point hovering with customizable tooltips\n * - Handles point focusing\n * - Provides graph state to child components through zustand store\n *\n * @example\n * ```tsx\n * const MyGraph = () => {\n * const config = {\n * backgroundColor: '#ffffff',\n * nodeSize: 5,\n * simulation: {\n * repulsion: 0.5,\n * gravity: 0.1,\n * },\n * };\n *\n * return (\n * <div style={{ width: '800px', height: '600px' }}>\n * <CosmosGraph\n * config={config}\n * pointPositions={new Float32Array([0, 0, 1, 1])}\n * pointColors={new Float32Array([1, 0, 0, 1, 0, 1, 0, 1])}\n * pointSizes={new Float32Array([5, 5])}\n * renderPointTooltip={(index) => `Point ${index}`}\n * >\n * <GraphControls />\n * </CosmosGraph>\n * </div>\n * );\n * };\n * ```\n */\nexport const CosmosGraph: FC<CosmosGraphProps> = ({\n config,\n pointPositions,\n pointSizes,\n pointColors,\n linkIndexes,\n linkColors,\n focusedPointIndex,\n renderPointTooltip,\n children,\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const calcRelativeCoordinates = useRelativeCoordinates(containerRef);\n const {hoveredPoint, eventHandlers} = useHoverState(calcRelativeCoordinates);\n const createGraph = useStoreWithCosmos((s) => s.cosmos.createGraph);\n const destroyGraph = useStoreWithCosmos((s) => s.cosmos.destroyGraph);\n const updateGraphConfig = useStoreWithCosmos(\n (s) => s.cosmos.updateGraphConfig,\n );\n const updateGraphData = useStoreWithCosmos((s) => s.cosmos.updateGraphData);\n const setFocusedPoint = useStoreWithCosmos((s) => s.cosmos.setFocusedPoint);\n\n // Create graph instance and clean up on unmount\n useEffect(() => {\n if (!containerRef.current) return;\n createGraph(containerRef.current);\n return () => destroyGraph();\n }, [createGraph, destroyGraph]);\n\n // Update graph config when it changes\n useEffect(() => {\n updateGraphConfig({\n ...config,\n ...eventHandlers,\n });\n }, [config, eventHandlers, updateGraphConfig]);\n\n // Update graph data when props change\n useEffect(() => {\n updateGraphData({\n pointPositions,\n pointColors,\n pointSizes,\n linkIndexes,\n linkColors,\n });\n }, [\n updateGraphData,\n pointPositions,\n pointColors,\n pointSizes,\n linkIndexes,\n linkColors,\n ]);\n\n // Update focus when it changes\n useEffect(() => {\n setFocusedPoint(focusedPointIndex);\n }, [setFocusedPoint, focusedPointIndex]);\n\n return (\n <div className=\"relative w-full h-full\">\n <div ref={containerRef} className=\"absolute w-full h-full\" />\n {renderPointTooltip ? (\n <div\n className={cn(\n 'absolute z-50 max-w-xs',\n 'bg-white/90 dark:bg-gray-800/90 rounded-md shadow-lg p-2',\n 'text-xs flex gap-2 items-center pointer-events-none transition-opacity duration-150',\n hoveredPoint ? 'opacity-100' : 'opacity-0',\n )}\n style={{\n transform: `translate(${hoveredPoint?.position?.[0] ?? 0}px, ${\n hoveredPoint?.position?.[1] ?? 0\n }px) translate(-50%, 5px)`,\n visibility: hoveredPoint ? 'visible' : 'hidden',\n }}\n >\n <div>{renderPointTooltip?.(hoveredPoint?.index ?? 0)}</div>\n </div>\n ) : null}\n {children}\n </div>\n );\n};\n"]}
|
|
@@ -13,7 +13,7 @@ interface CosmosGraphControlsProps {
|
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* A flexible control panel component for CosmosGraph that provides view controls.
|
|
16
|
-
* Must be used within a CosmosGraph component as it relies on the
|
|
16
|
+
* Must be used within a CosmosGraph component as it relies on the graph state from the store.
|
|
17
17
|
*
|
|
18
18
|
* The component shows the default fit view control and allows adding custom controls as children.
|
|
19
19
|
* For simulation controls, use the CosmosSimulationControls component.
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Button, cn } from '@sqlrooms/ui';
|
|
3
3
|
import { Maximize2 } from 'lucide-react';
|
|
4
|
-
import {
|
|
4
|
+
import { useStoreWithCosmos } from './CosmosSlice';
|
|
5
5
|
/**
|
|
6
6
|
* A flexible control panel component for CosmosGraph that provides view controls.
|
|
7
|
-
* Must be used within a CosmosGraph component as it relies on the
|
|
7
|
+
* Must be used within a CosmosGraph component as it relies on the graph state from the store.
|
|
8
8
|
*
|
|
9
9
|
* The component shows the default fit view control and allows adding custom controls as children.
|
|
10
10
|
* For simulation controls, use the CosmosSimulationControls component.
|
|
@@ -35,7 +35,7 @@ import { useCosmosGraph } from './CosmosGraphContext';
|
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
37
|
export const CosmosGraphControls = ({ className, children }) => {
|
|
38
|
-
const {
|
|
39
|
-
return (_jsxs("div", { className: cn('absolute top-1 left-1 flex gap-2', className), children: [_jsx(Button, { onClick:
|
|
38
|
+
const { fitView } = useStoreWithCosmos((s) => s.cosmos);
|
|
39
|
+
return (_jsxs("div", { className: cn('absolute top-1 left-1 flex gap-2', className), children: [_jsx(Button, { onClick: fitView, variant: "outline", size: "sm", children: _jsx(Maximize2, { className: "h-4 w-4" }) }), children] }));
|
|
40
40
|
};
|
|
41
41
|
//# sourceMappingURL=CosmosGraphControls.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CosmosGraphControls.js","sourceRoot":"","sources":["../src/CosmosGraphControls.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,EAAE,EAAC,MAAM,cAAc,CAAC;AACxC,OAAO,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"CosmosGraphControls.js","sourceRoot":"","sources":["../src/CosmosGraphControls.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAE,EAAE,EAAC,MAAM,cAAc,CAAC;AACxC,OAAO,EAAC,SAAS,EAAC,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAC,kBAAkB,EAAC,MAAM,eAAe,CAAC;AAejD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAE5B,CAAC,EAAC,SAAS,EAAE,QAAQ,EAAC,EAAE,EAAE;IAC5B,MAAM,EAAC,OAAO,EAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAEtD,OAAO,CACL,eAAK,SAAS,EAAE,EAAE,CAAC,kCAAkC,EAAE,SAAS,CAAC,aAC/D,KAAC,MAAM,IAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAC,SAAS,EAAC,IAAI,EAAC,IAAI,YACnD,KAAC,SAAS,IAAC,SAAS,EAAC,SAAS,GAAG,GAC1B,EACR,QAAQ,IACL,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {Button, cn} from '@sqlrooms/ui';\nimport {Maximize2} from 'lucide-react';\nimport {FC, PropsWithChildren} from 'react';\nimport {useStoreWithCosmos} from './CosmosSlice';\n\ninterface CosmosGraphControlsProps {\n /**\n * Optional className to override the default positioning and styling of the controls container.\n * By default, controls are positioned at the top-left corner.\n * @example\n * ```tsx\n * // Position controls at the bottom-right\n * <CosmosGraphControls className=\"absolute bottom-4 right-4\" />\n * ```\n */\n className?: string;\n}\n\n/**\n * A flexible control panel component for CosmosGraph that provides view controls.\n * Must be used within a CosmosGraph component as it relies on the graph state from the store.\n *\n * The component shows the default fit view control and allows adding custom controls as children.\n * For simulation controls, use the CosmosSimulationControls component.\n *\n * @example Default usage\n * ```tsx\n * <CosmosGraph {...graphProps}>\n * <CosmosGraphControls />\n * </CosmosGraph>\n * ```\n *\n * @example Custom positioning\n * ```tsx\n * <CosmosGraph {...graphProps}>\n * <CosmosGraphControls className=\"absolute bottom-4 right-4\" />\n * </CosmosGraph>\n * ```\n *\n * @example Adding custom controls\n * ```tsx\n * <CosmosGraph {...graphProps}>\n * <CosmosGraphControls>\n * <Button onClick={handleExport}>\n * <Download className=\"h-4 w-4\" />\n * </Button>\n * </CosmosGraphControls>\n * </CosmosGraph>\n * ```\n */\nexport const CosmosGraphControls: FC<\n PropsWithChildren<CosmosGraphControlsProps>\n> = ({className, children}) => {\n const {fitView} = useStoreWithCosmos((s) => s.cosmos);\n\n return (\n <div className={cn('absolute top-1 left-1 flex gap-2', className)}>\n <Button onClick={fitView} variant=\"outline\" size=\"sm\">\n <Maximize2 className=\"h-4 w-4\" />\n </Button>\n {children}\n </div>\n );\n};\n"]}
|
|
@@ -17,7 +17,7 @@ interface CosmosSimulationControlsProps {
|
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* A component that provides fine-grained controls for adjusting graph simulation parameters.
|
|
20
|
-
*
|
|
20
|
+
* Uses the zustand store to access and control the graph state.
|
|
21
21
|
*
|
|
22
22
|
* Features:
|
|
23
23
|
* - Slider controls for all simulation parameters
|
|
@@ -40,11 +40,9 @@ interface CosmosSimulationControlsProps {
|
|
|
40
40
|
*
|
|
41
41
|
* const MyGraph = () => {
|
|
42
42
|
* return (
|
|
43
|
-
* <div style={{ width: '800px', height: '600px' }}>
|
|
44
43
|
* <CosmosGraph {...graphProps}>
|
|
45
44
|
* <CosmosSimulationControls />
|
|
46
45
|
* </CosmosGraph>
|
|
47
|
-
* </div>
|
|
48
46
|
* );
|
|
49
47
|
* };
|
|
50
48
|
* ```
|
|
@@ -55,12 +53,10 @@ interface CosmosSimulationControlsProps {
|
|
|
55
53
|
*
|
|
56
54
|
* const MyGraph = () => {
|
|
57
55
|
* return (
|
|
58
|
-
* <div style={{ width: '800px', height: '600px' }}>
|
|
59
56
|
* <CosmosGraph {...graphProps}>
|
|
60
57
|
* <CosmosGraphControls className="absolute top-4 left-4" />
|
|
61
58
|
* <CosmosSimulationControls className="absolute top-4 right-4" />
|
|
62
59
|
* </CosmosGraph>
|
|
63
|
-
* </div>
|
|
64
60
|
* );
|
|
65
61
|
* };
|
|
66
62
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CosmosSimulationControls.d.ts","sourceRoot":"","sources":["../src/CosmosSimulationControls.tsx"],"names":[],"mappings":"AASA,OAAO,EAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"CosmosSimulationControls.d.ts","sourceRoot":"","sources":["../src/CosmosSimulationControls.tsx"],"names":[],"mappings":"AASA,OAAO,EAAC,EAAE,EAAC,MAAM,OAAO,CAAC;AAKzB;;GAEG;AACH,UAAU,6BAA6B;IACrC;;;;;;;;;OASG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAuDD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,eAAO,MAAM,wBAAwB,EAAE,EAAE,CAAC,6BAA6B,CA+FtE,CAAC"}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { cn, Slider, Label, Tooltip, TooltipContent, TooltipTrigger, Button, } from '@sqlrooms/ui';
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import { useCosmosGraph } from './CosmosGraphContext';
|
|
5
3
|
import { Info, Pause, Play, Wind } from 'lucide-react';
|
|
6
|
-
import {
|
|
4
|
+
import { useStoreWithCosmos } from './CosmosSlice';
|
|
5
|
+
import { CosmosSliceConfig } from './CosmosSliceConfig';
|
|
7
6
|
/**
|
|
8
7
|
* Configuration for each simulation parameter slider.
|
|
9
8
|
* These values define the range and step size for each parameter.
|
|
@@ -17,7 +16,6 @@ const simulationSliders = [
|
|
|
17
16
|
min: 0,
|
|
18
17
|
max: 0.5,
|
|
19
18
|
step: 0.01,
|
|
20
|
-
default: 0.25,
|
|
21
19
|
},
|
|
22
20
|
{
|
|
23
21
|
key: 'simulationRepulsion',
|
|
@@ -25,7 +23,6 @@ const simulationSliders = [
|
|
|
25
23
|
min: 0,
|
|
26
24
|
max: 2,
|
|
27
25
|
step: 0.01,
|
|
28
|
-
default: 1.0,
|
|
29
26
|
},
|
|
30
27
|
{
|
|
31
28
|
key: 'simulationLinkSpring',
|
|
@@ -33,7 +30,6 @@ const simulationSliders = [
|
|
|
33
30
|
min: 0,
|
|
34
31
|
max: 2,
|
|
35
32
|
step: 0.01,
|
|
36
|
-
default: 1,
|
|
37
33
|
},
|
|
38
34
|
{
|
|
39
35
|
key: 'simulationLinkDistance',
|
|
@@ -41,7 +37,6 @@ const simulationSliders = [
|
|
|
41
37
|
min: 1,
|
|
42
38
|
max: 20,
|
|
43
39
|
step: 1,
|
|
44
|
-
default: 10,
|
|
45
40
|
},
|
|
46
41
|
{
|
|
47
42
|
key: 'simulationFriction',
|
|
@@ -49,7 +44,6 @@ const simulationSliders = [
|
|
|
49
44
|
min: 0,
|
|
50
45
|
max: 1,
|
|
51
46
|
step: 0.01,
|
|
52
|
-
default: 0.85,
|
|
53
47
|
},
|
|
54
48
|
{
|
|
55
49
|
key: 'simulationDecay',
|
|
@@ -57,12 +51,11 @@ const simulationSliders = [
|
|
|
57
51
|
min: 100,
|
|
58
52
|
max: 10000,
|
|
59
53
|
step: 100,
|
|
60
|
-
default: 1000,
|
|
61
54
|
},
|
|
62
55
|
];
|
|
63
56
|
/**
|
|
64
57
|
* A component that provides fine-grained controls for adjusting graph simulation parameters.
|
|
65
|
-
*
|
|
58
|
+
* Uses the zustand store to access and control the graph state.
|
|
66
59
|
*
|
|
67
60
|
* Features:
|
|
68
61
|
* - Slider controls for all simulation parameters
|
|
@@ -85,11 +78,9 @@ const simulationSliders = [
|
|
|
85
78
|
*
|
|
86
79
|
* const MyGraph = () => {
|
|
87
80
|
* return (
|
|
88
|
-
* <div style={{ width: '800px', height: '600px' }}>
|
|
89
81
|
* <CosmosGraph {...graphProps}>
|
|
90
82
|
* <CosmosSimulationControls />
|
|
91
83
|
* </CosmosGraph>
|
|
92
|
-
* </div>
|
|
93
84
|
* );
|
|
94
85
|
* };
|
|
95
86
|
* ```
|
|
@@ -100,12 +91,10 @@ const simulationSliders = [
|
|
|
100
91
|
*
|
|
101
92
|
* const MyGraph = () => {
|
|
102
93
|
* return (
|
|
103
|
-
* <div style={{ width: '800px', height: '600px' }}>
|
|
104
94
|
* <CosmosGraph {...graphProps}>
|
|
105
95
|
* <CosmosGraphControls className="absolute top-4 left-4" />
|
|
106
96
|
* <CosmosSimulationControls className="absolute top-4 right-4" />
|
|
107
97
|
* </CosmosGraph>
|
|
108
|
-
* </div>
|
|
109
98
|
* );
|
|
110
99
|
* };
|
|
111
100
|
* ```
|
|
@@ -120,21 +109,11 @@ const simulationSliders = [
|
|
|
120
109
|
* ```
|
|
121
110
|
*/
|
|
122
111
|
export const CosmosSimulationControls = ({ className, }) => {
|
|
123
|
-
const {
|
|
124
|
-
const
|
|
125
|
-
key,
|
|
126
|
-
defaultValue,
|
|
127
|
-
])));
|
|
112
|
+
const { isSimulationRunning, toggleSimulation, startWithEnergy, updateSimulationConfig, } = useStoreWithCosmos((s) => s.cosmos);
|
|
113
|
+
const config = useStoreWithCosmos((s) => s.project.config.cosmos);
|
|
128
114
|
const handleParameterChange = (paramKey, value) => {
|
|
129
|
-
|
|
130
|
-
return;
|
|
131
|
-
const newValues = { ...values, [paramKey]: value[0] };
|
|
132
|
-
setValues(newValues);
|
|
133
|
-
const config = {
|
|
134
|
-
[paramKey]: value[0],
|
|
135
|
-
};
|
|
136
|
-
graphRef.current.setConfig(config);
|
|
115
|
+
updateSimulationConfig({ [paramKey]: value[0] });
|
|
137
116
|
};
|
|
138
|
-
return (_jsxs("div", { className: cn('
|
|
117
|
+
return (_jsxs("div", { className: cn('w-48 bg-card/90 dark:bg-card/90 rounded-lg shadow-lg p-3 space-y-4', className), children: [_jsxs("div", { className: "flex gap-2", children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { onClick: toggleSimulation, variant: "outline", size: "sm", className: "flex-1", children: isSimulationRunning ? (_jsx(Pause, { className: "h-4 w-4" })) : (_jsx(Play, { className: "h-4 w-4" })) }) }), _jsx(TooltipContent, { side: "bottom", children: isSimulationRunning ? 'Pause simulation' : 'Start simulation' })] }), _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { onClick: startWithEnergy, variant: "outline", size: "sm", className: "flex-1", children: _jsx(Wind, { className: "h-4 w-4" }) }) }), _jsx(TooltipContent, { side: "bottom", children: "Push energy into simulation" })] })] }), simulationSliders.map(({ key, label, min, max, step }) => (_jsxs("div", { className: "space-y-2", children: [_jsxs("div", { className: "flex items-center justify-between", children: [_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsxs(Label, { htmlFor: key, className: "text-xs font-medium flex items-center gap-1 cursor-help", children: [_jsx(Info, { className: "w-3 h-3 text-muted-foreground/50" }), label] }) }), _jsx(TooltipContent, { side: "left", className: "max-w-[200px]", children: CosmosSliceConfig.shape.cosmos.shape[key].description })] }), _jsx("span", { className: "text-xs tabular-nums text-muted-foreground", children: config[key].toFixed(2) })] }), _jsx(Slider, { id: key, min: min, max: max, step: step, value: [config[key]], onValueChange: (value) => handleParameterChange(key, value), className: "w-full" })] }, key)))] }));
|
|
139
118
|
};
|
|
140
119
|
//# sourceMappingURL=CosmosSimulationControls.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CosmosSimulationControls.js","sourceRoot":"","sources":["../src/CosmosSimulationControls.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,EAAE,EACF,MAAM,EACN,KAAK,EACL,OAAO,EACP,cAAc,EACd,cAAc,EACd,MAAM,GACP,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"CosmosSimulationControls.js","sourceRoot":"","sources":["../src/CosmosSimulationControls.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,EAAE,EACF,MAAM,EACN,KAAK,EACL,OAAO,EACP,cAAc,EACd,cAAc,EACd,MAAM,GACP,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAC,MAAM,cAAc,CAAC;AACrD,OAAO,EAAC,kBAAkB,EAAC,MAAM,eAAe,CAAC;AACjD,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAC;AAmBtD;;;;;GAKG;AACH,MAAM,iBAAiB,GAAG;IACxB;QACE,GAAG,EAAE,mBAAmB;QACxB,KAAK,EAAE,SAAS;QAChB,GAAG,EAAE,CAAC;QACN,GAAG,EAAE,GAAG;QACR,IAAI,EAAE,IAAI;KACX;IACD;QACE,GAAG,EAAE,qBAAqB;QAC1B,KAAK,EAAE,WAAW;QAClB,GAAG,EAAE,CAAC;QACN,GAAG,EAAE,CAAC;QACN,IAAI,EAAE,IAAI;KACX;IACD;QACE,GAAG,EAAE,sBAAsB;QAC3B,KAAK,EAAE,eAAe;QACtB,GAAG,EAAE,CAAC;QACN,GAAG,EAAE,CAAC;QACN,IAAI,EAAE,IAAI;KACX;IACD;QACE,GAAG,EAAE,wBAAwB;QAC7B,KAAK,EAAE,eAAe;QACtB,GAAG,EAAE,CAAC;QACN,GAAG,EAAE,EAAE;QACP,IAAI,EAAE,CAAC;KACR;IACD;QACE,GAAG,EAAE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GAAG,CAAC,CAAC,EACpB,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,qBAAqB,CAAC,GAAG,EAAE,KAAK,CAAC,EAC3D,SAAS,EAAC,QAAQ,GAClB,KA5BM,GAAG,CA6BP,CACP,CAAC,IACE,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {\n cn,\n Slider,\n Label,\n Tooltip,\n TooltipContent,\n TooltipTrigger,\n Button,\n} from '@sqlrooms/ui';\nimport {FC} from 'react';\nimport {Info, Pause, Play, Wind} from 'lucide-react';\nimport {useStoreWithCosmos} from './CosmosSlice';\nimport {CosmosSliceConfig} from './CosmosSliceConfig';\n\n/**\n * Props for the CosmosSimulationControls component.\n */\ninterface CosmosSimulationControlsProps {\n /**\n * Optional className to override the default positioning and styling of the controls container.\n * By default, controls are positioned at the top-right corner.\n *\n * @example\n * ```tsx\n * // Position controls at the bottom-right\n * <CosmosSimulationControls className=\"absolute bottom-4 right-4\" />\n * ```\n */\n className?: string;\n}\n\n/**\n * Configuration for each simulation parameter slider.\n * These values define the range and step size for each parameter.\n *\n * @internal\n */\nconst simulationSliders = [\n {\n key: 'simulationGravity',\n label: 'Gravity',\n min: 0,\n max: 0.5,\n step: 0.01,\n },\n {\n key: 'simulationRepulsion',\n label: 'Repulsion',\n min: 0,\n max: 2,\n step: 0.01,\n },\n {\n key: 'simulationLinkSpring',\n label: 'Link Strength',\n min: 0,\n max: 2,\n step: 0.01,\n },\n {\n key: 'simulationLinkDistance',\n label: 'Link Distance',\n min: 1,\n max: 20,\n step: 1,\n },\n {\n key: 'simulationFriction',\n label: 'Friction',\n min: 0,\n max: 1,\n step: 0.01,\n },\n {\n key: 'simulationDecay',\n label: 'Decay',\n min: 100,\n max: 10000,\n step: 100,\n },\n] as const;\n\ntype SimulationKey = (typeof simulationSliders)[number]['key'];\n\n/**\n * A component that provides fine-grained controls for adjusting graph simulation parameters.\n * Uses the zustand store to access and control the graph state.\n *\n * Features:\n * - Slider controls for all simulation parameters\n * - Real-time parameter adjustment\n * - Tooltips with parameter descriptions\n * - Customizable positioning\n * - Default values optimized for common use cases\n *\n * Available parameters:\n * - Gravity (0-0.5): Controls how strongly nodes are pulled toward the center\n * - Repulsion (0-2): Controls how strongly nodes push away from each other\n * - Link Strength (0-2): Controls the spring force between connected nodes\n * - Link Distance (1-20): Sets the natural length of links between nodes\n * - Friction (0-1): Controls how quickly node movement decays\n * - Decay (100-10000): Controls how quickly the simulation \"cools down\"\n *\n * @example Basic usage\n * ```tsx\n * import { CosmosGraph, CosmosSimulationControls } from '@sqlrooms/cosmos';\n *\n * const MyGraph = () => {\n * return (\n * <CosmosGraph {...graphProps}>\n * <CosmosSimulationControls />\n * </CosmosGraph>\n * );\n * };\n * ```\n *\n * @example Custom positioning with other controls\n * ```tsx\n * import { CosmosGraph, CosmosGraphControls, CosmosSimulationControls } from '@sqlrooms/cosmos';\n *\n * const MyGraph = () => {\n * return (\n * <CosmosGraph {...graphProps}>\n * <CosmosGraphControls className=\"absolute top-4 left-4\" />\n * <CosmosSimulationControls className=\"absolute top-4 right-4\" />\n * </CosmosGraph>\n * );\n * };\n * ```\n *\n * @example With custom styling\n * ```tsx\n * <CosmosGraph {...graphProps}>\n * <CosmosSimulationControls\n * className=\"absolute bottom-4 right-4 bg-opacity-75 backdrop-blur-sm\"\n * />\n * </CosmosGraph>\n * ```\n */\nexport const CosmosSimulationControls: FC<CosmosSimulationControlsProps> = ({\n className,\n}) => {\n const {\n isSimulationRunning,\n toggleSimulation,\n startWithEnergy,\n updateSimulationConfig,\n } = useStoreWithCosmos((s) => s.cosmos);\n\n const config = useStoreWithCosmos(\n (s) => s.project.config.cosmos,\n ) as CosmosSliceConfig['cosmos'];\n\n const handleParameterChange = (paramKey: SimulationKey, value: number[]) => {\n updateSimulationConfig({[paramKey]: value[0]});\n };\n\n return (\n <div\n className={cn(\n 'w-48 bg-card/90 dark:bg-card/90 rounded-lg shadow-lg p-3 space-y-4',\n className,\n )}\n >\n <div className=\"flex gap-2\">\n <Tooltip>\n <TooltipTrigger asChild>\n <Button\n onClick={toggleSimulation}\n variant=\"outline\"\n size=\"sm\"\n className=\"flex-1\"\n >\n {isSimulationRunning ? (\n <Pause className=\"h-4 w-4\" />\n ) : (\n <Play className=\"h-4 w-4\" />\n )}\n </Button>\n </TooltipTrigger>\n <TooltipContent side=\"bottom\">\n {isSimulationRunning ? 'Pause simulation' : 'Start simulation'}\n </TooltipContent>\n </Tooltip>\n <Tooltip>\n <TooltipTrigger asChild>\n <Button\n onClick={startWithEnergy}\n variant=\"outline\"\n size=\"sm\"\n className=\"flex-1\"\n >\n <Wind className=\"h-4 w-4\" />\n </Button>\n </TooltipTrigger>\n <TooltipContent side=\"bottom\">\n Push energy into simulation\n </TooltipContent>\n </Tooltip>\n </div>\n {simulationSliders.map(({key, label, min, max, step}) => (\n <div key={key} className=\"space-y-2\">\n <div className=\"flex items-center justify-between\">\n <Tooltip>\n <TooltipTrigger asChild>\n <Label\n htmlFor={key}\n className=\"text-xs font-medium flex items-center gap-1 cursor-help\"\n >\n <Info className=\"w-3 h-3 text-muted-foreground/50\" />\n {label}\n </Label>\n </TooltipTrigger>\n <TooltipContent side=\"left\" className=\"max-w-[200px]\">\n {CosmosSliceConfig.shape.cosmos.shape[key].description}\n </TooltipContent>\n </Tooltip>\n <span className=\"text-xs tabular-nums text-muted-foreground\">\n {config[key].toFixed(2)}\n </span>\n </div>\n <Slider\n id={key}\n min={min}\n max={max}\n step={step}\n value={[config[key]]}\n onValueChange={(value) => handleParameterChange(key, value)}\n className=\"w-full\"\n />\n </div>\n ))}\n </div>\n );\n};\n"]}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Cosmos graph visualization state management using Zustand.
|
|
3
|
+
* This module provides state management and control functions for the Cosmos graph visualization.
|
|
4
|
+
*/
|
|
5
|
+
import { Graph, GraphConfigInterface } from '@cosmograph/cosmos';
|
|
6
|
+
import { type ProjectState } from '@sqlrooms/project-builder';
|
|
7
|
+
import type { BaseProjectConfig } from '@sqlrooms/project-config';
|
|
8
|
+
import type { StateCreator } from 'zustand';
|
|
9
|
+
import { CosmosSliceConfig } from './CosmosSliceConfig';
|
|
10
|
+
/**
|
|
11
|
+
* Core state interface for the Cosmos graph visualization.
|
|
12
|
+
* Contains the graph instance, simulation state, and all control functions.
|
|
13
|
+
*/
|
|
14
|
+
export type CosmosSliceState = {
|
|
15
|
+
cosmos: {
|
|
16
|
+
/** The current graph instance */
|
|
17
|
+
graph: Graph | null;
|
|
18
|
+
/** Whether the physics simulation is currently running */
|
|
19
|
+
isSimulationRunning: boolean;
|
|
20
|
+
/** Creates a new graph instance in the specified container */
|
|
21
|
+
createGraph: (container: HTMLDivElement) => void;
|
|
22
|
+
/** Toggles the physics simulation on/off */
|
|
23
|
+
toggleSimulation: () => void;
|
|
24
|
+
/** Adjusts the view to fit all nodes */
|
|
25
|
+
fitView: () => void;
|
|
26
|
+
/** Starts the simulation with initial energy */
|
|
27
|
+
startWithEnergy: () => void;
|
|
28
|
+
/** Cleans up and removes the current graph */
|
|
29
|
+
destroyGraph: () => void;
|
|
30
|
+
/** Updates the simulation configuration parameters */
|
|
31
|
+
updateSimulationConfig: (config: Partial<CosmosSliceConfig['cosmos']>) => void;
|
|
32
|
+
/** Updates the graph's visual configuration */
|
|
33
|
+
updateGraphConfig: (config: Partial<GraphConfigInterface>) => void;
|
|
34
|
+
/** Updates the graph's data (points, links, colors, etc.) */
|
|
35
|
+
updateGraphData: (data: {
|
|
36
|
+
pointPositions?: Float32Array;
|
|
37
|
+
pointColors?: Float32Array;
|
|
38
|
+
pointSizes?: Float32Array;
|
|
39
|
+
linkIndexes?: Float32Array;
|
|
40
|
+
linkColors?: Float32Array;
|
|
41
|
+
}) => void;
|
|
42
|
+
/** Sets the currently focused point by its index */
|
|
43
|
+
setFocusedPoint: (index: number | undefined) => void;
|
|
44
|
+
/** Sets the zoom level of the graph view */
|
|
45
|
+
setZoomLevel: (level: number) => void;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Combined type representing the full project state including Cosmos functionality.
|
|
50
|
+
* Merges the base project state with Cosmos-specific state and configuration.
|
|
51
|
+
*/
|
|
52
|
+
export type ProjectStateWithCosmos = ProjectState<BaseProjectConfig & CosmosSliceConfig> & CosmosSliceState;
|
|
53
|
+
/**
|
|
54
|
+
* Creates a Zustand slice for managing Cosmos graph state.
|
|
55
|
+
* This slice handles graph creation, destruction, configuration, and data updates.
|
|
56
|
+
*
|
|
57
|
+
* @returns A state creator function for the Cosmos slice
|
|
58
|
+
*/
|
|
59
|
+
export declare function createCosmosSlice(): StateCreator<CosmosSliceState>;
|
|
60
|
+
/**
|
|
61
|
+
* Hook to access the Cosmos store with proper typing.
|
|
62
|
+
* Provides type-safe access to the combined project and Cosmos state.
|
|
63
|
+
*
|
|
64
|
+
* @template T The type of the selected state slice
|
|
65
|
+
* @param selector A function that selects a portion of the state
|
|
66
|
+
* @returns The selected state portion
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const graph = useStoreWithCosmos(state => state.cosmos.graph);
|
|
71
|
+
* const isRunning = useStoreWithCosmos(state => state.cosmos.isSimulationRunning);
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function useStoreWithCosmos<T>(selector: (state: ProjectStateWithCosmos) => T): T;
|
|
75
|
+
//# sourceMappingURL=CosmosSlice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CosmosSlice.d.ts","sourceRoot":"","sources":["../src/CosmosSlice.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAC,KAAK,EAAE,oBAAoB,EAAC,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAChE,OAAO,K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AAM,MAAM,sBAAsB,GAAG,YAAY,CAC/C,iBAAiB,GAAG,iBAAiB,CACtC,GACC,gBAAgB,CAAC;AAEnB;;;;;GAKG;AACH,wBAAgB,iBAAiB,IAAI,YAAY,CAAC,gBAAgB,CAAC,CAoJlE;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,QAAQ,EAAE,CAAC,KAAK,EAAE,sBAAsB,KAAK,CAAC,GAC7C,CAAC,CAMH"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Cosmos graph visualization state management using Zustand.
|
|
3
|
+
* This module provides state management and control functions for the Cosmos graph visualization.
|
|
4
|
+
*/
|
|
5
|
+
import { Graph } from '@cosmograph/cosmos';
|
|
6
|
+
import { createSlice, useBaseProjectStore, } from '@sqlrooms/project-builder';
|
|
7
|
+
import { produce } from 'immer';
|
|
8
|
+
/**
|
|
9
|
+
* Creates a Zustand slice for managing Cosmos graph state.
|
|
10
|
+
* This slice handles graph creation, destruction, configuration, and data updates.
|
|
11
|
+
*
|
|
12
|
+
* @returns A state creator function for the Cosmos slice
|
|
13
|
+
*/
|
|
14
|
+
export function createCosmosSlice() {
|
|
15
|
+
return createSlice((set, get) => ({
|
|
16
|
+
cosmos: {
|
|
17
|
+
graph: null,
|
|
18
|
+
isSimulationRunning: true,
|
|
19
|
+
createGraph: (container) => {
|
|
20
|
+
// Clean up old graph if it exists
|
|
21
|
+
const oldGraph = get().cosmos.graph;
|
|
22
|
+
if (oldGraph) {
|
|
23
|
+
oldGraph.pause();
|
|
24
|
+
oldGraph.destroy();
|
|
25
|
+
}
|
|
26
|
+
// Create and configure new graph
|
|
27
|
+
const graph = new Graph(container);
|
|
28
|
+
const config = get().project.config.cosmos;
|
|
29
|
+
graph.setConfig(config);
|
|
30
|
+
graph.start();
|
|
31
|
+
set((state) => produce(state, (draft) => {
|
|
32
|
+
draft.cosmos.graph = graph;
|
|
33
|
+
}));
|
|
34
|
+
},
|
|
35
|
+
toggleSimulation: () => {
|
|
36
|
+
const { graph } = get().cosmos;
|
|
37
|
+
if (!graph)
|
|
38
|
+
return;
|
|
39
|
+
if (graph.isSimulationRunning) {
|
|
40
|
+
graph.pause();
|
|
41
|
+
set((state) => produce(state, (draft) => {
|
|
42
|
+
draft.cosmos.isSimulationRunning = false;
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
graph.restart();
|
|
47
|
+
set((state) => produce(state, (draft) => {
|
|
48
|
+
draft.cosmos.isSimulationRunning = true;
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
fitView: () => {
|
|
53
|
+
const { graph } = get().cosmos;
|
|
54
|
+
if (!graph)
|
|
55
|
+
return;
|
|
56
|
+
graph.fitView();
|
|
57
|
+
},
|
|
58
|
+
startWithEnergy: () => {
|
|
59
|
+
const { graph } = get().cosmos;
|
|
60
|
+
if (!graph)
|
|
61
|
+
return;
|
|
62
|
+
graph.start(1);
|
|
63
|
+
set((state) => produce(state, (draft) => {
|
|
64
|
+
draft.cosmos.isSimulationRunning = true;
|
|
65
|
+
}));
|
|
66
|
+
},
|
|
67
|
+
updateSimulationConfig: (config) => {
|
|
68
|
+
const { graph } = get().cosmos;
|
|
69
|
+
set((state) => produce(state, (draft) => {
|
|
70
|
+
Object.assign(draft.project.config.cosmos, config);
|
|
71
|
+
if (graph) {
|
|
72
|
+
graph.setConfig(draft.project.config.cosmos);
|
|
73
|
+
}
|
|
74
|
+
}));
|
|
75
|
+
},
|
|
76
|
+
updateGraphConfig: (config) => {
|
|
77
|
+
const { graph } = get().cosmos;
|
|
78
|
+
set((state) => produce(state, (draft) => {
|
|
79
|
+
Object.assign(draft.project.config.cosmos, config);
|
|
80
|
+
if (graph) {
|
|
81
|
+
graph.setConfig(draft.project.config.cosmos);
|
|
82
|
+
}
|
|
83
|
+
}));
|
|
84
|
+
},
|
|
85
|
+
updateGraphData: (data) => {
|
|
86
|
+
const { graph } = get().cosmos;
|
|
87
|
+
if (!graph)
|
|
88
|
+
return;
|
|
89
|
+
if (data.pointPositions) {
|
|
90
|
+
graph.setPointPositions(data.pointPositions);
|
|
91
|
+
}
|
|
92
|
+
if (data.pointColors) {
|
|
93
|
+
graph.setPointColors(data.pointColors);
|
|
94
|
+
}
|
|
95
|
+
if (data.pointSizes) {
|
|
96
|
+
graph.setPointSizes(data.pointSizes);
|
|
97
|
+
}
|
|
98
|
+
if (data.linkIndexes) {
|
|
99
|
+
graph.setLinks(data.linkIndexes);
|
|
100
|
+
}
|
|
101
|
+
if (data.linkColors) {
|
|
102
|
+
graph.setLinkColors(data.linkColors);
|
|
103
|
+
}
|
|
104
|
+
graph.render();
|
|
105
|
+
},
|
|
106
|
+
setFocusedPoint: (index) => {
|
|
107
|
+
const { graph } = get().cosmos;
|
|
108
|
+
if (!graph)
|
|
109
|
+
return;
|
|
110
|
+
graph.setFocusedPointByIndex(index);
|
|
111
|
+
},
|
|
112
|
+
setZoomLevel: (level) => {
|
|
113
|
+
const { graph } = get().cosmos;
|
|
114
|
+
if (!graph)
|
|
115
|
+
return;
|
|
116
|
+
graph.setZoomLevel(level);
|
|
117
|
+
},
|
|
118
|
+
destroyGraph: () => {
|
|
119
|
+
const { graph } = get().cosmos;
|
|
120
|
+
if (!graph)
|
|
121
|
+
return;
|
|
122
|
+
// TODO: this should be happening in cosmos
|
|
123
|
+
if (graph.store.div?.firstChild) {
|
|
124
|
+
graph.store.div.innerHTML = '';
|
|
125
|
+
}
|
|
126
|
+
graph.pause();
|
|
127
|
+
graph.destroy();
|
|
128
|
+
set((state) => produce(state, (draft) => {
|
|
129
|
+
draft.cosmos.graph = null;
|
|
130
|
+
draft.cosmos.isSimulationRunning = false;
|
|
131
|
+
}));
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Hook to access the Cosmos store with proper typing.
|
|
138
|
+
* Provides type-safe access to the combined project and Cosmos state.
|
|
139
|
+
*
|
|
140
|
+
* @template T The type of the selected state slice
|
|
141
|
+
* @param selector A function that selects a portion of the state
|
|
142
|
+
* @returns The selected state portion
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const graph = useStoreWithCosmos(state => state.cosmos.graph);
|
|
147
|
+
* const isRunning = useStoreWithCosmos(state => state.cosmos.isSimulationRunning);
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export function useStoreWithCosmos(selector) {
|
|
151
|
+
return useBaseProjectStore((state) => selector(state));
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=CosmosSlice.js.map
|