in src/state/layout/layoutReducer.ts [10:50]
export function adjustWorkspace<NodeType, EdgeType>(
state: DiagramMakerData<NodeType, EdgeType>
): DiagramMakerData<NodeType, EdgeType> {
const nodes = state.nodes;
const workspaceSize = state.workspace.canvasSize;
let rightMost = workspaceSize.width;
let bottomMost = workspaceSize.height;
let leftMost = 0;
let topMost = 0;
const nodeIds = Object.keys(nodes);
nodeIds.forEach((nodeId) => {
const node = nodes[nodeId];
const position = node.diagramMakerData.position;
const size = node.diagramMakerData.size;
rightMost = Math.max(position.x + size.width, rightMost);
bottomMost = Math.max(position.y + size.height, bottomMost);
leftMost = Math.min(position.x, leftMost);
topMost = Math.min(position.y, topMost);
});
const width = rightMost - leftMost;
const height = bottomMost - topMost;
return produce(state, (draftState) => {
draftState.workspace.canvasSize.width = width;
draftState.workspace.canvasSize.height = height;
// move all nodes if node beyond top left boundary
if (leftMost < 0 || topMost < 0) {
nodeIds.forEach((nodeId) => {
const oldPos = draftState.nodes[nodeId].diagramMakerData.position;
const position = {
x: oldPos.x - leftMost,
y: oldPos.y - topMost
};
draftState.nodes[nodeId].diagramMakerData.position = position;
});
}
});
}