@tscircuit/schematic-viewer 2.0.96 → 2.0.98

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,6 +3,27 @@ import { ReactNode } from 'react';
3
3
  import { CircuitJson } from 'circuit-json';
4
4
  import { ColorOverrides, AcSweepView } from 'circuit-to-svg';
5
5
 
6
+ interface SchematicViewerController {
7
+ /** @internal Pending command, consumed after the viewer focuses the component. */
8
+ focusRequest: {
9
+ schematicComponentId: string;
10
+ } | null;
11
+ /** @internal Acknowledge only this request, preserving newer commands. */
12
+ onFocusRequestHandled: (request: {
13
+ schematicComponentId: string;
14
+ }) => void;
15
+ }
16
+ /** Commands may be issued before the viewer mounts (for example while switching tabs). */
17
+ declare const useSchematicViewerController: () => {
18
+ controller: {
19
+ focusRequest: {
20
+ schematicComponentId: string;
21
+ } | null;
22
+ onFocusRequestHandled: (request: NonNullable<SchematicViewerController["focusRequest"]>) => void;
23
+ };
24
+ focusSchematicComponent: (schematicComponentId: string) => void;
25
+ };
26
+
6
27
  interface Props$1 {
7
28
  circuitJson: CircuitJson;
8
29
  containerStyle?: React.CSSProperties;
@@ -28,8 +49,10 @@ interface Props$1 {
28
49
  onSchematicSheetChange?: (schematicSheetId: string) => void;
29
50
  /** Show component and net-label search. Default true. */
30
51
  searchEnabled?: boolean;
52
+ /** Connect the controller returned by useSchematicViewerController. */
53
+ controller?: SchematicViewerController;
31
54
  }
32
- declare const SchematicViewer: ({ circuitJson, containerStyle, debugGrid, debug, clickToInteractEnabled, colorOverrides, disableGroups, netHoverHighlightEnabled, onSchematicComponentClicked, showSchematicPorts, onSchematicPortClicked, onSchematicSheetChange, searchEnabled, css, className, }: Props$1) => react.JSX.Element;
55
+ declare const SchematicViewer: ({ circuitJson, containerStyle, debugGrid, debug, clickToInteractEnabled, colorOverrides, disableGroups, netHoverHighlightEnabled, onSchematicComponentClicked, showSchematicPorts, onSchematicPortClicked, onSchematicSheetChange, searchEnabled, controller, css, className, }: Props$1) => react.JSX.Element;
33
56
 
34
57
  interface BoundingBoxBounds {
35
58
  minX: number;
@@ -65,4 +88,4 @@ interface Props {
65
88
  }
66
89
  declare const AnalogSimulationViewer: ({ circuitJson: inputCircuitJson, containerStyle, colorOverrides, width, height, className, hideSchematic, onSimulationChange, acSweepView, }: Props) => react.JSX.Element;
67
90
 
68
- export { AnalogSimulationViewer, MouseTracker, SchematicViewer, useMouseEventsOverBoundingBox };
91
+ export { AnalogSimulationViewer, MouseTracker, SchematicViewer, type SchematicViewerController, useMouseEventsOverBoundingBox, useSchematicViewerController };
package/dist/index.js CHANGED
@@ -873,6 +873,23 @@ var getSchematicSearchResults = (circuitJson, query) => {
873
873
  results.push(result);
874
874
  resultScores.set(result, getTextMatchScore(netLabel.text, normalizedQuery));
875
875
  }
876
+ for (const element of circuitJson) {
877
+ if (element.type !== "schematic_text" || !element.source_trace_id) continue;
878
+ const score = getTextMatchScore(element.text, normalizedQuery);
879
+ if (!Number.isFinite(score)) continue;
880
+ const result = {
881
+ label: element.text,
882
+ kind: "net",
883
+ schematicSheetId: element.schematic_sheet_id,
884
+ schematicSheetName: getSchematicSheetName({
885
+ schematicSheets,
886
+ schematicSheetId: element.schematic_sheet_id
887
+ }),
888
+ target: { type: "schematic_text", id: element.schematic_text_id }
889
+ };
890
+ results.push(result);
891
+ resultScores.set(result, score);
892
+ }
876
893
  return results.sort((a, b) => {
877
894
  return (resultScores.get(a) ?? Number.POSITIVE_INFINITY) - (resultScores.get(b) ?? Number.POSITIVE_INFINITY) || a.label.localeCompare(b.label);
878
895
  });
@@ -909,6 +926,7 @@ var getSearchResultTransform = ({
909
926
  var MIN_SEARCH_RESULT_ZOOM = 1.8;
910
927
  var SEARCH_FOCUS_ANIMATION_MS = 350;
911
928
  var useSchematicSearch = ({
929
+ controller,
912
930
  circuitJson,
913
931
  circuitJsonKey,
914
932
  svgDivRef,
@@ -939,12 +957,16 @@ var useSchematicSearch = ({
939
957
  if (result.target.type === "schematic_component") {
940
958
  attribute = "data-schematic-component-id";
941
959
  }
960
+ if (result.target.type === "schematic_text") {
961
+ attribute = "data-schematic-text-id";
962
+ }
942
963
  const target = Array.from(
943
964
  svgRoot.querySelectorAll(`[${attribute}]`)
944
965
  ).find((element) => element.getAttribute(attribute) === result.target.id);
945
966
  if (!target) return false;
946
967
  const targetRect = target.getBoundingClientRect();
947
968
  const containerRect = container.getBoundingClientRect();
969
+ if (!containerRect.width || !containerRect.height) return false;
948
970
  if (!targetRect.width && !targetRect.height) return false;
949
971
  if (searchAnimationTimerRef.current) {
950
972
  clearTimeout(searchAnimationTimerRef.current);
@@ -1017,15 +1039,62 @@ var useSchematicSearch = ({
1017
1039
  );
1018
1040
  }
1019
1041
  }, [svgDivRef]);
1042
+ const focusRequest = controller?.focusRequest;
1043
+ const onFocusRequestHandled = controller?.onFocusRequestHandled;
1044
+ useEffect4(() => {
1045
+ if (!focusRequest) return;
1046
+ const component = circuitJson.find(
1047
+ (element) => element.type === "schematic_component" && element.schematic_component_id === focusRequest.schematicComponentId
1048
+ );
1049
+ if (component?.type !== "schematic_component") {
1050
+ setPendingSearchResult(null);
1051
+ onFocusRequestHandled?.(focusRequest);
1052
+ return;
1053
+ }
1054
+ setIsInteractionEnabled(true);
1055
+ if (component.schematic_sheet_id && hasMultipleSheets) {
1056
+ handleSelectSheet(component.schematic_sheet_id);
1057
+ }
1058
+ setPendingSearchResult({
1059
+ kind: "component",
1060
+ label: component.schematic_component_id,
1061
+ schematicSheetId: component.schematic_sheet_id,
1062
+ target: {
1063
+ type: "schematic_component",
1064
+ id: component.schematic_component_id
1065
+ }
1066
+ });
1067
+ }, [
1068
+ focusRequest,
1069
+ onFocusRequestHandled,
1070
+ circuitJson,
1071
+ circuitJsonKey,
1072
+ hasMultipleSheets,
1073
+ handleSelectSheet,
1074
+ setIsInteractionEnabled
1075
+ ]);
1020
1076
  useEffect4(() => {
1021
1077
  if (!pendingSearchResult || !svgString) return;
1078
+ if (hasMultipleSheets && pendingSearchResult.schematicSheetId && pendingSearchResult.schematicSheetId !== activeSheetId)
1079
+ return;
1022
1080
  const frame = requestAnimationFrame(() => {
1023
1081
  if (focusSearchResult(pendingSearchResult)) {
1024
1082
  setPendingSearchResult(null);
1083
+ if (focusRequest?.schematicComponentId === pendingSearchResult.target.id) {
1084
+ onFocusRequestHandled?.(focusRequest);
1085
+ }
1025
1086
  }
1026
1087
  });
1027
1088
  return () => cancelAnimationFrame(frame);
1028
- }, [focusSearchResult, pendingSearchResult, svgString]);
1089
+ }, [
1090
+ focusSearchResult,
1091
+ focusRequest,
1092
+ onFocusRequestHandled,
1093
+ pendingSearchResult,
1094
+ svgString,
1095
+ hasMultipleSheets,
1096
+ activeSheetId
1097
+ ]);
1029
1098
  useEffect4(() => {
1030
1099
  if (searchQuery) return;
1031
1100
  svgDivRef.current?.querySelectorAll(".schematic-search-match").forEach((element) => element.classList.remove("schematic-search-match"));
@@ -2876,7 +2945,7 @@ import { useMemo as useMemo5 } from "react";
2876
2945
  // package.json
2877
2946
  var package_default = {
2878
2947
  name: "@tscircuit/schematic-viewer",
2879
- version: "2.0.95",
2948
+ version: "2.0.97",
2880
2949
  repository: {
2881
2950
  type: "git",
2882
2951
  url: "https://github.com/tscircuit/schematic-viewer"
@@ -2923,7 +2992,7 @@ var package_default = {
2923
2992
  "@radix-ui/react-dropdown-menu": "^2.1.16",
2924
2993
  "@tscircuit/circuit-json-util": "^0.0.108",
2925
2994
  "circuit-json": "^0.0.479",
2926
- "circuit-to-svg": "^0.0.421",
2995
+ "circuit-to-svg": "^0.0.432",
2927
2996
  debug: "^4.4.0",
2928
2997
  fflate: "^0.8.3",
2929
2998
  "performance-now": "^2.1.0",
@@ -3189,6 +3258,7 @@ var SchematicViewer = ({
3189
3258
  onSchematicPortClicked,
3190
3259
  onSchematicSheetChange,
3191
3260
  searchEnabled = true,
3261
+ controller,
3192
3262
  css,
3193
3263
  className
3194
3264
  }) => {
@@ -3485,6 +3555,7 @@ var SchematicViewer = ({
3485
3555
  handleSearchResultSelect,
3486
3556
  handleCancelSearch
3487
3557
  } = useSchematicSearch({
3558
+ controller,
3488
3559
  circuitJson,
3489
3560
  circuitJsonKey,
3490
3561
  svgDivRef,
@@ -4229,10 +4300,34 @@ var AnalogSimulationViewer = ({
4229
4300
  }
4230
4301
  );
4231
4302
  };
4303
+
4304
+ // lib/hooks/useSchematicViewerController.ts
4305
+ import { useCallback as useCallback8, useMemo as useMemo8, useState as useState12 } from "react";
4306
+ var useSchematicViewerController = () => {
4307
+ const [focusRequest, setFocusRequest] = useState12(null);
4308
+ const focusSchematicComponent = useCallback8(
4309
+ (schematicComponentId) => {
4310
+ setFocusRequest({ schematicComponentId });
4311
+ },
4312
+ []
4313
+ );
4314
+ const onFocusRequestHandled = useCallback8(
4315
+ (request) => {
4316
+ setFocusRequest((current) => current === request ? null : current);
4317
+ },
4318
+ []
4319
+ );
4320
+ const controller = useMemo8(
4321
+ () => ({ focusRequest, onFocusRequestHandled }),
4322
+ [focusRequest, onFocusRequestHandled]
4323
+ );
4324
+ return { controller, focusSchematicComponent };
4325
+ };
4232
4326
  export {
4233
4327
  AnalogSimulationViewer,
4234
4328
  MouseTracker,
4235
4329
  SchematicViewer,
4236
- useMouseEventsOverBoundingBox
4330
+ useMouseEventsOverBoundingBox,
4331
+ useSchematicViewerController
4237
4332
  };
4238
4333
  //# sourceMappingURL=index.js.map