Skip to content

Commit 9ee122b

Browse files
committed
feat: edge click focus to connected node
1 parent 89c29e9 commit 9ee122b

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/features/editor/views/GraphView/CustomEdge/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@ import React from "react";
22
import { useComputedColorScheme } from "@mantine/core";
33
import type { EdgeProps } from "reaflow";
44
import { Edge } from "reaflow";
5+
import useGraph from "../stores/useGraph";
56

67
const CustomEdgeWrapper = (props: EdgeProps) => {
78
const colorScheme = useComputedColorScheme();
9+
const viewPort = useGraph(state => state.viewPort);
10+
const targetNodeId = props.id.split("-")[props.id.split("-").length - 1];
11+
const [hovered, setHovered] = React.useState(false);
12+
13+
const handeClick = () => {
14+
const targetNode = document.querySelector(`[data-id="node-${targetNodeId}"]`) as HTMLElement;
15+
if (targetNode && targetNode.parentElement) {
16+
viewPort?.camera.centerFitElementIntoView(targetNode.parentElement, {
17+
elementExtraMarginForZoom: 150,
18+
});
19+
}
20+
};
821

922
return (
1023
<Edge
1124
containerClassName={`edge-${props.id}`}
25+
onClick={handeClick}
26+
onEnter={() => setHovered(true)}
27+
onLeave={() => setHovered(false)}
1228
style={{
1329
stroke: colorScheme === "dark" ? "#444444" : "#BCBEC0",
30+
...(hovered && { stroke: "#3B82F6" }),
1431
strokeWidth: 1.5,
1532
}}
1633
{...props}

src/features/editor/views/GraphView/CustomNode/ObjectNode.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ const Row = ({ val, x, y, index }: RowProps) => {
2929
};
3030

3131
const Node = ({ node, x, y }: CustomNodeProps) => (
32-
<Styled.StyledForeignObject width={node.width} height={node.height} x={0} y={0} $isObject>
32+
<Styled.StyledForeignObject
33+
data-id={`node-${node.id}`}
34+
width={node.width}
35+
height={node.height}
36+
x={0}
37+
y={0}
38+
$isObject
39+
>
3340
{(node.text as Value[]).map((val, idx) => (
3441
<Row val={val} index={idx} x={x} y={y} key={idx} />
3542
))}

src/features/editor/views/GraphView/CustomNode/TextNode.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ const Node = ({ node, x, y, hasCollapse = false }: CustomNodeProps) => {
7979
}, [childrenCount, type]);
8080

8181
return (
82-
<Styled.StyledForeignObject width={width} height={height} x={0} y={0}>
82+
<Styled.StyledForeignObject
83+
data-id={`node-${node.id}`}
84+
width={width}
85+
height={height}
86+
x={0}
87+
y={0}
88+
>
8389
{isImage ? (
8490
<StyledImageWrapper>
8591
<StyledImage src={text as string} width="70" height="70" loading="lazy" />

src/features/editor/views/GraphView/CustomNode/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const CustomNodeWrapper = (nodeProps: NodeProps<NodeData["data"]>) => {
5050
style={{
5151
fill: colorScheme === "dark" ? "#292929" : "#ffffff",
5252
stroke: colorScheme === "dark" ? "#424242" : "#BCBEC0",
53-
strokeWidth: 1.5,
53+
strokeWidth: 1,
5454
}}
5555
>
5656
{({ node, x, y }) => {

0 commit comments

Comments
 (0)