Skip to content

Automate browser based workflows with AI

fix(SKY-11759): studio editor pane — recenter on pane layout changes + start-anchored initial viewport (SKY-11761) (#7043)

Celal Zamanoğlu4439cabdbd57
diff --git a/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx b/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
index ff14a4782..456ad4b5d 100644
--- a/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
@@ -101,7 +101,10 @@ import {
isMeaningfulPaneResize,
isViewportStranded,
PANE_FIT_DEBOUNCE_MS,
+ paneRecenterViewport,
paneRefitDuration,
+ START_ANCHOR_MIN_ZOOM,
+ startAnchoredViewport,
} from "./paneFit";
import { WorkflowScopeContext } from "./WorkflowScopeContext";
import { FitViewControl } from "./controls/FitViewControl";
@@ -414,6 +417,8 @@ type Props = {
// Embedded in the studio shell: aligns the block-config sidebar's top/bottom
// edges with the Copilot column instead of the legacy header offsets.
embedded?: boolean;
+ // Studio open-pane key; a change recenters the canvas once the layout settles.
+ paneLayoutKey?: string;
};
function FlowRenderer({
@@ -439,6 +444,7 @@ function FlowRenderer({
onLayoutPhaseChange,
centerOffsetX = 0,
embedded = false,
+ paneLayoutKey,
}: Props) {
const { blockLabel: targettedBlockLabel } = useParams();
const reactFlowInstance = useReactFlow();
@@ -486,6 +492,10 @@ function FlowRenderer({
// before invoking the API and cleared after the animation duration.
const fitViewInProgressRef = useRef(false);
+ // Last wheel/pointer/keyboard-zoom touch on the canvas; a pane-layout
+ // recenter yields to any interaction newer than the layout change.
+ const lastCanvasInteractionAtRef = useRef(0);
+
const runFitView = useCallback(
(options?: { maxZoom?: number; duration?: number }) => {
const duration = options?.duration ?? 300;
@@ -625,11 +635,13 @@ function FlowRenderer({
}
if (meta && (event.key === "=" || event.key === "+")) {
event.preventDefault();
+ lastCanvasInteractionAtRef.current = Date.now();
reactFlowInstance.zoomIn({ duration: 200 });
return;
}
if (meta && (event.key === "-" || event.key === "_")) {
event.preventDefault();
+ lastCanvasInteractionAtRef.current = Date.now();
reactFlowInstance.zoomOut({ duration: 200 });
return;
}
@@ -1601,15 +1613,88 @@ function FlowRenderer({
// Studio pane fit. The editor pane can mount hidden (display:none while
// closed) and resizes in discrete steps as sibling panes toggle. Fit once
// when the initial Dagre pass has settled AND the pane is visible; after
- // that, a debounced ResizeObserver re-fits only when a real geometry change
- // leaves the viewport stranded, so it never fights a deliberate pan/zoom.
+ // that, a pane-layout change (?panes=) recenters once the flex layout
+ // settles, and a debounced ResizeObserver re-fits only when a real
+ // geometry change leaves the viewport stranded, so neither path fights a
+ // deliberate pan/zoom.
const hasInitialPaneFitRef = useRef(false);
const lastPaneSizeRef = useRef<{ width: number; height: number } | null>(
null,
);
+ const pendingPaneRecenterRef = useRef(false);
+ const paneLayoutChangedAtRef = useRef(0);
+ const paneSettleTimerRef = useRef<number | null>(null);
+ const paneSettleRef = useRef<(() => void) | null>(null);
const layoutSettledRef = useRef(false);
layoutSettledRef.current = layoutPhase !== "pre-layout";
+ // One debounce shared by the ResizeObserver and pane-layout changes, so a
+ // recenter fires once, after the last of the layout event and its resize
+ // burst (per-frame reactions exhaust the dimension→layout budget).
+ const schedulePaneSettle = useCallback(() => {
+ if (paneSettleTimerRef.current !== null) {
+ window.clearTimeout(paneSettleTimerRef.current);
+ }
+ paneSettleTimerRef.current = window.setTimeout(() => {
+ paneSettleTimerRef.current = null;
+ paneSettleRef.current?.();
+ }, PANE_FIT_DEBOUNCE_MS);
+ }, []);
+
+ // Anchors the flow's start at the pane top instead of centering the whole
+ // graph, matching the legacy editor's default zoom on long workflows.
+ const runStartAnchoredFit = useCallback(
+ (pane: { width: number; height: number }) => {
+ const visibleNodes = reactFlowInstance
+ .getNodes()
+ .filter((node) => !node.hidden);
+ if (visibleNodes.length === 0) {
+ return;
+ }
+ const viewport = startAnchoredViewport({
+ pane,
+ bounds: getNodesBounds(visibleNodes),
+ });
+ if (viewport === null) {
+ return;
+ }
+ // The initial anchor is deliberately instant (no animation, unlike the
+ // recenter below); 50ms just covers the viewport state flush.
+ fitViewInProgressRef.current = true;
+ reactFlowInstance.setViewport(viewport);
+ window.setTimeout(() => {
+ fitViewInProgressRef.current = false;
+ }, 50);
+ },
+ [reactFlowInstance],
+ );
+
+ const runPaneRecenter = useCallback(
+ (pane: { width: number; height: number }) => {
+ const visibleNodes = reactFlowInstance
+ .getNodes()
+ .filter((node) => !node.hidden);
+ if (visibleNodes.length === 0) {
+ return;
+ }
+ const viewport = paneRecenterViewport({
+ pane,
+ bounds: getNodesBounds(visibleNodes),
+ viewport: reactFlowInstance.getViewport(),
+ });
+ if (viewport === null) {
+ return;
+ }
+ const duration = paneRefitDuration();
+ fitViewInProgressRef.current = true;
+ reactFlowInstance.setViewport(viewport, { duration });
+ window.setTimeout(() => {
+ fitViewInProgressRef.current = false;
+ }, duration + 50);
+ },
+ [reactFlowInstance],
+ );
+
// "initial-load" lands one frame after Dagre positions commit (mid fade-in),
// so fitting here can't read pre-layout node positions. layoutPhase only
// advances once nodesInitialized flips, but guard explicitly so a future
@@ -1629,8 +1714,25 @@ function FlowRenderer({
}
hasInitialPaneFitRef.current = true;
lastPaneSizeRef.current = { width: rect.width, height: rect.height };
- runFitView({ duration: 0 });
- }, [embedded, layoutPhase, nodesInitialized, runFitView]);
+ runStartAnchoredFit({ width: rect.width, height: rect.height });
+ }, [embedded, layoutPhase, nodesInitialized, runStartAnchoredFit]);
+
+ // Pane-set/order changes are explicit recenter triggers: a sibling pane
+ // toggling shifts the canvas without tripping the stranded threshold, but
+ // the flow should read as centered again once the layout settles.
+ const prevPaneLayoutKeyRef = useRef(paneLayoutKey);
+ useEffect(() => {
+ if (!embedded || paneLayoutKey === undefined) {
+ return;
+ }
+ if (prevPaneLayoutKeyRef.current === paneLayoutKey) {
+ return;
+ }
+ prevPaneLayoutKeyRef.current = paneLayoutKey;
+ pendingPaneRecenterRef.current = true;
+ paneLayoutChangedAtRef.current = Date.now();
+ schedulePaneSettle();
+ }, [embedded, paneLayoutKey, schedulePaneSettle]);
useEffect(() => {
if (!embedded) {
@@ -1640,7 +1742,6 @@ function FlowRenderer({
if (!el) {
return;
}
- let timer: number | null = null;
// Covers a pane that was hidden while layout settled; before that, the
// layout-phase effect above owns the first fit.
const initialFit = (size: { width: number; height: number }) => {
@@ -1648,8 +1749,17 @@ function FlowRenderer({
return;
}
hasInitialPaneFitRef.current = true;
+ pendingPaneRecenterRef.current = false;
lastPaneSizeRef.current = size;
- runFitView({ duration: 0 });
+ runStartAnchoredFit(size);
+ };
+ const paneRecenter = (size: { width: number; height: number }) => {
+ lastPaneSizeRef.current = size;
+ // A deliberate pan/zoom after the layout change wins over the recenter.
+ if (lastCanvasInteractionAtRef.current > paneLayoutChangedAtRef.current) {
+ return;
+ }
+ runPaneRecenter(size);
};
const strandedRefit = (size: { width: number; height: number }) => {
const prev = lastPaneSizeRef.current;
@@ -1673,7 +1783,6 @@ function FlowRenderer({
}
};
const settle = () => {
- timer = null;
const rect = el.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) {
return;
@@ -1681,26 +1790,48 @@ function FlowRenderer({
const size = { width: rect.width, height: rect.height };
if (!hasInitialPaneFitRef.current) {
initialFit(size);
- } else {
- strandedRefit(size);
+ return;
}
+ if (pendingPaneRecenterRef.current) {
+ pendingPaneRecenterRef.current = false;
+ paneRecenter(size);
+ return;
+ }
+ strandedRefit(size);
+ };
+ paneSettleRef.current = settle;
+ const markInteraction = () => {
+ lastCanvasInteractionAtRef.current = Date.now();
};
+ el.addEventListener("wheel", markInteraction, {
+ capture: true,
+ passive: true,
+ });
+ el.addEventListener("pointerdown", markInteraction, { capture: true });
const observer = new ResizeObserver(() => {
- // Settle after the burst; per-frame reactions exhaust the
- // dimension→layout convergence budget (see dimensionConvergence.ts).
- if (timer !== null) {
- window.clearTimeout(timer);
- }
- timer = window.setTimeout(settle, PANE_FIT_DEBOUNCE_MS);
+ schedulePaneSettle();
});
observer.observe(el);
return () => {
observer.disconnect();
- if (timer !== null) {
- window.clearTimeout(timer);
+ el.removeEventListener("wheel", markInteraction, { capture: true });
+ el.removeEventListener("pointerdown", markInteraction, {
+ capture: true,
+ });
+ paneSettleRef.current = null;
+ if (paneSettleTimerRef.current !== null) {
+ window.clearTimeout(paneSettleTimerRef.current);
+ paneSettleTimerRef.current = null;
}
};
- }, [embedded, reactFlowInstance, runFitView]);
+ }, [
+ embedded,
+ reactFlowInstance,
+ runFitView,
+ runPaneRecenter,
+ runStartAnchoredFit,
+ schedulePaneSettle,
+ ]);
const zoomLock = 1 as const;
const yLockMax = 140 as const;
@@ -2028,7 +2159,7 @@ function FlowRenderer({
}
}}
maxZoom={flowIsConstrained ? 1 : 2}
- minZoom={flowIsConstrained ? 1 : 0.5}
+ minZoom={flowIsConstrained ? 1 : START_ANCHOR_MIN_ZOOM}
panOnDrag={true}
panOnScroll={true}
panOnScrollMode={PanOnScrollMode.Vertical}
diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
index 8030d969f..ce4f298e5 100644
--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
@@ -2091,6 +2091,7 @@ function Workspace({
initialTitle={initialTitle}
workflow={workflow}
embedded={embedded}
+ paneLayoutKey={embedded ? studioPanes.join(",") : undefined}
onRequestDeleteNode={handleRequestDeleteNode}
captureHistoryImmediately={captureWorkflowEditImmediately}
onAddNode={addNode}
diff --git a/skyvern-frontend/src/routes/workflows/editor/paneFit.test.ts b/skyvern-frontend/src/routes/workflows/editor/paneFit.test.ts
index 86b6fd204..84391346e 100644
--- a/skyvern-frontend/src/routes/workflows/editor/paneFit.test.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/paneFit.test.ts
@@ -4,6 +4,11 @@ import {
isMeaningfulPaneResize,
isViewportStranded,
PANE_RESIZE_EPSILON_PX,
+ paneRecenterViewport,
+ START_ANCHOR_MARGIN_X_PX,
+ START_ANCHOR_MIN_ZOOM,
+ START_ANCHOR_TOP_PX,
+ startAnchoredViewport,
} from "./paneFit";
describe("isMeaningfulPaneResize", () => {
@@ -110,3 +115,131 @@ describe("isViewportStranded", () => {
).toBe(false);
});
});
+
+describe("startAnchoredViewport", () => {
+ // A long chain: taller than any pane, so a whole-graph fit would zoom far
+ // out and center the middle.
+ const longChain = { x: 0, y: 0, width: 500, height: 4000 };
+
+ test("anchors the flow start at the pane top at 1:1 zoom when the chain fits", () => {
+ expect(
+ startAnchoredViewport({
+ pane: { width: 800, height: 600 },
+ bounds: longChain,
+ }),
+ ).toEqual({ x: 150, y: START_ANCHOR_TOP_PX, zoom: 1 });
+ });
+
+ test("never zooms in past 1:1 on a very wide pane", () => {
+ const viewport = startAnchoredViewport({
+ pane: { width: 2000, height: 900 },
+ bounds: longChain,
+ });
+ expect(viewport?.zoom).toBe(1);
+ expect(viewport?.x).toBe((2000 - 500) / 2);
+ });
+
+ test("zooms out to fit the chain width in a narrow pane", () => {
+ const pane = { width: 424, height: 600 };
+ const viewport = startAnchoredViewport({ pane, bounds: longChain });
+ expect(viewport?.zoom).toBeCloseTo(
+ (pane.width - 2 * START_ANCHOR_MARGIN_X_PX) / longChain.width,
+ );
+ // Fit-width leaves exactly the margin as equal gutters.
+ expect(viewport?.x).toBeCloseTo(START_ANCHOR_MARGIN_X_PX);
+ });
+
+ test("respects the canvas minimum zoom in an over-tight pane", () => {
+ const viewport = startAnchoredViewport({
+ pane: { width: 220, height: 600 },
+ bounds: longChain,
+ });
+ expect(viewport?.zoom).toBe(START_ANCHOR_MIN_ZOOM);
+ // Still centered: overflow crops both sides equally.
+ expect(viewport?.x).toBeCloseTo((220 - 500 * START_ANCHOR_MIN_ZOOM) / 2);
+ });
+
+ test("offset bounds still land the first block at the top margin", () => {
+ const bounds = { x: -100, y: 300, width: 500, height: 2000 };
+ const viewport = startAnchoredViewport({
+ pane: { width: 800, height: 600 },
+ bounds,
+ });
+ expect(viewport).not.toBeNull();
+ const { x, y, zoom } = viewport!;
+ // Screen-space position of the flow's top-left corner.
+ expect(bounds.y * zoom + y).toBeCloseTo(START_ANCHOR_TOP_PX);
+ expect(bounds.x * zoom + x).toBeCloseTo((800 - 500) / 2);
+ });
+
+ test("degenerate panes and empty flows return null", () => {
+ expect(
+ startAnchoredViewport({
+ pane: { width: 0, height: 0 },
+ bounds: longChain,
+ }),
+ ).toBeNull();
+ expect(
+ startAnchoredViewport({
+ pane: { width: 800, height: 600 },
+ bounds: { x: 0, y: 0, width: 0, height: 0 },
+ }),
+ ).toBeNull();
+ });
+});
+
+describe("paneRecenterViewport", () => {
+ const chain = { x: 0, y: 0, width: 500, height: 4000 };
+
+ test("recenters horizontally and keeps the scroll position at unchanged zoom", () => {
+ // Fitted for a 1000-wide pane (x=250), scrolled into the flow (y=-800),
+ // then a sibling pane opened and the pane shrank to 700.
+ expect(
+ paneRecenterViewport({
+ pane: { width: 700, height: 600 },
+ bounds: chain,
+ viewport: { x: 250, y: -800, zoom: 1 },
+ }),
+ ).toEqual({ x: 100, y: -800, zoom: 1 });
+ });
+
+ test("scales the scroll anchor when the new width changes zoom", () => {
+ const pane = { width: 424, height: 600 };
+ const viewport = paneRecenterViewport({
+ pane,
+ bounds: chain,
+ viewport: { x: 250, y: -800, zoom: 1 },
+ });
+ expect(viewport).not.toBeNull();
+ const expectedZoom = (pane.width - 2 * START_ANCHOR_MARGIN_X_PX) / 500;
+ expect(viewport!.zoom).toBeCloseTo(expectedZoom);
+ // The content point at the pane's top edge stays put across the zoom.
+ expect(-viewport!.y / viewport!.zoom).toBeCloseTo(800);
+ });
+
+ test("is a no-op for an already start-anchored viewport", () => {
+ const pane = { width: 800, height: 600 };
+ const anchored = startAnchoredViewport({ pane, bounds: chain });
+ expect(anchored).not.toBeNull();
+ expect(
+ paneRecenterViewport({ pane, bounds: chain, viewport: anchored! }),
+ ).toEqual(anchored);
+ });
+
+ test("degenerate geometry returns null", () => {
+ expect(
+ paneRecenterViewport({
+ pane: { width: 0, height: 0 },
+ bounds: chain,
+ viewport: { x: 0, y: 0, zoom: 1 },
+ }),
+ ).toBeNull();
+ expect(
+ paneRecenterViewport({
+ pane: { width: 800, height: 600 },
+ bounds: chain,
+ viewport: { x: 0, y: 0, zoom: 0 },
+ }),
+ ).toBeNull();
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/paneFit.ts b/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
index 6ecd7fbd2..5aa44bf07 100644
--- a/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
@@ -82,3 +82,74 @@ export function paneRefitDuration(): number {
? 0
: 150;
}
+
+// The legacy editor renders blocks at 1:1; the start anchor only zooms out
+// when the chain can't fit the pane width. The floor is also the canvas's
+// minZoom prop, so the computed x-centering matches the zoom actually applied.
+export const START_ANCHOR_MAX_ZOOM = 1;
+export const START_ANCHOR_MIN_ZOOM = 0.5;
+
+export const START_ANCHOR_MARGIN_X_PX = 24;
+export const START_ANCHOR_TOP_PX = 24;
+
+/**
+ * Viewport that anchors the flow's START at the pane top, horizontally
+ * centered, at a legacy-like zoom (1:1, or fit-width when the pane is
+ * narrower than the chain). Unlike a whole-graph fitView, a long flow loads
+ * reading from its first block instead of centered on its middle.
+ */
+export function startAnchoredViewport({
+ pane,
+ bounds,
+}: {
+ pane: Size;
+ bounds: Rect;
+}): Viewport | null {
+ if (pane.width <= 0 || pane.height <= 0) {
+ return null;
+ }
+ if (bounds.width <= 0 || bounds.height <= 0) {
+ return null;
+ }
+ const fitWidthZoom =
+ (pane.width - 2 * START_ANCHOR_MARGIN_X_PX) / bounds.width;
+ const zoom = Math.min(
+ START_ANCHOR_MAX_ZOOM,
+ Math.max(START_ANCHOR_MIN_ZOOM, fitWidthZoom),
+ );
+ return {
+ x: (pane.width - bounds.width * zoom) / 2 - bounds.x * zoom,
+ y: START_ANCHOR_TOP_PX - bounds.y * zoom,
+ zoom,
+ };
+}
+
+/**
+ * Viewport for a pane-layout change (panes opened/closed/reordered):
+ * re-fits horizontally at the start-anchor zoom while keeping the content
+ * point at the pane's top edge fixed, so the user's scroll position through
+ * the flow survives the new geometry. Idempotent for an already-anchored
+ * viewport, so back-to-back layout events converge.
+ */
+export function paneRecenterViewport({
+ pane,
+ bounds,
+ viewport,
+}: {
+ pane: Size;
+ bounds: Rect;
+ viewport: Viewport;
+}): Viewport | null {
+ if (viewport.zoom <= 0) {
+ return null;
+ }
+ const target = startAnchoredViewport({ pane, bounds });
+ if (target === null) {
+ return null;
+ }
+ return {
+ x: target.x,
+ y: viewport.y * (target.zoom / viewport.zoom),
+ zoom: target.zoom,
+ };
+}