Skip to content

Commit

Permalink
fix: Reset button resets the zoom [PT-187399412]
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmartin committed Apr 9, 2024
1 parent 5f0c584 commit 474a6cb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const App = () => {
const [initialGraph, setInitialGraph] = useState<GraphData>();
const [fitViewAt, setFitViewAt] = useState<number>();
const [recenterViewAt, setRecenterViewAt] = useState<number>();
const [resetZoomAt, setResetZoomAt] = useState<number>();
const onCODAPDataChanged = (values: string[]) => {
updateGraph(values);
setFitViewAt(Date.now());
Expand Down Expand Up @@ -473,6 +474,7 @@ export const App = () => {
setStartingState(defaultStartingState);
setSequenceGroups([]);
setFastSimulation(defaultFastSimulation);
setResetZoomAt(Date.now());
setGraph(initialGraph ? {...initialGraph} : {nodes: [], edges: []});
}
}, [initialGraph, setGraph]);
Expand Down Expand Up @@ -557,6 +559,7 @@ export const App = () => {
onRecenterView={handleRecenterView}
fitViewAt={fitViewAt}
recenterViewAt={recenterViewAt}
resetZoomAt={resetZoomAt}
onDimensions={handleDimensionChange}
/>
:
Expand All @@ -576,6 +579,7 @@ export const App = () => {
onRecenterView={handleRecenterView}
fitViewAt={fitViewAt}
recenterViewAt={recenterViewAt}
resetZoomAt={resetZoomAt}
onDimensions={handleDimensionChange}
/>
}
Expand Down
4 changes: 3 additions & 1 deletion src/components/dataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface Props {
graphEmpty: boolean;
fitViewAt?: number;
recenterViewAt?: number;
resetZoomAt?: number;
setSelectedNodeId: (id?: string, skipToggle?: boolean) => void;
onReset: () => void;
onReturnToMainMenu: () => void;
Expand All @@ -27,7 +28,7 @@ interface Props {
export const Dataset = (props: Props) => {
const {highlightNode, highlightLoopOnNode, highlightEdge, highlightAllNextNodes,
graph, graphEmpty, setSelectedNodeId, selectedNodeId, animating,
fitViewAt, recenterViewAt,
fitViewAt, recenterViewAt, resetZoomAt,
onReset, onReturnToMainMenu, onFitView, onRecenterView} = props;

const handleToolSelected = (tool: Tool) => {
Expand Down Expand Up @@ -86,6 +87,7 @@ export const Dataset = (props: Props) => {
setSelectedNodeId={setSelectedNodeId}
fitViewAt={fitViewAt}
recenterViewAt={recenterViewAt}
resetZoomAt={resetZoomAt}
/>
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion src/components/drawing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface Props {
animating: boolean;
fitViewAt?: number;
recenterViewAt?: number;
resetZoomAt?: number;
setGraph: React.Dispatch<React.SetStateAction<GraphData>>;
setHighlightNode: React.Dispatch<React.SetStateAction<Node | undefined>>
setSelectedNodeId: (id?: string, skipToggle?: boolean) => void;
Expand All @@ -40,7 +41,7 @@ const removePunctuationRegex = /["(){}[\]_+=|\\/><]/g;
export const Drawing = (props: Props) => {
const {highlightNode, highlightLoopOnNode, highlightEdge, highlightAllNextNodes,
graph, setGraph, setHighlightNode, setSelectedNodeId: _setSelectedNodeId,
fitViewAt, recenterViewAt,
fitViewAt, recenterViewAt, resetZoomAt,
selectedNodeId, animating, onReset, onReturnToMainMenu, onFitView, onRecenterView} = props;
const [drawingMode, setDrawingMode] = useState<DrawingMode>("select");
const [firstEdgeNode, setFirstEdgeNode] = useState<Node|undefined>(undefined);
Expand Down Expand Up @@ -330,6 +331,7 @@ export const Drawing = (props: Props) => {
onTransformed={handleTransformed}
fitViewAt={fitViewAt}
recenterViewAt={recenterViewAt}
resetZoomAt={resetZoomAt}
/>
<DragIcon drawingMode={drawingMode} />
<NodeModal
Expand Down
12 changes: 11 additions & 1 deletion src/components/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Props = {
animating: boolean;
fitViewAt?: number;
recenterViewAt?: number;
resetZoomAt?: number;
onClick?: (e: React.MouseEvent<HTMLDivElement>) => void;
onNodeClick?: (id: string, onLoop?: boolean) => void;
onNodeDoubleClick?: (id: string) => void;
Expand Down Expand Up @@ -245,7 +246,7 @@ export const Graph = (props: Props) => {
const {graph, highlightNode, highlightLoopOnNode, highlightEdge, highlightAllNextNodes,
allowDragging, autoArrange, rubberBand, drawingMode,
onClick, onNodeClick, onNodeDoubleClick, onEdgeClick, onDragStop,
fitViewAt, recenterViewAt,
fitViewAt, recenterViewAt, resetZoomAt,
selectedNodeId, setSelectedNodeId, animating, onDimensions, onTransformed} = props;
const svgRef = useRef<SVGSVGElement | null>(null);
const wrapperRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -847,6 +848,15 @@ export const Graph = (props: Props) => {
}
}, [recenterViewAt, fitOrCenter]);

// listen for reset zoom requests
useEffect(() => {
if ((resetZoomAt ?? 0) && zoomRef.current) {
const svg = d3.select(svgRef.current);
const transform = new d3.ZoomTransform(1, 0, 0);
svg.call(zoomRef.current.transform, transform);
}
}, [resetZoomAt]);

const handleClick = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
if (!autoArrange && onClick) {
onClick(e);
Expand Down

0 comments on commit 474a6cb

Please sign in to comment.