Skip to content

Commit

Permalink
fix: Switch to using single viewbox size [PT-187321121]
Browse files Browse the repository at this point in the history
This is needed for text input in drawing mode so the auto layout works.  The drawing mode was using a viewport centered at the top left, now it uses a viewport centered in the center of the graph like the dataset mode.
  • Loading branch information
dougmartin committed Apr 7, 2024
1 parent cce5db7 commit 6cb74bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
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 @@ export const Drawing = (props: Props) => {
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 @@ -271,6 +278,7 @@ export const Drawing = (props: Props) => {
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

0 comments on commit 6cb74bf

Please sign in to comment.