@tscircuit/core 0.0.160 → 0.0.161

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.d.ts CHANGED
@@ -3,7 +3,7 @@ import { traceProps, SchematicPortArrangement, groupProps, boardProps, capacitor
3
3
  import React, { ReactElement } from 'react';
4
4
  import * as zod from 'zod';
5
5
  import { ZodType, z } from 'zod';
6
- import { SchSymbol, BaseSymbolName } from 'schematic-symbols';
6
+ import { symbols, SchSymbol, BaseSymbolName } from 'schematic-symbols';
7
7
  import { PcbTraceError, PcbPlacementError, LayerRef, AnyCircuitElement, AnySourceComponent, RouteHintPoint } from 'circuit-json';
8
8
  import { Matrix } from 'transformation-matrix';
9
9
  import { SoupUtilObjects } from '@tscircuit/soup-util';
@@ -130,7 +130,7 @@ interface ISubcircuit extends PrimitiveComponent {
130
130
 
131
131
  interface BaseComponentConfig {
132
132
  componentName: string;
133
- schematicSymbolName?: BaseSymbolName | null;
133
+ schematicSymbolName?: string | null;
134
134
  zodProps: ZodType;
135
135
  sourceFtype?: AnySourceComponent["ftype"] | null;
136
136
  shouldRenderAsSchematicBox?: boolean;
@@ -238,6 +238,8 @@ declare abstract class PrimitiveComponent<ZodProps extends ZodType = any> extend
238
238
  * component
239
239
  */
240
240
  computeSchematicGlobalTransform(): Matrix;
241
+ _getSchematicSymbolName(): keyof typeof symbols | undefined;
242
+ _getSchematicSymbolNameOrThrow(): keyof typeof symbols;
241
243
  getSchematicSymbol(): SchSymbol | null;
242
244
  /**
243
245
  * Subcircuit groups have a prop called "layout" that can include manual
@@ -281,12 +283,12 @@ declare abstract class PrimitiveComponent<ZodProps extends ZodType = any> extend
281
283
  doesSelectorMatch(selector: string): boolean;
282
284
  getSubcircuit(): ISubcircuit;
283
285
  selectAll(selector: string): PrimitiveComponent[];
284
- selectOne(selector: string, options?: {
286
+ selectOne<T = PrimitiveComponent>(selector: string, options?: {
285
287
  type?: string;
286
288
  port?: boolean;
287
289
  pcbPrimitive?: boolean;
288
290
  schematicPrimitive?: boolean;
289
- }): PrimitiveComponent | null;
291
+ }): T | null;
290
292
  getAvailablePcbLayers(): string[];
291
293
  getDescendants(): PrimitiveComponent[];
292
294
  /**
@@ -722,7 +724,9 @@ declare class NormalComponent<ZodProps extends ZodType = any, PortNames extends
722
724
  * 2. `props.footprint`
723
725
  *
724
726
  */
725
- initPorts(): void;
727
+ initPorts(opts?: {
728
+ additionalAliases?: Record<`pin${number}`, string[]>;
729
+ }): void;
726
730
  _getImpliedFootprintString(): string | null;
727
731
  _addChildrenFromStringFootprint(): void;
728
732
  get portMap(): PortMap<PortNames>;
@@ -3787,7 +3791,7 @@ declare class PowerSource extends NormalComponent<typeof powerSourceProps, Polar
3787
3791
  declare class Resistor extends NormalComponent<typeof resistorProps, PassivePorts> {
3788
3792
  get config(): {
3789
3793
  componentName: string;
3790
- schematicSymbolName: BaseSymbolName;
3794
+ schematicSymbolName: string;
3791
3795
  zodProps: zod.ZodObject<zod.objectUtil.extendShape<zod.objectUtil.extendShape<zod.objectUtil.extendShape<{
3792
3796
  pcbX: zod.ZodOptional<zod.ZodEffects<zod.ZodUnion<[zod.ZodString, zod.ZodNumber]>, number, string | number>>;
3793
3797
  pcbY: zod.ZodOptional<zod.ZodEffects<zod.ZodUnion<[zod.ZodString, zod.ZodNumber]>, number, string | number>>;
package/dist/index.js CHANGED
@@ -545,7 +545,7 @@ var PrimitiveComponent = class extends Renderable {
545
545
  newProps,
546
546
  changedProps: Object.keys(props)
547
547
  });
548
- this.parent?.onChildChanged(this);
548
+ this.parent?.onChildChanged?.(this);
549
549
  }
550
550
  /**
551
551
  * Computes a transformation matrix from the props of this component for PCB
@@ -664,10 +664,36 @@ var PrimitiveComponent = class extends Renderable {
664
664
  this.computeSchematicPropsTransform()
665
665
  );
666
666
  }
667
+ _getSchematicSymbolName() {
668
+ const { _parsedProps: props } = this;
669
+ const base_symbol_name = this.config.schematicSymbolName;
670
+ const symbol_name_horz = `${base_symbol_name}_horz`;
671
+ const symbol_name_vert = `${base_symbol_name}_vert`;
672
+ if (base_symbol_name in symbols) return base_symbol_name;
673
+ if (symbol_name_horz in symbols) {
674
+ if (props.schRotation === 0 || props.schRotation === void 0)
675
+ return symbol_name_horz;
676
+ if (props.schRotation === 180) return symbol_name_vert;
677
+ }
678
+ if (symbol_name_vert in symbols) {
679
+ if (props.schRotation === 90) return symbol_name_vert;
680
+ if (props.schRotation === 270) return symbol_name_vert;
681
+ }
682
+ return void 0;
683
+ }
684
+ _getSchematicSymbolNameOrThrow() {
685
+ const symbol_name = this._getSchematicSymbolName();
686
+ if (!symbol_name) {
687
+ throw new Error(
688
+ `No schematic symbol found (given: "${this.config.schematicSymbolName}")`
689
+ );
690
+ }
691
+ return symbol_name;
692
+ }
667
693
  getSchematicSymbol() {
668
- const { config } = this;
669
- if (!config.schematicSymbolName) return null;
670
- return symbols[`${config.schematicSymbolName}`] ?? null;
694
+ const symbol_name = this._getSchematicSymbolName();
695
+ if (!symbol_name) return null;
696
+ return symbols[symbol_name] ?? null;
671
697
  }
672
698
  /**
673
699
  * Subcircuit groups have a prop called "layout" that can include manual
@@ -712,7 +738,7 @@ var PrimitiveComponent = class extends Renderable {
712
738
  onPropsChange(params) {
713
739
  }
714
740
  onChildChanged(child) {
715
- this.parent?.onChildChanged(child);
741
+ this.parent?.onChildChanged?.(child);
716
742
  }
717
743
  add(component) {
718
744
  component.onAddToParent(this);
@@ -1727,9 +1753,9 @@ var Port = class extends PrimitiveComponent {
1727
1753
  return Array.from(
1728
1754
  /* @__PURE__ */ new Set([
1729
1755
  ...props.aliases ?? [],
1730
- props.name,
1756
+ ...props.name ? [props.name] : [],
1731
1757
  ...typeof props.pinNumber === "number" ? [`pin${props.pinNumber}`, props.pinNumber.toString()] : [],
1732
- ...this.externallyAddedAliases
1758
+ ...this.externallyAddedAliases ?? []
1733
1759
  ])
1734
1760
  );
1735
1761
  }
@@ -1867,12 +1893,19 @@ var Port = class extends PrimitiveComponent {
1867
1893
  };
1868
1894
 
1869
1895
  // lib/utils/getPortFromHints.ts
1896
+ var getPinNumberFromLabels = (labels) => {
1897
+ const pinNumber = labels.find((p) => /^(pin)?\d+$/.test(p));
1898
+ if (!pinNumber) return null;
1899
+ return Number.parseInt(pinNumber.replace(/^pin/, ""));
1900
+ };
1870
1901
  function getPortFromHints(hints) {
1871
- const pinNumber = hints.find((p) => /^(pin)?\d+$/.test(p));
1902
+ const pinNumber = getPinNumberFromLabels(hints);
1872
1903
  if (!pinNumber) return null;
1873
1904
  return new Port({
1874
- pinNumber: Number.parseInt(pinNumber.replace(/^pin/, "")),
1875
- aliases: hints.filter((p) => p !== pinNumber)
1905
+ pinNumber,
1906
+ aliases: hints.filter(
1907
+ (p) => p.toString() !== pinNumber.toString() && p !== `pin${pinNumber}`
1908
+ )
1876
1909
  });
1877
1910
  }
1878
1911
 
@@ -2365,7 +2398,7 @@ var NormalComponent = class extends PrimitiveComponent {
2365
2398
  * 2. `props.footprint`
2366
2399
  *
2367
2400
  */
2368
- initPorts() {
2401
+ initPorts(opts = {}) {
2369
2402
  const { config } = this;
2370
2403
  const portsToCreate = [];
2371
2404
  const schPortArrangement = this._parsedProps.schPortArrangement;
@@ -2377,7 +2410,8 @@ var NormalComponent = class extends PrimitiveComponent {
2377
2410
  portsToCreate.push(
2378
2411
  new Port(
2379
2412
  {
2380
- pinNumber
2413
+ pinNumber,
2414
+ aliases: opts.additionalAliases?.[`pin${pinNumber}`] ?? []
2381
2415
  },
2382
2416
  {
2383
2417
  originDescription: `schPortArrangement:${side}`
@@ -2395,7 +2429,8 @@ var NormalComponent = class extends PrimitiveComponent {
2395
2429
  portsToCreate.push(
2396
2430
  new Port(
2397
2431
  {
2398
- pinNumber: pinNum++
2432
+ pinNumber: pinNum++,
2433
+ aliases: opts.additionalAliases?.[`pin${pinNum}`] ?? []
2399
2434
  },
2400
2435
  {
2401
2436
  originDescription: `schPortArrangement:${side}`
@@ -2419,7 +2454,10 @@ var NormalComponent = class extends PrimitiveComponent {
2419
2454
  {
2420
2455
  pinNumber: parseInt(pinNumber),
2421
2456
  name: primaryLabel,
2422
- aliases: otherLabels
2457
+ aliases: [
2458
+ ...otherLabels,
2459
+ ...opts.additionalAliases?.[`pin${parseInt(pinNumber)}`] ?? []
2460
+ ]
2423
2461
  },
2424
2462
  {
2425
2463
  originDescription: `pinLabels:pin${pinNumber}`
@@ -2433,10 +2471,16 @@ var NormalComponent = class extends PrimitiveComponent {
2433
2471
  }
2434
2472
  }
2435
2473
  if (config.schematicSymbolName) {
2436
- const sym = symbols2[`${config.schematicSymbolName}_horz`];
2474
+ const sym = symbols2[this._getSchematicSymbolNameOrThrow()];
2437
2475
  if (!sym) return;
2438
2476
  for (const symPort of sym.ports) {
2439
- const port = getPortFromHints(symPort.labels);
2477
+ const pinNumber = getPinNumberFromLabels(symPort.labels);
2478
+ if (!pinNumber) continue;
2479
+ const port = getPortFromHints(
2480
+ symPort.labels.concat(
2481
+ opts.additionalAliases?.[`pin${pinNumber}`] ?? []
2482
+ )
2483
+ );
2440
2484
  if (port) {
2441
2485
  port.originDescription = `schematicSymbol:labels[0]:${symPort.labels[0]}`;
2442
2486
  port.schematicSymbolPortDef = symPort;
@@ -2533,21 +2577,7 @@ var NormalComponent = class extends PrimitiveComponent {
2533
2577
  _doInitialSchematicComponentRenderWithSymbol() {
2534
2578
  const { db } = this.root;
2535
2579
  const { _parsedProps: props } = this;
2536
- const base_symbol_name = this.config.schematicSymbolName;
2537
- const symbol_name_horz = `${base_symbol_name}_horz`;
2538
- let symbol_name;
2539
- if (!base_symbol_name) {
2540
- throw new Error(
2541
- "No schematic symbol defined (schematicSymbolName not provided)"
2542
- );
2543
- }
2544
- if (base_symbol_name in symbols2) {
2545
- symbol_name = base_symbol_name;
2546
- } else if (symbol_name_horz in symbols2) {
2547
- symbol_name = symbol_name_horz;
2548
- } else {
2549
- throw new Error(`Could not find schematic-symbol: "${base_symbol_name}"`);
2550
- }
2580
+ const symbol_name = this._getSchematicSymbolNameOrThrow();
2551
2581
  const symbol = symbols2[symbol_name];
2552
2582
  if (symbol) {
2553
2583
  const schematic_component2 = db.schematic_component.insert({
@@ -4369,26 +4399,18 @@ var Resistor = class extends NormalComponent {
4369
4399
  get config() {
4370
4400
  return {
4371
4401
  componentName: "Resistor",
4372
- schematicSymbolName: this.props.symbolName ?? "boxresistor_horz",
4402
+ schematicSymbolName: this.props.symbolName ?? "boxresistor",
4373
4403
  zodProps: resistorProps,
4374
4404
  sourceFtype: "simple_resistor"
4375
4405
  };
4376
4406
  }
4377
4407
  initPorts() {
4378
- this.add(
4379
- new Port({
4380
- name: "pin1",
4381
- pinNumber: 1,
4382
- aliases: ["anode", "pos", "left"]
4383
- })
4384
- );
4385
- this.add(
4386
- new Port({
4387
- name: "pin2",
4388
- pinNumber: 2,
4389
- aliases: ["cathode", "neg", "right"]
4390
- })
4391
- );
4408
+ super.initPorts({
4409
+ additionalAliases: {
4410
+ pin1: ["anode", "pos", "left"],
4411
+ pin2: ["cathode", "neg", "right"]
4412
+ }
4413
+ });
4392
4414
  }
4393
4415
  _getSchematicSymbolDisplayValue() {
4394
4416
  return `${formatSiUnit2(this._parsedProps.resistance)}\u03A9`;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tscircuit/core",
3
3
  "type": "module",
4
- "version": "0.0.160",
4
+ "version": "0.0.161",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
@@ -45,7 +45,7 @@
45
45
  "@tscircuit/soup-util": "^0.0.33",
46
46
  "circuit-json": "^0.0.99",
47
47
  "circuit-json-to-connectivity-map": "^0.0.17",
48
- "circuit-to-svg": "^0.0.67",
48
+ "circuit-to-svg": "^0.0.71",
49
49
  "format-si-unit": "^0.0.2",
50
50
  "nanoid": "^5.0.7",
51
51
  "performance-now": "^2.1.0",