@tscircuit/schematic-viewer 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/bun-pver-release.yml +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
|
|
3
|
+
interface Props {
|
|
4
|
+
circuitJson: Array<{
|
|
5
|
+
type: string;
|
|
6
|
+
}>;
|
|
7
|
+
containerStyle?: React.CSSProperties;
|
|
8
|
+
}
|
|
9
|
+
declare const SchematicViewer: ({ circuitJson, containerStyle }: Props) => react_jsx_runtime.JSX.Element;
|
|
10
|
+
|
|
11
|
+
export { SchematicViewer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// lib/components/SchematicViewer.tsx
|
|
2
|
+
import { useMouseMatrixTransform } from "use-mouse-matrix-transform";
|
|
3
|
+
import { convertCircuitJsonToSchematicSvg } from "circuit-to-svg";
|
|
4
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { toString as transformToString } from "transformation-matrix";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
var SchematicViewer = ({ circuitJson, containerStyle }) => {
|
|
8
|
+
const svgDivRef = useRef(null);
|
|
9
|
+
const { ref: containerRef } = useMouseMatrixTransform({
|
|
10
|
+
onSetTransform(transform) {
|
|
11
|
+
if (!svgDivRef.current) return;
|
|
12
|
+
svgDivRef.current.style.transform = transformToString(transform);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const [containerWidth, setContainerWidth] = useState(0);
|
|
16
|
+
const [containerHeight, setContainerHeight] = useState(0);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (!containerRef.current) return;
|
|
19
|
+
const updateDimensions = () => {
|
|
20
|
+
const rect = containerRef.current?.getBoundingClientRect();
|
|
21
|
+
setContainerWidth(rect?.width || 0);
|
|
22
|
+
setContainerHeight(rect?.height || 0);
|
|
23
|
+
};
|
|
24
|
+
updateDimensions();
|
|
25
|
+
const resizeObserver = new ResizeObserver(updateDimensions);
|
|
26
|
+
resizeObserver.observe(containerRef.current);
|
|
27
|
+
window.addEventListener("resize", updateDimensions);
|
|
28
|
+
return () => {
|
|
29
|
+
resizeObserver.disconnect();
|
|
30
|
+
window.removeEventListener("resize", updateDimensions);
|
|
31
|
+
};
|
|
32
|
+
}, []);
|
|
33
|
+
const svg = useMemo(() => {
|
|
34
|
+
if (!containerWidth || !containerHeight) return "";
|
|
35
|
+
return convertCircuitJsonToSchematicSvg(circuitJson, {
|
|
36
|
+
width: containerWidth,
|
|
37
|
+
height: containerHeight || 720
|
|
38
|
+
});
|
|
39
|
+
}, [circuitJson, containerWidth, containerHeight]);
|
|
40
|
+
return /* @__PURE__ */ jsx(
|
|
41
|
+
"div",
|
|
42
|
+
{
|
|
43
|
+
ref: containerRef,
|
|
44
|
+
style: {
|
|
45
|
+
backgroundColor: "#F5F1ED",
|
|
46
|
+
overflow: "hidden",
|
|
47
|
+
cursor: "grab",
|
|
48
|
+
minHeight: "300px",
|
|
49
|
+
...containerStyle
|
|
50
|
+
},
|
|
51
|
+
children: /* @__PURE__ */ jsx(
|
|
52
|
+
"div",
|
|
53
|
+
{
|
|
54
|
+
ref: svgDivRef,
|
|
55
|
+
style: {
|
|
56
|
+
pointerEvents: "auto",
|
|
57
|
+
transformOrigin: "0 0"
|
|
58
|
+
},
|
|
59
|
+
dangerouslySetInnerHTML: { __html: svg }
|
|
60
|
+
}
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
SchematicViewer
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../lib/components/SchematicViewer.tsx"],"sourcesContent":["import { useMouseMatrixTransform } from \"use-mouse-matrix-transform\"\nimport { convertCircuitJsonToSchematicSvg } from \"circuit-to-svg\"\nimport { useEffect, useMemo, useRef, useState } from \"react\"\nimport { toString as transformToString } from \"transformation-matrix\"\n\ninterface Props {\n circuitJson: Array<{ type: string }>\n containerStyle?: React.CSSProperties\n}\n\nexport const SchematicViewer = ({ circuitJson, containerStyle }: Props) => {\n const svgDivRef = useRef<HTMLDivElement>(null)\n const { ref: containerRef } = useMouseMatrixTransform({\n onSetTransform(transform) {\n if (!svgDivRef.current) return\n svgDivRef.current.style.transform = transformToString(transform)\n },\n })\n const [containerWidth, setContainerWidth] = useState(0)\n const [containerHeight, setContainerHeight] = useState(0)\n\n useEffect(() => {\n if (!containerRef.current) return\n\n const updateDimensions = () => {\n const rect = containerRef.current?.getBoundingClientRect()\n\n setContainerWidth(rect?.width || 0)\n setContainerHeight(rect?.height || 0)\n }\n\n // Set initial dimensions\n updateDimensions()\n\n // Add resize listener\n const resizeObserver = new ResizeObserver(updateDimensions)\n resizeObserver.observe(containerRef.current)\n\n // Fallback to window resize\n window.addEventListener(\"resize\", updateDimensions)\n\n return () => {\n resizeObserver.disconnect()\n window.removeEventListener(\"resize\", updateDimensions)\n }\n }, [])\n\n const svg = useMemo(() => {\n if (!containerWidth || !containerHeight) return \"\"\n\n return convertCircuitJsonToSchematicSvg(circuitJson as any, {\n width: containerWidth,\n height: containerHeight || 720,\n })\n }, [circuitJson, containerWidth, containerHeight])\n\n return (\n <div\n ref={containerRef}\n style={{\n backgroundColor: \"#F5F1ED\",\n overflow: \"hidden\",\n cursor: \"grab\",\n minHeight: \"300px\",\n ...containerStyle,\n }}\n >\n <div\n ref={svgDivRef}\n style={{\n pointerEvents: \"auto\",\n transformOrigin: \"0 0\",\n }}\n // biome-ignore lint/security/noDangerouslySetInnerHtml: <explanation>\n dangerouslySetInnerHTML={{ __html: svg }}\n />\n </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