Skip to content

Fix: Simplify the code to resize a node #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 44 additions & 27 deletions ui/src/components/repo/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ReactFlow, {
MiniMap,
Controls,
Handle,
useReactFlow
} from "react-flow-renderer";
import Box from "@mui/material/Box";
import CircularProgress from "@mui/material/CircularProgress";
Expand Down Expand Up @@ -48,6 +49,25 @@ const ScopeNode = memo(({ data, id, isConnectable, selected }) => {
const [frame] = React.useState({
translate: [0, 0],
});
const { setNodes } = useReactFlow();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know this; this is convenient.


const onResize = useCallback(
({width, height, offx, offy }) => {
setNodes((nds) => {
return nds.map((node) => {
if (node.id === id) {
// CAUTION I have to return a new object here.
node.style = { ...node.style, width, height };
node.position.x += offx;
node.position.y += offy;
}
return node;
});
});
},
[setNodes]
);

React.useEffect(() => {
setTarget(ref.current);
}, []);
Expand Down Expand Up @@ -85,7 +105,7 @@ const ScopeNode = memo(({ data, id, isConnectable, selected }) => {
e.target.style.width = `${e.width}px`;
e.target.style.height = `${e.height}px`;
e.target.style.transform = `translate(${beforeTranslate[0]}px, ${beforeTranslate[1]}px)`;
data.onResize({
onResize({
id,
width: e.width,
height: e.height,
Expand Down Expand Up @@ -119,6 +139,25 @@ const CodeNode = memo(({ data, id, isConnectable, selected }) => {
const [frame] = React.useState({
translate: [0, 0],
});
const { setNodes } = useReactFlow();

const onResize = useCallback(
({width, height, offx, offy }) => {
setNodes((nds) => {
return nds.map((node) => {
if (node.id === id) {
// CAUTION I have to return a new object here.
node.style = { ...node.style, width, height };
node.position.x += offx;
node.position.y += offy;
}
return node;
});
});
},
[setNodes]
);

React.useEffect(() => {
setTarget(ref.current);
}, []);
Expand All @@ -141,7 +180,7 @@ const CodeNode = memo(({ data, id, isConnectable, selected }) => {
onClick={(e) => {
// If the node is selected (for resize), the cursor is not shown. So
// we need to deselect it when we re-focus on the editor.
data.setNodes((nds) =>
setNodes((nds) =>
applyNodeChanges(
[
{
Expand Down Expand Up @@ -274,8 +313,7 @@ const CodeNode = memo(({ data, id, isConnectable, selected }) => {
e.target.style.width = `${e.width}px`;
e.target.style.height = `${e.height}px`;
e.target.style.transform = `translate(${beforeTranslate[0]}px, ${beforeTranslate[1]}px)`;
data.onResize({
id,
onResize({
width: e.width,
height: e.height,
offx: beforeTranslate[0],
Expand Down Expand Up @@ -317,23 +355,6 @@ export function Deck({ props }) {
const id2children = useStore(store, (state) => state.id2children);
const pods = useStore(store, (state) => state.pods);

const onResize = useCallback(
({ id, width, height, offx, offy }) => {
setNodes((nds) => {
return nds.map((node) => {
if (node.id === id) {
// CAUTION I have to return a new object here.
node.style = { ...node.style, width, height };
node.position.x += offx;
node.position.y += offy;
}
return node;
});
});
},
[setNodes]
);

const getRealNodes = useCallback(
(id, level) => {
let res = [];
Expand All @@ -345,8 +366,6 @@ export function Deck({ props }) {
data: {
// label: `ID: ${id}, parent: ${pods[id].parent}, pos: ${pods[id].x}, ${pods[id].y}`,
label: id,
onResize,
setNodes,
},
// position: { x: 100, y: 100 },
position: { x: pods[id].x, y: pods[id].y },
Expand All @@ -369,7 +388,7 @@ export function Deck({ props }) {
}
return res;
},
[id2children, onResize, pods]
[id2children, pods]
);
useEffect(() => {
let nodes = getRealNodes("ROOT", -1);
Expand Down Expand Up @@ -431,8 +450,6 @@ export function Deck({ props }) {
style,
data: {
label: id,
onResize,
setNodes,
},
level: 0,
extent: "parent",
Expand All @@ -454,7 +471,7 @@ export function Deck({ props }) {
height: style.height,
});
},
[addPod, onResize, reactFlowInstance]
[addPod, reactFlowInstance]
);

const getAbsPos = useCallback(
Expand Down