-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
193 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"$schema": "https://biomejs.dev/schemas/1.9.3/schema.json", | ||
"organizeImports": { | ||
"enabled": true | ||
}, | ||
"formatter": { | ||
"enabled": true, | ||
"indentStyle": "space" | ||
}, | ||
"files": { | ||
"ignore": ["cosmos-export", "dist", "package.json"] | ||
}, | ||
"javascript": { | ||
"formatter": { | ||
"jsxQuoteStyle": "double", | ||
"quoteProperties": "asNeeded", | ||
"trailingCommas": "all", | ||
"semicolons": "asNeeded", | ||
"arrowParentheses": "always", | ||
"bracketSpacing": true, | ||
"bracketSameLine": false | ||
} | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true, | ||
"suspicious": { | ||
"noExplicitAny": "off" | ||
}, | ||
"complexity": { | ||
"noForEach": "info" | ||
}, | ||
"correctness": { | ||
"useExhaustiveDependencies": "off" | ||
}, | ||
"style": { | ||
"noUselessElse": "off", | ||
"noNonNullAssertion": "off", | ||
"useNumberNamespace": "off", | ||
"useFilenamingConvention": { | ||
"level": "error", | ||
"options": { | ||
"strictCase": true, | ||
"requireAscii": true, | ||
"filenameCases": ["kebab-case", "export"] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SchematicViewer } from "lib/components/SchematicViewer" | ||
import { renderToCircuitJson } from "lib/dev/render-to-circuit-json" | ||
|
||
export default () => ( | ||
<div style={{ width: "100vw", height: "90vh" }}> | ||
<SchematicViewer | ||
circuitJson={renderToCircuitJson( | ||
<board width="10mm" height="10mm"> | ||
<resistor name="R1" resistance={1000} schX={-2} /> | ||
<capacitor name="C1" capacitance="1uF" schX={2} /> | ||
<trace from=".R1 .pin2" to=".C1 .pin1" /> | ||
</board>, | ||
)} | ||
/> | ||
</div> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { useMouseMatrixTransform } from "use-mouse-matrix-transform" | ||
import { convertCircuitJsonToSchematicSvg } from "circuit-to-svg" | ||
import { useEffect, useMemo, useRef, useState } from "react" | ||
import { toString as transformToString } from "transformation-matrix" | ||
|
||
interface Props { | ||
circuitJson: Array<{ type: string }> | ||
} | ||
|
||
export const SchematicViewer = ({ circuitJson }: Props) => { | ||
const svgDivRef = useRef<HTMLDivElement>(null) | ||
const { ref: containerRef } = useMouseMatrixTransform({ | ||
onSetTransform(transform) { | ||
if (!svgDivRef.current) return | ||
svgDivRef.current.style.transform = transformToString(transform) | ||
}, | ||
}) | ||
const [containerWidth, setContainerWidth] = useState(0) | ||
const [containerHeight, setContainerHeight] = useState(0) | ||
|
||
useEffect(() => { | ||
if (!containerRef.current) return | ||
|
||
const updateDimensions = () => { | ||
const rect = containerRef.current?.getBoundingClientRect() | ||
|
||
setContainerWidth(rect?.width || 0) | ||
setContainerHeight(rect?.height || 0) | ||
} | ||
|
||
// Set initial dimensions | ||
updateDimensions() | ||
|
||
// Add resize listener | ||
const resizeObserver = new ResizeObserver(updateDimensions) | ||
resizeObserver.observe(containerRef.current) | ||
|
||
// Fallback to window resize | ||
window.addEventListener("resize", updateDimensions) | ||
|
||
return () => { | ||
resizeObserver.disconnect() | ||
window.removeEventListener("resize", updateDimensions) | ||
} | ||
}, []) | ||
|
||
const svg = useMemo(() => { | ||
if (!containerWidth || !containerHeight) return "" | ||
|
||
return convertCircuitJsonToSchematicSvg(circuitJson as any, { | ||
width: containerWidth, | ||
height: containerHeight || 720, | ||
}) | ||
}, [circuitJson, containerWidth, containerHeight]) | ||
|
||
return ( | ||
<div | ||
ref={containerRef} | ||
style={{ | ||
backgroundColor: "#F5F1ED", | ||
overflow: "hidden", | ||
cursor: "grab", | ||
minHeight: "300px", | ||
height: "100%", | ||
}} | ||
> | ||
<div | ||
ref={svgDivRef} | ||
style={{ | ||
pointerEvents: "auto", | ||
transformOrigin: "0 0", | ||
}} | ||
// biome-ignore lint/security/noDangerouslySetInnerHtml: <explanation> | ||
dangerouslySetInnerHTML={{ __html: svg }} | ||
/> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as Core from "@tscircuit/core" | ||
console.log(Core) | ||
|
||
export const renderToCircuitJson = (board: React.ReactElement) => { | ||
const circuit = new Core.Circuit() | ||
circuit.add(board) | ||
return circuit.getCircuitJson() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from "react" | ||
import { createRoot } from "react-dom/client" | ||
import { renderToCircuitJson } from "lib/dev/render-to-circuit-json" | ||
import { SchematicViewer } from "lib/components/SchematicViewer" | ||
|
||
const root = createRoot(document.getElementById("root")!) | ||
|
||
root.render( | ||
<React.StrictMode> | ||
<div style={{ width: "100vw", height: "100vh" }}> | ||
<SchematicViewer | ||
circuitJson={renderToCircuitJson( | ||
<board width="10mm" height="10mm"> | ||
<resistor name="R1" resistance={1000} /> | ||
</board>, | ||
)} | ||
/> | ||
</div> | ||
</React.StrictMode>, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import { defineConfig } from "vite" | ||
import react from "@vitejs/plugin-react" | ||
import { resolve } from "path" | ||
import { resolve } from "node:path" | ||
|
||
export default defineConfig({ | ||
plugins: [react()], | ||
resolve: { | ||
alias: { | ||
lib: resolve(__dirname, "./lib") | ||
} | ||
lib: resolve(__dirname, "./lib"), | ||
}, | ||
}, | ||
define: { | ||
global: {}, | ||
}, | ||
}) |
This file was deleted.
Oops, something went wrong.