@tscircuit/schematic-viewer 1.2.9 → 1.2.11

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": "1.2.9",
3
+ "version": "1.2.11",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "repository": "tscircuit/schematic-viewer",
package/src/Schematic.tsx CHANGED
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useEffect, useState } from "react"
2
- import { ProjectComponent } from "schematic-components"
2
+ import { ContextProviders, ProjectComponent } from "schematic-components"
3
3
  import { SuperGrid, toMMSI } from "react-supergrid"
4
4
  import {
5
5
  AnyElement,
@@ -15,7 +15,7 @@ import { collectElementRefs } from "lib/utils/collect-element-refs"
15
15
  import { useMouseMatrixTransform } from "use-mouse-matrix-transform"
16
16
  import { ErrorBoundary as TypedErrorBoundary } from "react-error-boundary"
17
17
  import { identity, compose, scale, translate } from "transformation-matrix"
18
- import { useRenderContext } from "lib/render-context"
18
+ import { useGlobalStore } from "lib/render-context"
19
19
  import useMeasure from "react-use-measure"
20
20
  import { TableViewer } from "./schematic-components/TableViewer"
21
21
 
@@ -34,13 +34,7 @@ const fallbackRender: any =
34
34
  const toMMSINeg = (v: number, z: number) =>
35
35
  v >= 0 ? toMMSI(v, z) : `-${toMMSI(-v, z)}`
36
36
 
37
- export const Schematic = ({
38
- children,
39
- elements: initialElements,
40
- soup: initialSoup,
41
- style,
42
- showTable = false,
43
- }: {
37
+ export interface SchematicProps {
44
38
  children?: any
45
39
 
46
40
  /** @deprecated use soup */
@@ -51,13 +45,29 @@ export const Schematic = ({
51
45
  style?: any
52
46
 
53
47
  showTable?: boolean
54
- }) => {
48
+ }
49
+
50
+ export const Schematic = (props: SchematicProps) => {
51
+ return (
52
+ <ContextProviders>
53
+ <SchematicWithoutContext {...props} />
54
+ </ContextProviders>
55
+ )
56
+ }
57
+
58
+ export const SchematicWithoutContext = ({
59
+ children,
60
+ elements: initialElements,
61
+ soup: initialSoup,
62
+ style,
63
+ showTable = false,
64
+ }: SchematicProps) => {
55
65
  initialSoup = initialSoup ?? initialElements ?? []
56
66
 
57
67
  const [elements, setElements] = useState<any>(initialSoup ?? [])
58
68
  const [project, setProject] = useState<any>(null)
59
- const setCameraTransform = useRenderContext((s) => s.setCameraTransform)
60
- const cameraTransform = useRenderContext((s) => s.camera_transform)
69
+ const { setCameraTransform, camera_transform: cameraTransform } =
70
+ useGlobalStore()
61
71
  const [boundsRef, bounds] = useMeasure()
62
72
  const { ref, setTransform } = useMouseMatrixTransform({
63
73
  onSetTransform: (transform) => {
@@ -82,13 +92,11 @@ export const Schematic = ({
82
92
  (elmBounds.height ?? 0) / height,
83
93
  100
84
94
  )
85
- // console.log(elements)
86
95
  setElements(elements)
87
96
  setProject(createProjectFromElements(elements))
88
97
  setTransform(
89
98
  compose(
90
99
  translate((elmBounds.width ?? 0) / 2, (elmBounds.height ?? 0) / 2),
91
- // translate(100, 0),
92
100
  scale(scaleFactor, -scaleFactor, 0, 0),
93
101
  translate(-center.x, -center.y)
94
102
  )
@@ -1,16 +1,28 @@
1
- import createStore from "zustand"
1
+ import {
2
+ createStore as createZustandStore,
3
+ useStore as useZustandStore,
4
+ UseBoundStore,
5
+ } from "zustand"
2
6
  import { Matrix, compose, scale } from "transformation-matrix"
7
+ import { useContext } from "react"
8
+ import { StoreContext } from "schematic-components/"
3
9
 
4
10
  interface RenderContextState {
5
11
  camera_transform: Matrix
6
12
  setCameraTransform: (transform: Matrix) => void
7
13
  }
8
14
 
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
- }))
15
+ export const createRenderContextStore = () =>
16
+ createZustandStore<RenderContextState>((set) => ({
17
+ camera_transform: compose(scale(100, 100, 0, 0)),
18
+ setCameraTransform: (transform: Matrix) =>
19
+ set({ camera_transform: transform }),
20
+ }))
14
21
 
15
- export const useCameraTransform = () =>
16
- useRenderContext((s) => s.camera_transform)
22
+ export const useGlobalStore = <T = RenderContextState>(
23
+ s?: (state: RenderContextState) => T
24
+ ): T => {
25
+ const store = useContext(StoreContext)
26
+
27
+ return useZustandStore(store as any, s as any)
28
+ }
@@ -0,0 +1,15 @@
1
+ import { createRenderContextStore } from "lib/render-context"
2
+ import { useMemo } from "react"
3
+ import { createContext } from "react"
4
+
5
+ export const StoreContext = createContext(null)
6
+
7
+ export const ContextProviders = ({ children }: { children?: any }) => {
8
+ const store = useMemo(() => createRenderContextStore(), [])
9
+
10
+ return (
11
+ <StoreContext.Provider value={store as any}>
12
+ {children}
13
+ </StoreContext.Provider>
14
+ )
15
+ }
@@ -1,4 +1,4 @@
1
- import { useCameraTransform } from "lib/render-context"
1
+ import { useGlobalStore } from "lib/render-context"
2
2
  import getSVGPathBounds from "lib/utils/get-svg-path-bounds"
3
3
  import { useCallback, useState } from "react"
4
4
 
@@ -37,7 +37,7 @@ export const SVGPathComponent = ({
37
37
  shiftToBottom,
38
38
  hoverContent,
39
39
  }: Props) => {
40
- const ct = useCameraTransform()
40
+ const ct = useGlobalStore((s) => s.camera_transform)
41
41
  const pathBounds = getSVGPathBounds(paths.map((p) => p.d))
42
42
  // Margin in SVG Space
43
43
  const badRatio =
@@ -1,4 +1,4 @@
1
- import { useCameraTransform } from "lib/render-context"
1
+ import { useGlobalStore } from "lib/render-context"
2
2
  import getSVGPathBounds from "lib/utils/get-svg-path-bounds"
3
3
  import { useCallback, useState } from "react"
4
4
 
@@ -37,7 +37,7 @@ export const SVGPathComponent2 = ({
37
37
  shiftToBottom,
38
38
  hoverContent,
39
39
  }: Props) => {
40
- const ct = useCameraTransform()
40
+ const ct = useGlobalStore((c) => c.camera_transform)
41
41
  const pathBounds = getSVGPathBounds(paths.map((p) => p.d))
42
42
  // Margin in SVG Space
43
43
 
@@ -37,9 +37,7 @@ export const SchematicComponent = ({ component }: Props) => {
37
37
  return <Component.SchematicBug component={{ source, schematic }} />
38
38
  }
39
39
  case "simple_diode": {
40
- // Replaced by builder
41
- return null
42
- // return <Component.SimpleDiode component={{ source, schematic }} />
40
+ return <Component.SimpleDiode component={{ source, schematic }} />
43
41
  }
44
42
  default: {
45
43
  return <div>unknown ftype: {component.source.ftype}</div>
@@ -2,7 +2,7 @@ import * as Type from "lib/types"
2
2
  import SVGPathComponent from "./SVGPathComponent"
3
3
  import Path from "svg-path-generator"
4
4
  import getSVGPathBounds from "lib/utils/get-svg-path-bounds"
5
- import { useCameraTransform } from "lib/render-context"
5
+ import { useGlobalStore } from "lib/render-context"
6
6
  import { applyToPoint } from "transformation-matrix"
7
7
  import useMeasure from "react-use-measure"
8
8
 
@@ -11,7 +11,7 @@ interface Props {
11
11
  }
12
12
 
13
13
  export const SchematicText = ({ schematic_text }: Props) => {
14
- const ct = useCameraTransform()
14
+ const ct = useGlobalStore((s) => s.camera_transform)
15
15
  const { text, position, anchor } = schematic_text
16
16
  const tPos = applyToPoint(ct, position)
17
17
  const [boundsRef, bounds] = useMeasure()
@@ -14,3 +14,4 @@ export * from "./SimpleGround"
14
14
  export * from "./SimpleInductor"
15
15
  export * from "./RenderError"
16
16
  export * from "./SimpleDiode"
17
+ export * from "./ContextProviders"
@@ -137,7 +137,7 @@ export const SchematicNetLabel = () => {
137
137
  type: "source_net",
138
138
  member_source_group_ids: [],
139
139
  source_net_id: "net_0",
140
- name: "GND",
140
+ name: "N1",
141
141
  source: null,
142
142
  },
143
143
  {
@@ -150,7 +150,7 @@ export const SchematicNetLabel = () => {
150
150
  {
151
151
  type: "schematic_net_label",
152
152
  source_net_id: "net_0",
153
- text: "GND",
153
+ text: "N1",
154
154
  anchor_side: "left",
155
155
  center: {
156
156
  x: 1.5,
@@ -158,6 +158,25 @@ export const SchematicNetLabel = () => {
158
158
  },
159
159
  source: null,
160
160
  },
161
+ {
162
+ type: "schematic_trace",
163
+ schematic_trace_id: "schematic_trace_0",
164
+ source_trace_id: "source_trace_0",
165
+ edges: [
166
+ {
167
+ from: {
168
+ x: 0.5,
169
+ y: 0,
170
+ },
171
+ to: {
172
+ x: 1.5,
173
+ y: 0,
174
+ },
175
+ from_schematic_port_id: "schematic_port_1",
176
+ },
177
+ ],
178
+ source: null,
179
+ },
161
180
  ],
162
181
  }.elements
163
182
  }
@@ -0,0 +1,23 @@
1
+ import { Schematic } from "../../Schematic"
2
+
3
+ export const Bug7MultipleSchematicPanning = () => {
4
+ return (
5
+ <div>
6
+ <Schematic style={{ height: 500 }}>
7
+ <resistor name="R1" resistance="10 ohm" schX={2} schY={1} />
8
+ <resistor resistance="1k" schX={1} schY={2} name="main_power" />
9
+ <trace path={[".main_power > port.right", ".R1 > port.left"]} />
10
+ </Schematic>
11
+ <Schematic style={{ height: 500 }}>
12
+ <resistor name="R1" resistance="10 ohm" schX={2} schY={1} />
13
+ <resistor resistance="1k" schX={1} schY={2} name="main_power" />
14
+ <trace path={[".main_power > port.right", ".R1 > port.left"]} />
15
+ </Schematic>
16
+ </div>
17
+ )
18
+ }
19
+
20
+ export default {
21
+ title: "Bugs/Bug7MultipleSchematicPanning",
22
+ component: Bug7MultipleSchematicPanning,
23
+ }
@@ -0,0 +1,14 @@
1
+ import { Schematic } from "../../Schematic"
2
+
3
+ export const Diode = () => {
4
+ return (
5
+ <Schematic style={{ height: 500 }}>
6
+ <diode name="D1" schX={2} schY={1} />
7
+ </Schematic>
8
+ )
9
+ }
10
+
11
+ export default {
12
+ title: "Circuit Components/Diode",
13
+ component: Diode,
14
+ }
@@ -1,4 +1,4 @@
1
- import { Schematic } from "../Schematic"
1
+ import { Schematic } from "../../Schematic"
2
2
 
3
3
  export const Resistor = () => {
4
4
  return (
@@ -9,6 +9,6 @@ export const Resistor = () => {
9
9
  }
10
10
 
11
11
  export default {
12
- title: "Resistor",
12
+ title: "Circuit Components/Resistor",
13
13
  component: Resistor,
14
14
  }