schematic-symbols 0.0.112 → 0.0.113

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.
@@ -1,5 +1,16 @@
1
1
  import type { SchSymbol } from "./types"
2
+ import { getBoundsOfPrimitives } from "./utils/getBoundsOfPrimitives"
2
3
 
3
- export function defineSymbol(symbol: SchSymbol): SchSymbol {
4
- return symbol
4
+ export function defineSymbol(
5
+ symbol: Omit<SchSymbol, "size"> & Partial<Pick<SchSymbol, "size">>,
6
+ ): SchSymbol {
7
+ let size = symbol.size
8
+ if (!size) {
9
+ const bounds = getBoundsOfPrimitives(symbol.primitives)
10
+ size = {
11
+ width: bounds.maxX - bounds.minX,
12
+ height: bounds.maxY - bounds.minY,
13
+ }
14
+ }
15
+ return { ...symbol, size }
5
16
  }
package/drawing/getSvg.ts CHANGED
@@ -137,7 +137,35 @@ export function getInnerSvg(
137
137
 
138
138
  const centerDiamond = createDiamondElement(symbol.center)
139
139
 
140
- return [svgElements.join("\n "), portElements, centerDiamond].join("\n")
140
+ const debugElements = []
141
+ if (debug) {
142
+ const topLeft = {
143
+ x: symbol.center.x - size.width / 2,
144
+ y: symbol.center.y - size.height / 2,
145
+ }
146
+ debugElements.push(
147
+ `<text x="${topLeft.x}" y="${topLeft.y}" style="font: 0.05px monospace; fill: #833;">${size.width.toFixed(2)} x ${size.height.toFixed(2)}</text>`,
148
+ )
149
+
150
+ // Show all available port labels
151
+ ports.forEach((port, i) => {
152
+ if (port.labels.length > 1) {
153
+ const alternateLabels = port.labels.slice(1).join(", ")
154
+ debugElements.push(
155
+ `<text x="${topLeft.x}" y="${topLeft.y - (i + 1) * 0.05}" dy="-0.15" style="font: 0.05px monospace; fill: #833;">${port.labels[0]} [${alternateLabels}]</text>`,
156
+ )
157
+ }
158
+ })
159
+
160
+ debugElements.push(...debugElements)
161
+ }
162
+
163
+ return [
164
+ svgElements.join("\n "),
165
+ portElements,
166
+ centerDiamond,
167
+ ...debugElements,
168
+ ].join("\n")
141
169
  }
142
170
 
143
171
  export function getSvg(
@@ -1,5 +1,6 @@
1
1
  import { NinePointAnchor, SchSymbol } from "drawing/types"
2
2
  import { rotateRightFacingSymbol } from "drawing/rotateSymbol"
3
+ import { getBoundsOfPrimitives } from "drawing/utils/getBoundsOfPrimitives"
3
4
 
4
5
  interface ModifySymbolBuilder {
5
6
  changeTextAnchor(
@@ -18,6 +19,7 @@ class SymbolModifier implements ModifySymbolBuilder {
18
19
 
19
20
  constructor(symbol: SchSymbol) {
20
21
  this.symbol = JSON.parse(JSON.stringify(symbol))
22
+ this.symbol.size = this.computeSize()
21
23
  }
22
24
 
23
25
  changeTextAnchor(
@@ -60,8 +62,16 @@ class SymbolModifier implements ModifySymbolBuilder {
60
62
  return this
61
63
  }
62
64
 
65
+ computeSize(): { width: number; height: number } {
66
+ const bounds = getBoundsOfPrimitives(this.symbol.primitives)
67
+ return {
68
+ width: bounds.maxX - bounds.minX,
69
+ height: bounds.maxY - bounds.minY,
70
+ }
71
+ }
72
+
63
73
  build(): SchSymbol {
64
- return this.symbol
74
+ return { ...this.symbol, size: this.computeSize() }
65
75
  }
66
76
  }
67
77
 
@@ -83,9 +93,5 @@ export const modifySymbol = (symbol: any): ModifySymbolBuilder => {
83
93
  x: symbol.bounds.centerX,
84
94
  y: symbol.bounds.centerY,
85
95
  },
86
- size: symbol.size ?? {
87
- width: symbol.bounds.width,
88
- height: symbol.bounds.height,
89
- },
90
96
  })
91
97
  }
@@ -6,6 +6,7 @@ import type {
6
6
  Port,
7
7
  NinePointAnchor,
8
8
  } from "./types"
9
+ import { getBoundsOfPrimitives } from "./utils/getBoundsOfPrimitives"
9
10
 
10
11
  // Update rotateAnchor to handle all anchor rotations based on orientation
11
12
  const rotateRightFacingAnchor = (
@@ -295,19 +296,15 @@ export const rotateRightFacingSymbol = (
295
296
  }),
296
297
  )
297
298
 
299
+ const bounds = getBoundsOfPrimitives(rotatedPrimitives)
300
+
298
301
  return {
299
302
  primitives: rotatedPrimitives,
300
303
  center,
301
304
  ports: rotatedPorts,
302
305
  size: {
303
- width:
304
- newOrientation === "up" || newOrientation === "down"
305
- ? size.width
306
- : size.height,
307
- height:
308
- newOrientation === "up" || newOrientation === "down"
309
- ? size.height
310
- : size.width,
306
+ width: bounds.maxX - bounds.minX,
307
+ height: bounds.maxY - bounds.minY,
311
308
  },
312
309
  ...overrides,
313
310
  }
@@ -0,0 +1,51 @@
1
+ import { Primitive, Point } from "../types"
2
+
3
+ export function getBoundsOfPrimitives(primitives: Primitive[]): {
4
+ minX: number
5
+ maxX: number
6
+ minY: number
7
+ maxY: number
8
+ } {
9
+ if (primitives.length === 0) {
10
+ return { minX: 0, maxX: 0, minY: 0, maxY: 0 }
11
+ }
12
+
13
+ let minX = Infinity
14
+ let maxX = -Infinity
15
+ let minY = Infinity
16
+ let maxY = -Infinity
17
+
18
+ const updateBounds = (point: Point) => {
19
+ minX = Math.min(minX, point.x)
20
+ maxX = Math.max(maxX, point.x)
21
+ minY = Math.min(minY, point.y)
22
+ maxY = Math.max(maxY, point.y)
23
+ }
24
+
25
+ primitives.forEach((primitive) => {
26
+ switch (primitive.type) {
27
+ case "path":
28
+ primitive.points.forEach(updateBounds)
29
+ break
30
+ case "text":
31
+ updateBounds({ x: primitive.x, y: primitive.y })
32
+ break
33
+ case "circle": {
34
+ const { x, y, radius } = primitive
35
+ updateBounds({ x: x - radius, y: y - radius })
36
+ updateBounds({ x: x + radius, y: y + radius })
37
+ break
38
+ }
39
+ case "box": {
40
+ const { x, y, width, height } = primitive
41
+ const halfWidth = width / 2
42
+ const halfHeight = height / 2
43
+ updateBounds({ x: x - halfWidth, y: y - halfHeight })
44
+ updateBounds({ x: x + halfWidth, y: y + halfHeight })
45
+ break
46
+ }
47
+ }
48
+ })
49
+
50
+ return { minX, maxX, minY, maxY }
51
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "schematic-symbols",
3
3
  "main": "./dist/index.js",
4
- "version": "0.0.112",
4
+ "version": "0.0.113",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist",
@@ -23,7 +23,6 @@ export default modifySymbol({
23
23
  { ...refblocks.left1, labels: ["1"] }, // TODO add more "standard" labels
24
24
  { ...refblocks.right1, labels: ["2"] }, // TODO add more "standard" labels
25
25
  ],
26
- size: { width: bounds.width, height: bounds.height },
27
26
  center: { x: bounds.centerX, y: bounds.centerY },
28
27
  })
29
28
  .changeTextAnchor("{VAL}", "middle_bottom")
@@ -23,7 +23,6 @@ export default modifySymbol({
23
23
  { ...refblocks.left1, labels: ["1"] }, // TODO add more "standard" labels
24
24
  { ...refblocks.right1, labels: ["2"] }, // TODO add more "standard" labels
25
25
  ],
26
- size: { width: bounds.width, height: bounds.height },
27
26
  center: { x: bounds.centerX, y: bounds.centerY },
28
27
  })
29
28
  .changeTextAnchor("{VAL}", "middle_top")
@@ -23,7 +23,6 @@ export default modifySymbol({
23
23
  { ...refblocks.left1, labels: ["1"] }, // TODO add more "standard" labels
24
24
  { ...refblocks.right1, labels: ["2"] }, // TODO add more "standard" labels
25
25
  ],
26
- size: { width: bounds.width, height: bounds.height },
27
26
  center: { x: bounds.centerX, y: bounds.centerY },
28
27
  })
29
28
  .changeTextAnchor("{VAL}", "middle_top")
@@ -23,7 +23,6 @@ export default modifySymbol({
23
23
  { ...refblocks.left1, labels: ["1"] }, // TODO add more "standard" labels
24
24
  { ...refblocks.right1, labels: ["2"] }, // TODO add more "standard" labels
25
25
  ],
26
- size: { width: bounds.width, height: bounds.height },
27
26
  center: { x: bounds.centerX, y: bounds.centerY },
28
27
  })
29
28
  .changeTextAnchor("{VAL}", "middle_bottom")