@tscircuit/schematic-viewer 1.1.9 → 1.1.10

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.1.9",
3
+ "version": "1.1.10",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "repository": "tscircuit/schematic-viewer",
@@ -33,7 +33,7 @@
33
33
  "@storybook/react": "^7.4.0",
34
34
  "@storybook/testing-library": "^0.0.14-next.2",
35
35
  "@tscircuit/builder": "^1.5.5",
36
- "@tscircuit/react-fiber": "^1.0.7",
36
+ "@tscircuit/react-fiber": "^1.0.12",
37
37
  "@tscircuit/routing": "^1.3.0",
38
38
  "@tscircuit/table-viewer": "^0.0.6",
39
39
  "@types/node": "^18.6.0",
@@ -1,4 +1,4 @@
1
- import { SourceComponent } from "./source-component"
1
+ import { AnySourceComponent, SourceComponent } from "./source-component"
2
2
 
3
3
  export interface SchematicConfig {
4
4
  type: "schematic_config"
@@ -69,6 +69,19 @@ export interface SchematicLine {
69
69
  y2: number
70
70
  }
71
71
 
72
+ export interface SchematicPath {
73
+ type: "schematic_path"
74
+ drawing_type: "path"
75
+ schematic_component_id: string
76
+ points: Array<{
77
+ x: number
78
+ y: number
79
+ }>
80
+ is_filled: boolean
81
+ is_closed: boolean
82
+ fill_color?: "red" | "blue"
83
+ }
84
+
72
85
  export interface SchematicTrace {
73
86
  type: "schematic_trace"
74
87
  schematic_trace_id: string
@@ -177,7 +190,7 @@ export interface Project {
177
190
  export type AnyElement =
178
191
  | Project
179
192
  | SourceConfig
180
- | SourceComponent
193
+ | AnySourceComponent
181
194
  | SourceGroup
182
195
  | SourceTrace
183
196
  | SourcePort
@@ -192,6 +205,8 @@ export type AnyElement =
192
205
  | SchematicConfig
193
206
  | SchematicPort
194
207
  | SchematicText
208
+ | SchematicLine
209
+ | SchematicPath
195
210
 
196
211
  export type ElementType = AnyElement["type"]
197
212
  export type ElementOfType<T extends ElementType> = Extract<
@@ -56,8 +56,12 @@ export type AnySourceComponent =
56
56
  | SimpleGround
57
57
  | SimpleDiode
58
58
  | LightEmittingDiode
59
+ | Omit<SourceComponentBase, "ftype">
59
60
 
60
- export type SourceComponentFType = AnySourceComponent["ftype"]
61
+ export type SourceComponentFType = Extract<
62
+ AnySourceComponent,
63
+ { ftype: string }
64
+ >["ftype"]
61
65
  export type SourceComponent<
62
66
  T extends SourceComponentFType = SourceComponentFType
63
67
  > = Extract<AnySourceComponent, { ftype: T }>
@@ -13,6 +13,7 @@ export const collectElementRefs = (elm: AnyElement, allElms: AnyElement[]) => {
13
13
  "schematic_port",
14
14
  "schematic_box",
15
15
  "schematic_line",
16
+ "schematic_path",
16
17
  ].includes(elm.type)
17
18
  ) {
18
19
  const schematic_children = allElms.filter(
@@ -10,6 +10,7 @@ interface Props {
10
10
  paths: Array<{
11
11
  strokeWidth: number
12
12
  stroke: string
13
+ fill?: string
13
14
  d: string
14
15
  }>
15
16
  }
@@ -51,7 +52,7 @@ export const SVGPathComponent = ({ size, center, rotation, paths }: Props) => {
51
52
  {paths.map((p, i) => (
52
53
  <path
53
54
  key={i}
54
- fill="none"
55
+ fill={p.fill ?? "none"}
55
56
  strokeLinecap="round"
56
57
  strokeWidth={2 * (p.strokeWidth || 1)}
57
58
  stroke={p.stroke || "red"}
@@ -15,6 +15,8 @@ interface Props {
15
15
  */
16
16
  export const SchematicComponent = ({ component }: Props) => {
17
17
  const { source, schematic } = component
18
+ if (!source.ftype) return null
19
+
18
20
  switch (source.ftype) {
19
21
  case "simple_resistor": {
20
22
  return <Component.SimpleResistor component={{ source, schematic }} />
@@ -7,6 +7,7 @@ import SchematicBox from "./SchematicBox"
7
7
  import SchematicTrace from "./SchematicTrace"
8
8
  import SchematicLine from "./SchematicLine"
9
9
  import RenderError from "./RenderError"
10
+ import SchematicPath from "./SchematicPath"
10
11
 
11
12
  /**
12
13
  * Render any @tsbuilder/builder AnyElement that can be put on a schematic.
@@ -52,6 +53,12 @@ export const SchematicElement = ({
52
53
  )
53
54
  }
54
55
 
56
+ if (element.type === "schematic_path") {
57
+ return (
58
+ <SchematicPath path={collectElementRefs(element, allElements) as any} />
59
+ )
60
+ }
61
+
55
62
  if (element.type === "schematic_text") {
56
63
  return <SchematicText schematic_text={element} />
57
64
  }
@@ -0,0 +1,52 @@
1
+ import * as Type from "lib/types"
2
+ import { directionToVec } from "lib/utils/direction-to-vec"
3
+ import * as Component from "."
4
+ import Path from "svg-path-generator"
5
+ import getSVGPathBounds from "lib/utils/get-svg-path-bounds"
6
+
7
+ interface Props {
8
+ path: {
9
+ schematic: Type.SchematicPath
10
+ }
11
+ }
12
+
13
+ export const SchematicPath = (props: Props) => {
14
+ console.log("SchematicPath", props)
15
+ const { points, is_filled, is_closed, fill_color } = props.path.schematic
16
+
17
+ if (points.length === 0) return null
18
+ const path = Path()
19
+ path.moveTo(points[0].x, points[0].y)
20
+ for (const point of points.slice(1)) {
21
+ path.lineTo(point.x, point.y)
22
+ }
23
+ if (is_closed) {
24
+ path.closePath()
25
+ }
26
+ const d = path.toString()
27
+ const pathBounds = getSVGPathBounds(d)
28
+ pathBounds.height = Math.max(pathBounds.height, 1)
29
+ pathBounds.width = Math.max(pathBounds.width, 1)
30
+ const center = {
31
+ x: pathBounds.minX + pathBounds.width / 2,
32
+ y: pathBounds.minY + pathBounds.height / 2,
33
+ }
34
+
35
+ return (
36
+ <Component.SVGPathComponent
37
+ rotation={0}
38
+ center={center}
39
+ size={pathBounds}
40
+ paths={[
41
+ {
42
+ stroke: is_filled ? "none" : fill_color ?? "red",
43
+ strokeWidth: 0.02,
44
+ fill: is_filled ? fill_color ?? "red" : undefined,
45
+ d: d,
46
+ },
47
+ ]}
48
+ />
49
+ )
50
+ }
51
+
52
+ export default SchematicPath
@@ -0,0 +1,17 @@
1
+ import { Schematic } from "Schematic"
2
+
3
+ export const ComponentDrawingExample = () => {
4
+ return (
5
+ <Schematic style={{ height: 600 }}>
6
+ <component>
7
+ <schematicbox x={0} y={0} width="6mm" height="6mm" />
8
+ <schematicline x1={0} y1={0} x2={1} y2={1} />
9
+ </component>
10
+ </Schematic>
11
+ )
12
+ }
13
+
14
+ export default {
15
+ title: "ComponentDrawingExample",
16
+ component: ComponentDrawingExample,
17
+ }
@@ -0,0 +1,40 @@
1
+ import { AnyElement } from "lib/types"
2
+ import { Schematic } from "../Schematic"
3
+
4
+ const soup: AnyElement[] = [
5
+ {
6
+ type: "source_component",
7
+ source_component_id: "source_component_1",
8
+ name: "my source component",
9
+ },
10
+ {
11
+ type: "schematic_component",
12
+ schematic_component_id: "schematic_component_1",
13
+ center: { x: 0, y: 0 },
14
+ rotation: 0,
15
+ size: { width: 1, height: 1 },
16
+ source_component_id: "source_component_1",
17
+ },
18
+ {
19
+ type: "schematic_path",
20
+ drawing_type: "path",
21
+ is_filled: true,
22
+ is_closed: true,
23
+ fill_color: "red",
24
+ points: [
25
+ { x: 0, y: 0 },
26
+ { x: 1, y: 1 },
27
+ { x: 0, y: 1 },
28
+ ],
29
+ schematic_component_id: "schematic_component_1",
30
+ },
31
+ ]
32
+
33
+ export const SchematicPathSoup = () => {
34
+ return <Schematic style={{ height: 500 }} soup={soup} />
35
+ }
36
+
37
+ export default {
38
+ title: "SchematicPathSoup",
39
+ component: SchematicPathSoup,
40
+ }