@foresthubai/workflow-builder 0.3.0 → 0.4.0

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.
Files changed (120) hide show
  1. package/LICENSE +661 -661
  2. package/NOTICE +16 -16
  3. package/README.md +110 -93
  4. package/dist/components/ui/command.d.ts +2 -2
  5. package/dist/components/ui/input.d.ts +1 -1
  6. package/dist/components/ui/resizable.d.ts +1 -1
  7. package/dist/components/ui/textarea.d.ts +1 -1
  8. package/dist/graph/BaseNode.js +10 -10
  9. package/dist/graph/reactFlowRegistry.d.ts.map +1 -1
  10. package/dist/lib/utils.d.ts +3 -0
  11. package/dist/lib/utils.d.ts.map +1 -0
  12. package/dist/lib/utils.js +6 -0
  13. package/dist/lib/utils.js.map +1 -0
  14. package/dist/toolbars/CanvasTabsToolbar.d.ts +11 -0
  15. package/dist/toolbars/CanvasTabsToolbar.d.ts.map +1 -0
  16. package/dist/toolbars/CanvasTabsToolbar.js +101 -0
  17. package/dist/toolbars/CanvasTabsToolbar.js.map +1 -0
  18. package/package.json +2 -2
  19. package/src/BuilderLayout.tsx +345 -345
  20. package/src/Canvas.tsx +261 -261
  21. package/src/CanvasEditor.tsx +142 -142
  22. package/src/CanvasTabsToolbar.tsx +176 -176
  23. package/src/RightConfigPanel.tsx +266 -266
  24. package/src/WorkflowBuilder.tsx +412 -412
  25. package/src/cn.ts +6 -6
  26. package/src/components/ui/add-button.tsx +39 -39
  27. package/src/components/ui/alert-dialog.tsx +141 -141
  28. package/src/components/ui/alert.tsx +59 -59
  29. package/src/components/ui/badge.tsx +36 -36
  30. package/src/components/ui/button.tsx +85 -85
  31. package/src/components/ui/card.tsx +79 -79
  32. package/src/components/ui/checkbox.tsx +28 -28
  33. package/src/components/ui/collapsible.tsx +9 -9
  34. package/src/components/ui/command.tsx +153 -153
  35. package/src/components/ui/delete-button.tsx +23 -23
  36. package/src/components/ui/dialog.tsx +125 -125
  37. package/src/components/ui/dropdown-menu.tsx +198 -198
  38. package/src/components/ui/input.tsx +55 -55
  39. package/src/components/ui/label.tsx +24 -24
  40. package/src/components/ui/readonly-banner.tsx +15 -15
  41. package/src/components/ui/resizable.tsx +43 -43
  42. package/src/components/ui/scroll-area.tsx +102 -102
  43. package/src/components/ui/select.tsx +160 -160
  44. package/src/components/ui/separator.tsx +29 -29
  45. package/src/components/ui/switch.tsx +27 -27
  46. package/src/components/ui/textarea.tsx +51 -51
  47. package/src/components/ui/toast.tsx +127 -127
  48. package/src/components/ui/toaster.tsx +33 -33
  49. package/src/components/ui/toggle-group.tsx +59 -59
  50. package/src/components/ui/toggle.tsx +43 -43
  51. package/src/components/ui/tooltip.tsx +32 -32
  52. package/src/dialogs/NodePickerDialog.tsx +84 -84
  53. package/src/dialogs/ValidationDialog.tsx +184 -184
  54. package/src/graph/BaseNode.tsx +557 -557
  55. package/src/graph/CustomEdge.tsx +185 -185
  56. package/src/graph/CustomNode.tsx +16 -16
  57. package/src/graph/FunctionCallNode.tsx +30 -30
  58. package/src/graph/PortHandle.tsx +189 -189
  59. package/src/graph/reactFlowRegistry.ts +26 -26
  60. package/src/hooks/use-toast.ts +125 -125
  61. package/src/hooks/useAvailableVariables.ts +20 -20
  62. package/src/hooks/useCanvasHistory.ts +22 -22
  63. package/src/hooks/useCanvasTabs.ts +168 -168
  64. package/src/hooks/useFunctionDiagnosticsSync.ts +40 -40
  65. package/src/hooks/useFunctionRegistry.ts +26 -26
  66. package/src/hooks/useFunctions.ts +44 -44
  67. package/src/hooks/useGraph.ts +161 -161
  68. package/src/hooks/useNodeDefinitions.ts +82 -82
  69. package/src/hooks/useParamErrors.ts +26 -26
  70. package/src/hooks/useResolvedTheme.ts +30 -30
  71. package/src/hooks/useResourceDiagnosticsSync.ts +58 -58
  72. package/src/hooks/useSuppressThemeTransition.ts +79 -79
  73. package/src/hooks/useWorkflowSerialization.ts +127 -127
  74. package/src/i18n/index.ts +53 -53
  75. package/src/i18n/locales/de.json +501 -501
  76. package/src/i18n/locales/en.json +557 -557
  77. package/src/index.ts +27 -27
  78. package/src/inputs/ExpressionInput.tsx +297 -297
  79. package/src/inputs/ParameterEditor.tsx +515 -515
  80. package/src/inputs/PortSection.tsx +144 -144
  81. package/src/panels/BuilderSidebar.tsx +301 -301
  82. package/src/panels/ChannelConfigPanel.tsx +49 -49
  83. package/src/panels/ChannelsPanel.tsx +28 -28
  84. package/src/panels/DebugConsolePanel.tsx +73 -73
  85. package/src/panels/DebugContextPanel.tsx +77 -77
  86. package/src/panels/DebugExternalIOPanel.tsx +180 -180
  87. package/src/panels/DiagnosticsPanel.tsx +170 -170
  88. package/src/panels/EdgeConfigPanel.tsx +104 -104
  89. package/src/panels/FunctionConfigPanel.tsx +179 -179
  90. package/src/panels/FunctionListPanel.tsx +45 -45
  91. package/src/panels/MemoryConfigPanel.tsx +55 -55
  92. package/src/panels/MemoryPanel.tsx +40 -40
  93. package/src/panels/ModelConfigPanel.tsx +41 -41
  94. package/src/panels/ModelsPanel.tsx +36 -36
  95. package/src/panels/NodeConfigPanel.tsx +630 -630
  96. package/src/panels/NodeLibrary.tsx +288 -288
  97. package/src/panels/ResourceConfigPanel.tsx +132 -132
  98. package/src/panels/ResourceListPanel.tsx +113 -113
  99. package/src/panels/VariableConfigPanel.tsx +161 -161
  100. package/src/panels/VariablesPanel.tsx +145 -145
  101. package/src/stores/canvasStore.test.ts +44 -44
  102. package/src/stores/canvasStore.ts +245 -245
  103. package/src/stores/debugStore.ts +74 -74
  104. package/src/stores/diagnosticsStore.ts +130 -130
  105. package/src/stores/editorStore.ts +202 -202
  106. package/src/styles/index.css +526 -526
  107. package/src/utils/categoryConstants.ts +26 -26
  108. package/src/utils/channelOperations.ts +86 -86
  109. package/src/utils/connectionRules.ts +137 -137
  110. package/src/utils/functionOperations.ts +179 -179
  111. package/src/utils/graphOperations.ts +550 -550
  112. package/src/utils/history.ts +207 -207
  113. package/src/utils/memoryOperations.ts +57 -57
  114. package/src/utils/migrateFunctionNodes.ts +107 -107
  115. package/src/utils/modelOperations.ts +55 -55
  116. package/src/utils/paramDisplay.ts +71 -71
  117. package/src/utils/resourceHelpers.ts +32 -32
  118. package/src/utils/translation.ts +28 -28
  119. package/src/utils/variableOperations.ts +75 -75
  120. package/tailwind-preset.ts +166 -166
@@ -1,142 +1,142 @@
1
- import type { Dispatch, MutableRefObject, SetStateAction } from "react";
2
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
- import type { Connection, Node, OnSelectionChangeFunc } from "@xyflow/react";
4
- import type { NodeDefinition, NodeData } from "@foresthubai/workflow-core/node";
5
- import { getPorts } from "@foresthubai/workflow-core/node";
6
- import { getCompatibleNodeDefs } from "./utils/connectionRules";
7
-
8
- import Canvas from "./Canvas";
9
- import { NodePickerDialog } from "./dialogs/NodePickerDialog";
10
- import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "./stores/canvasStore";
11
- import type { PortActionDetail } from "./graph/PortHandle";
12
-
13
- /**
14
- * The per-canvas editing surface. Wraps {@link Canvas} (the ReactFlow
15
- * primitive) with the popup state and viewport-center machinery that has
16
- * to remount when the user switches canvases.
17
- *
18
- * Mounted with `key={canvasId}` by {@link BuilderLayout} — when the active
19
- * canvas changes, this component fully remounts so port-popup, selection-
20
- * drag, and the viewport-center ref all reset cleanly without imperative
21
- * teardown.
22
- *
23
- * Graph mutations, history, and keyboard handling live one level up in
24
- * BuilderLayout so they can drive the sidebar and right panel siblings.
25
- */
26
- export interface CanvasEditorProps {
27
- canvasId: string;
28
-
29
- /** Populated by Canvas/ReactFlow on mount; read by sidebar's click-to-add. */
30
- viewportCenterRef: MutableRefObject<(() => { x: number; y: number }) | null>;
31
-
32
- /** Node-palette registry for the contextual port-plus picker. */
33
- nodeDefinitions: NodeDefinition[];
34
-
35
- // Event handlers wired by BuilderLayout
36
- onConnect: (connection: Connection) => void;
37
- onAddNode: (nodeDef: NodeDefinition, position?: { x: number; y: number }) => string | null | undefined;
38
- onAddNodeAndConnect: (
39
- nodeDef: NodeDefinition,
40
- position: { x: number; y: number },
41
- connection: { source: string; sourceHandle: string; target: string; targetHandle: string },
42
- ) => string | null | undefined;
43
- onSelectionChange: OnSelectionChangeFunc;
44
- onPaneClick: () => void;
45
- onNodeDragStart: (event: React.MouseEvent, node: Node<NodeData>) => void;
46
-
47
- // Selection-drag flag (lifted to BuilderLayout so the right panel can read it)
48
- setSelectionDrag: Dispatch<SetStateAction<boolean>>;
49
- }
50
-
51
- export const CanvasEditor = ({
52
- canvasId,
53
- viewportCenterRef,
54
- nodeDefinitions,
55
- onConnect,
56
- onAddNode,
57
- onAddNodeAndConnect,
58
- onSelectionChange,
59
- onPaneClick,
60
- onNodeDragStart,
61
- setSelectionDrag,
62
- }: CanvasEditorProps) => {
63
- const isFunctionCanvas = canvasId !== MAIN_CANVAS_ID;
64
-
65
- // Contextual node picker ("+" on output ports). PortHandle lives deep
66
- // inside ReactFlow's render tree, so it dispatches a bubbling CustomEvent
67
- // that we catch on this container — no prop drilling through RF needed.
68
- const [portAction, setPortAction] = useState<PortActionDetail | null>(null);
69
- const containerRef = useRef<HTMLDivElement>(null);
70
- useEffect(() => {
71
- const el = containerRef.current;
72
- if (!el) return;
73
- const handler = (e: Event) => setPortAction((e as CustomEvent<PortActionDetail>).detail);
74
- el.addEventListener("port-plus-click", handler);
75
- return () => el.removeEventListener("port-plus-click", handler);
76
- }, []);
77
-
78
- const compatibleDefs = useMemo(() => {
79
- if (!portAction) return [];
80
- const { nodes, edges } = getOrCreateCanvasStore(canvasId).getState();
81
- return getCompatibleNodeDefs(
82
- portAction.nodeId,
83
- portAction.handleId,
84
- nodes,
85
- edges,
86
- nodeDefinitions,
87
- isFunctionCanvas,
88
- );
89
- }, [portAction, canvasId, nodeDefinitions, isFunctionCanvas]);
90
-
91
- const handleAddAndConnect = useCallback(
92
- (nodeDef: NodeDefinition) => {
93
- if (!portAction) return;
94
- const { nodes: currentNodes } = getOrCreateCanvasStore(canvasId).getState();
95
- const originNode = currentNodes.find((n) => n.id === portAction.nodeId);
96
- if (!originNode) return;
97
- const originPos = originNode.position;
98
- const position =
99
- portAction.portType === "tool"
100
- ? { x: originPos.x, y: originPos.y + 200 }
101
- : { x: originPos.x + 280, y: originPos.y };
102
- const newNodePorts = getPorts({ type: nodeDef.type } as NodeData);
103
- const targetPort = newNodePorts.input.find((p) => p.type === portAction.portType);
104
- if (!targetPort) return;
105
- onAddNodeAndConnect(nodeDef, position, {
106
- source: portAction.nodeId,
107
- sourceHandle: portAction.handleId,
108
- target: "",
109
- targetHandle: targetPort.id,
110
- });
111
- setPortAction(null);
112
- },
113
- [portAction, canvasId, onAddNodeAndConnect],
114
- );
115
-
116
- return (
117
- <div ref={containerRef} className="h-full flex flex-col">
118
- <div className="flex-1 relative">
119
- <Canvas
120
- canvasId={canvasId}
121
- onConnect={onConnect}
122
- onSelectionChange={onSelectionChange}
123
- onSelectionStart={() => setSelectionDrag(true)}
124
- onSelectionStop={() => setSelectionDrag(false)}
125
- onPaneClick={onPaneClick}
126
- onAddNode={onAddNode}
127
- onNodeDragStart={onNodeDragStart}
128
- viewportCenterRef={viewportCenterRef}
129
- />
130
- </div>
131
-
132
- <NodePickerDialog
133
- open={portAction !== null}
134
- onOpenChange={(open) => {
135
- if (!open) setPortAction(null);
136
- }}
137
- compatibleDefs={compatibleDefs}
138
- onSelect={handleAddAndConnect}
139
- />
140
- </div>
141
- );
142
- };
1
+ import type { Dispatch, MutableRefObject, SetStateAction } from "react";
2
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
+ import type { Connection, Node, OnSelectionChangeFunc } from "@xyflow/react";
4
+ import type { NodeDefinition, NodeData } from "@foresthubai/workflow-core/node";
5
+ import { getPorts } from "@foresthubai/workflow-core/node";
6
+ import { getCompatibleNodeDefs } from "./utils/connectionRules";
7
+
8
+ import Canvas from "./Canvas";
9
+ import { NodePickerDialog } from "./dialogs/NodePickerDialog";
10
+ import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "./stores/canvasStore";
11
+ import type { PortActionDetail } from "./graph/PortHandle";
12
+
13
+ /**
14
+ * The per-canvas editing surface. Wraps {@link Canvas} (the ReactFlow
15
+ * primitive) with the popup state and viewport-center machinery that has
16
+ * to remount when the user switches canvases.
17
+ *
18
+ * Mounted with `key={canvasId}` by {@link BuilderLayout} — when the active
19
+ * canvas changes, this component fully remounts so port-popup, selection-
20
+ * drag, and the viewport-center ref all reset cleanly without imperative
21
+ * teardown.
22
+ *
23
+ * Graph mutations, history, and keyboard handling live one level up in
24
+ * BuilderLayout so they can drive the sidebar and right panel siblings.
25
+ */
26
+ export interface CanvasEditorProps {
27
+ canvasId: string;
28
+
29
+ /** Populated by Canvas/ReactFlow on mount; read by sidebar's click-to-add. */
30
+ viewportCenterRef: MutableRefObject<(() => { x: number; y: number }) | null>;
31
+
32
+ /** Node-palette registry for the contextual port-plus picker. */
33
+ nodeDefinitions: NodeDefinition[];
34
+
35
+ // Event handlers wired by BuilderLayout
36
+ onConnect: (connection: Connection) => void;
37
+ onAddNode: (nodeDef: NodeDefinition, position?: { x: number; y: number }) => string | null | undefined;
38
+ onAddNodeAndConnect: (
39
+ nodeDef: NodeDefinition,
40
+ position: { x: number; y: number },
41
+ connection: { source: string; sourceHandle: string; target: string; targetHandle: string },
42
+ ) => string | null | undefined;
43
+ onSelectionChange: OnSelectionChangeFunc;
44
+ onPaneClick: () => void;
45
+ onNodeDragStart: (event: React.MouseEvent, node: Node<NodeData>) => void;
46
+
47
+ // Selection-drag flag (lifted to BuilderLayout so the right panel can read it)
48
+ setSelectionDrag: Dispatch<SetStateAction<boolean>>;
49
+ }
50
+
51
+ export const CanvasEditor = ({
52
+ canvasId,
53
+ viewportCenterRef,
54
+ nodeDefinitions,
55
+ onConnect,
56
+ onAddNode,
57
+ onAddNodeAndConnect,
58
+ onSelectionChange,
59
+ onPaneClick,
60
+ onNodeDragStart,
61
+ setSelectionDrag,
62
+ }: CanvasEditorProps) => {
63
+ const isFunctionCanvas = canvasId !== MAIN_CANVAS_ID;
64
+
65
+ // Contextual node picker ("+" on output ports). PortHandle lives deep
66
+ // inside ReactFlow's render tree, so it dispatches a bubbling CustomEvent
67
+ // that we catch on this container — no prop drilling through RF needed.
68
+ const [portAction, setPortAction] = useState<PortActionDetail | null>(null);
69
+ const containerRef = useRef<HTMLDivElement>(null);
70
+ useEffect(() => {
71
+ const el = containerRef.current;
72
+ if (!el) return;
73
+ const handler = (e: Event) => setPortAction((e as CustomEvent<PortActionDetail>).detail);
74
+ el.addEventListener("port-plus-click", handler);
75
+ return () => el.removeEventListener("port-plus-click", handler);
76
+ }, []);
77
+
78
+ const compatibleDefs = useMemo(() => {
79
+ if (!portAction) return [];
80
+ const { nodes, edges } = getOrCreateCanvasStore(canvasId).getState();
81
+ return getCompatibleNodeDefs(
82
+ portAction.nodeId,
83
+ portAction.handleId,
84
+ nodes,
85
+ edges,
86
+ nodeDefinitions,
87
+ isFunctionCanvas,
88
+ );
89
+ }, [portAction, canvasId, nodeDefinitions, isFunctionCanvas]);
90
+
91
+ const handleAddAndConnect = useCallback(
92
+ (nodeDef: NodeDefinition) => {
93
+ if (!portAction) return;
94
+ const { nodes: currentNodes } = getOrCreateCanvasStore(canvasId).getState();
95
+ const originNode = currentNodes.find((n) => n.id === portAction.nodeId);
96
+ if (!originNode) return;
97
+ const originPos = originNode.position;
98
+ const position =
99
+ portAction.portType === "tool"
100
+ ? { x: originPos.x, y: originPos.y + 200 }
101
+ : { x: originPos.x + 280, y: originPos.y };
102
+ const newNodePorts = getPorts({ type: nodeDef.type } as NodeData);
103
+ const targetPort = newNodePorts.input.find((p) => p.type === portAction.portType);
104
+ if (!targetPort) return;
105
+ onAddNodeAndConnect(nodeDef, position, {
106
+ source: portAction.nodeId,
107
+ sourceHandle: portAction.handleId,
108
+ target: "",
109
+ targetHandle: targetPort.id,
110
+ });
111
+ setPortAction(null);
112
+ },
113
+ [portAction, canvasId, onAddNodeAndConnect],
114
+ );
115
+
116
+ return (
117
+ <div ref={containerRef} className="h-full flex flex-col">
118
+ <div className="flex-1 relative">
119
+ <Canvas
120
+ canvasId={canvasId}
121
+ onConnect={onConnect}
122
+ onSelectionChange={onSelectionChange}
123
+ onSelectionStart={() => setSelectionDrag(true)}
124
+ onSelectionStop={() => setSelectionDrag(false)}
125
+ onPaneClick={onPaneClick}
126
+ onAddNode={onAddNode}
127
+ onNodeDragStart={onNodeDragStart}
128
+ viewportCenterRef={viewportCenterRef}
129
+ />
130
+ </div>
131
+
132
+ <NodePickerDialog
133
+ open={portAction !== null}
134
+ onOpenChange={(open) => {
135
+ if (!open) setPortAction(null);
136
+ }}
137
+ compatibleDefs={compatibleDefs}
138
+ onSelect={handleAddAndConnect}
139
+ />
140
+ </div>
141
+ );
142
+ };
@@ -1,176 +1,176 @@
1
- import { ScrollArea } from "./components/ui/scroll-area";
2
- import { cn } from "./cn";
3
- import { FunctionSquare, Workflow, X } from "lucide-react";
4
- import React, { useCallback, useEffect, useRef, useState } from "react";
5
- import { CanvasTab } from "./hooks/useCanvasTabs";
6
- import { MAIN_CANVAS_ID } from "./stores/canvasStore";
7
-
8
- interface CanvasTabsToolbarProps {
9
- tabs: CanvasTab[];
10
- activeTabId: string;
11
- onTabChange: (tabId: string) => void;
12
- onTabClose: (tabId: string) => void;
13
- onTabReorder: (fromIndex: number, toIndex: number) => void;
14
- }
15
-
16
- export const CanvasTabsToolbar = ({
17
- tabs,
18
- activeTabId,
19
- onTabChange,
20
- onTabClose,
21
- onTabReorder,
22
- }: CanvasTabsToolbarProps) => {
23
- const containerRef = useRef<HTMLDivElement>(null);
24
- const viewportRef = useRef<HTMLDivElement>(null);
25
- const dragIndex = useRef<number | null>(null);
26
-
27
- // Translate vertical mouse-wheel deltas into horizontal scroll on the tabs
28
- // viewport. A non-passive native listener is required because React's
29
- // synthetic onWheel is passive — preventDefault() there is a no-op, so the
30
- // page would also scroll vertically alongside the toolbar shift. We leave
31
- // genuine horizontal wheels (touchpads, tilt wheels) alone by gating on
32
- // deltaY, and skip the override entirely when there's nothing to scroll
33
- // so vertical-page scrolling still works when the pointer happens to
34
- // hover an unfilled toolbar.
35
- useEffect(() => {
36
- const el = viewportRef.current;
37
- if (!el) return;
38
- const handler = (e: WheelEvent) => {
39
- if (e.deltaY === 0) return;
40
- if (el.scrollWidth <= el.clientWidth) return;
41
- e.preventDefault();
42
- el.scrollLeft += e.deltaY;
43
- };
44
- el.addEventListener("wheel", handler, { passive: false });
45
- return () => el.removeEventListener("wheel", handler);
46
- }, []);
47
-
48
- // dropSlot: insertion index (before which tab the dragged tab lands)
49
- const [dropSlot, setDropSlot] = useState<number | null>(null);
50
- // indicatorX: pixel offset from container left for the visual line
51
- const [indicatorX, setIndicatorX] = useState<number | null>(null);
52
-
53
- const isMainTab = (index: number) => tabs[index]?.id === MAIN_CANVAS_ID;
54
-
55
- const clearDrag = useCallback(() => {
56
- dragIndex.current = null;
57
- setDropSlot(null);
58
- setIndicatorX(null);
59
- }, []);
60
-
61
- const handleTabDragOver = useCallback(
62
- (e: React.DragEvent<HTMLDivElement>, index: number) => {
63
- if (dragIndex.current === null || !containerRef.current) return;
64
- // Ignore Main tab entirely — not a valid drag target
65
- if (tabs[index]?.id === MAIN_CANVAS_ID) return;
66
- e.preventDefault();
67
- e.stopPropagation();
68
- e.dataTransfer.dropEffect = "move";
69
-
70
- const rect = e.currentTarget.getBoundingClientRect();
71
- const containerRect = containerRef.current.getBoundingClientRect();
72
- const midX = rect.left + rect.width / 2;
73
- const isLeftHalf = e.clientX < midX;
74
-
75
- // Slot: insertion index
76
- const slot = isLeftHalf ? index : index + 1;
77
- // Tabs are now flush with a 1px separator between them — land the indicator on that seam.
78
- const raw = isLeftHalf ? rect.left - containerRect.left : rect.right - containerRect.left;
79
- // Clamp so the indicator stays fully visible inside the container
80
- const x = Math.round(Math.max(0, Math.min(raw, containerRef.current.clientWidth - 2)));
81
-
82
- setDropSlot(slot);
83
- setIndicatorX(x);
84
- },
85
- [tabs],
86
- );
87
-
88
- return (
89
- // ScrollArea provides the horizontal overlay scrollbar (hover-only, in the
90
- // panel gutter so the tab row doesn't shift). The Root carries the bg +
91
- // bottom border; the inner div inside the Viewport stays the drag/drop
92
- // container — containerRef points at it so the indicator's coordinates
93
- // remain relative to the (scrollable) tab row, not the fixed Root.
94
- <ScrollArea className="bg-card/80 border-b border-border/50" viewportRef={viewportRef}>
95
- <div
96
- ref={containerRef}
97
- className="relative flex items-stretch"
98
- onDragOver={(e) => {
99
- // Fallback for empty area past last tab
100
- if (dragIndex.current === null || !containerRef.current) return;
101
- e.preventDefault();
102
- e.dataTransfer.dropEffect = "move";
103
- }}
104
- onDrop={(e) => {
105
- e.preventDefault();
106
- if (dragIndex.current !== null && dropSlot !== null) {
107
- const from = dragIndex.current;
108
- const target = dropSlot > from ? dropSlot - 1 : dropSlot;
109
- if (target > 0 && target !== from) {
110
- onTabReorder(from, target);
111
- }
112
- }
113
- clearDrag();
114
- }}
115
- >
116
- {/* Absolute drop indicator — no layout shift */}
117
- {indicatorX !== null && (
118
- <div
119
- className="absolute top-0 bottom-0 bg-primary z-10 pointer-events-none"
120
- style={{ left: 0, width: "2px", transform: `translateX(${indicatorX}px)` }}
121
- />
122
- )}
123
-
124
- {tabs.map((tab, index) => {
125
- const isDraggable = !isMainTab(index);
126
-
127
- return (
128
- <React.Fragment key={tab.id}>
129
- {index > 0 && <div className="w-px bg-border/70 shrink-0" />}
130
- <div
131
- draggable={isDraggable}
132
- onDragStart={(e) => {
133
- if (!isDraggable) return;
134
- dragIndex.current = index;
135
- e.dataTransfer.effectAllowed = "move";
136
- }}
137
- onDragOver={(e) => handleTabDragOver(e, index)}
138
- onDragEnd={clearDrag}
139
- className={cn(
140
- "group flex items-center gap-1.5 pl-2 pr-1 text-sm font-medium cursor-pointer transition-colors",
141
- "hover:bg-field/80",
142
- activeTabId === tab.id ? "bg-field text-foreground" : "text-muted-foreground hover:text-foreground",
143
- isDraggable ? "cursor-grab active:cursor-grabbing" : "select-none",
144
- )}
145
- onClick={() => onTabChange(tab.id)}
146
- >
147
- {isMainTab(index) ? (
148
- <Workflow className="w-3.5 h-3.5 shrink-0" />
149
- ) : (
150
- <FunctionSquare className="w-3.5 h-3.5 shrink-0" />
151
- )}
152
- <span className="truncate max-w-[120px] py-1">{tab.label}</span>
153
- {tab.id !== MAIN_CANVAS_ID ? (
154
- <button
155
- type="button"
156
- aria-label={`Close tab ${tab.label}`}
157
- title={`Close tab ${tab.label}`}
158
- className="flex items-center justify-center w-4 h-4 shrink-0 rounded opacity-0 group-hover:opacity-100 transition-opacity hover:bg-destructive/15 hover:text-destructive"
159
- onClick={(e) => {
160
- e.stopPropagation();
161
- onTabClose(tab.id);
162
- }}
163
- >
164
- <X className="w-3.5 h-3.5" />
165
- </button>
166
- ) : (
167
- <span className="w-1 shrink-0" />
168
- )}
169
- </div>
170
- </React.Fragment>
171
- );
172
- })}
173
- </div>
174
- </ScrollArea>
175
- );
176
- };
1
+ import { ScrollArea } from "./components/ui/scroll-area";
2
+ import { cn } from "./cn";
3
+ import { FunctionSquare, Workflow, X } from "lucide-react";
4
+ import React, { useCallback, useEffect, useRef, useState } from "react";
5
+ import { CanvasTab } from "./hooks/useCanvasTabs";
6
+ import { MAIN_CANVAS_ID } from "./stores/canvasStore";
7
+
8
+ interface CanvasTabsToolbarProps {
9
+ tabs: CanvasTab[];
10
+ activeTabId: string;
11
+ onTabChange: (tabId: string) => void;
12
+ onTabClose: (tabId: string) => void;
13
+ onTabReorder: (fromIndex: number, toIndex: number) => void;
14
+ }
15
+
16
+ export const CanvasTabsToolbar = ({
17
+ tabs,
18
+ activeTabId,
19
+ onTabChange,
20
+ onTabClose,
21
+ onTabReorder,
22
+ }: CanvasTabsToolbarProps) => {
23
+ const containerRef = useRef<HTMLDivElement>(null);
24
+ const viewportRef = useRef<HTMLDivElement>(null);
25
+ const dragIndex = useRef<number | null>(null);
26
+
27
+ // Translate vertical mouse-wheel deltas into horizontal scroll on the tabs
28
+ // viewport. A non-passive native listener is required because React's
29
+ // synthetic onWheel is passive — preventDefault() there is a no-op, so the
30
+ // page would also scroll vertically alongside the toolbar shift. We leave
31
+ // genuine horizontal wheels (touchpads, tilt wheels) alone by gating on
32
+ // deltaY, and skip the override entirely when there's nothing to scroll
33
+ // so vertical-page scrolling still works when the pointer happens to
34
+ // hover an unfilled toolbar.
35
+ useEffect(() => {
36
+ const el = viewportRef.current;
37
+ if (!el) return;
38
+ const handler = (e: WheelEvent) => {
39
+ if (e.deltaY === 0) return;
40
+ if (el.scrollWidth <= el.clientWidth) return;
41
+ e.preventDefault();
42
+ el.scrollLeft += e.deltaY;
43
+ };
44
+ el.addEventListener("wheel", handler, { passive: false });
45
+ return () => el.removeEventListener("wheel", handler);
46
+ }, []);
47
+
48
+ // dropSlot: insertion index (before which tab the dragged tab lands)
49
+ const [dropSlot, setDropSlot] = useState<number | null>(null);
50
+ // indicatorX: pixel offset from container left for the visual line
51
+ const [indicatorX, setIndicatorX] = useState<number | null>(null);
52
+
53
+ const isMainTab = (index: number) => tabs[index]?.id === MAIN_CANVAS_ID;
54
+
55
+ const clearDrag = useCallback(() => {
56
+ dragIndex.current = null;
57
+ setDropSlot(null);
58
+ setIndicatorX(null);
59
+ }, []);
60
+
61
+ const handleTabDragOver = useCallback(
62
+ (e: React.DragEvent<HTMLDivElement>, index: number) => {
63
+ if (dragIndex.current === null || !containerRef.current) return;
64
+ // Ignore Main tab entirely — not a valid drag target
65
+ if (tabs[index]?.id === MAIN_CANVAS_ID) return;
66
+ e.preventDefault();
67
+ e.stopPropagation();
68
+ e.dataTransfer.dropEffect = "move";
69
+
70
+ const rect = e.currentTarget.getBoundingClientRect();
71
+ const containerRect = containerRef.current.getBoundingClientRect();
72
+ const midX = rect.left + rect.width / 2;
73
+ const isLeftHalf = e.clientX < midX;
74
+
75
+ // Slot: insertion index
76
+ const slot = isLeftHalf ? index : index + 1;
77
+ // Tabs are now flush with a 1px separator between them — land the indicator on that seam.
78
+ const raw = isLeftHalf ? rect.left - containerRect.left : rect.right - containerRect.left;
79
+ // Clamp so the indicator stays fully visible inside the container
80
+ const x = Math.round(Math.max(0, Math.min(raw, containerRef.current.clientWidth - 2)));
81
+
82
+ setDropSlot(slot);
83
+ setIndicatorX(x);
84
+ },
85
+ [tabs],
86
+ );
87
+
88
+ return (
89
+ // ScrollArea provides the horizontal overlay scrollbar (hover-only, in the
90
+ // panel gutter so the tab row doesn't shift). The Root carries the bg +
91
+ // bottom border; the inner div inside the Viewport stays the drag/drop
92
+ // container — containerRef points at it so the indicator's coordinates
93
+ // remain relative to the (scrollable) tab row, not the fixed Root.
94
+ <ScrollArea className="bg-card/80 border-b border-border/50" viewportRef={viewportRef}>
95
+ <div
96
+ ref={containerRef}
97
+ className="relative flex items-stretch"
98
+ onDragOver={(e) => {
99
+ // Fallback for empty area past last tab
100
+ if (dragIndex.current === null || !containerRef.current) return;
101
+ e.preventDefault();
102
+ e.dataTransfer.dropEffect = "move";
103
+ }}
104
+ onDrop={(e) => {
105
+ e.preventDefault();
106
+ if (dragIndex.current !== null && dropSlot !== null) {
107
+ const from = dragIndex.current;
108
+ const target = dropSlot > from ? dropSlot - 1 : dropSlot;
109
+ if (target > 0 && target !== from) {
110
+ onTabReorder(from, target);
111
+ }
112
+ }
113
+ clearDrag();
114
+ }}
115
+ >
116
+ {/* Absolute drop indicator — no layout shift */}
117
+ {indicatorX !== null && (
118
+ <div
119
+ className="absolute top-0 bottom-0 bg-primary z-10 pointer-events-none"
120
+ style={{ left: 0, width: "2px", transform: `translateX(${indicatorX}px)` }}
121
+ />
122
+ )}
123
+
124
+ {tabs.map((tab, index) => {
125
+ const isDraggable = !isMainTab(index);
126
+
127
+ return (
128
+ <React.Fragment key={tab.id}>
129
+ {index > 0 && <div className="w-px bg-border/70 shrink-0" />}
130
+ <div
131
+ draggable={isDraggable}
132
+ onDragStart={(e) => {
133
+ if (!isDraggable) return;
134
+ dragIndex.current = index;
135
+ e.dataTransfer.effectAllowed = "move";
136
+ }}
137
+ onDragOver={(e) => handleTabDragOver(e, index)}
138
+ onDragEnd={clearDrag}
139
+ className={cn(
140
+ "group flex items-center gap-1.5 pl-2 pr-1 text-sm font-medium cursor-pointer transition-colors",
141
+ "hover:bg-field/80",
142
+ activeTabId === tab.id ? "bg-field text-foreground" : "text-muted-foreground hover:text-foreground",
143
+ isDraggable ? "cursor-grab active:cursor-grabbing" : "select-none",
144
+ )}
145
+ onClick={() => onTabChange(tab.id)}
146
+ >
147
+ {isMainTab(index) ? (
148
+ <Workflow className="w-3.5 h-3.5 shrink-0" />
149
+ ) : (
150
+ <FunctionSquare className="w-3.5 h-3.5 shrink-0" />
151
+ )}
152
+ <span className="truncate max-w-[120px] py-1">{tab.label}</span>
153
+ {tab.id !== MAIN_CANVAS_ID ? (
154
+ <button
155
+ type="button"
156
+ aria-label={`Close tab ${tab.label}`}
157
+ title={`Close tab ${tab.label}`}
158
+ className="flex items-center justify-center w-4 h-4 shrink-0 rounded opacity-0 group-hover:opacity-100 transition-opacity hover:bg-destructive/15 hover:text-destructive"
159
+ onClick={(e) => {
160
+ e.stopPropagation();
161
+ onTabClose(tab.id);
162
+ }}
163
+ >
164
+ <X className="w-3.5 h-3.5" />
165
+ </button>
166
+ ) : (
167
+ <span className="w-1 shrink-0" />
168
+ )}
169
+ </div>
170
+ </React.Fragment>
171
+ );
172
+ })}
173
+ </div>
174
+ </ScrollArea>
175
+ );
176
+ };