diff --git a/src/PCBViewer.tsx b/src/PCBViewer.tsx index 6ce85371..685a3536 100644 --- a/src/PCBViewer.tsx +++ b/src/PCBViewer.tsx @@ -13,6 +13,7 @@ import { CanvasElementsRenderer } from "./components/CanvasElementsRenderer" import type { ManualEditEvent } from "@tscircuit/props" import { zIndexMap } from "lib/util/z-index-map" import { calculateCircuitJsonKey } from "lib/calculate-circuit-json-key" +import { calculateBoardSizeKey } from "lib/calculate-board-size-key" const defaultTransform = compose(translate(400, 300), scale(40, -40)) @@ -65,6 +66,7 @@ export const PCBViewer = ({ () => calculateCircuitJsonKey(circuitJson), [circuitJson], ) + const boardSizeKey = calculateBoardSizeKey(circuitJson) const resetTransform = () => { const elmBounds = @@ -93,6 +95,12 @@ export const PCBViewer = ({ return } + useEffect(() => { + if (initialRenderCompleted.current === true) { + resetTransform() + } + }, [boardSizeKey]) + useEffect(() => { if (!refDimensions?.width) return if (!circuitJson) return diff --git a/src/lib/calculate-board-size-key.ts b/src/lib/calculate-board-size-key.ts new file mode 100644 index 00000000..aad052a2 --- /dev/null +++ b/src/lib/calculate-board-size-key.ts @@ -0,0 +1,17 @@ +import type { AnyCircuitElement } from "circuit-json" + +export const calculateBoardSizeKey = ( + circuitJson?: AnyCircuitElement[], +): string => { + if (!circuitJson) return "empty" + + const board = circuitJson.find((e) => e.type === "pcb_board") + + if (!board) return "no-board" + + if (board.outline) { + return board?.outline.map((o) => `${o.x}_${o.y}`).join(",") + } + + return `${board?.width}_${board?.height}` +}