@tscircuit/schematic-viewer 2.0.93 → 2.0.95

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.js CHANGED
@@ -606,8 +606,116 @@ function buildNetRegistry(circuitJson) {
606
606
  return { componentIdToKeys, netLabelIdToKey };
607
607
  }
608
608
 
609
+ // lib/hooks/useSchematicWarnings.ts
610
+ import { useLayoutEffect, useRef } from "react";
611
+ var SVG_NS = "http://www.w3.org/2000/svg";
612
+ var useSchematicWarnings = ({
613
+ svgDivRef,
614
+ svgContent,
615
+ circuitJsonKey
616
+ }) => {
617
+ const minimizedIds = useRef(/* @__PURE__ */ new Set());
618
+ useLayoutEffect(() => {
619
+ minimizedIds.current.clear();
620
+ }, [circuitJsonKey]);
621
+ useLayoutEffect(() => {
622
+ const cleanups = [];
623
+ for (const warning of svgDivRef.current?.querySelectorAll(
624
+ ".schematic-warning"
625
+ ) ?? []) {
626
+ const id = warning.getAttribute("data-warning-id");
627
+ const callout = warning.querySelector(
628
+ '[data-warning-reference="callout"]'
629
+ );
630
+ if (!id || !callout) continue;
631
+ const content = Array.from(warning.children);
632
+ const originalDisplays = content.map((child) => child.style.display);
633
+ const originalRole = warning.getAttribute("role");
634
+ const message = warning.getAttribute("aria-label") ?? "Warning";
635
+ const icon = document.createElementNS(SVG_NS, "g");
636
+ icon.setAttribute("data-warning-icon", "");
637
+ icon.setAttribute("aria-hidden", "true");
638
+ icon.setAttribute(
639
+ "transform",
640
+ `translate(${callout.getAttribute("x")} ${callout.getAttribute("y")})`
641
+ );
642
+ const badge = document.createElementNS(SVG_NS, "rect");
643
+ badge.setAttribute("width", "24");
644
+ badge.setAttribute("height", "24");
645
+ badge.setAttribute("fill", callout.getAttribute("fill") ?? "#fffaeb");
646
+ badge.setAttribute("stroke", callout.getAttribute("stroke") ?? "#d99a00");
647
+ badge.setAttribute("stroke-width", "1");
648
+ const mark = document.createElementNS(SVG_NS, "text");
649
+ mark.setAttribute("x", "12");
650
+ mark.setAttribute("y", "12");
651
+ mark.setAttribute("dominant-baseline", "central");
652
+ mark.setAttribute("text-anchor", "middle");
653
+ mark.setAttribute("font-size", "16");
654
+ mark.setAttribute("font-family", "sans-serif");
655
+ mark.setAttribute("font-weight", "bold");
656
+ mark.setAttribute("fill", "#5c4300");
657
+ mark.textContent = "!";
658
+ icon.append(badge, mark);
659
+ warning.append(icon);
660
+ warning.setAttribute("role", "button");
661
+ warning.setAttribute("tabindex", "0");
662
+ warning.style.cursor = "pointer";
663
+ warning.style.outline = "none";
664
+ const update = () => {
665
+ const minimized = minimizedIds.current.has(id);
666
+ content.forEach((child, index) => {
667
+ child.style.display = minimized ? "none" : originalDisplays[index];
668
+ });
669
+ icon.style.display = minimized ? "" : "none";
670
+ warning.setAttribute("aria-expanded", String(!minimized));
671
+ warning.setAttribute(
672
+ "aria-label",
673
+ `${minimized ? "Expand" : "Minimize"} warning: ${message}`
674
+ );
675
+ };
676
+ const toggle = (event) => {
677
+ event.preventDefault();
678
+ event.stopPropagation();
679
+ if (minimizedIds.current.has(id)) minimizedIds.current.delete(id);
680
+ else minimizedIds.current.add(id);
681
+ update();
682
+ };
683
+ const onKeyDown = (event) => {
684
+ if (event.key === "Enter" || event.key === " ") {
685
+ if (!event.repeat) toggle(event);
686
+ else event.preventDefault();
687
+ }
688
+ };
689
+ const stopPropagation = (event) => event.stopPropagation();
690
+ warning.addEventListener("click", toggle);
691
+ warning.addEventListener("keydown", onKeyDown);
692
+ warning.addEventListener("mousedown", stopPropagation);
693
+ warning.addEventListener("touchstart", stopPropagation);
694
+ update();
695
+ cleanups.push(() => {
696
+ warning.removeEventListener("click", toggle);
697
+ warning.removeEventListener("keydown", onKeyDown);
698
+ warning.removeEventListener("mousedown", stopPropagation);
699
+ warning.removeEventListener("touchstart", stopPropagation);
700
+ icon.remove();
701
+ content.forEach((child, index) => {
702
+ child.style.display = originalDisplays[index];
703
+ });
704
+ if (originalRole) warning.setAttribute("role", originalRole);
705
+ else warning.removeAttribute("role");
706
+ warning.setAttribute("aria-label", message);
707
+ warning.removeAttribute("aria-expanded");
708
+ warning.removeAttribute("tabindex");
709
+ warning.style.removeProperty("cursor");
710
+ warning.style.removeProperty("outline");
711
+ });
712
+ }
713
+ return () => cleanups.forEach((cleanup) => cleanup());
714
+ }, [svgDivRef, svgContent, circuitJsonKey]);
715
+ };
716
+
609
717
  // lib/hooks/useSchematicSearch.ts
610
- import { useCallback as useCallback2, useEffect as useEffect4, useMemo, useRef, useState as useState2 } from "react";
718
+ import { useCallback as useCallback2, useEffect as useEffect4, useMemo, useRef as useRef2, useState as useState2 } from "react";
611
719
  import { fromString } from "transformation-matrix";
612
720
 
613
721
  // lib/utils/get-schematic-search-results.ts
@@ -815,7 +923,7 @@ var useSchematicSearch = ({
815
923
  }) => {
816
924
  const [searchQuery, setSearchQuery] = useState2("");
817
925
  const [pendingSearchResult, setPendingSearchResult] = useState2(null);
818
- const searchAnimationTimerRef = useRef(
926
+ const searchAnimationTimerRef = useRef2(
819
927
  null
820
928
  );
821
929
  const searchResults = useMemo(
@@ -947,7 +1055,7 @@ var enableDebug = () => {
947
1055
  };
948
1056
 
949
1057
  // lib/components/SchematicViewer.tsx
950
- import { useCallback as useCallback7, useEffect as useEffect12, useMemo as useMemo6, useRef as useRef8, useState as useState9 } from "react";
1058
+ import { useCallback as useCallback7, useEffect as useEffect12, useMemo as useMemo6, useRef as useRef9, useState as useState9 } from "react";
951
1059
  import { toString as transformToString } from "transformation-matrix";
952
1060
  import { useMouseMatrixTransform } from "use-mouse-matrix-transform";
953
1061
 
@@ -976,16 +1084,16 @@ var useResizeHandling = (containerRef) => {
976
1084
  };
977
1085
 
978
1086
  // lib/hooks/useContextMenu.ts
979
- import { useCallback as useCallback3, useEffect as useEffect6, useRef as useRef2, useState as useState4 } from "react";
1087
+ import { useCallback as useCallback3, useEffect as useEffect6, useRef as useRef3, useState as useState4 } from "react";
980
1088
  var LONG_PRESS_DURATION_MS = 600;
981
1089
  var MOVEMENT_THRESHOLD_PX = 10;
982
1090
  var useContextMenu = ({ containerRef }) => {
983
1091
  const [menuVisible, setMenuVisible] = useState4(false);
984
1092
  const [menuPos, setMenuPos] = useState4({ x: 0, y: 0 });
985
- const menuRef = useRef2(null);
986
- const interactionOriginRef = useRef2(null);
987
- const longPressTimeoutRef = useRef2(null);
988
- const ignoreContextMenuUntilRef = useRef2(0);
1093
+ const menuRef = useRef3(null);
1094
+ const interactionOriginRef = useRef3(null);
1095
+ const longPressTimeoutRef = useRef3(null);
1096
+ const ignoreContextMenuUntilRef = useRef3(0);
989
1097
  const clearLongPressTimeout = useCallback3(() => {
990
1098
  if (longPressTimeoutRef.current === null) return;
991
1099
  window.clearTimeout(longPressTimeoutRef.current);
@@ -1090,6 +1198,20 @@ import {
1090
1198
  getPcbElementsWithinBounds
1091
1199
  } from "@tscircuit/circuit-json-util";
1092
1200
  import { gzipSync, strToU8 } from "fflate";
1201
+ var getComponentWarnings = (circuitJson, schematicComponent, pcbComponent) => circuitJson.filter((element) => {
1202
+ if (!element.type.endsWith("_warning")) return false;
1203
+ const warning = element;
1204
+ const references = [
1205
+ ["source_component", schematicComponent.source_component_id],
1206
+ ["schematic_component", schematicComponent.schematic_component_id],
1207
+ ["pcb_component", pcbComponent?.pcb_component_id]
1208
+ ];
1209
+ return references.some(([type, id]) => {
1210
+ if (!id) return false;
1211
+ const ids = warning[`${type}_ids`];
1212
+ return warning[`${type}_id`] === id || Array.isArray(ids) && ids.includes(id) || warning.schematic_element_type === type && warning.schematic_element_id === id;
1213
+ });
1214
+ });
1093
1215
  var hiddenSourceComponentKeys = /* @__PURE__ */ new Set([
1094
1216
  "type",
1095
1217
  "source_component_id",
@@ -1198,7 +1320,12 @@ var getSchematicComponentDetails = (circuitJson, schematicComponentId) => {
1198
1320
  pcbComponent,
1199
1321
  footprinterString,
1200
1322
  footprintPreviewCircuitJson: footprintPreview?.circuitJson,
1201
- footprintPreviewViewBox: footprintPreview?.viewBox
1323
+ footprintPreviewViewBox: footprintPreview?.viewBox,
1324
+ warnings: getComponentWarnings(
1325
+ circuitJson,
1326
+ schematicComponent,
1327
+ pcbComponent
1328
+ )
1202
1329
  };
1203
1330
  };
1204
1331
  var PCB_PREVIEW_PADDING_MM = 2;
@@ -1250,7 +1377,7 @@ import {
1250
1377
  useContext,
1251
1378
  useEffect as useEffect7,
1252
1379
  useMemo as useMemo2,
1253
- useRef as useRef3
1380
+ useRef as useRef4
1254
1381
  } from "react";
1255
1382
  import { Fragment as Fragment2, jsx as jsx2 } from "react/jsx-runtime";
1256
1383
  var MouseTrackerContext = createContext(null);
@@ -1268,7 +1395,7 @@ var MouseTracker = ({ children }) => {
1268
1395
  return /* @__PURE__ */ jsx2(MouseTrackerProvider, { children });
1269
1396
  };
1270
1397
  var MouseTrackerProvider = ({ children }) => {
1271
- const storeRef = useRef3({
1398
+ const storeRef = useRef4({
1272
1399
  pointer: null,
1273
1400
  boundingBoxes: /* @__PURE__ */ new Map(),
1274
1401
  hoveringIds: /* @__PURE__ */ new Set(),
@@ -1433,6 +1560,7 @@ var detailLabelStyle = {
1433
1560
  };
1434
1561
  var SchematicComponentDetailsTooltip = ({
1435
1562
  sourceComponent,
1563
+ warnings = [],
1436
1564
  footprinterString,
1437
1565
  footprintPreviewCircuitJson,
1438
1566
  footprintPreviewViewBox,
@@ -1567,6 +1695,31 @@ var SchematicComponentDetailsTooltip = ({
1567
1695
  ]
1568
1696
  }
1569
1697
  ),
1698
+ warnings.length > 0 && /* @__PURE__ */ jsxs2(
1699
+ "section",
1700
+ {
1701
+ "aria-label": "Component warnings",
1702
+ style: {
1703
+ margin: "0 8px 8px",
1704
+ padding: "8px",
1705
+ border: "1px solid #fcd34d",
1706
+ borderRadius: "4px",
1707
+ backgroundColor: "#fffbeb",
1708
+ color: "#92400e",
1709
+ fontSize: "12px",
1710
+ lineHeight: 1.45,
1711
+ overflowWrap: "anywhere"
1712
+ },
1713
+ children: [
1714
+ /* @__PURE__ */ jsxs2("strong", { children: [
1715
+ "Warnings (",
1716
+ warnings.length,
1717
+ ")"
1718
+ ] }),
1719
+ /* @__PURE__ */ jsx3("ul", { style: { margin: "4px 0 0", paddingLeft: "18px" }, children: warnings.map((warning, index) => /* @__PURE__ */ jsx3("li", { children: warning.message }, `${warning.type}-${index}`)) })
1720
+ ]
1721
+ }
1722
+ ),
1570
1723
  footprintPreviewUrl && /* @__PURE__ */ jsx3("div", { style: { padding: "0 4px 4px" }, children: /* @__PURE__ */ jsx3(
1571
1724
  "div",
1572
1725
  {
@@ -1600,7 +1753,7 @@ var SchematicComponentDetailsTooltip = ({
1600
1753
  };
1601
1754
 
1602
1755
  // lib/components/SchematicComponentMouseTarget.tsx
1603
- import { useCallback as useCallback5, useEffect as useEffect9, useRef as useRef5, useState as useState5 } from "react";
1756
+ import { useCallback as useCallback5, useEffect as useEffect9, useRef as useRef6, useState as useState5 } from "react";
1604
1757
 
1605
1758
  // lib/hooks/useMouseEventsOverBoundingBox.ts
1606
1759
  import {
@@ -1608,7 +1761,7 @@ import {
1608
1761
  useEffect as useEffect8,
1609
1762
  useId,
1610
1763
  useMemo as useMemo4,
1611
- useRef as useRef4,
1764
+ useRef as useRef5,
1612
1765
  useSyncExternalStore
1613
1766
  } from "react";
1614
1767
  var useMouseEventsOverBoundingBox = (options) => {
@@ -1619,7 +1772,7 @@ var useMouseEventsOverBoundingBox = (options) => {
1619
1772
  );
1620
1773
  }
1621
1774
  const id = useId();
1622
- const latestOptionsRef = useRef4(options);
1775
+ const latestOptionsRef = useRef5(options);
1623
1776
  latestOptionsRef.current = options;
1624
1777
  const handleClick = useMemo4(
1625
1778
  () => (event) => {
@@ -1676,7 +1829,7 @@ var SchematicComponentMouseTarget = ({
1676
1829
  circuitJsonKey
1677
1830
  }) => {
1678
1831
  const [measurement, setMeasurement] = useState5(null);
1679
- const frameRef = useRef5(null);
1832
+ const frameRef = useRef6(null);
1680
1833
  const measure = useCallback5(() => {
1681
1834
  frameRef.current = null;
1682
1835
  const svgDiv = svgDivRef.current;
@@ -1791,7 +1944,7 @@ var SchematicComponentMouseTarget = ({
1791
1944
  };
1792
1945
 
1793
1946
  // lib/components/SchematicPortMouseTarget.tsx
1794
- import { useCallback as useCallback6, useEffect as useEffect10, useRef as useRef6, useState as useState6 } from "react";
1947
+ import { useCallback as useCallback6, useEffect as useEffect10, useRef as useRef7, useState as useState6 } from "react";
1795
1948
  import { Fragment as Fragment3, jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
1796
1949
  var areMeasurementsEqual2 = (a, b) => {
1797
1950
  if (!a && !b) return true;
@@ -1809,7 +1962,7 @@ var SchematicPortMouseTarget = ({
1809
1962
  circuitJsonKey
1810
1963
  }) => {
1811
1964
  const [measurement, setMeasurement] = useState6(null);
1812
- const frameRef = useRef6(null);
1965
+ const frameRef = useRef7(null);
1813
1966
  const measure = useCallback6(() => {
1814
1967
  frameRef.current = null;
1815
1968
  const svgDiv = svgDivRef.current;
@@ -1951,7 +2104,7 @@ var SchematicPortMouseTarget = ({
1951
2104
  };
1952
2105
 
1953
2106
  // lib/components/SchematicSearch.tsx
1954
- import { useEffect as useEffect11, useRef as useRef7, useState as useState7 } from "react";
2107
+ import { useEffect as useEffect11, useRef as useRef8, useState as useState7 } from "react";
1955
2108
 
1956
2109
  // lib/components/SchematicSearchIcons.tsx
1957
2110
  import { jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime";
@@ -2039,8 +2192,8 @@ var SchematicSearch = ({
2039
2192
  const [isOpen, setIsOpen] = useState7(false);
2040
2193
  const [activeResultId, setActiveResultId] = useState7(null);
2041
2194
  const [hoveredResultId, setHoveredResultId] = useState7(null);
2042
- const inputRef = useRef7(null);
2043
- const resultsListRef = useRef7(null);
2195
+ const inputRef = useRef8(null);
2196
+ const resultsListRef = useRef8(null);
2044
2197
  const shortcutLabel = getShortcutLabel();
2045
2198
  useEffect11(() => {
2046
2199
  setActiveResultId((currentId) => {
@@ -2699,7 +2852,7 @@ var SchematicWarningsButton = ({
2699
2852
  viewBox: "0 0 24 24",
2700
2853
  fill: "none",
2701
2854
  stroke: "currentColor",
2702
- strokeWidth: "2",
2855
+ strokeWidth: "1",
2703
2856
  strokeLinecap: "round",
2704
2857
  strokeLinejoin: "round",
2705
2858
  "aria-hidden": "true",
@@ -2723,7 +2876,7 @@ import { useMemo as useMemo5 } from "react";
2723
2876
  // package.json
2724
2877
  var package_default = {
2725
2878
  name: "@tscircuit/schematic-viewer",
2726
- version: "2.0.92",
2879
+ version: "2.0.94",
2727
2880
  repository: {
2728
2881
  type: "git",
2729
2882
  url: "https://github.com/tscircuit/schematic-viewer"
@@ -2770,7 +2923,7 @@ var package_default = {
2770
2923
  "@radix-ui/react-dropdown-menu": "^2.1.16",
2771
2924
  "@tscircuit/circuit-json-util": "^0.0.108",
2772
2925
  "circuit-json": "^0.0.479",
2773
- "circuit-to-svg": "^0.0.416",
2926
+ "circuit-to-svg": "^0.0.421",
2774
2927
  debug: "^4.4.0",
2775
2928
  fflate: "^0.8.3",
2776
2929
  "performance-now": "^2.1.0",
@@ -3106,7 +3259,7 @@ var SchematicViewer = ({
3106
3259
  }
3107
3260
  }, [showSchematicPorts]);
3108
3261
  const [isHoveringClickableComponent, setIsHoveringClickableComponent] = useState9(false);
3109
- const hoveringComponentsRef = useRef8(/* @__PURE__ */ new Set());
3262
+ const hoveringComponentsRef = useRef9(/* @__PURE__ */ new Set());
3110
3263
  const [selectedSchematicComponent, setSelectedSchematicComponent] = useState9(null);
3111
3264
  const handleComponentHoverChange = useCallback7(
3112
3265
  (componentId, isHovering) => {
@@ -3120,7 +3273,7 @@ var SchematicViewer = ({
3120
3273
  []
3121
3274
  );
3122
3275
  const [isHoveringClickablePort, setIsHoveringClickablePort] = useState9(false);
3123
- const hoveringPortsRef = useRef8(/* @__PURE__ */ new Set());
3276
+ const hoveringPortsRef = useRef9(/* @__PURE__ */ new Set());
3124
3277
  const handlePortHoverChange = useCallback7(
3125
3278
  (portId, isHovering) => {
3126
3279
  if (isHovering) {
@@ -3132,9 +3285,9 @@ var SchematicViewer = ({
3132
3285
  },
3133
3286
  []
3134
3287
  );
3135
- const svgDivRef = useRef8(null);
3136
- const touchStartRef = useRef8(null);
3137
- const zoomScaleRef = useRef8({ x: 1, y: 1 });
3288
+ const svgDivRef = useRef9(null);
3289
+ const touchStartRef = useRef9(null);
3290
+ const zoomScaleRef = useRef9({ x: 1, y: 1 });
3138
3291
  const schematicComponentIds = useMemo6(() => {
3139
3292
  try {
3140
3293
  const components = su4(circuitJson).schematic_component?.list() ?? [];
@@ -3189,7 +3342,7 @@ var SchematicViewer = ({
3189
3342
  const shouldPanSchematic = useCallback7(
3190
3343
  (event) => {
3191
3344
  if (event.target instanceof Element && event.target.closest(
3192
- "[data-schematic-search], [data-schematic-warnings]"
3345
+ "[data-schematic-search], [data-schematic-warnings], .schematic-warning"
3193
3346
  )) {
3194
3347
  return false;
3195
3348
  }
@@ -3371,6 +3524,7 @@ var SchematicViewer = ({
3371
3524
  ),
3372
3525
  [svgString, isInteractionEnabled, clickToInteractEnabled]
3373
3526
  );
3527
+ useSchematicWarnings({ svgDivRef, svgContent: svgDiv, circuitJsonKey });
3374
3528
  return /* @__PURE__ */ jsxs9(MouseTracker, { children: [
3375
3529
  netHoverHighlightEnabled && /* @__PURE__ */ jsx11("style", { children: `.sch-net-faded { opacity: 0.35; }
3376
3530
  svg :is(g.trace, g.trace-overlays, g[data-schematic-component-id], [data-schematic-net-label-id]) { transition: opacity 0.12s ease-in-out; }` }),
@@ -3565,6 +3719,7 @@ var SchematicViewer = ({
3565
3719
  SchematicComponentDetailsTooltip,
3566
3720
  {
3567
3721
  sourceComponent: selectedComponentDetails.sourceComponent,
3722
+ warnings: selectedComponentDetails.warnings,
3568
3723
  footprinterString: selectedComponentDetails.footprinterString,
3569
3724
  footprintPreviewCircuitJson: selectedComponentDetails.footprintPreviewCircuitJson,
3570
3725
  footprintPreviewViewBox: selectedComponentDetails.footprintPreviewViewBox,
@@ -3601,7 +3756,7 @@ import {
3601
3756
  convertCircuitJsonToSchematicSimulationSvg,
3602
3757
  convertCircuitJsonToSimulationGraphSvg
3603
3758
  } from "circuit-to-svg";
3604
- import { useEffect as useEffect13, useMemo as useMemo7, useRef as useRef9, useState as useState11 } from "react";
3759
+ import { useEffect as useEffect13, useMemo as useMemo7, useRef as useRef10, useState as useState11 } from "react";
3605
3760
  import { toString as transformToString2 } from "transformation-matrix";
3606
3761
  import { useMouseMatrixTransform as useMouseMatrixTransform2 } from "use-mouse-matrix-transform";
3607
3762
 
@@ -3816,8 +3971,8 @@ var AnalogSimulationViewer = ({
3816
3971
  const [isLoading, setIsLoading] = useState11(true);
3817
3972
  const [error, setError] = useState11(null);
3818
3973
  const [svgObjectUrl, setSvgObjectUrl] = useState11(null);
3819
- const containerRef = useRef9(null);
3820
- const imgRef = useRef9(null);
3974
+ const containerRef = useRef10(null);
3975
+ const imgRef = useRef10(null);
3821
3976
  const { containerWidth } = useResizeHandling(
3822
3977
  containerRef
3823
3978
  );