@tscircuit/schematic-viewer 2.0.96 → 2.0.97

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
@@ -909,6 +909,7 @@ var getSearchResultTransform = ({
909
909
  var MIN_SEARCH_RESULT_ZOOM = 1.8;
910
910
  var SEARCH_FOCUS_ANIMATION_MS = 350;
911
911
  var useSchematicSearch = ({
912
+ controller,
912
913
  circuitJson,
913
914
  circuitJsonKey,
914
915
  svgDivRef,
@@ -945,6 +946,7 @@ var useSchematicSearch = ({
945
946
  if (!target) return false;
946
947
  const targetRect = target.getBoundingClientRect();
947
948
  const containerRect = container.getBoundingClientRect();
949
+ if (!containerRect.width || !containerRect.height) return false;
948
950
  if (!targetRect.width && !targetRect.height) return false;
949
951
  if (searchAnimationTimerRef.current) {
950
952
  clearTimeout(searchAnimationTimerRef.current);
@@ -1017,15 +1019,62 @@ var useSchematicSearch = ({
1017
1019
  );
1018
1020
  }
1019
1021
  }, [svgDivRef]);
1022
+ const focusRequest = controller?.focusRequest;
1023
+ const onFocusRequestHandled = controller?.onFocusRequestHandled;
1024
+ useEffect4(() => {
1025
+ if (!focusRequest) return;
1026
+ const component = circuitJson.find(
1027
+ (element) => element.type === "schematic_component" && element.schematic_component_id === focusRequest.schematicComponentId
1028
+ );
1029
+ if (component?.type !== "schematic_component") {
1030
+ setPendingSearchResult(null);
1031
+ onFocusRequestHandled?.(focusRequest);
1032
+ return;
1033
+ }
1034
+ setIsInteractionEnabled(true);
1035
+ if (component.schematic_sheet_id && hasMultipleSheets) {
1036
+ handleSelectSheet(component.schematic_sheet_id);
1037
+ }
1038
+ setPendingSearchResult({
1039
+ kind: "component",
1040
+ label: component.schematic_component_id,
1041
+ schematicSheetId: component.schematic_sheet_id,
1042
+ target: {
1043
+ type: "schematic_component",
1044
+ id: component.schematic_component_id
1045
+ }
1046
+ });
1047
+ }, [
1048
+ focusRequest,
1049
+ onFocusRequestHandled,
1050
+ circuitJson,
1051
+ circuitJsonKey,
1052
+ hasMultipleSheets,
1053
+ handleSelectSheet,
1054
+ setIsInteractionEnabled
1055
+ ]);
1020
1056
  useEffect4(() => {
1021
1057
  if (!pendingSearchResult || !svgString) return;
1058
+ if (hasMultipleSheets && pendingSearchResult.schematicSheetId && pendingSearchResult.schematicSheetId !== activeSheetId)
1059
+ return;
1022
1060
  const frame = requestAnimationFrame(() => {
1023
1061
  if (focusSearchResult(pendingSearchResult)) {
1024
1062
  setPendingSearchResult(null);
1063
+ if (focusRequest?.schematicComponentId === pendingSearchResult.target.id) {
1064
+ onFocusRequestHandled?.(focusRequest);
1065
+ }
1025
1066
  }
1026
1067
  });
1027
1068
  return () => cancelAnimationFrame(frame);
1028
- }, [focusSearchResult, pendingSearchResult, svgString]);
1069
+ }, [
1070
+ focusSearchResult,
1071
+ focusRequest,
1072
+ onFocusRequestHandled,
1073
+ pendingSearchResult,
1074
+ svgString,
1075
+ hasMultipleSheets,
1076
+ activeSheetId
1077
+ ]);
1029
1078
  useEffect4(() => {
1030
1079
  if (searchQuery) return;
1031
1080
  svgDivRef.current?.querySelectorAll(".schematic-search-match").forEach((element) => element.classList.remove("schematic-search-match"));
@@ -2876,7 +2925,7 @@ import { useMemo as useMemo5 } from "react";
2876
2925
  // package.json
2877
2926
  var package_default = {
2878
2927
  name: "@tscircuit/schematic-viewer",
2879
- version: "2.0.95",
2928
+ version: "2.0.96",
2880
2929
  repository: {
2881
2930
  type: "git",
2882
2931
  url: "https://github.com/tscircuit/schematic-viewer"
@@ -3189,6 +3238,7 @@ var SchematicViewer = ({
3189
3238
  onSchematicPortClicked,
3190
3239
  onSchematicSheetChange,
3191
3240
  searchEnabled = true,
3241
+ controller,
3192
3242
  css,
3193
3243
  className
3194
3244
  }) => {
@@ -3485,6 +3535,7 @@ var SchematicViewer = ({
3485
3535
  handleSearchResultSelect,
3486
3536
  handleCancelSearch
3487
3537
  } = useSchematicSearch({
3538
+ controller,
3488
3539
  circuitJson,
3489
3540
  circuitJsonKey,
3490
3541
  svgDivRef,
@@ -4229,10 +4280,34 @@ var AnalogSimulationViewer = ({
4229
4280
  }
4230
4281
  );
4231
4282
  };
4283
+
4284
+ // lib/hooks/useSchematicViewerController.ts
4285
+ import { useCallback as useCallback8, useMemo as useMemo8, useState as useState12 } from "react";
4286
+ var useSchematicViewerController = () => {
4287
+ const [focusRequest, setFocusRequest] = useState12(null);
4288
+ const focusSchematicComponent = useCallback8(
4289
+ (schematicComponentId) => {
4290
+ setFocusRequest({ schematicComponentId });
4291
+ },
4292
+ []
4293
+ );
4294
+ const onFocusRequestHandled = useCallback8(
4295
+ (request) => {
4296
+ setFocusRequest((current) => current === request ? null : current);
4297
+ },
4298
+ []
4299
+ );
4300
+ const controller = useMemo8(
4301
+ () => ({ focusRequest, onFocusRequestHandled }),
4302
+ [focusRequest, onFocusRequestHandled]
4303
+ );
4304
+ return { controller, focusSchematicComponent };
4305
+ };
4232
4306
  export {
4233
4307
  AnalogSimulationViewer,
4234
4308
  MouseTracker,
4235
4309
  SchematicViewer,
4236
- useMouseEventsOverBoundingBox
4310
+ useMouseEventsOverBoundingBox,
4311
+ useSchematicViewerController
4237
4312
  };
4238
4313
  //# sourceMappingURL=index.js.map