Skip to content
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

fix: Switch to using single viewbox size [PT-187321121] #51

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions src/components/drawing.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from "react";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { nanoid } from "nanoid";

import { DrawingMode, Graph, Point, RubberBand } from "./graph";
Expand Down Expand Up @@ -36,18 +36,25 @@
const [firstEdgeNode, setFirstEdgeNode] = useState<Node|undefined>(undefined);
const [rubberBand, setRubberBand] = useState<RubberBand|undefined>(undefined);
const [selectedNodeForModal, setSelectedNodeForModal] = useState<Node|undefined>(undefined);
const widthRef = useRef(0);
const heightRef = useRef(0);

const setSelectedNodeId = useCallback((id?: string, skipToggle?: boolean) => {
if (drawingMode === "select") {
_setSelectedNodeId(id, skipToggle);
}
}, [drawingMode, _setSelectedNodeId]);

const handleDimensionChange = ({width, height}: {width: number, height: number}) => {
widthRef.current = width;
heightRef.current = height;
};

const translateToGraphPoint = (e: MouseEvent|React.MouseEvent<HTMLDivElement>): Point => {
// the offsets were determined visually to put the state centered on the mouse
return {
x: e.clientX - 50,
y: e.clientY - 12,
x: e.clientX - 50 - (widthRef.current / 2),
y: e.clientY - 10 - (heightRef.current / 2),
};
};

Expand Down Expand Up @@ -99,7 +106,7 @@
*/

const addNode = useCallback(({x, y}: {x: number, y: number}) => {
console.log("ADD NODE");

Check warning on line 109 in src/components/drawing.tsx

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement

Check warning on line 109 in src/components/drawing.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
setGraph(prev => {
const id = nanoid();
const label = `State ${prev.nodes.length + 1}`;
Expand Down Expand Up @@ -271,6 +278,7 @@
onEdgeClick={handleEdgeClicked}
onDragStop={handleDragStop}
setSelectedNodeId={setSelectedNodeId}
onDimensions={handleDimensionChange}
/>
<DragIcon drawingMode={drawingMode} />
<NodeModal
Expand Down
14 changes: 7 additions & 7 deletions src/components/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Props = {
onNodeDoubleClick?: (id: string) => void;
onEdgeClick?: (options: {from: string, to: string}) => void;
onDragStop?: (id: string, pos: Point) => void;
onDimensions?: (dimensions: {width: number, height: number}) => void;
setSelectedNodeId: (id?: string, skipToggle?: boolean) => void;
};

Expand Down Expand Up @@ -234,9 +235,9 @@ const lineDashArray = (edge: D3Edge) => edge.value ? "" : "4";

export const Graph = (props: Props) => {
const {graph, highlightNode, highlightLoopOnNode, highlightEdge, highlightAllNextNodes,
allowDragging, autoArrange, mode, rubberBand, drawingMode,
allowDragging, autoArrange, rubberBand, drawingMode,
onClick, onNodeClick, onNodeDoubleClick, onEdgeClick, onDragStop,
selectedNodeId, setSelectedNodeId, animating} = props;
selectedNodeId, setSelectedNodeId, animating, onDimensions} = props;
const svgRef = useRef<SVGSVGElement | null>(null);
const wrapperRef = useRef<HTMLDivElement | null>(null);
const dimensions = useResizeObserver(wrapperRef);
Expand Down Expand Up @@ -317,9 +318,10 @@ export const Graph = (props: Props) => {
useEffect(() => {
if (dimensions) {
setWidth(dimensions.width);
setHeight(dimensions.height - 5);
setHeight(dimensions.height);
onDimensions?.({width: dimensions.width, height: dimensions.height});
}
}, [dimensions]);
}, [dimensions, onDimensions]);

// create the d3 graph info
useEffect(() => {
Expand Down Expand Up @@ -759,14 +761,12 @@ export const Graph = (props: Props) => {
}
}, [autoArrange, onClick]);

const viewBox = mode === "drawing" ? `0 0 ${width} ${height}` : `${-width / 2} ${-height / 2} ${width} ${height}`;

return (
<div className="graph" ref={wrapperRef} onClick={handleClick}>
<svg
width="100%"
height="calc(100vh - 20px)"
viewBox={viewBox}
viewBox={`${-width / 2} ${-height / 2} ${width} ${height}`}
ref={svgRef}
/>
</div>
Expand Down
Loading