@tscircuit/schematic-viewer 1.0.12 → 1.0.14
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 +99 -19
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
- package/src/Schematic.tsx +20 -1
- package/src/lib/types/core.ts +21 -0
- package/src/lib/utils/collect-element-refs.ts +14 -3
- package/src/schematic-components/SchematicBox.tsx +29 -0
- 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/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.14",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "tscircuit/schematic-viewer",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
60
|
"react-error-boundary": "^4.0.4",
|
|
61
|
+
"react-supergrid": "^1.0.3",
|
|
61
62
|
"use-mouse-matrix-transform": "^1.1.5"
|
|
62
63
|
}
|
|
63
64
|
}
|
package/src/Schematic.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useCallback, useEffect, useState } from "react"
|
|
2
2
|
import { ProjectComponent } from "schematic-components"
|
|
3
|
+
import { SuperGrid, toMMSI } from "react-supergrid"
|
|
3
4
|
import {
|
|
4
5
|
createProjectBuilder,
|
|
5
6
|
createProjectFromElements,
|
|
@@ -13,6 +14,7 @@ import { useMouseMatrixTransform } from "use-mouse-matrix-transform"
|
|
|
13
14
|
import { ErrorBoundary } from "react-error-boundary"
|
|
14
15
|
import { identity, compose, scale, translate } from "transformation-matrix"
|
|
15
16
|
import { useRenderContext } from "lib/render-context"
|
|
17
|
+
import useMeasure from "react-use-measure"
|
|
16
18
|
|
|
17
19
|
const fallbackRender =
|
|
18
20
|
(elm) =>
|
|
@@ -24,6 +26,9 @@ const fallbackRender =
|
|
|
24
26
|
)
|
|
25
27
|
}
|
|
26
28
|
|
|
29
|
+
const toMMSINeg = (v: number, z: number) =>
|
|
30
|
+
v >= 0 ? toMMSI(v, z) : `-${toMMSI(-v, z)}`
|
|
31
|
+
|
|
27
32
|
export const Schematic = ({
|
|
28
33
|
children,
|
|
29
34
|
elements: initialElements,
|
|
@@ -44,6 +49,8 @@ export const Schematic = ({
|
|
|
44
49
|
const [elements, setElements] = useState<any>(initialSoup ?? [])
|
|
45
50
|
const [project, setProject] = useState<any>(null)
|
|
46
51
|
const setCameraTransform = useRenderContext((s) => s.setCameraTransform)
|
|
52
|
+
const cameraTransform = useRenderContext((s) => s.camera_transform)
|
|
53
|
+
const [boundsRef, bounds] = useMeasure()
|
|
47
54
|
const { ref, setTransform } = useMouseMatrixTransform({
|
|
48
55
|
onSetTransform: (transform) => {
|
|
49
56
|
setCameraTransform(transform)
|
|
@@ -96,8 +103,20 @@ export const Schematic = ({
|
|
|
96
103
|
cursor: "grab",
|
|
97
104
|
...style,
|
|
98
105
|
}}
|
|
99
|
-
ref={
|
|
106
|
+
ref={(el) => {
|
|
107
|
+
ref.current = el
|
|
108
|
+
boundsRef(el)
|
|
109
|
+
}}
|
|
100
110
|
>
|
|
111
|
+
<SuperGrid
|
|
112
|
+
stringifyCoord={(x, y, z) => {
|
|
113
|
+
if (z === 0) return ""
|
|
114
|
+
return `${toMMSINeg(x, z)}, ${toMMSINeg(y, z)}`
|
|
115
|
+
}}
|
|
116
|
+
width={bounds.width}
|
|
117
|
+
height={bounds.height}
|
|
118
|
+
transform={cameraTransform}
|
|
119
|
+
/>
|
|
101
120
|
{elements?.map((elm, i) => (
|
|
102
121
|
<ErrorBoundary key={i} fallbackRender={fallbackRender(elm)}>
|
|
103
122
|
<SchematicElement
|
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
|
}
|
|
@@ -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
|
|
@@ -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,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
|
+
}
|