Skip to content

Commit 9e79019

Browse files
committed
fix(studio): fix seek after code edit, improve scrub performance, add click-to-source
Three issues addressed: 1. **Seek breaks after code edit**: During crossfade refreshes the retiring Player's cleanup unconditionally nulled `iframeRef.current`, clobbering the reference the new Player had already assigned. Guard the cleanup to only clear the ref when it still points to the retiring Player's own iframe. 2. **Scrubber/timeline drag jank**: Every pointermove during a drag called the full seek pipeline (adapter.seek + setCurrentTime + React re-render cascade). RAF-throttle the expensive onSeek call during drags while keeping slider and playhead visuals updated on every pointer event for instant feedback. 3. **Click-to-source**: Clicking an element in the preview now switches to the Code tab, opens the element's source file, and scrolls the editor to the element's opening tag. Uses the existing `findTagByTarget` source patcher to locate the element by id/selector in the HTML source.
1 parent f6e187c commit 9e79019

11 files changed

Lines changed: 163 additions & 10 deletions

File tree

packages/studio/src/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ export function StudioApp() {
215215
syncPreviewHistoryHotkey: appHotkeys.syncPreviewHistoryHotkey,
216216
reloadPreview,
217217
setRefreshKey,
218+
openSourceForSelection: fileManager.openSourceForSelection,
219+
selectSidebarTab: (tab: "code" | "compositions" | "assets") =>
220+
leftSidebarRef.current?.selectTab(tab),
218221
});
219222

220223
domEditSelectionBridgeRef.current = domEditSession.domEditSelection;

packages/studio/src/components/StudioLeftSidebar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function StudioLeftSidebar({
3434
assets,
3535
editingFile,
3636
fileTree,
37+
revealSourceOffset,
3738
handleFileSelect,
3839
handleCreateFile,
3940
handleCreateFolder,
@@ -103,6 +104,7 @@ export function StudioLeftSidebar({
103104
content={editingFile.content ?? ""}
104105
filePath={editingFile.path}
105106
onChange={handleContentChange}
107+
revealOffset={revealSourceOffset}
106108
/>
107109
)
108110
) : undefined

packages/studio/src/components/editor/SourceEditor.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ interface SourceEditorProps {
5555
language?: string;
5656
onChange?: (content: string) => void;
5757
readOnly?: boolean;
58+
revealOffset?: number | null;
5859
}
5960

6061
export const SourceEditor = memo(function SourceEditor({
@@ -63,6 +64,7 @@ export const SourceEditor = memo(function SourceEditor({
6364
language,
6465
onChange,
6566
readOnly = false,
67+
revealOffset,
6668
}: SourceEditorProps) {
6769
const editorRef = useRef<EditorView | null>(null);
6870
const containerRef = useRef<HTMLDivElement | null>(null);
@@ -132,5 +134,17 @@ export const SourceEditor = memo(function SourceEditor({
132134
}
133135
}, [content]);
134136

137+
useEffect(() => {
138+
const view = editorRef.current;
139+
if (!view || revealOffset == null || revealOffset < 0) return;
140+
const docLen = view.state.doc.length;
141+
const pos = Math.min(revealOffset, docLen);
142+
view.dispatch({
143+
selection: { anchor: pos },
144+
effects: EditorView.scrollIntoView(pos, { y: "center" }),
145+
});
146+
view.focus();
147+
}, [revealOffset]);
148+
135149
return <div ref={mountEditor} className="h-full w-full overflow-hidden" />;
136150
});

packages/studio/src/contexts/FileManagerContext.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export function FileManagerProvider({
2626
readProjectFile,
2727
writeProjectFile,
2828
readOptionalProjectFile,
29+
revealSourceOffset,
30+
openSourceForSelection,
2931
handleFileSelect,
3032
handleContentChange,
3133
refreshFileTree,
@@ -62,6 +64,8 @@ export function FileManagerProvider({
6264
readProjectFile,
6365
writeProjectFile,
6466
readOptionalProjectFile,
67+
revealSourceOffset,
68+
openSourceForSelection,
6569
handleFileSelect,
6670
handleContentChange,
6771
refreshFileTree,
@@ -92,6 +96,8 @@ export function FileManagerProvider({
9296
readProjectFile,
9397
writeProjectFile,
9498
readOptionalProjectFile,
99+
revealSourceOffset,
100+
openSourceForSelection,
95101
handleFileSelect,
96102
handleContentChange,
97103
refreshFileTree,

packages/studio/src/hooks/useDomEditSession.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { useEffect } from "react";
1+
import { useCallback, useEffect } from "react";
22
import type { TimelineElement } from "../player";
33
import { STUDIO_INSPECTOR_PANELS_ENABLED } from "../components/editor/manualEditingAvailability";
4-
import { findElementForSelection } from "../components/editor/domEditing";
4+
import { findElementForSelection, type DomEditSelection } from "../components/editor/domEditing";
55
import type { ImportedFontAsset } from "../components/editor/fontAssets";
66
import type { EditHistoryKind } from "../utils/editHistory";
77
import type { RightPanelTab } from "../utils/studioHelpers";
8+
import type { PatchTarget } from "../utils/sourcePatcher";
89
import { useAskAgentModal } from "./useAskAgentModal";
910
import { useDomSelection } from "./useDomSelection";
1011
import { usePreviewInteraction } from "./usePreviewInteraction";
@@ -52,6 +53,8 @@ export interface UseDomEditSessionParams {
5253
syncPreviewHistoryHotkey: (iframe: HTMLIFrameElement | null) => void;
5354
reloadPreview: () => void;
5455
setRefreshKey: React.Dispatch<React.SetStateAction<number>>;
56+
openSourceForSelection?: (sourceFile: string, target: PatchTarget) => void;
57+
selectSidebarTab?: (tab: "code" | "compositions" | "assets") => void;
5558
}
5659

5760
// ── Hook ──
@@ -87,8 +90,25 @@ export function useDomEditSession({
8790
syncPreviewHistoryHotkey,
8891
reloadPreview,
8992
setRefreshKey: _setRefreshKey,
93+
openSourceForSelection,
94+
selectSidebarTab,
9095
}: UseDomEditSessionParams) {
9196
void _setRefreshKey;
97+
98+
const onClickToSource = useCallback(
99+
(selection: DomEditSelection) => {
100+
if (!openSourceForSelection || !selectSidebarTab) return;
101+
if (!selection.sourceFile) return;
102+
selectSidebarTab("code");
103+
openSourceForSelection(selection.sourceFile, {
104+
id: selection.id,
105+
selector: selection.selector,
106+
selectorIndex: selection.selectorIndex,
107+
});
108+
},
109+
[openSourceForSelection, selectSidebarTab],
110+
);
111+
92112
// ── Selection (delegated to useDomSelection) ──
93113

94114
const {
@@ -164,6 +184,7 @@ export function useDomEditSession({
164184
setAgentPromptSelectionContext,
165185
setAgentModalAnchorPoint,
166186
setAgentModalOpen,
187+
onClickToSource,
167188
});
168189

169190
// ── Commit handlers (delegated to useDomEditCommits) ──

packages/studio/src/hooks/useFileManager.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FONT_EXT, isMediaFile } from "../utils/mediaTypes";
44
import { fontFamilyFromAssetPath, type ImportedFontAsset } from "../components/editor/fontAssets";
55
import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
66
import type { EditHistoryKind } from "../utils/editHistory";
7+
import { findTagByTarget, type PatchTarget } from "../utils/sourcePatcher";
78

89
// ── Types ──
910

@@ -37,6 +38,7 @@ export function useFileManager({
3738
const [projectDir, setProjectDir] = useState<string | null>(null);
3839
const [fileTree, setFileTree] = useState<string[]>([]);
3940
const [fileTreeLoaded, setFileTreeLoaded] = useState(false);
41+
const [revealSourceOffset, setRevealSourceOffset] = useState<number | null>(null);
4042

4143
// ── Refs ──
4244

@@ -169,6 +171,40 @@ export function useFileManager({
169171
[domEditSaveTimestampRef, readProjectFile, recordEdit, setRefreshKey, writeProjectFile],
170172
);
171173

174+
// ── Open source for selection (click-to-source) ──
175+
176+
const pendingRevealRef = useRef<PatchTarget | null>(null);
177+
178+
const openSourceForSelection = useCallback(
179+
(sourceFile: string, target: PatchTarget) => {
180+
const pid = projectIdRef.current;
181+
if (!pid || !sourceFile) return;
182+
if (editingPathRef.current === sourceFile && editingFile?.content != null) {
183+
const match = findTagByTarget(editingFile.content, target);
184+
setRevealSourceOffset(match ? match.start : null);
185+
return;
186+
}
187+
pendingRevealRef.current = target;
188+
fetch(`/api/projects/${pid}/files/${encodeURIComponent(sourceFile)}`)
189+
.then((r) => r.json())
190+
.then((data: { content?: string }) => {
191+
if (data.content != null) {
192+
setEditingFile({ path: sourceFile, content: data.content });
193+
const pending = pendingRevealRef.current;
194+
pendingRevealRef.current = null;
195+
if (pending) {
196+
const match = findTagByTarget(data.content, pending);
197+
setRevealSourceOffset(match ? match.start : null);
198+
}
199+
}
200+
})
201+
.catch(() => {
202+
pendingRevealRef.current = null;
203+
});
204+
},
205+
[editingFile?.content],
206+
);
207+
172208
// ── File tree refresh ──
173209

174210
const refreshFileTree = useCallback(async () => {
@@ -418,6 +454,10 @@ export function useFileManager({
418454
writeProjectFile,
419455
readOptionalProjectFile,
420456

457+
// Click-to-source
458+
revealSourceOffset,
459+
openSourceForSelection,
460+
421461
// Callbacks
422462
handleFileSelect,
423463
handleContentChange,

packages/studio/src/hooks/usePreviewInteraction.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export interface UsePreviewInteractionParams {
3737
setAgentPromptSelectionContext: (context: string | undefined) => void;
3838
setAgentModalAnchorPoint: (point: AgentModalAnchorPoint | null) => void;
3939
setAgentModalOpen: (open: boolean) => void;
40+
41+
onClickToSource?: (selection: DomEditSelection) => void;
4042
}
4143

4244
// ── Hook ──
@@ -53,6 +55,7 @@ export function usePreviewInteraction({
5355
setAgentPromptSelectionContext,
5456
setAgentModalAnchorPoint,
5557
setAgentModalOpen,
58+
onClickToSource,
5659
}: UsePreviewInteractionParams) {
5760
const handlePreviewCanvasMouseDown = useCallback(
5861
(e: React.MouseEvent<HTMLDivElement>, options?: { preferClipAncestor?: boolean }) => {
@@ -70,6 +73,9 @@ export function usePreviewInteraction({
7073
? getPreviewLocalPointer(previewIframeRef.current, e.clientX, e.clientY)
7174
: null;
7275
applyDomSelection(nextSelection, { additive: e.shiftKey });
76+
if (!e.shiftKey && onClickToSource) {
77+
onClickToSource(nextSelection);
78+
}
7379
if (
7480
!e.shiftKey &&
7581
localPointer &&
@@ -87,6 +93,7 @@ export function usePreviewInteraction({
8793
applyDomSelection,
8894
captionEditMode,
8995
compositionLoading,
96+
onClickToSource,
9097
preloadAgentPromptSnippet,
9198
resolveDomSelectionFromPreviewPoint,
9299
previewIframeRef,

packages/studio/src/player/components/Player.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,17 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
268268
if (assetPollRef.current) clearInterval(assetPollRef.current);
269269
assetPollRef.current = null;
270270
container.removeChild(player);
271-
// Clear the forwarded ref
271+
// Clear the forwarded ref only if it still points to THIS iframe.
272+
// During crossfade refreshes the retiring Player unmounts after the
273+
// new Player has already assigned its iframe to the same ref — blindly
274+
// nulling it would break seeking in the new Player.
272275
if (typeof ref === "function") {
273276
ref(null);
274277
} else if (ref) {
275-
(ref as React.MutableRefObject<HTMLIFrameElement | null>).current = null;
278+
const mutableRef = ref as React.MutableRefObject<HTMLIFrameElement | null>;
279+
if (mutableRef.current === iframe) {
280+
mutableRef.current = null;
281+
}
276282
}
277283
};
278284
});

packages/studio/src/player/components/PlayerControls.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,39 @@ export const PlayerControls = memo(function PlayerControls({
207207

208208
seekFromClientX(e.clientX);
209209

210+
// During drag, update the slider visual immediately on every pointer
211+
// event but RAF-throttle the actual onSeek call. The seek path triggers
212+
// adapter.seek + setCurrentTime + React re-renders which can take >16ms
213+
// on complex compositions — keeping visual feedback on the raw event and
214+
// batching the expensive work to one call per frame keeps scrubbing at
215+
// 60 fps.
216+
let seekRafId = 0;
217+
let pendingClientX = e.clientX;
210218
const onMove = (ev: PointerEvent) => {
211-
if (ev.pointerId !== pointerId) return;
212-
if (isDraggingRef.current) seekFromClientX(ev.clientX);
219+
if (ev.pointerId !== pointerId || !isDraggingRef.current) return;
220+
pendingClientX = ev.clientX;
221+
const bar = seekBarRef.current;
222+
const dur = durationRef.current;
223+
if (bar && dur > 0) {
224+
const rect = bar.getBoundingClientRect();
225+
const pct = resolveSeekPercent(ev.clientX, rect.left, rect.width) * 100;
226+
if (progressFillRef.current) progressFillRef.current.style.width = `${pct}%`;
227+
if (progressThumbRef.current) progressThumbRef.current.style.left = `${pct}%`;
228+
}
229+
if (!seekRafId) {
230+
seekRafId = requestAnimationFrame(() => {
231+
seekRafId = 0;
232+
if (isDraggingRef.current) seekFromClientX(pendingClientX);
233+
});
234+
}
213235
};
214236
const cleanup = () => {
215237
isDraggingRef.current = false;
238+
if (seekRafId) {
239+
cancelAnimationFrame(seekRafId);
240+
seekRafId = 0;
241+
}
242+
seekFromClientX(pendingClientX);
216243
try {
217244
target.releasePointerCapture(pointerId);
218245
} catch {

packages/studio/src/player/components/useTimelineRangeSelection.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export function useTimelineRangeSelection({
3838
anchorY: number;
3939
} | null>(null);
4040

41+
const seekRafRef = useRef(0);
42+
const pendingClientXRef = useRef(0);
43+
4144
const handlePointerDown = useCallback(
4245
(e: React.PointerEvent) => {
4346
if (e.button !== 0) return;
@@ -80,8 +83,27 @@ export function useTimelineRangeSelection({
8083
return;
8184
}
8285
if (!isDragging.current) return;
83-
seekFromX(e.clientX);
84-
autoScrollDuringDrag(e.clientX);
86+
pendingClientXRef.current = e.clientX;
87+
// Update the playhead visual immediately via liveTime for smooth feedback,
88+
// then RAF-throttle the full seek (adapter + React state sync).
89+
const el = scrollRef.current;
90+
if (el) {
91+
const rect = el.getBoundingClientRect();
92+
const x = e.clientX - rect.left + el.scrollLeft - GUTTER;
93+
if (x >= 0) {
94+
const dur = el.scrollWidth / pps;
95+
liveTime.notify(Math.max(0, Math.min(dur, x / pps)));
96+
}
97+
}
98+
if (!seekRafRef.current) {
99+
seekRafRef.current = requestAnimationFrame(() => {
100+
seekRafRef.current = 0;
101+
if (isDragging.current) {
102+
seekFromX(pendingClientXRef.current);
103+
autoScrollDuringDrag(pendingClientXRef.current);
104+
}
105+
});
106+
}
85107
},
86108
[seekFromX, autoScrollDuringDrag, pps, scrollRef, isDragging],
87109
);
@@ -104,9 +126,14 @@ export function useTimelineRangeSelection({
104126
});
105127
return;
106128
}
129+
if (seekRafRef.current) {
130+
cancelAnimationFrame(seekRafRef.current);
131+
seekRafRef.current = 0;
132+
}
133+
seekFromX(pendingClientXRef.current);
107134
isDragging.current = false;
108135
cancelAnimationFrame(dragScrollRaf.current);
109-
}, [isDragging, dragScrollRaf, setShowPopover]);
136+
}, [isDragging, dragScrollRaf, setShowPopover, seekFromX]);
110137

111138
return {
112139
rangeSelection,

0 commit comments

Comments
 (0)