@tscircuit/schematic-viewer 1.2.14 → 1.4.0

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.
Files changed (54) hide show
  1. package/.github/workflows/chromatic.yml +30 -0
  2. package/.github/workflows/npm-build.yml +26 -0
  3. package/.github/workflows/npm-typecheck.yml +26 -0
  4. package/README.md +1 -1
  5. package/dist/index.d.ts +6 -7
  6. package/dist/index.js +690 -664
  7. package/dist/index.js.map +1 -1
  8. package/package.json +21 -12
  9. package/renovate.json +12 -1
  10. package/src/Schematic.tsx +148 -77
  11. package/src/lib/types/core.ts +14 -49
  12. package/src/lib/types/source-component.ts +6 -0
  13. package/src/lib/utils/collect-element-refs.ts +4 -3
  14. package/src/lib/utils/colors.ts +236 -0
  15. package/src/schematic-components/SVGPathComponent.tsx +84 -144
  16. package/src/schematic-components/SchematicChip.tsx +183 -0
  17. package/src/schematic-components/SchematicComponent.tsx +18 -24
  18. package/src/schematic-components/SchematicComponentFromSymbol.tsx +44 -0
  19. package/src/schematic-components/SchematicElement.tsx +4 -38
  20. package/src/schematic-components/SchematicTrace.tsx +4 -3
  21. package/src/schematic-components/index.tsx +7 -14
  22. package/src/stories/basics/schematic-net-label.stories.tsx +112 -166
  23. package/src/stories/basics/schematic-net-labels-2.stories.tsx +22 -20
  24. package/src/stories/bug-connections.stories.tsx +9 -6
  25. package/src/stories/bug-high-port-numbers.stories.tsx +99 -82
  26. package/src/stories/bug-pin-spacing.stories.tsx +1 -0
  27. package/src/stories/bugs/bug1-y-flip.stories.tsx +3 -2
  28. package/src/stories/bugs/bug3-scaling-trace.stories.tsx +11 -5
  29. package/src/stories/bugs/bug4-schematic-line.stories.tsx +0 -1
  30. package/src/stories/bugs/bug5-diode.stories.tsx +0 -1
  31. package/src/stories/bugs/bug8-autolayout.stories.tsx +22 -31
  32. package/src/stories/circuit-components/diode.stories.tsx +3 -1
  33. package/src/stories/circuit-components/resistor.stories.tsx +3 -1
  34. package/src/stories/component-drawing-example.stories.tsx +2 -2
  35. package/src/stories/led-circuit-react.stories.tsx +40 -45
  36. package/src/stories/net-alias.stories.tsx +1 -1
  37. package/src/stories/off-center-render.stories.tsx +6 -6
  38. package/src/stories/rotated-resistor.stories.tsx +1 -1
  39. package/src/stories/schematic-path.stories.tsx +1 -1
  40. package/src/stories/three-sided-bug.stories.tsx +8 -8
  41. package/src/pages/led-circuit.tsx +0 -96
  42. package/src/schematic-components/ProjectComponent.tsx +0 -70
  43. package/src/schematic-components/SchematicBox.tsx +0 -29
  44. package/src/schematic-components/SchematicBug.tsx +0 -90
  45. package/src/schematic-components/SchematicLine.tsx +0 -48
  46. package/src/schematic-components/SchematicPath.tsx +0 -51
  47. package/src/schematic-components/SchematicPort.tsx +0 -63
  48. package/src/schematic-components/SimpleCapacitor.tsx +0 -29
  49. package/src/schematic-components/SimpleDiode.tsx +0 -42
  50. package/src/schematic-components/SimpleGround.tsx +0 -30
  51. package/src/schematic-components/SimpleInductor.tsx +0 -29
  52. package/src/schematic-components/SimplePowerSource.tsx +0 -43
  53. package/src/schematic-components/SimpleResistor.tsx +0 -28
  54. package/src/stories/led-circuit-builder.stories.tsx +0 -104
@@ -1,11 +1,12 @@
1
- import * as Type from "lib/types"
1
+ import { AnyCircuitElement, SchematicComponent as SchematicComponentType } from "circuit-json"
2
2
  import * as Component from "./"
3
3
 
4
4
  interface Props {
5
5
  component: {
6
- source: Type.SourceComponent
7
- schematic: Type.SchematicComponent
6
+ source: any
7
+ schematic: SchematicComponentType
8
8
  schematic_children: any[]
9
+ allElements: AnyCircuitElement[]
9
10
  }
10
11
  }
11
12
 
@@ -14,30 +15,23 @@ interface Props {
14
15
  * generating schematic lines directly
15
16
  */
16
17
  export const SchematicComponent = ({ component }: Props) => {
17
- const { source, schematic } = component
18
+ const { source, schematic, allElements } = component
18
19
  if (!source.ftype) return null
19
20
 
20
21
  switch (source.ftype) {
21
- case "simple_resistor": {
22
- return <Component.SimpleResistor component={{ source, schematic }} />
23
- }
24
- case "simple_capacitor": {
25
- return <Component.SimpleCapacitor component={{ source, schematic }} />
26
- }
27
- case "simple_power_source": {
28
- return <Component.SimplePowerSource component={{ source, schematic }} />
29
- }
30
- case "simple_ground": {
31
- return <Component.SimpleGround component={{ source, schematic }} />
32
- }
33
- case "simple_inductor": {
34
- return <Component.SimpleInductor component={{ source, schematic }} />
35
- }
36
- case "simple_bug": {
37
- return <Component.SchematicBug component={{ source, schematic }} />
38
- }
39
- case "simple_diode": {
40
- return <Component.SimpleDiode component={{ source, schematic }} />
22
+ case "simple_resistor":
23
+ case "simple_capacitor":
24
+ case "simple_power_source":
25
+ case "simple_ground":
26
+ case "simple_inductor":
27
+ case "simple_diode":
28
+ {
29
+ return <Component.SchematicComponentFromSymbol component={{ source, schematic }} />
30
+ }
31
+ case "simple_chip":
32
+ case "simple_bug":
33
+ {
34
+ return <Component.SchematicChip component={{ source, schematic, allElements }} />
41
35
  }
42
36
  default: {
43
37
  return <div>unknown ftype: {component.source.ftype}</div>
@@ -0,0 +1,44 @@
1
+ import { SchematicComponent, SourceSimpleResistor } from "circuit-json"
2
+ import { colorMap } from "lib/utils/colors"
3
+ import { symbols } from "schematic-symbols"
4
+ import SVGPathComponent from "./SVGPathComponent"
5
+
6
+ interface Props {
7
+ component: {
8
+ source: SourceSimpleResistor
9
+ schematic: SchematicComponent
10
+ }
11
+ }
12
+
13
+ export const SchematicComponentFromSymbol = ({ component: { source, schematic } }: Props) => {
14
+ const { center, rotation } = schematic
15
+ // Get the resistor symbol paths
16
+ const symbol = symbols[schematic.symbol_name]
17
+ const paths = symbol.primitives
18
+ .filter((p: any) => p.type === "path")
19
+ .map((p: any) => ({
20
+ stroke: colorMap.schematic.component_outline,
21
+ strokeWidth: 0.02,
22
+ d: p.points.reduce(
23
+ (acc: string, point: { x: number; y: number }, index: number) => {
24
+ const command = index === 0 ? "M" : "L"
25
+ return `${acc} ${command} ${point.x} ${point.y}`
26
+ },
27
+ "",
28
+ ),
29
+ }))
30
+
31
+ return (
32
+ <SVGPathComponent
33
+ rotation={rotation}
34
+ center={center}
35
+ size={{
36
+ width: symbol?.size.width,
37
+ height: symbol?.size.height,
38
+ }}
39
+ paths={paths}
40
+ />
41
+ )
42
+ }
43
+
44
+ export default SchematicComponentFromSymbol
@@ -1,14 +1,9 @@
1
- import { AnySoupElement } from "@tscircuit/soup"
1
+ import { AnyCircuitElement } from "circuit-json"
2
2
  import { collectElementRefs } from "lib/utils/collect-element-refs"
3
3
  import SchematicComponent from "./SchematicComponent"
4
- import SchematicPort from "./SchematicPort"
4
+ import { SchematicNetLabel } from "./SchematicNetLabel"
5
5
  import SchematicText from "./SchematicText"
6
- import SchematicBox from "./SchematicBox"
7
6
  import SchematicTrace from "./SchematicTrace"
8
- import SchematicLine from "./SchematicLine"
9
- import RenderError from "./RenderError"
10
- import SchematicPath from "./SchematicPath"
11
- import { SchematicNetLabel } from "./SchematicNetLabel"
12
7
 
13
8
  /**
14
9
  * Render any @tsbuilder/builder AnyElement that can be put on a schematic.
@@ -17,8 +12,8 @@ export const SchematicElement = ({
17
12
  element,
18
13
  allElements,
19
14
  }: {
20
- element: AnySoupElement
21
- allElements: AnySoupElement[]
15
+ element: AnyCircuitElement
16
+ allElements: AnyCircuitElement[]
22
17
  }) => {
23
18
  // A lot of the split logic for element types into a project is here:
24
19
  // https://github.com/tscircuit/builder/blob/7e7bef9c0aadd11999795003b8986f0d244c111f/src/lib/project/create-project-from-elements.ts#L13
@@ -36,30 +31,6 @@ export const SchematicElement = ({
36
31
  )
37
32
  }
38
33
 
39
- if (element.type === "schematic_port") {
40
- return (
41
- <SchematicPort port={collectElementRefs(element, allElements) as any} />
42
- )
43
- }
44
-
45
- if (element.type === "schematic_box") {
46
- return (
47
- <SchematicBox box={collectElementRefs(element, allElements) as any} />
48
- )
49
- }
50
-
51
- if (element.type === "schematic_line") {
52
- return (
53
- <SchematicLine line={collectElementRefs(element, allElements) as any} />
54
- )
55
- }
56
-
57
- if (element.type === "schematic_path") {
58
- return (
59
- <SchematicPath path={collectElementRefs(element, allElements) as any} />
60
- )
61
- }
62
-
63
34
  if (element.type === "schematic_text") {
64
35
  return <SchematicText schematic_text={element} />
65
36
  }
@@ -68,10 +39,5 @@ export const SchematicElement = ({
68
39
  return <SchematicNetLabel net_label={element} />
69
40
  }
70
41
 
71
- if (element.type === "source_error") {
72
- // TODO use the ids on the source error to put this in the right place
73
- return <RenderError text={element.message} />
74
- }
75
-
76
42
  return null
77
43
  }
@@ -4,6 +4,7 @@ import Path from "svg-path-generator"
4
4
  import getSVGPathBounds from "lib/utils/get-svg-path-bounds"
5
5
  import RenderError from "./RenderError"
6
6
  import SVGPathComponent2 from "./SVGPathComponent2"
7
+ import { colorMap } from "lib/utils/colors"
7
8
 
8
9
  interface Props {
9
10
  trace: {
@@ -33,14 +34,14 @@ export const SchematicTrace = ({ trace: { source, schematic } }: Props) => {
33
34
  y: pathBounds.minY + pathBounds.height / 2,
34
35
  }
35
36
  return (
36
- <SVGPathComponent2
37
+ <SVGPathComponent
37
38
  rotation={0}
38
39
  center={center}
39
40
  size={pathBounds}
40
41
  paths={[
41
42
  {
42
- stroke: "green",
43
- strokeWidth: 0.02,
43
+ stroke:colorMap.schematic.wire,
44
+ strokeWidth: 0.01,
44
45
  d,
45
46
  },
46
47
  ]}
@@ -1,17 +1,10 @@
1
- export * from "./SchematicGroup"
2
- export * from "./SimpleResistor"
3
- export * from "./SimpleCapacitor"
4
- export * from "./SVGPathComponent"
5
- export * from "./ProjectComponent"
1
+ export * from "./ContextProviders"
2
+ export * from "./RenderError"
3
+ export * from "./SchematicChip"
6
4
  export * from "./SchematicComponent"
7
- export * from "./SchematicPort"
5
+ export * from "./SchematicComponentFromSymbol"
6
+ export * from "./SchematicGroup"
8
7
  export * from "./SchematicText"
9
- export * from "./ProjectComponent"
10
8
  export * from "./SchematicTrace"
11
- export * from "./SchematicBug"
12
- export * from "./SimplePowerSource"
13
- export * from "./SimpleGround"
14
- export * from "./SimpleInductor"
15
- export * from "./RenderError"
16
- export * from "./SimpleDiode"
17
- export * from "./ContextProviders"
9
+ export * from "./SVGPathComponent"
10
+
@@ -4,182 +4,128 @@ export const SchematicNetLabel = () => {
4
4
  return (
5
5
  <Schematic
6
6
  style={{ height: 500 }}
7
- soup={
7
+ soup={[
8
8
  {
9
- elements: [
10
- {
11
- type: "source_component",
12
- source_component_id: "simple_resistor_0",
13
- name: "R1",
14
- supplier_part_numbers: {},
15
- ftype: "simple_resistor",
16
- resistance: 100,
17
- source: {
18
- type: "source_component",
19
- source_component_id: "simple_resistor_0",
20
- name: "R1",
21
- supplier_part_numbers: {},
22
- ftype: "simple_resistor",
23
- resistance: 100,
24
- },
25
- },
26
- {
27
- type: "schematic_component",
28
- source_component_id: "simple_resistor_0",
29
- schematic_component_id: "schematic_component_simple_resistor_0",
30
- rotation: 0,
31
- size: {
32
- width: 1,
33
- height: 0.3,
34
- },
35
- center: {
36
- x: 0,
37
- y: 0,
38
- },
39
- source: {
40
- type: "source_component",
41
- source_component_id: "simple_resistor_0",
42
- name: "R1",
43
- supplier_part_numbers: {},
44
- ftype: "simple_resistor",
45
- resistance: 100,
46
- },
47
- },
48
- {
49
- type: "source_port",
50
- name: "left",
51
- source_port_id: "source_port_0",
52
- source_component_id: "simple_resistor_0",
53
- source: {
54
- type: "source_component",
55
- source_component_id: "simple_resistor_0",
56
- name: "R1",
57
- supplier_part_numbers: {},
58
- ftype: "simple_resistor",
59
- resistance: 100,
60
- },
61
- },
62
- {
63
- type: "schematic_port",
64
- schematic_port_id: "schematic_port_0",
65
- source_port_id: "source_port_0",
66
- center: {
67
- x: -0.5,
68
- y: 0,
69
- },
70
- facing_direction: "left",
71
- schematic_component_id: "schematic_component_simple_resistor_0",
72
- source: {
73
- type: "source_port",
74
- name: "left",
75
- source_port_id: "source_port_0",
76
- source_component_id: "simple_resistor_0",
77
- },
78
- },
79
- {
80
- type: "source_port",
81
- name: "right",
82
- source_port_id: "source_port_1",
83
- source_component_id: "simple_resistor_0",
84
- source: {
85
- type: "source_component",
86
- source_component_id: "simple_resistor_0",
87
- name: "R1",
88
- supplier_part_numbers: {},
89
- ftype: "simple_resistor",
90
- resistance: 100,
91
- },
92
- },
9
+ type: "source_component",
10
+ source_component_id: "simple_resistor_0",
11
+ name: "R1",
12
+ supplier_part_numbers: {},
13
+ ftype: "simple_resistor",
14
+ resistance: 100,
15
+ },
16
+ {
17
+ type: "schematic_component",
18
+ source_component_id: "simple_resistor_0",
19
+ schematic_component_id: "schematic_component_simple_resistor_0",
20
+ rotation: 0,
21
+ size: {
22
+ width: 1,
23
+ height: 0.3,
24
+ },
25
+ center: {
26
+ x: 0,
27
+ y: 0,
28
+ },
29
+ },
30
+ {
31
+ type: "source_port",
32
+ name: "left",
33
+ source_port_id: "source_port_0",
34
+ source_component_id: "simple_resistor_0",
35
+ },
36
+ {
37
+ type: "schematic_port",
38
+ schematic_port_id: "schematic_port_0",
39
+ source_port_id: "source_port_0",
40
+ center: {
41
+ x: -0.5,
42
+ y: 0,
43
+ },
44
+ facing_direction: "left",
45
+ schematic_component_id: "schematic_component_simple_resistor_0",
46
+ },
47
+ {
48
+ type: "source_port",
49
+ name: "right",
50
+ source_port_id: "source_port_1",
51
+ source_component_id: "simple_resistor_0",
52
+ },
53
+ {
54
+ type: "schematic_port",
55
+ schematic_port_id: "schematic_port_1",
56
+ source_port_id: "source_port_1",
57
+ center: {
58
+ x: 0.5,
59
+ y: 0,
60
+ },
61
+ facing_direction: "right",
62
+ schematic_component_id: "schematic_component_simple_resistor_0",
63
+ },
64
+ {
65
+ type: "schematic_text",
66
+ text: "R1",
67
+ schematic_text_id: "schematic_text_0",
68
+ schematic_component_id: "schematic_component_simple_resistor_0",
69
+ anchor: "left",
70
+ position: {
71
+ x: -0.2,
72
+ y: -0.5,
73
+ },
74
+ rotation: 0,
75
+ },
76
+ {
77
+ type: "schematic_text",
78
+ text: "100",
79
+ schematic_text_id: "schematic_text_1",
80
+ schematic_component_id: "schematic_component_simple_resistor_0",
81
+ anchor: "left",
82
+ position: {
83
+ x: -0.2,
84
+ y: -0.3,
85
+ },
86
+ rotation: 0,
87
+ },
88
+ {
89
+ type: "source_net",
90
+ member_source_group_ids: [],
91
+ source_net_id: "net_0",
92
+ name: "N1",
93
+ },
94
+ {
95
+ type: "source_trace",
96
+ connected_source_port_ids: ["source_port_1"],
97
+ connected_source_net_ids: ["net_0"],
98
+ source_trace_id: "source_trace_0",
99
+ },
100
+ {
101
+ type: "schematic_net_label",
102
+ source_net_id: "net_0",
103
+ text: "N1",
104
+ anchor_side: "left",
105
+ center: {
106
+ x: 1.5,
107
+ y: 0,
108
+ },
109
+ },
110
+ {
111
+ type: "schematic_trace",
112
+ schematic_trace_id: "schematic_trace_0",
113
+ source_trace_id: "source_trace_0",
114
+ edges: [
93
115
  {
94
- type: "schematic_port",
95
- schematic_port_id: "schematic_port_1",
96
- source_port_id: "source_port_1",
97
- center: {
116
+ from: {
98
117
  x: 0.5,
99
118
  y: 0,
100
119
  },
101
- facing_direction: "right",
102
- schematic_component_id: "schematic_component_simple_resistor_0",
103
- source: {
104
- type: "source_port",
105
- name: "right",
106
- source_port_id: "source_port_1",
107
- source_component_id: "simple_resistor_0",
108
- },
109
- },
110
- {
111
- type: "schematic_text",
112
- text: "R1",
113
- schematic_text_id: "schematic_text_0",
114
- schematic_component_id: "schematic_component_simple_resistor_0",
115
- anchor: "left",
116
- position: {
117
- x: -0.2,
118
- y: -0.5,
119
- },
120
- rotation: 0,
121
- source: null,
122
- },
123
- {
124
- type: "schematic_text",
125
- text: 100,
126
- schematic_text_id: "schematic_text_1",
127
- schematic_component_id: "schematic_component_simple_resistor_0",
128
- anchor: "left",
129
- position: {
130
- x: -0.2,
131
- y: -0.3,
132
- },
133
- rotation: 0,
134
- source: null,
135
- },
136
- {
137
- type: "source_net",
138
- member_source_group_ids: [],
139
- source_net_id: "net_0",
140
- name: "N1",
141
- source: null,
142
- },
143
- {
144
- type: "source_trace",
145
- connected_source_port_ids: ["source_port_1"],
146
- connected_source_net_ids: ["net_0"],
147
- source_trace_id: "source_trace_0",
148
- source: null,
149
- },
150
- {
151
- type: "schematic_net_label",
152
- source_net_id: "net_0",
153
- text: "N1",
154
- anchor_side: "left",
155
- center: {
120
+ to: {
156
121
  x: 1.5,
157
122
  y: 0,
158
123
  },
159
- source: null,
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,
124
+ from_schematic_port_id: "schematic_port_1",
179
125
  },
180
126
  ],
181
- }.elements
182
- }
127
+ },
128
+ ]}
183
129
  />
184
130
  )
185
131
  }
@@ -3,26 +3,28 @@ import { Schematic } from "../../Schematic"
3
3
  export const SchematicNetLabel2 = () => {
4
4
  return (
5
5
  <Schematic style={{ height: 500 }}>
6
- <resistor resistance="1k" name="R1" schX={-2} schY={0} />
7
- <resistor resistance="1k" name="R2" schX={2} schY={0} />
8
- <resistor
9
- schRotation="90deg"
10
- resistance="1k"
11
- name="R3"
12
- schX={0}
13
- schY={2}
14
- />
15
- <resistor
16
- schRotation="90deg"
17
- resistance="1k"
18
- name="R4"
19
- schX={0}
20
- schY={-2}
21
- />
22
- <trace from=".R1 > .right" to="net.N1" />
23
- <trace from=".R2 > .left" to="net.N2" />
24
- <trace from=".R3 > .left" to="net.N3" />
25
- <trace from=".R4 > .right" to="net.GND2" />
6
+ <board width={10} height={10}>
7
+ <resistor resistance="1k" name="R1" schX={-2} schY={0} />
8
+ <resistor resistance="1k" name="R2" schX={2} schY={0} />
9
+ <resistor
10
+ schRotation="90deg"
11
+ resistance="1k"
12
+ name="R3"
13
+ schX={0}
14
+ schY={2}
15
+ />
16
+ <resistor
17
+ schRotation="90deg"
18
+ resistance="1k"
19
+ name="R4"
20
+ schX={0}
21
+ schY={-2}
22
+ />
23
+ <trace from=".R1 > .right" to="net.N1" />
24
+ <trace from=".R2 > .left" to="net.N2" />
25
+ <trace from=".R3 > .left" to="net.N3" />
26
+ <trace from=".R4 > .right" to="net.GND2" />
27
+ </board>
26
28
  </Schematic>
27
29
  )
28
30
  }
@@ -5,13 +5,16 @@ export const BugConnections = () => {
5
5
  <Schematic style={{ height: 600 }}>
6
6
  <bug
7
7
  name="B1"
8
- port_arrangement={{
9
- left_size: 3,
10
- right_size: 2,
8
+ schPortArrangement={{
9
+ leftSize: 3,
10
+ rightSize: 2,
11
11
  }}
12
- sch_cx={8}
13
- sch_cy={3}
14
- port_labels={{
12
+ schHeight={5}
13
+ schWidth={3}
14
+ schPinSpacing={2}
15
+ schX={8}
16
+ schY={3}
17
+ pinLabels={{
15
18
  "1": "D0",
16
19
  "2": "D1",
17
20
  }}