@skillpet/circuit 0.6.1 → 0.6.3

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.
@@ -17738,29 +17738,56 @@ var Circuit = (() => {
17738
17738
  const p = mergeParamsFirstWins(el.userParams, el.elmParams, el.defaults, el.dwgParams);
17739
17739
  return p.color ?? "black";
17740
17740
  }
17741
+ var EXCLUDED_ANCHORS = /* @__PURE__ */ new Set([
17742
+ "xy",
17743
+ "center",
17744
+ "istart",
17745
+ "iend",
17746
+ "mid",
17747
+ "label",
17748
+ "vd",
17749
+ "vs",
17750
+ "n1",
17751
+ "n2",
17752
+ "n1a",
17753
+ "n2a"
17754
+ ]);
17755
+ function isConnectionAnchor(name) {
17756
+ return !EXCLUDED_ANCHORS.has(name);
17757
+ }
17758
+ function getConnectionAnchors(el) {
17759
+ const pts = [];
17760
+ for (const [name, pt] of Object.entries(el.absanchors)) {
17761
+ if (isConnectionAnchor(name)) pts.push(pt);
17762
+ }
17763
+ return pts;
17764
+ }
17741
17765
  function buildConnectionGraph(elements) {
17742
17766
  const edges = [];
17743
17767
  const seen = /* @__PURE__ */ new Set();
17744
17768
  const tol = 0.5;
17745
17769
  for (let i = 0; i < elements.length; i++) {
17746
- const fromEl = elements[i];
17747
- const fromEnd = fromEl.absanchors.end;
17748
- if (!fromEnd) continue;
17749
- for (let j = 0; j < elements.length; j++) {
17750
- if (i === j) continue;
17751
- const toEl = elements[j];
17752
- const toStart = toEl.absanchors.start;
17753
- if (!toStart) continue;
17754
- const dx = fromEnd.x - toStart.x;
17755
- const dy = fromEnd.y - toStart.y;
17756
- if (Math.hypot(dx, dy) > tol) continue;
17757
- const fromColor = resolveElementColor(fromEl);
17758
- const toColor = resolveElementColor(toEl);
17759
- if (colorsEqual(fromColor, toColor)) continue;
17760
- const key = `${i}|${j}`;
17761
- if (seen.has(key)) continue;
17762
- seen.add(key);
17763
- edges.push({ fromEl, toEl, fromColor, toColor });
17770
+ const elA = elements[i];
17771
+ const colorA = resolveElementColor(elA);
17772
+ const anchorsA = getConnectionAnchors(elA);
17773
+ if (anchorsA.length === 0) continue;
17774
+ for (let j = i + 1; j < elements.length; j++) {
17775
+ const elB = elements[j];
17776
+ const colorB = resolveElementColor(elB);
17777
+ if (colorsEqual(colorA, colorB)) continue;
17778
+ const anchorsB = getConnectionAnchors(elB);
17779
+ if (anchorsB.length === 0) continue;
17780
+ for (const pa of anchorsA) {
17781
+ for (const pb of anchorsB) {
17782
+ if (Math.hypot(pa.x - pb.x, pa.y - pb.y) > tol) continue;
17783
+ const key = `${i}|${j}`;
17784
+ if (seen.has(key)) continue;
17785
+ seen.add(key);
17786
+ edges.push({ elA, elB, colorA, colorB, junctionPt: pa });
17787
+ break;
17788
+ }
17789
+ if (seen.has(`${i}|${j}`)) break;
17790
+ }
17764
17791
  }
17765
17792
  }
17766
17793
  return edges;
@@ -17775,26 +17802,28 @@ var Circuit = (() => {
17775
17802
  const cleanups = [];
17776
17803
  let n = 0;
17777
17804
  for (const edge of edges) {
17778
- const lead2Seg = findLeadSegment(edge.fromEl, "lead2");
17779
- const lead1Seg = findLeadSegment(edge.toEl, "lead1");
17780
- if (!lead2Seg && !lead1Seg) continue;
17781
- if (lead2Seg?.gradientStrokeId && lead1Seg?.gradientStrokeId) continue;
17805
+ const segA = findSegAtJunction(edge.elA, edge.junctionPt);
17806
+ const segB = findSegAtJunction(edge.elB, edge.junctionPt);
17807
+ if (!segA && !segB) continue;
17808
+ if (segA?.gradientStrokeId && segB?.gradientStrokeId) continue;
17782
17809
  let gx1, gy1, gx2, gy2;
17783
- if (lead2Seg && lead1Seg) {
17784
- const from = segEndpointsSvg(edge.fromEl, lead2Seg, scale);
17785
- const to = segEndpointsSvg(edge.toEl, lead1Seg, scale);
17786
- gx1 = from.x1;
17787
- gy1 = from.y1;
17788
- gx2 = to.x2;
17789
- gy2 = to.y2;
17790
- } else if (lead2Seg) {
17791
- const c = segEndpointsSvg(edge.fromEl, lead2Seg, scale);
17810
+ if (segA && segB) {
17811
+ const a = segEndpointsSvg(edge.elA, segA, scale);
17812
+ const b = segEndpointsSvg(edge.elB, segB, scale);
17813
+ const aNear = nearFarSvg(edge.elA, segA, edge.junctionPt, scale);
17814
+ const bNear = nearFarSvg(edge.elB, segB, edge.junctionPt, scale);
17815
+ gx1 = aNear.farX;
17816
+ gy1 = aNear.farY;
17817
+ gx2 = bNear.farX;
17818
+ gy2 = bNear.farY;
17819
+ } else if (segA) {
17820
+ const c = segEndpointsSvg(edge.elA, segA, scale);
17792
17821
  gx1 = c.x1;
17793
17822
  gy1 = c.y1;
17794
17823
  gx2 = c.x2;
17795
17824
  gy2 = c.y2;
17796
17825
  } else {
17797
- const c = segEndpointsSvg(edge.toEl, lead1Seg, scale);
17826
+ const c = segEndpointsSvg(edge.elB, segB, scale);
17798
17827
  gx1 = c.x1;
17799
17828
  gy1 = c.y1;
17800
17829
  gx2 = c.x2;
@@ -17802,17 +17831,17 @@ var Circuit = (() => {
17802
17831
  }
17803
17832
  if (gradientVectorLength({ x1: gx1, y1: gy1, x2: gx2, y2: gy2 }) < 1e-6) continue;
17804
17833
  const id = `sd-trans-${n++}`;
17805
- parts.push(gradientXml(id, gx1, gy1, gx2, gy2, edge.fromColor, edge.toColor));
17806
- if (lead2Seg && !lead2Seg.gradientStrokeId) {
17807
- lead2Seg.gradientStrokeId = id;
17834
+ parts.push(gradientXml(id, gx1, gy1, gx2, gy2, edge.colorA, edge.colorB));
17835
+ if (segA && !segA.gradientStrokeId) {
17836
+ segA.gradientStrokeId = id;
17808
17837
  cleanups.push(() => {
17809
- lead2Seg.gradientStrokeId = void 0;
17838
+ segA.gradientStrokeId = void 0;
17810
17839
  });
17811
17840
  }
17812
- if (lead1Seg && !lead1Seg.gradientStrokeId) {
17813
- lead1Seg.gradientStrokeId = id;
17841
+ if (segB && !segB.gradientStrokeId) {
17842
+ segB.gradientStrokeId = id;
17814
17843
  cleanups.push(() => {
17815
- lead1Seg.gradientStrokeId = void 0;
17844
+ segB.gradientStrokeId = void 0;
17816
17845
  });
17817
17846
  }
17818
17847
  }
@@ -17823,11 +17852,46 @@ var Circuit = (() => {
17823
17852
  }
17824
17853
  };
17825
17854
  }
17826
- function findLeadSegment(el, role) {
17855
+ function findSegAtJunction(el, junctionPt) {
17856
+ const tol = 0.6;
17827
17857
  for (const s of el.segments) {
17828
- if (s instanceof Segment && s.role === role) return s;
17858
+ if (!(s instanceof Segment)) continue;
17859
+ if (s.role !== "lead1" && s.role !== "lead2") continue;
17860
+ if (segTouchesPoint(el, s, junctionPt, tol)) return s;
17829
17861
  }
17830
- return void 0;
17862
+ let best;
17863
+ let bestDist = Infinity;
17864
+ for (const s of el.segments) {
17865
+ if (!(s instanceof Segment)) continue;
17866
+ if (s.path.length < 2) continue;
17867
+ if (s.role === "body") continue;
17868
+ if (s.path.length > 6) continue;
17869
+ const d = segDistToPoint(el, s, junctionPt);
17870
+ if (d < bestDist && d < tol) {
17871
+ bestDist = d;
17872
+ best = s;
17873
+ }
17874
+ }
17875
+ return best;
17876
+ }
17877
+ function segTouchesPoint(el, seg, absPt, tol) {
17878
+ return segDistToPoint(el, seg, absPt) < tol;
17879
+ }
17880
+ function segDistToPoint(el, seg, absPt) {
17881
+ const p0 = el.transform.transform(seg.path[0]);
17882
+ const pN = el.transform.transform(seg.path[seg.path.length - 1]);
17883
+ return Math.min(
17884
+ Math.hypot(p0.x - absPt.x, p0.y - absPt.y),
17885
+ Math.hypot(pN.x - absPt.x, pN.y - absPt.y)
17886
+ );
17887
+ }
17888
+ function nearFarSvg(el, seg, junctionPt, scale) {
17889
+ const p0 = el.transform.transform(seg.path[0]);
17890
+ const pN = el.transform.transform(seg.path[seg.path.length - 1]);
17891
+ const d0 = Math.hypot(p0.x - junctionPt.x, p0.y - junctionPt.y);
17892
+ const dN = Math.hypot(pN.x - junctionPt.x, pN.y - junctionPt.y);
17893
+ const far = d0 > dN ? p0 : pN;
17894
+ return { farX: far.x * scale, farY: -far.y * scale };
17831
17895
  }
17832
17896
  function segEndpointsSvg(el, seg, scale) {
17833
17897
  const p0 = el.transform.transform(seg.path[0]);