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

Commit

Permalink
fix(Visualization): position after dropping the first node. (#1139)
Browse files Browse the repository at this point in the history
* Revert "fix(Visualization): Initial node isn't always centered (#1010)"

This reverts commit a408a13.

* fix(Visualisation): tweak centering of the initial node
fix #1096
  • Loading branch information
mmelko authored Jan 20, 2023
1 parent a507c7f commit 23610d2
Showing 1 changed file with 8 additions and 43 deletions.
51 changes: 8 additions & 43 deletions src/components/Visualization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
findStepIdxWithUUID,
flattenSteps,
getLayoutedElements,
containsAddStepPlaceholder,
filterStepWithBranches,
} from '@kaoto/services';
import { useIntegrationJsonStore, useNestedStepsStore, useVisualizationStore } from '@kaoto/store';
Expand All @@ -31,15 +30,16 @@ interface IVisualization {
const Visualization = ({ toggleCatalog }: IVisualization) => {
// `nodes` is an array of UI-specific objects that represent
// the Integration.Steps model visually, while `edges` connect them

const defaultViewport: Viewport = {
x: 0,
y: 0,
// 80/2 means half of the size of the icon so the placeholder icon can be centered
x: window.innerWidth / 2 - 80 / 2,
y: (window.innerHeight - 77) / 2 - 80 / 2,
zoom: 1.2,
};

const [isPanelExpanded, setIsPanelExpanded] = useState(false);
const [, setReactFlowInstance] = useState(null);
const reactFlowWrapperRef = useRef<HTMLDivElement>(null);
const reactFlowWrapper = useRef(null);
const [selectedStep, setSelectedStep] = useState<IStepProps>({
maxBranches: 0,
minBranches: 0,
Expand All @@ -56,42 +56,7 @@ const Visualization = ({ toggleCatalog }: IVisualization) => {
const { nodes, setNodes, onNodesChange, edges, setEdges, onEdgesChange, deleteNode } =
useVisualizationStore();

/**
* Center first node if it is the initial `add a step `
* node into react flow viewport.
*/
useEffect(() => {
const isAddStepPlaceholder = containsAddStepPlaceholder(nodes);

if (isAddStepPlaceholder) {
const reactFlowWrapper = reactFlowWrapperRef.current;

let reactFlowWrapperRect;

if (reactFlowWrapper) {
reactFlowWrapperRect = reactFlowWrapper.getBoundingClientRect();
}

if (
nodes[0]?.width &&
nodes[0]?.height &&
reactFlowWrapperRect?.width &&
reactFlowWrapperRect?.height
) {
const firstNodeWidth = nodes[0].width;
const firstNodeHeight = nodes[0].height;
const reactFlowWrapperRectWidth = reactFlowWrapperRect.width;
const reactFlowWrapperRectHeight = reactFlowWrapperRect.height;

nodes[0].position.x = (reactFlowWrapperRectWidth / 2 - firstNodeWidth / 2) * 0.8;
nodes[0].position.y = (reactFlowWrapperRectHeight / 2 - firstNodeHeight / 2) * 0.8;
}
}
});

/**
* Initial loading of visualization steps
*/
// initial loading of visualization steps
useEffect(() => {
const { stepNodes, stepEdges } = buildNodesAndEdges(integrationJson.steps);

Expand Down Expand Up @@ -295,7 +260,7 @@ const Visualization = ({ toggleCatalog }: IVisualization) => {
<div
className="reactflow-wrapper"
data-testid={'react-flow-wrapper'}
ref={reactFlowWrapperRef}
ref={reactFlowWrapper}
style={{
width: window.innerWidth,
height: window.innerHeight - 77,
Expand All @@ -304,8 +269,8 @@ const Visualization = ({ toggleCatalog }: IVisualization) => {
<ReactFlow
nodes={nodes}
edges={edges}
edgeTypes={edgeTypes}
defaultViewport={defaultViewport}
edgeTypes={edgeTypes}
nodeTypes={nodeTypes}
onDragOver={onDragOver}
onNodeClick={onNodeClick}
Expand Down

0 comments on commit 23610d2

Please sign in to comment.