@tscircuit/schematic-viewer 0.0.6 → 0.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tscircuit/schematic-viewer",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "scripts": {
@@ -35,6 +35,7 @@
35
35
  "framer-motion": "^6",
36
36
  "next": "^12.2.3",
37
37
  "parse-svg-path": "^0.1.2",
38
+ "react": "^18.2.0",
38
39
  "react-dom": "^18.2.0",
39
40
  "react-no-ssr": "^1.1.0",
40
41
  "react-use-measure": "^2.1.1",
@@ -48,7 +49,9 @@
48
49
  "zustand": "^4.0.0"
49
50
  },
50
51
  "dependencies": {
51
- "@tscircuit/builder": "^1.0.11",
52
- "@tscircuit/react-fiber": "^0.0.5"
52
+ "@tscircuit/builder": "^1.0.12",
53
+ "@tscircuit/react-fiber": "^0.0.5",
54
+ "react-error-boundary": "^4.0.4",
55
+ "use-mouse-matrix-transform": "^1.0.2"
53
56
  }
54
57
  }
package/src/Schematic.tsx CHANGED
@@ -3,8 +3,25 @@ import { ProjectComponent } from "schematic-components"
3
3
  import {
4
4
  createProjectBuilder,
5
5
  createProjectFromElements,
6
+ transformSchematicElement,
6
7
  } from "@tscircuit/builder"
7
8
  import { createRoot } from "@tscircuit/react-fiber"
9
+ import { SchematicElement } from "schematic-components/SchematicElement"
10
+ import { collectElementRefs } from "lib/utils/collect-element-refs"
11
+ import { useMouseMatrixTransform } from "use-mouse-matrix-transform"
12
+ import { ErrorBoundary } from "react-error-boundary"
13
+ import { identity, compose, scale } from "transformation-matrix"
14
+ import { useRenderContext } from "lib/render-context"
15
+
16
+ const fallbackRender =
17
+ (elm) =>
18
+ ({ error, resetErrorBoundary }: any) => {
19
+ return (
20
+ <div style={{ color: "red" }}>
21
+ error rendering {elm.type}: {error.toString()}
22
+ </div>
23
+ )
24
+ }
8
25
 
9
26
  export const Schematic = ({
10
27
  children,
@@ -15,6 +32,11 @@ export const Schematic = ({
15
32
  }) => {
16
33
  const [elements, setElements] = useState<any>(initialElements)
17
34
  const [project, setProject] = useState<any>(null)
35
+ const setCameraTransform = useRenderContext((s) => s.setCameraTransform)
36
+ const { ref } = useMouseMatrixTransform({
37
+ onSetTransform: (transform) => setCameraTransform(transform),
38
+ initialTransform: compose(scale(100, 100, 0, 0)),
39
+ })
18
40
 
19
41
  useEffect(() => {
20
42
  if (initialElements.length > 0) {
@@ -36,5 +58,17 @@ export const Schematic = ({
36
58
 
37
59
  if (elements.length === 0) return null
38
60
 
39
- return <ProjectComponent project={project} />
61
+ return (
62
+ <div ref={ref}>
63
+ {elements.map((elm) => (
64
+ <ErrorBoundary fallbackRender={fallbackRender(elm)}>
65
+ <SchematicElement
66
+ element={elm}
67
+ allElements={elements}
68
+ key={JSON.stringify(elm)}
69
+ />
70
+ </ErrorBoundary>
71
+ ))}
72
+ </div>
73
+ )
40
74
  }
@@ -3,13 +3,14 @@ import { Matrix, compose, scale } from "transformation-matrix"
3
3
 
4
4
  interface RenderContextState {
5
5
  camera_transform: Matrix
6
+ setCameraTransform: (transform: Matrix) => void
6
7
  }
7
8
 
8
- export const useRenderContext = createStore<RenderContextState>()(
9
- (set, get) => ({
10
- camera_transform: compose(scale(100, 100, 0, 0)),
11
- })
12
- )
9
+ export const useRenderContext = createStore<RenderContextState>((set, get) => ({
10
+ camera_transform: compose(scale(100, 100, 0, 0)),
11
+ setCameraTransform: (transform: Matrix) =>
12
+ set({ camera_transform: transform }),
13
+ }))
13
14
 
14
15
  export const useCameraTransform = () =>
15
16
  useRenderContext((s) => s.camera_transform)
@@ -0,0 +1,20 @@
1
+ import { AnyElement } from "@tscircuit/builder"
2
+
3
+ export const collectElementRefs = (elm: AnyElement, allElms: AnyElement[]) => {
4
+ const source_component = allElms.find(
5
+ (e) =>
6
+ e.type === "source_component" &&
7
+ e.source_component_id === (elm as any).source_component_id
8
+ )
9
+ if (
10
+ ["schematic_component", "schematic_trace", "schematic_port"].includes(
11
+ elm.type
12
+ )
13
+ ) {
14
+ return {
15
+ schematic: elm,
16
+ source: source_component,
17
+ }
18
+ }
19
+ return null
20
+ }
@@ -7,6 +7,10 @@ interface Props {
7
7
  project: Types.Project | Promise<Types.Project>
8
8
  }
9
9
 
10
+ /**
11
+ * @deprecated use SchematicElement instead because we're most likely
12
+ * deprecating projects
13
+ */
10
14
  export const ProjectComponent = ({ project: $project }: Props) => {
11
15
  const project = useMaybePromise($project)
12
16
 
@@ -0,0 +1,45 @@
1
+ import { AnyElement } from "@tscircuit/builder"
2
+ import { collectElementRefs } from "lib/utils/collect-element-refs"
3
+ import SchematicComponent from "./SchematicComponent"
4
+ import SchematicPort from "./SchematicPort"
5
+ import SchematicText from "./SchematicText"
6
+ import SchematicTrace from "./SchematicTrace"
7
+
8
+ /**
9
+ * Render any @tsbuilder/builder AnyElement that can be put on a schematic.
10
+ */
11
+ export const SchematicElement = ({
12
+ element,
13
+ allElements,
14
+ }: {
15
+ element: AnyElement
16
+ allElements: AnyElement[]
17
+ }) => {
18
+ // A lot of the split logic for element types into a project is here:
19
+ // https://github.com/tscircuit/builder/blob/7e7bef9c0aadd11999795003b8986f0d244c111f/src/lib/project/create-project-from-elements.ts#L13
20
+ if (element.type === "schematic_component") {
21
+ return (
22
+ <SchematicComponent
23
+ component={collectElementRefs(element, allElements) as any}
24
+ />
25
+ )
26
+ }
27
+
28
+ if (element.type === "schematic_trace") {
29
+ return (
30
+ <SchematicTrace trace={collectElementRefs(element, allElements) as any} />
31
+ )
32
+ }
33
+
34
+ if (element.type === "schematic_port") {
35
+ return (
36
+ <SchematicPort port={collectElementRefs(element, allElements) as any} />
37
+ )
38
+ }
39
+
40
+ if (element.type === "schematic_text") {
41
+ return <SchematicText schematic_text={element} />
42
+ }
43
+
44
+ return null
45
+ }
package/tsconfig.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "baseUrl": "./src",
18
18
  "resolveJsonModule": true,
19
19
  "isolatedModules": true,
20
- "jsx": "react-jsx",
20
+ "jsx": "preserve",
21
21
  "incremental": false
22
22
  },
23
23
  "include": [