Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 23610d2

Browse files
authored
fix(Visualization): position after dropping the first node. (#1139)
* Revert "fix(Visualization): Initial node isn't always centered (#1010)" This reverts commit a408a13. * fix(Visualisation): tweak centering of the initial node fix #1096
1 parent a507c7f commit 23610d2

File tree

1 file changed

+8
-43
lines changed

1 file changed

+8
-43
lines changed

src/components/Visualization.tsx

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
findStepIdxWithUUID,
1616
flattenSteps,
1717
getLayoutedElements,
18-
containsAddStepPlaceholder,
1918
filterStepWithBranches,
2019
} from '@kaoto/services';
2120
import { useIntegrationJsonStore, useNestedStepsStore, useVisualizationStore } from '@kaoto/store';
@@ -31,15 +30,16 @@ interface IVisualization {
3130
const Visualization = ({ toggleCatalog }: IVisualization) => {
3231
// `nodes` is an array of UI-specific objects that represent
3332
// the Integration.Steps model visually, while `edges` connect them
33+
3434
const defaultViewport: Viewport = {
35-
x: 0,
36-
y: 0,
35+
// 80/2 means half of the size of the icon so the placeholder icon can be centered
36+
x: window.innerWidth / 2 - 80 / 2,
37+
y: (window.innerHeight - 77) / 2 - 80 / 2,
3738
zoom: 1.2,
3839
};
39-
4040
const [isPanelExpanded, setIsPanelExpanded] = useState(false);
4141
const [, setReactFlowInstance] = useState(null);
42-
const reactFlowWrapperRef = useRef<HTMLDivElement>(null);
42+
const reactFlowWrapper = useRef(null);
4343
const [selectedStep, setSelectedStep] = useState<IStepProps>({
4444
maxBranches: 0,
4545
minBranches: 0,
@@ -56,42 +56,7 @@ const Visualization = ({ toggleCatalog }: IVisualization) => {
5656
const { nodes, setNodes, onNodesChange, edges, setEdges, onEdgesChange, deleteNode } =
5757
useVisualizationStore();
5858

59-
/**
60-
* Center first node if it is the initial `add a step `
61-
* node into react flow viewport.
62-
*/
63-
useEffect(() => {
64-
const isAddStepPlaceholder = containsAddStepPlaceholder(nodes);
65-
66-
if (isAddStepPlaceholder) {
67-
const reactFlowWrapper = reactFlowWrapperRef.current;
68-
69-
let reactFlowWrapperRect;
70-
71-
if (reactFlowWrapper) {
72-
reactFlowWrapperRect = reactFlowWrapper.getBoundingClientRect();
73-
}
74-
75-
if (
76-
nodes[0]?.width &&
77-
nodes[0]?.height &&
78-
reactFlowWrapperRect?.width &&
79-
reactFlowWrapperRect?.height
80-
) {
81-
const firstNodeWidth = nodes[0].width;
82-
const firstNodeHeight = nodes[0].height;
83-
const reactFlowWrapperRectWidth = reactFlowWrapperRect.width;
84-
const reactFlowWrapperRectHeight = reactFlowWrapperRect.height;
85-
86-
nodes[0].position.x = (reactFlowWrapperRectWidth / 2 - firstNodeWidth / 2) * 0.8;
87-
nodes[0].position.y = (reactFlowWrapperRectHeight / 2 - firstNodeHeight / 2) * 0.8;
88-
}
89-
}
90-
});
91-
92-
/**
93-
* Initial loading of visualization steps
94-
*/
59+
// initial loading of visualization steps
9560
useEffect(() => {
9661
const { stepNodes, stepEdges } = buildNodesAndEdges(integrationJson.steps);
9762

@@ -295,7 +260,7 @@ const Visualization = ({ toggleCatalog }: IVisualization) => {
295260
<div
296261
className="reactflow-wrapper"
297262
data-testid={'react-flow-wrapper'}
298-
ref={reactFlowWrapperRef}
263+
ref={reactFlowWrapper}
299264
style={{
300265
width: window.innerWidth,
301266
height: window.innerHeight - 77,
@@ -304,8 +269,8 @@ const Visualization = ({ toggleCatalog }: IVisualization) => {
304269
<ReactFlow
305270
nodes={nodes}
306271
edges={edges}
307-
edgeTypes={edgeTypes}
308272
defaultViewport={defaultViewport}
273+
edgeTypes={edgeTypes}
309274
nodeTypes={nodeTypes}
310275
onDragOver={onDragOver}
311276
onNodeClick={onNodeClick}

0 commit comments

Comments
 (0)