@skillpet/circuit 0.6.2 → 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,51 +17738,55 @@ var Circuit = (() => {
17738
17738
  const p = mergeParamsFirstWins(el.userParams, el.elmParams, el.defaults, el.dwgParams);
17739
17739
  return p.color ?? "black";
17740
17740
  }
17741
- function getOutputAnchors(el) {
17742
- const pts = [];
17743
- const aa = el.absanchors;
17744
- if (aa.end) pts.push(aa.end);
17745
- if (aa.out && (!aa.end || !pointsClose(aa.out, aa.end, 0.01))) pts.push(aa.out);
17746
- return pts;
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);
17747
17757
  }
17748
- function getInputAnchors(el) {
17758
+ function getConnectionAnchors(el) {
17749
17759
  const pts = [];
17750
- const aa = el.absanchors;
17751
- if (aa.start) pts.push(aa.start);
17752
- for (const [name, pt] of Object.entries(aa)) {
17753
- if (name.startsWith("in") && /^in\d+$/.test(name)) {
17754
- if (!aa.start || !pointsClose(pt, aa.start, 0.01)) pts.push(pt);
17755
- }
17760
+ for (const [name, pt] of Object.entries(el.absanchors)) {
17761
+ if (isConnectionAnchor(name)) pts.push(pt);
17756
17762
  }
17757
17763
  return pts;
17758
17764
  }
17759
- function pointsClose(a, b, tol) {
17760
- return Math.hypot(a.x - b.x, a.y - b.y) <= tol;
17761
- }
17762
17765
  function buildConnectionGraph(elements) {
17763
17766
  const edges = [];
17764
17767
  const seen = /* @__PURE__ */ new Set();
17765
17768
  const tol = 0.5;
17766
17769
  for (let i = 0; i < elements.length; i++) {
17767
- const fromEl = elements[i];
17768
- const fromPts = getOutputAnchors(fromEl);
17769
- if (fromPts.length === 0) continue;
17770
- const fromColor = resolveElementColor(fromEl);
17771
- for (let j = 0; j < elements.length; j++) {
17772
- if (i === j) continue;
17773
- const toEl = elements[j];
17774
- const toPts = getInputAnchors(toEl);
17775
- if (toPts.length === 0) continue;
17776
- const toColor = resolveElementColor(toEl);
17777
- if (colorsEqual(fromColor, toColor)) continue;
17778
- for (const fp of fromPts) {
17779
- for (const tp of toPts) {
17780
- if (!pointsClose(fp, tp, tol)) continue;
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;
17781
17783
  const key = `${i}|${j}`;
17782
17784
  if (seen.has(key)) continue;
17783
17785
  seen.add(key);
17784
- edges.push({ fromEl, toEl, fromColor, toColor, junctionPt: fp });
17786
+ edges.push({ elA, elB, colorA, colorB, junctionPt: pa });
17787
+ break;
17785
17788
  }
17789
+ if (seen.has(`${i}|${j}`)) break;
17786
17790
  }
17787
17791
  }
17788
17792
  }
@@ -17798,26 +17802,28 @@ var Circuit = (() => {
17798
17802
  const cleanups = [];
17799
17803
  let n = 0;
17800
17804
  for (const edge of edges) {
17801
- const lead2Seg = findLeadSegmentFor(edge.fromEl, "out", edge.junctionPt);
17802
- const lead1Seg = findLeadSegmentFor(edge.toEl, "in", edge.junctionPt);
17803
- if (!lead2Seg && !lead1Seg) continue;
17804
- 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;
17805
17809
  let gx1, gy1, gx2, gy2;
17806
- if (lead2Seg && lead1Seg) {
17807
- const from = segEndpointsSvg(edge.fromEl, lead2Seg, scale);
17808
- const to = segEndpointsSvg(edge.toEl, lead1Seg, scale);
17809
- gx1 = from.x1;
17810
- gy1 = from.y1;
17811
- gx2 = to.x2;
17812
- gy2 = to.y2;
17813
- } else if (lead2Seg) {
17814
- 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);
17815
17821
  gx1 = c.x1;
17816
17822
  gy1 = c.y1;
17817
17823
  gx2 = c.x2;
17818
17824
  gy2 = c.y2;
17819
17825
  } else {
17820
- const c = segEndpointsSvg(edge.toEl, lead1Seg, scale);
17826
+ const c = segEndpointsSvg(edge.elB, segB, scale);
17821
17827
  gx1 = c.x1;
17822
17828
  gy1 = c.y1;
17823
17829
  gx2 = c.x2;
@@ -17825,17 +17831,17 @@ var Circuit = (() => {
17825
17831
  }
17826
17832
  if (gradientVectorLength({ x1: gx1, y1: gy1, x2: gx2, y2: gy2 }) < 1e-6) continue;
17827
17833
  const id = `sd-trans-${n++}`;
17828
- parts.push(gradientXml(id, gx1, gy1, gx2, gy2, edge.fromColor, edge.toColor));
17829
- if (lead2Seg && !lead2Seg.gradientStrokeId) {
17830
- 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;
17831
17837
  cleanups.push(() => {
17832
- lead2Seg.gradientStrokeId = void 0;
17838
+ segA.gradientStrokeId = void 0;
17833
17839
  });
17834
17840
  }
17835
- if (lead1Seg && !lead1Seg.gradientStrokeId) {
17836
- lead1Seg.gradientStrokeId = id;
17841
+ if (segB && !segB.gradientStrokeId) {
17842
+ segB.gradientStrokeId = id;
17837
17843
  cleanups.push(() => {
17838
- lead1Seg.gradientStrokeId = void 0;
17844
+ segB.gradientStrokeId = void 0;
17839
17845
  });
17840
17846
  }
17841
17847
  }
@@ -17846,26 +17852,21 @@ var Circuit = (() => {
17846
17852
  }
17847
17853
  };
17848
17854
  }
17849
- function findLeadSegmentFor(el, side, junctionPt) {
17850
- const role = side === "in" ? "lead1" : "lead2";
17855
+ function findSegAtJunction(el, junctionPt) {
17856
+ const tol = 0.6;
17851
17857
  for (const s of el.segments) {
17852
- 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;
17853
17861
  }
17854
- return findSegmentNearPoint(el, junctionPt);
17855
- }
17856
- function findSegmentNearPoint(el, absPt) {
17857
17862
  let best;
17858
17863
  let bestDist = Infinity;
17859
- const tol = 0.6;
17860
17864
  for (const s of el.segments) {
17861
17865
  if (!(s instanceof Segment)) continue;
17862
17866
  if (s.path.length < 2) continue;
17863
17867
  if (s.role === "body") continue;
17864
- const p0 = el.transform.transform(s.path[0]);
17865
- const pN = el.transform.transform(s.path[s.path.length - 1]);
17866
- const d0 = Math.hypot(p0.x - absPt.x, p0.y - absPt.y);
17867
- const dN = Math.hypot(pN.x - absPt.x, pN.y - absPt.y);
17868
- const d = Math.min(d0, dN);
17868
+ if (s.path.length > 6) continue;
17869
+ const d = segDistToPoint(el, s, junctionPt);
17869
17870
  if (d < bestDist && d < tol) {
17870
17871
  bestDist = d;
17871
17872
  best = s;
@@ -17873,6 +17874,25 @@ var Circuit = (() => {
17873
17874
  }
17874
17875
  return best;
17875
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 };
17895
+ }
17876
17896
  function segEndpointsSvg(el, seg, scale) {
17877
17897
  const p0 = el.transform.transform(seg.path[0]);
17878
17898
  const p1 = el.transform.transform(seg.path[seg.path.length - 1]);