@tscircuit/schematic-viewer 1.0.11 → 1.0.13
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/dist/index.js +83 -22
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/Schematic.tsx +3 -4
- package/src/lib/types/core.ts +21 -0
- package/src/lib/utils/collect-element-refs.ts +14 -3
- package/src/schematic-components/SVGPathComponent.tsx +3 -3
- package/src/schematic-components/SchematicBox.tsx +29 -0
- package/src/schematic-components/SchematicBug.tsx +11 -6
- package/src/schematic-components/SchematicComponent.tsx +1 -0
- package/src/schematic-components/SchematicElement.tsx +14 -0
- package/src/schematic-components/SchematicLine.tsx +35 -0
- package/src/schematic-components/SchematicText.tsx +1 -0
- package/src/stories/bug-connections.stories.tsx +26 -0
- package/src/stories/net-alias.stories.tsx +92 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tscircuit/schematic-viewer",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "tscircuit/schematic-viewer",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@storybook/nextjs": "^7.0.26",
|
|
32
32
|
"@storybook/react": "^7.0.26",
|
|
33
33
|
"@storybook/testing-library": "^0.0.14-next.2",
|
|
34
|
-
"@tscircuit/builder": "^1.2.
|
|
34
|
+
"@tscircuit/builder": "^1.2.12",
|
|
35
35
|
"@tscircuit/react-fiber": "^1.0.4",
|
|
36
36
|
"@types/node": "^18.6.0",
|
|
37
37
|
"@types/react": "^18.0.15",
|
|
@@ -58,6 +58,6 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"react-error-boundary": "^4.0.4",
|
|
61
|
-
"use-mouse-matrix-transform": "^1.1.
|
|
61
|
+
"use-mouse-matrix-transform": "^1.1.5"
|
|
62
62
|
}
|
|
63
63
|
}
|
package/src/Schematic.tsx
CHANGED
|
@@ -56,17 +56,16 @@ export const Schematic = ({
|
|
|
56
56
|
const { center, width, height } = findBoundsAndCenter(elements)
|
|
57
57
|
setElements(elements)
|
|
58
58
|
setProject(createProjectFromElements(elements))
|
|
59
|
-
console.log(elmBounds.width)
|
|
60
59
|
setTransform(
|
|
61
60
|
compose(
|
|
62
61
|
translate((elmBounds.width ?? 0) / 2, (elmBounds.height ?? 0) / 2),
|
|
63
62
|
// translate(100, 0),
|
|
64
63
|
scale(100, 100, 0, 0),
|
|
65
|
-
translate(-center.x, -center.y)
|
|
66
|
-
)
|
|
64
|
+
translate(-center.x, -center.y)
|
|
65
|
+
)
|
|
67
66
|
)
|
|
68
67
|
},
|
|
69
|
-
[setElements, setTransform]
|
|
68
|
+
[setElements, setTransform]
|
|
70
69
|
)
|
|
71
70
|
|
|
72
71
|
useEffect(() => {
|
package/src/lib/types/core.ts
CHANGED
|
@@ -48,6 +48,27 @@ export interface SchematicComponent {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
export interface SchematicBox {
|
|
52
|
+
type: "schematic_box"
|
|
53
|
+
drawing_type: "box"
|
|
54
|
+
schematic_box_id: string
|
|
55
|
+
schematic_component_id: string
|
|
56
|
+
x: number
|
|
57
|
+
y: number
|
|
58
|
+
width: number
|
|
59
|
+
height: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface SchematicLine {
|
|
63
|
+
type: "schematic_line"
|
|
64
|
+
drawing_type: "line"
|
|
65
|
+
schematic_component_id: string
|
|
66
|
+
x1: number
|
|
67
|
+
y1: number
|
|
68
|
+
x2: number
|
|
69
|
+
y2: number
|
|
70
|
+
}
|
|
71
|
+
|
|
51
72
|
export interface SchematicTrace {
|
|
52
73
|
type: "schematic_trace"
|
|
53
74
|
schematic_trace_id: string
|
|
@@ -7,11 +7,22 @@ export const collectElementRefs = (elm: AnyElement, allElms: AnyElement[]) => {
|
|
|
7
7
|
e.source_component_id === (elm as any).source_component_id
|
|
8
8
|
)
|
|
9
9
|
if (
|
|
10
|
-
[
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
[
|
|
11
|
+
"schematic_component",
|
|
12
|
+
"schematic_trace",
|
|
13
|
+
"schematic_port",
|
|
14
|
+
"schematic_box",
|
|
15
|
+
"schematic_line",
|
|
16
|
+
].includes(elm.type)
|
|
13
17
|
) {
|
|
18
|
+
const schematic_children = allElms.filter(
|
|
19
|
+
(e) =>
|
|
20
|
+
"schematic_component_id" in e &&
|
|
21
|
+
e.schematic_component_id === (elm as any).schematic_component_id
|
|
22
|
+
)
|
|
23
|
+
|
|
14
24
|
return {
|
|
25
|
+
schematic_children,
|
|
15
26
|
schematic: elm,
|
|
16
27
|
source: source_component,
|
|
17
28
|
}
|
|
@@ -22,9 +22,9 @@ export const SVGPathComponent = ({ size, center, rotation, paths }: Props) => {
|
|
|
22
22
|
Math.abs(pathBounds.width / pathBounds.height - size.width / size.height) >
|
|
23
23
|
0.01
|
|
24
24
|
if (badRatio) {
|
|
25
|
-
console.warn(
|
|
26
|
-
|
|
27
|
-
)
|
|
25
|
+
// console.warn(
|
|
26
|
+
// `Ratio doesn't match for component. ${pathBounds.width}:${pathBounds.height} is not close to ${size.width}:${size.height}`
|
|
27
|
+
// )
|
|
28
28
|
}
|
|
29
29
|
pathBounds.height = Math.max(pathBounds.height, 1)
|
|
30
30
|
pathBounds.width = Math.max(pathBounds.width, 1)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as Type from "lib/types"
|
|
2
|
+
import { directionToVec } from "lib/utils/direction-to-vec"
|
|
3
|
+
import * as Component from "./"
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
box: {
|
|
7
|
+
schematic: Type.SchematicBox
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const SchematicBox = ({ box: { schematic } }: Props) => {
|
|
12
|
+
const { width: w, height: h } = schematic
|
|
13
|
+
return (
|
|
14
|
+
<Component.SVGPathComponent
|
|
15
|
+
rotation={0}
|
|
16
|
+
center={schematic}
|
|
17
|
+
size={{ width: schematic.width, height: schematic.height }}
|
|
18
|
+
paths={[
|
|
19
|
+
{
|
|
20
|
+
stroke: "red",
|
|
21
|
+
strokeWidth: 0.2,
|
|
22
|
+
d: `M 0 0 l ${w} 0 l 0 ${h} l -${w} 0 z`,
|
|
23
|
+
},
|
|
24
|
+
]}
|
|
25
|
+
/>
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default SchematicBox
|
|
@@ -12,16 +12,21 @@ interface Props {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export const SchematicBug = ({ component: { source, schematic } }: Props) => {
|
|
15
|
-
const
|
|
15
|
+
const port_arrangement = {
|
|
16
16
|
top_size: 0,
|
|
17
17
|
bottom_size: 0,
|
|
18
18
|
...schematic.port_arrangement,
|
|
19
19
|
}
|
|
20
20
|
// const port_labels = schematic.port_labels!
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const { total_ports } =
|
|
24
|
-
|
|
21
|
+
let bugw = schematic.size.width // bug width
|
|
22
|
+
let bugh = schematic.size.height
|
|
23
|
+
const { total_ports, width, height } =
|
|
24
|
+
getPortArrangementSize(port_arrangement)
|
|
25
|
+
|
|
26
|
+
// TODO remove, this seems to be due to a builder bug
|
|
27
|
+
if (isNaN(bugw)) bugw = width
|
|
28
|
+
if (isNaN(bugh)) bugh = height
|
|
29
|
+
// TODO throw if schematic.size doesn't match computed port_arrangement size
|
|
25
30
|
|
|
26
31
|
const paths = [
|
|
27
32
|
{
|
|
@@ -32,7 +37,7 @@ export const SchematicBug = ({ component: { source, schematic } }: Props) => {
|
|
|
32
37
|
} ${bugh / 2} L ${-bugw / 2} ${bugh / 2}Z`,
|
|
33
38
|
},
|
|
34
39
|
...range(1, total_ports + 1).map((portNum) => {
|
|
35
|
-
const pos = getPortPosition(
|
|
40
|
+
const pos = getPortPosition(port_arrangement, portNum)
|
|
36
41
|
|
|
37
42
|
const x2 =
|
|
38
43
|
pos.side === "left"
|
|
@@ -3,7 +3,9 @@ import { collectElementRefs } from "lib/utils/collect-element-refs"
|
|
|
3
3
|
import SchematicComponent from "./SchematicComponent"
|
|
4
4
|
import SchematicPort from "./SchematicPort"
|
|
5
5
|
import SchematicText from "./SchematicText"
|
|
6
|
+
import SchematicBox from "./SchematicBox"
|
|
6
7
|
import SchematicTrace from "./SchematicTrace"
|
|
8
|
+
import SchematicLine from "./SchematicLine"
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* Render any @tsbuilder/builder AnyElement that can be put on a schematic.
|
|
@@ -37,6 +39,18 @@ export const SchematicElement = ({
|
|
|
37
39
|
)
|
|
38
40
|
}
|
|
39
41
|
|
|
42
|
+
if (element.type === "schematic_box") {
|
|
43
|
+
return (
|
|
44
|
+
<SchematicBox box={collectElementRefs(element, allElements) as any} />
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (element.type === "schematic_line") {
|
|
49
|
+
return (
|
|
50
|
+
<SchematicLine line={collectElementRefs(element, allElements) as any} />
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
40
54
|
if (element.type === "schematic_text") {
|
|
41
55
|
return <SchematicText schematic_text={element} />
|
|
42
56
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as Type from "lib/types"
|
|
2
|
+
import { directionToVec } from "lib/utils/direction-to-vec"
|
|
3
|
+
import * as Component from "."
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
line: {
|
|
7
|
+
schematic: Type.SchematicLine
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const SchematicLine = ({ line: { schematic } }: Props) => {
|
|
12
|
+
const { x1, x2, y1, y2 } = schematic
|
|
13
|
+
const dx = x2 - x1
|
|
14
|
+
const dy = y2 - y1
|
|
15
|
+
const width = Math.abs(dx) + 0.1
|
|
16
|
+
const height = Math.abs(dy) + 0.1
|
|
17
|
+
const center = { x: x1 + dx / 2, y: y1 + dy / 2 }
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<Component.SVGPathComponent
|
|
21
|
+
rotation={0}
|
|
22
|
+
center={center}
|
|
23
|
+
size={{ width, height }}
|
|
24
|
+
paths={[
|
|
25
|
+
{
|
|
26
|
+
stroke: "red",
|
|
27
|
+
strokeWidth: 10,
|
|
28
|
+
d: `M 0 0 l ${dx * 100} ${dy * 100}`,
|
|
29
|
+
},
|
|
30
|
+
]}
|
|
31
|
+
/>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default SchematicLine
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Schematic } from "Schematic"
|
|
2
|
+
|
|
3
|
+
export const BugConnections = () => {
|
|
4
|
+
return (
|
|
5
|
+
<Schematic style={{ height: 600 }}>
|
|
6
|
+
<bug
|
|
7
|
+
name="B1"
|
|
8
|
+
port_arrangement={{
|
|
9
|
+
left_size: 3,
|
|
10
|
+
right_size: 2,
|
|
11
|
+
}}
|
|
12
|
+
sch_cx={8}
|
|
13
|
+
sch_cy={3}
|
|
14
|
+
port_labels={{
|
|
15
|
+
"1": "D0",
|
|
16
|
+
"2": "D1",
|
|
17
|
+
}}
|
|
18
|
+
/>
|
|
19
|
+
</Schematic>
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default {
|
|
24
|
+
title: "BugConnections",
|
|
25
|
+
component: BugConnections,
|
|
26
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Schematic } from "../Schematic"
|
|
2
|
+
|
|
3
|
+
const soup = [
|
|
4
|
+
{
|
|
5
|
+
type: "source_component",
|
|
6
|
+
source_component_id: "net_alias_0",
|
|
7
|
+
ftype: "net_alias",
|
|
8
|
+
source: {
|
|
9
|
+
type: "source_component",
|
|
10
|
+
source_component_id: "net_alias_0",
|
|
11
|
+
ftype: "net_alias",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
type: "schematic_component",
|
|
16
|
+
schematic_component_id: "schematic_net_alias_component_0",
|
|
17
|
+
source_component_id: "net_alias_0",
|
|
18
|
+
center: {
|
|
19
|
+
x: 2,
|
|
20
|
+
y: 2,
|
|
21
|
+
},
|
|
22
|
+
rotation: 0,
|
|
23
|
+
size: {
|
|
24
|
+
width: 0,
|
|
25
|
+
height: 0,
|
|
26
|
+
},
|
|
27
|
+
source: {
|
|
28
|
+
type: "source_component",
|
|
29
|
+
source_component_id: "net_alias_0",
|
|
30
|
+
ftype: "net_alias",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
type: "source_port",
|
|
35
|
+
name: "gnd",
|
|
36
|
+
source_port_id: "source_port_0",
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
type: "schematic_port",
|
|
40
|
+
schematic_port_id: "schematic_port_0",
|
|
41
|
+
source_port_id: "source_port_0",
|
|
42
|
+
center: {
|
|
43
|
+
x: 2,
|
|
44
|
+
y: 2,
|
|
45
|
+
},
|
|
46
|
+
source: {
|
|
47
|
+
type: "source_port",
|
|
48
|
+
name: "gnd",
|
|
49
|
+
source_port_id: "source_port_0",
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
drawing_type: "line",
|
|
54
|
+
type: "schematic_line",
|
|
55
|
+
x1: 0,
|
|
56
|
+
y1: 1,
|
|
57
|
+
x2: 4,
|
|
58
|
+
y2: 1,
|
|
59
|
+
schematic_component_id: "schematic_net_alias_component_0",
|
|
60
|
+
source: null,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
drawing_type: "line",
|
|
64
|
+
type: "schematic_line",
|
|
65
|
+
x1: 2,
|
|
66
|
+
y1: 1,
|
|
67
|
+
x2: 2,
|
|
68
|
+
y2: 2,
|
|
69
|
+
schematic_component_id: "schematic_net_alias_component_0",
|
|
70
|
+
source: null,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
anchor: "center",
|
|
74
|
+
type: "schematic_text",
|
|
75
|
+
position: {
|
|
76
|
+
x: 2,
|
|
77
|
+
y: 0.75,
|
|
78
|
+
},
|
|
79
|
+
text: "gnd",
|
|
80
|
+
schematic_component_id: "schematic_net_alias_component_0",
|
|
81
|
+
source: null,
|
|
82
|
+
},
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
export const NetAliasSoup = () => {
|
|
86
|
+
return <Schematic style={{ height: 500 }} soup={soup} />
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export default {
|
|
90
|
+
title: "NetAliasSoup",
|
|
91
|
+
component: NetAliasSoup,
|
|
92
|
+
}
|