Skip to content
Draft
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
8 changes: 8 additions & 0 deletions src/PCBViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -65,6 +66,7 @@ export const PCBViewer = ({
() => calculateCircuitJsonKey(circuitJson),
[circuitJson],
)
const boardSizeKey = calculateBoardSizeKey(circuitJson)

const resetTransform = () => {
const elmBounds =
Expand Down Expand Up @@ -93,6 +95,12 @@ export const PCBViewer = ({
return
}

useEffect(() => {
if (initialRenderCompleted.current === true) {
resetTransform()
}
}, [boardSizeKey])

useEffect(() => {
if (!refDimensions?.width) return
if (!circuitJson) return
Expand Down
17 changes: 17 additions & 0 deletions src/lib/calculate-board-size-key.ts
Original file line number Diff line number Diff line change
@@ -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}`
}