@vessel-dsp/core 0.5.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 (99) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +6 -0
  3. package/package.json +56 -0
  4. package/src/editor/commands.ts +344 -0
  5. package/src/editor/factory.ts +148 -0
  6. package/src/editor/history.ts +142 -0
  7. package/src/editor/index.ts +11 -0
  8. package/src/editor/layout.ts +207 -0
  9. package/src/formats/circuit-json/serializer.ts +1410 -0
  10. package/src/formats/document.ts +274 -0
  11. package/src/formats/interchange/parser.ts +1165 -0
  12. package/src/formats/interchange/serializer.ts +594 -0
  13. package/src/formats/ltspice/catalog.ts +181 -0
  14. package/src/formats/ltspice/encoding.ts +151 -0
  15. package/src/formats/ltspice/parser.ts +432 -0
  16. package/src/formats/ltspice/serializer.ts +169 -0
  17. package/src/formats/schx/catalog.ts +439 -0
  18. package/src/formats/schx/parser.ts +261 -0
  19. package/src/formats/schx/runtime-descriptors.ts +502 -0
  20. package/src/formats/schx/serializer.ts +211 -0
  21. package/src/formats/schx/transforms.ts +38 -0
  22. package/src/formats/spice/parser.ts +373 -0
  23. package/src/formats/spice/serializer.ts +43 -0
  24. package/src/index.ts +205 -0
  25. package/src/model/connectivity.ts +239 -0
  26. package/src/model/netlist.ts +375 -0
  27. package/src/model/properties.ts +101 -0
  28. package/src/model/quantity.ts +173 -0
  29. package/src/model/types.ts +309 -0
  30. package/src/model/validation.ts +985 -0
  31. package/src/model/wires.ts +86 -0
  32. package/src/panel/extract.ts +878 -0
  33. package/src/panel/index.ts +39 -0
  34. package/src/panel/knobs.ts +70 -0
  35. package/src/panel/protocol.ts +117 -0
  36. package/src/panel/types.ts +180 -0
  37. package/src/preview/bounds.ts +85 -0
  38. package/src/preview/box-layout.ts +24 -0
  39. package/src/preview/colors.ts +43 -0
  40. package/src/preview/hanging.ts +94 -0
  41. package/src/preview/junctions.ts +94 -0
  42. package/src/preview/label-layout.ts +90 -0
  43. package/src/preview/ports.ts +101 -0
  44. package/src/preview/renderable-wires.ts +113 -0
  45. package/src/preview/routing.ts +15 -0
  46. package/src/preview/snap.ts +104 -0
  47. package/src/preview/symbols/analog-switch.svg +17 -0
  48. package/src/preview/symbols/battery.svg +16 -0
  49. package/src/preview/symbols/bbd.svg +21 -0
  50. package/src/preview/symbols/bjt-npn.svg +16 -0
  51. package/src/preview/symbols/bjt-pnp.svg +17 -0
  52. package/src/preview/symbols/capacitor-electrolytic.svg +13 -0
  53. package/src/preview/symbols/capacitor.svg +12 -0
  54. package/src/preview/symbols/current-source.svg +14 -0
  55. package/src/preview/symbols/delay-ic.svg +22 -0
  56. package/src/preview/symbols/diode-schottky.svg +12 -0
  57. package/src/preview/symbols/diode-zener.svg +12 -0
  58. package/src/preview/symbols/diode.svg +13 -0
  59. package/src/preview/symbols/flipflop.svg +20 -0
  60. package/src/preview/symbols/ground.svg +12 -0
  61. package/src/preview/symbols/ic-block.svg +20 -0
  62. package/src/preview/symbols/ic.svg +19 -0
  63. package/src/preview/symbols/inductor.svg +11 -0
  64. package/src/preview/symbols/jack-input.svg +16 -0
  65. package/src/preview/symbols/jack-output.svg +16 -0
  66. package/src/preview/symbols/jfet-junction-n.svg +17 -0
  67. package/src/preview/symbols/jfet-n.svg +17 -0
  68. package/src/preview/symbols/jfet-p.svg +17 -0
  69. package/src/preview/symbols/label.svg +8 -0
  70. package/src/preview/symbols/led.svg +18 -0
  71. package/src/preview/symbols/mosfet-n.svg +21 -0
  72. package/src/preview/symbols/mosfet-p.svg +21 -0
  73. package/src/preview/symbols/named-wire.svg +11 -0
  74. package/src/preview/symbols/opamp.svg +21 -0
  75. package/src/preview/symbols/optocoupler.svg +30 -0
  76. package/src/preview/symbols/ota.svg +20 -0
  77. package/src/preview/symbols/pentode.svg +25 -0
  78. package/src/preview/symbols/photoresistor.svg +19 -0
  79. package/src/preview/symbols/port.svg +8 -0
  80. package/src/preview/symbols/potentiometer.svg +15 -0
  81. package/src/preview/symbols/power-amp.svg +20 -0
  82. package/src/preview/symbols/rail.svg +11 -0
  83. package/src/preview/symbols/regulator.svg +13 -0
  84. package/src/preview/symbols/relay.svg +20 -0
  85. package/src/preview/symbols/resistor.svg +11 -0
  86. package/src/preview/symbols/svg-content.ts +59 -0
  87. package/src/preview/symbols/switch-3pdt.svg +32 -0
  88. package/src/preview/symbols/switch-rotary.svg +23 -0
  89. package/src/preview/symbols/switch-spdt.svg +16 -0
  90. package/src/preview/symbols/switch-spst.svg +14 -0
  91. package/src/preview/symbols/switch-toggle.svg +14 -0
  92. package/src/preview/symbols/transformer.svg +17 -0
  93. package/src/preview/symbols/triode.svg +17 -0
  94. package/src/preview/symbols/tube-diode.svg +13 -0
  95. package/src/preview/symbols/unsupported.svg +8 -0
  96. package/src/preview/symbols/variable-resistor.svg +13 -0
  97. package/src/preview/symbols/voltage-source.svg +15 -0
  98. package/src/preview/symbols.ts +207 -0
  99. package/src/preview/wire-chains.ts +153 -0
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Joseph Cheng
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # @vessel-dsp/core
2
+
3
+ Headless Vessel DSP circuit, device, format conversion, and layout model APIs.
4
+
5
+ This package has no React, DOM rendering, AudioContext, or AudioWorklet
6
+ dependency.
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@vessel-dsp/core",
3
+ "version": "0.5.0",
4
+ "description": "Headless Vessel DSP circuit, device, format conversion, and layout model APIs.",
5
+ "keywords": [
6
+ "guitar-pedal",
7
+ "electronics",
8
+ "spice",
9
+ "ltspice",
10
+ "livespice",
11
+ "circuit"
12
+ ],
13
+ "homepage": "https://vessel-dsp.github.io/core/",
14
+ "bugs": {
15
+ "url": "https://github.com/vessel-dsp/core/issues"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/vessel-dsp/core.git"
20
+ },
21
+ "license": "MIT",
22
+ "sideEffects": false,
23
+ "type": "module",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "bun": "./src/index.ts",
28
+ "import": "./dist/index.js"
29
+ },
30
+ "./package.json": {
31
+ "default": "./package.json"
32
+ }
33
+ },
34
+ "main": "./dist/index.js",
35
+ "module": "./dist/index.js",
36
+ "types": "./dist/index.d.ts",
37
+ "files": [
38
+ "dist",
39
+ "src",
40
+ "README.md",
41
+ "LICENSE.md"
42
+ ],
43
+ "scripts": {
44
+ "build": "tsc -p tsconfig.build.json && bun run ../../scripts/fix-dist-imports.ts dist",
45
+ "clean": "rm -rf dist",
46
+ "pack:dry-run": "npm pack --dry-run",
47
+ "typecheck": "tsc --noEmit -p tsconfig.json"
48
+ },
49
+ "dependencies": {
50
+ "circuit-json": "0.0.433",
51
+ "zod": "3"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ }
56
+ }
@@ -0,0 +1,344 @@
1
+ import { parseQuantity } from '../model/quantity';
2
+ import type { CircuitDocument, Component, ComponentKind, Point, PropertyValue } from '../model/types';
3
+ import { isParsedQuantity } from '../model/properties';
4
+ import { buildComponent } from './factory';
5
+ import { tidyDocumentLayout } from './layout';
6
+
7
+ export type DocumentCommand =
8
+ | Readonly<{ type: 'delete-component'; componentId: string }>
9
+ | Readonly<{ type: 'rename-component'; componentId: string; newName: string }>
10
+ | Readonly<{ type: 'set-property'; componentId: string; propertyName: string; value: string }>
11
+ | Readonly<{ type: 'remove-property'; componentId: string; propertyName: string }>
12
+ | Readonly<{ type: 'move-component'; componentId: string; origin: Point }>
13
+ | Readonly<{ type: 'delete-wire'; wireId: string }>
14
+ | Readonly<{ type: 'delete-wires'; wireIds: readonly string[] }>
15
+ | Readonly<{ type: 'add-wire'; from: Point; to: Point }>
16
+ | Readonly<{ type: 'split-wire'; wireId: string; at: Point }>
17
+ | Readonly<{ type: 'merge-wires'; at: Point }>
18
+ | Readonly<{ type: 'tidy-layout' }>
19
+ | Readonly<{
20
+ type: 'add-component';
21
+ kind: ComponentKind;
22
+ origin: Point;
23
+ sourceTypeName?: string | null;
24
+ }>;
25
+
26
+ export function applyDocumentCommand(doc: CircuitDocument, command: DocumentCommand): CircuitDocument {
27
+ switch (command.type) {
28
+ case 'delete-component':
29
+ return deleteComponent(doc, command.componentId);
30
+ case 'rename-component':
31
+ return renameComponent(doc, command.componentId, command.newName);
32
+ case 'set-property':
33
+ return setProperty(doc, command.componentId, command.propertyName, command.value);
34
+ case 'remove-property':
35
+ return removeProperty(doc, command.componentId, command.propertyName);
36
+ case 'move-component':
37
+ return moveComponent(doc, command.componentId, command.origin);
38
+ case 'delete-wire':
39
+ return deleteWire(doc, command.wireId);
40
+ case 'delete-wires':
41
+ return deleteWires(doc, command.wireIds);
42
+ case 'add-wire':
43
+ return addWire(doc, command.from, command.to);
44
+ case 'split-wire':
45
+ return splitWire(doc, command.wireId, command.at);
46
+ case 'merge-wires':
47
+ return mergeWires(doc, command.at);
48
+ case 'tidy-layout':
49
+ return tidyDocumentLayout(doc);
50
+ case 'add-component':
51
+ return addComponent(doc, command.kind, command.origin, command.sourceTypeName ?? null);
52
+ }
53
+ }
54
+
55
+ function addComponent(
56
+ doc: CircuitDocument,
57
+ kind: ComponentKind,
58
+ origin: Point,
59
+ sourceTypeName: string | null,
60
+ ): CircuitDocument {
61
+ const existingIds = new Set(doc.components.map((c) => c.id));
62
+ const component = buildComponent({ kind, origin, sourceTypeName, existingIds });
63
+ return { ...doc, components: [...doc.components, component] };
64
+ }
65
+
66
+ function moveComponent(doc: CircuitDocument, componentId: string, origin: Point): CircuitDocument {
67
+ const target = doc.components.find((c) => c.id === componentId);
68
+ if (target === undefined || (target.origin.x === origin.x && target.origin.y === origin.y)) {
69
+ return doc;
70
+ }
71
+ const dx = origin.x - target.origin.x;
72
+ const dy = origin.y - target.origin.y;
73
+ const terminalMoves = new Map<string, Point>();
74
+ for (const terminal of target.terminals) {
75
+ const newPosition = { x: terminal.position.x + dx, y: terminal.position.y + dy };
76
+ terminalMoves.set(pointKey(terminal.position), newPosition);
77
+ }
78
+
79
+ const components = doc.components.map((c) => {
80
+ if (c.id !== componentId) {
81
+ return c;
82
+ }
83
+ const terminals = c.terminals.map((t) => ({
84
+ name: t.name,
85
+ position: terminalMoves.get(pointKey(t.position)) ?? { x: t.position.x + dx, y: t.position.y + dy },
86
+ }));
87
+ return { ...c, origin, terminals };
88
+ });
89
+
90
+ const wires = doc.wires.map((w) => {
91
+ const newA = terminalMoves.get(pointKey(w.endpoints[0]));
92
+ const newB = terminalMoves.get(pointKey(w.endpoints[1]));
93
+ if (newA === undefined && newB === undefined) {
94
+ return w;
95
+ }
96
+ return {
97
+ ...w,
98
+ endpoints: [newA ?? w.endpoints[0], newB ?? w.endpoints[1]] as readonly [Point, Point],
99
+ };
100
+ });
101
+
102
+ return { ...doc, components, wires };
103
+ }
104
+
105
+ function pointKey(p: Point): string {
106
+ return `${p.x},${p.y}`;
107
+ }
108
+
109
+ function deleteComponent(doc: CircuitDocument, componentId: string): CircuitDocument {
110
+ const components = doc.components.filter((c) => c.id !== componentId);
111
+ if (components.length === doc.components.length) {
112
+ return doc;
113
+ }
114
+ return { ...doc, components };
115
+ }
116
+
117
+ function deleteWire(doc: CircuitDocument, wireId: string): CircuitDocument {
118
+ const wires = doc.wires.filter((w) => w.id !== wireId);
119
+ if (wires.length === doc.wires.length) {
120
+ return doc;
121
+ }
122
+ return { ...doc, wires };
123
+ }
124
+
125
+ function deleteWires(doc: CircuitDocument, wireIds: readonly string[]): CircuitDocument {
126
+ if (wireIds.length === 0) {
127
+ return doc;
128
+ }
129
+ const targets = new Set(wireIds);
130
+ const wires = doc.wires.filter((w) => !targets.has(w.id));
131
+ if (wires.length === doc.wires.length) {
132
+ return doc;
133
+ }
134
+ return { ...doc, wires };
135
+ }
136
+
137
+ function addWire(doc: CircuitDocument, from: Point, to: Point): CircuitDocument {
138
+ if (from.x === to.x && from.y === to.y) {
139
+ return doc;
140
+ }
141
+ const id = uniqueWireId(doc);
142
+ const wire = { id, endpoints: [from, to] as const };
143
+ return { ...doc, wires: [...doc.wires, wire] };
144
+ }
145
+
146
+ function splitWire(doc: CircuitDocument, wireId: string, at: Point): CircuitDocument {
147
+ const target = doc.wires.find((w) => w.id === wireId);
148
+ if (target === undefined) {
149
+ return doc;
150
+ }
151
+ const [a, b] = target.endpoints;
152
+ const snapped = projectOntoSegment(at, a, b);
153
+ if (pointEquals(snapped, a) || pointEquals(snapped, b)) {
154
+ return doc;
155
+ }
156
+ const taken = new Set(doc.wires.map((w) => w.id));
157
+ taken.delete(wireId);
158
+ const firstId = uniqueWireIdFromSet(taken);
159
+ taken.add(firstId);
160
+ const secondId = uniqueWireIdFromSet(taken);
161
+ const replacement = [
162
+ { id: firstId, endpoints: [a, snapped] as const },
163
+ { id: secondId, endpoints: [snapped, b] as const },
164
+ ];
165
+ const wires = doc.wires.flatMap((w) => (w.id === wireId ? replacement : [w]));
166
+ return { ...doc, wires };
167
+ }
168
+
169
+ function mergeWires(doc: CircuitDocument, at: Point): CircuitDocument {
170
+ const meeting: typeof doc.wires[number][] = [];
171
+ for (const wire of doc.wires) {
172
+ if (pointEquals(wire.endpoints[0], at) || pointEquals(wire.endpoints[1], at)) {
173
+ meeting.push(wire);
174
+ }
175
+ }
176
+ if (meeting.length !== 2) {
177
+ return doc;
178
+ }
179
+ for (const component of doc.components) {
180
+ for (const terminal of component.terminals) {
181
+ if (pointEquals(terminal.position, at)) {
182
+ return doc;
183
+ }
184
+ }
185
+ }
186
+ // Reject if another wire's body crosses the corner — that's a T-junction.
187
+ for (const wire of doc.wires) {
188
+ if (wire === meeting[0] || wire === meeting[1]) continue;
189
+ if (pointOnSegmentInterior(at, wire.endpoints[0], wire.endpoints[1])) {
190
+ return doc;
191
+ }
192
+ }
193
+ const [w1, w2] = meeting as [typeof doc.wires[number], typeof doc.wires[number]];
194
+ const outer1 = pointEquals(w1.endpoints[0], at) ? w1.endpoints[1] : w1.endpoints[0];
195
+ const outer2 = pointEquals(w2.endpoints[0], at) ? w2.endpoints[1] : w2.endpoints[0];
196
+ if (pointEquals(outer1, outer2)) {
197
+ // Degenerate: two wires looping back to the same outer point. Just drop both.
198
+ const wires = doc.wires.filter((w) => w.id !== w1.id && w.id !== w2.id);
199
+ return { ...doc, wires };
200
+ }
201
+ const taken = new Set(doc.wires.map((w) => w.id));
202
+ taken.delete(w1.id);
203
+ taken.delete(w2.id);
204
+ const newId = uniqueWireIdFromSet(taken);
205
+ const replacement = { id: newId, endpoints: [outer1, outer2] as const };
206
+ const seen = new Set([w1.id, w2.id]);
207
+ const wires = doc.wires
208
+ .filter((w) => !seen.has(w.id))
209
+ .concat([replacement]);
210
+ return { ...doc, wires };
211
+ }
212
+
213
+ function projectOntoSegment(p: Point, a: Point, b: Point): Point {
214
+ const dx = b.x - a.x;
215
+ const dy = b.y - a.y;
216
+ if (dx === 0 && dy === 0) {
217
+ return { x: a.x, y: a.y };
218
+ }
219
+ const t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / (dx * dx + dy * dy);
220
+ const clamped = t < 0 ? 0 : t > 1 ? 1 : t;
221
+ return {
222
+ x: Math.round(a.x + clamped * dx),
223
+ y: Math.round(a.y + clamped * dy),
224
+ };
225
+ }
226
+
227
+ function pointEquals(a: Point, b: Point): boolean {
228
+ return a.x === b.x && a.y === b.y;
229
+ }
230
+
231
+ function pointOnSegmentInterior(p: Point, a: Point, b: Point): boolean {
232
+ const cross = (p.x - a.x) * (b.y - a.y) - (p.y - a.y) * (b.x - a.x);
233
+ if (cross !== 0) return false;
234
+ const minX = Math.min(a.x, b.x);
235
+ const maxX = Math.max(a.x, b.x);
236
+ const minY = Math.min(a.y, b.y);
237
+ const maxY = Math.max(a.y, b.y);
238
+ if (minX === maxX && minY === maxY) return false;
239
+ const inX = p.x > minX && p.x < maxX;
240
+ const inY = p.y > minY && p.y < maxY;
241
+ if (minX === maxX) return inY;
242
+ if (minY === maxY) return inX;
243
+ return inX && inY;
244
+ }
245
+
246
+ function uniqueWireId(doc: CircuitDocument): string {
247
+ return uniqueWireIdFromSet(new Set(doc.wires.map((w) => w.id)));
248
+ }
249
+
250
+ function uniqueWireIdFromSet(taken: Set<string>): string {
251
+ let n = taken.size + 1;
252
+ while (taken.has(`wire-${n}`)) {
253
+ n += 1;
254
+ }
255
+ return `wire-${n}`;
256
+ }
257
+
258
+ function renameComponent(doc: CircuitDocument, componentId: string, newName: string): CircuitDocument {
259
+ const trimmed = newName.trim();
260
+ if (trimmed.length === 0) {
261
+ return doc;
262
+ }
263
+ let changed = false;
264
+ const components = doc.components.map((c) => {
265
+ if (c.id !== componentId || c.name === trimmed) {
266
+ return c;
267
+ }
268
+ changed = true;
269
+ return { ...c, name: trimmed };
270
+ });
271
+ return changed ? { ...doc, components } : doc;
272
+ }
273
+
274
+ function setProperty(
275
+ doc: CircuitDocument,
276
+ componentId: string,
277
+ propertyName: string,
278
+ rawValue: string,
279
+ ): CircuitDocument {
280
+ const trimmedName = propertyName.trim();
281
+ if (trimmedName.length === 0) {
282
+ return doc;
283
+ }
284
+ let changed = false;
285
+ const components = doc.components.map((c) => {
286
+ if (c.id !== componentId) {
287
+ return c;
288
+ }
289
+ const next: PropertyValue = nextValue(c, trimmedName, rawValue);
290
+ if (propertyEquals(c.properties[trimmedName], next)) {
291
+ return c;
292
+ }
293
+ changed = true;
294
+ return { ...c, properties: { ...c.properties, [trimmedName]: next } };
295
+ });
296
+ return changed ? { ...doc, components } : doc;
297
+ }
298
+
299
+ function removeProperty(doc: CircuitDocument, componentId: string, propertyName: string): CircuitDocument {
300
+ let changed = false;
301
+ const components = doc.components.map((c) => {
302
+ if (c.id !== componentId || c.properties[propertyName] === undefined) {
303
+ return c;
304
+ }
305
+ changed = true;
306
+ const { [propertyName]: _omitted, ...rest } = c.properties;
307
+ return { ...c, properties: rest };
308
+ });
309
+ return changed ? { ...doc, components } : doc;
310
+ }
311
+
312
+ function nextValue(component: Component, propertyName: string, rawValue: string): PropertyValue {
313
+ const existing = component.properties[propertyName];
314
+ const existingWasQuantity = isParsedQuantity(existing);
315
+ if (existingWasQuantity) {
316
+ const parsed = parseQuantity(rawValue);
317
+ if (parsed !== null) {
318
+ return parsed;
319
+ }
320
+ return rawValue;
321
+ }
322
+ const parsed = parseQuantity(rawValue);
323
+ if (parsed !== null && parsed.unit.length > 0) {
324
+ return parsed;
325
+ }
326
+ return rawValue;
327
+ }
328
+
329
+ function propertyEquals(a: PropertyValue | undefined, b: PropertyValue): boolean {
330
+ if (a === undefined) {
331
+ return false;
332
+ }
333
+ if (typeof a === 'string' && typeof b === 'string') {
334
+ return a === b;
335
+ }
336
+ if (isParsedQuantity(a) && isParsedQuantity(b)) {
337
+ return a.raw === b.raw && a.value === b.value && a.unit === b.unit;
338
+ }
339
+ if ((typeof a === 'number' || typeof a === 'boolean' || a === null) &&
340
+ (typeof b === 'number' || typeof b === 'boolean' || b === null)) {
341
+ return Object.is(a, b);
342
+ }
343
+ return false;
344
+ }
@@ -0,0 +1,148 @@
1
+ import { defaultDefForKind, lookupSchxDef, type SchxComponentDef } from '../formats/schx/catalog';
2
+ import { mapTerminal } from '../formats/schx/transforms';
3
+ import type { Component, ComponentKind, Point } from '../model/types';
4
+
5
+ export type CreateComponentArgs = Readonly<{
6
+ kind: ComponentKind;
7
+ origin: Point;
8
+ sourceTypeName?: string | null;
9
+ existingIds?: ReadonlySet<string>;
10
+ name?: string;
11
+ }>;
12
+
13
+ export function buildComponent(args: CreateComponentArgs): Component {
14
+ const def = resolveDef(args.kind, args.sourceTypeName ?? null);
15
+ const terminals = def.terminals.map((t) => ({
16
+ name: t.name,
17
+ position: mapTerminal(t.local, args.origin, 0, false),
18
+ }));
19
+ const id = args.name ?? uniqueId(args.kind, args.existingIds ?? new Set());
20
+ return {
21
+ id,
22
+ kind: args.kind,
23
+ name: id,
24
+ origin: args.origin,
25
+ rotation: 0,
26
+ flipped: false,
27
+ terminals,
28
+ properties: defaultPropertiesForKind(args.kind),
29
+ sourceTypeName: args.sourceTypeName ?? null,
30
+ };
31
+ }
32
+
33
+ function defaultPropertiesForKind(kind: ComponentKind): Readonly<Record<string, string>> {
34
+ // Labels are pure annotation — without a Text property they render their
35
+ // auto-generated id (e.g. "LBL1") which is confusing. Seed Text so the
36
+ // Inspector has something editable the moment the label lands.
37
+ if (kind === 'label') {
38
+ return { Text: 'Note' };
39
+ }
40
+ return {};
41
+ }
42
+
43
+ function resolveDef(kind: ComponentKind, sourceTypeName: string | null): SchxComponentDef {
44
+ const short = shortName(sourceTypeName);
45
+ if (short !== null) {
46
+ const byShort = lookupSchxDef(short);
47
+ if (byShort !== undefined) {
48
+ return byShort;
49
+ }
50
+ }
51
+ const byKind = defaultDefForKind(kind);
52
+ if (byKind !== undefined) {
53
+ return byKind;
54
+ }
55
+ const editorDefault = EDITOR_DEFAULT_DEFS[kind];
56
+ if (editorDefault !== undefined) {
57
+ return editorDefault;
58
+ }
59
+ return FALLBACK_DEF;
60
+ }
61
+
62
+ function shortName(fullType: string | null): string | null {
63
+ if (fullType === null) {
64
+ return null;
65
+ }
66
+ const match = fullType.match(/Circuit\.(?:Components\.)?([A-Za-z0-9_]+)/);
67
+ if (match?.[1] !== undefined) {
68
+ return match[1];
69
+ }
70
+ return fullType;
71
+ }
72
+
73
+ const ID_PREFIX: Readonly<Record<ComponentKind, string>> = {
74
+ resistor: 'R',
75
+ capacitor: 'C',
76
+ inductor: 'L',
77
+ diode: 'D',
78
+ led: 'LED',
79
+ bjt: 'Q',
80
+ jfet: 'J',
81
+ mosfet: 'M',
82
+ opamp: 'U',
83
+ ota: 'U',
84
+ triode: 'VT',
85
+ pentode: 'VP',
86
+ 'tube-diode': 'VD',
87
+ transformer: 'T',
88
+ potentiometer: 'RV',
89
+ 'variable-resistor': 'VR',
90
+ switch: 'SW',
91
+ optocoupler: 'OPT',
92
+ 'voltage-source': 'V',
93
+ 'current-source': 'I',
94
+ battery: 'BAT',
95
+ ground: 'GND',
96
+ rail: 'RAIL',
97
+ jack: 'JK',
98
+ bbd: 'U',
99
+ 'delay-ic': 'U',
100
+ 'power-amp': 'U',
101
+ regulator: 'REG',
102
+ 'analog-switch': 'SW',
103
+ flipflop: 'FF',
104
+ ic: 'U',
105
+ label: 'LBL',
106
+ 'named-wire': 'NW',
107
+ port: 'P',
108
+ unsupported: 'X',
109
+ };
110
+
111
+ function uniqueId(kind: ComponentKind, existing: ReadonlySet<string>): string {
112
+ const prefix = ID_PREFIX[kind];
113
+ let n = 1;
114
+ while (existing.has(`${prefix}${n}`)) {
115
+ n += 1;
116
+ }
117
+ return `${prefix}${n}`;
118
+ }
119
+
120
+ const FALLBACK_DEF: SchxComponentDef = {
121
+ shortType: 'Unknown',
122
+ kind: 'unsupported',
123
+ terminals: [],
124
+ quantityProps: [],
125
+ };
126
+
127
+ const EDITOR_DEFAULT_DEFS: Partial<Record<ComponentKind, SchxComponentDef>> = {
128
+ led: {
129
+ shortType: 'LED',
130
+ kind: 'led',
131
+ terminals: [
132
+ { name: 'anode', local: { x: 0, y: 20 } },
133
+ { name: 'cathode', local: { x: 0, y: -20 } },
134
+ ],
135
+ quantityProps: [],
136
+ },
137
+ optocoupler: {
138
+ shortType: 'Optocoupler',
139
+ kind: 'optocoupler',
140
+ terminals: [
141
+ { name: 'led+', local: { x: -20, y: 10 } },
142
+ { name: 'led-', local: { x: -20, y: -10 } },
143
+ { name: 'r1', local: { x: 20, y: 10 } },
144
+ { name: 'r2', local: { x: 20, y: -10 } },
145
+ ],
146
+ quantityProps: [],
147
+ },
148
+ };