Skip to content

Commit 81f1a90

Browse files
fix(studio): rebind paused preview after live edits (#3596)
1 parent 6b360f5 commit 81f1a90

5 files changed

Lines changed: 82 additions & 17 deletions

File tree

‎packages/studio/src/hooks/timelineTrackVisibility.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,15 @@ describe("toggleTimelineElementHidden", () => {
316316
const iframe = document.createElement("iframe");
317317
document.body.append(iframe);
318318
const seek = vi.fn();
319+
const forceTimelineRebind = vi.fn();
319320
const win = iframe.contentWindow;
320321
if (!win) throw new Error("Expected iframe contentWindow");
321-
const playerWindow: Window & { __player?: { seek?: (time: number) => void } } = win;
322+
const playerWindow: Window & {
323+
__player?: { seek?: (time: number) => void };
324+
__hfForceTimelineRebind?: () => void;
325+
} = win;
322326
playerWindow.__player = { seek };
327+
playerWindow.__hfForceTimelineRebind = forceTimelineRebind;
323328

324329
const files = new Map([
325330
[
@@ -369,6 +374,7 @@ describe("toggleTimelineElementHidden", () => {
369374
expect(recordEdit).toHaveBeenCalledTimes(1);
370375
expect(recordEdit.mock.calls[0]?.[0]?.label).toBe("Hide element");
371376
expect(seek).toHaveBeenCalledWith(1.25);
377+
expect(forceTimelineRebind).toHaveBeenCalledTimes(1);
372378
expect(
373379
usePlayerStore.getState().elements.find((el) => el.key === "index.html:#hero")?.hidden,
374380
).toBe(true);

‎packages/studio/src/hooks/timelineTrackVisibility.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useCallback } from "react";
22
import { usePlayerStore, type TimelineElement } from "../player";
3+
import { reseekPreviewAtTime } from "../player/hooks/timelineSyncHydration";
34
import { useExpandedTimelineElements } from "../player/hooks/useExpandedTimelineElements";
5+
import { applySoftReloadFinalization } from "../utils/gsapSoftReload";
46
import {
57
timelineTrackOrder,
68
trackDisplayNumber,
@@ -106,11 +108,9 @@ function patchLiveHiddenState(
106108
}
107109

108110
export function reseekPreviewRuntime(iframe: HTMLIFrameElement | null): void {
109-
try {
110-
const win: (Window & { __player?: { seek?: (time: number) => void } }) | null =
111-
iframe?.contentWindow ?? null;
112-
win?.__player?.seek?.(usePlayerStore.getState().currentTime);
113-
} catch {}
111+
const store = usePlayerStore.getState();
112+
if (applySoftReloadFinalization(iframe, store.currentTime)) return;
113+
reseekPreviewAtTime({ seek: store.requestSeek }, store.currentTime);
114114
}
115115

116116
export function groupElementsByTargetPath(

‎packages/studio/src/hooks/useDomEditCommits.test.tsx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,42 @@ describe("useDomEditCommits style persist handling", () => {
981981
}
982982
});
983983

984+
it("rebinds the paused preview after a saved optimistic style commit", async () => {
985+
stubPatchFetch({
986+
ok: true,
987+
changed: true,
988+
matched: true,
989+
path: "index.html",
990+
version: '"sha256:changed"',
991+
});
992+
const { iframe, element } = createPreviewElement();
993+
const seek = vi.fn();
994+
const forceTimelineRebind = vi.fn();
995+
Object.defineProperty(iframe.contentWindow, "__player", {
996+
configurable: true,
997+
value: { seek },
998+
});
999+
Object.defineProperty(iframe.contentWindow, "__hfForceTimelineRebind", {
1000+
configurable: true,
1001+
value: forceTimelineRebind,
1002+
});
1003+
usePlayerStore.setState({ currentTime: 2.4 });
1004+
const rendered = renderDomEditCommits(createSelection(element), iframe);
1005+
1006+
try {
1007+
await act(async () => {
1008+
await rendered.hook.handleDomStyleCommit("color", "blue");
1009+
});
1010+
1011+
expect(seek).toHaveBeenCalledWith(2.4);
1012+
expect(forceTimelineRebind).toHaveBeenCalledTimes(1);
1013+
expect(rendered.reloadPreview).not.toHaveBeenCalled();
1014+
} finally {
1015+
rendered.cleanup();
1016+
usePlayerStore.getState().reset();
1017+
}
1018+
});
1019+
9841020
it("toasts and reverts a style commit when the patch request rejects", async () => {
9851021
const { element, rendered, cleanup } = await commitStyleAgainst(new Error("network down"));
9861022

‎packages/studio/src/hooks/useDomEditCommits.ts‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from "./useDomEditCommitsHelpers";
3535
import type { CutoverResult } from "../utils/sdkCutover";
3636
import { studioWriteHeaders } from "../utils/studioFileVersion";
37+
import { reseekPreviewRuntime } from "./timelineTrackVisibility";
3738
interface RecordEditInput {
3839
label: string;
3940
kind: EditHistoryKind;
@@ -154,6 +155,10 @@ export function useDomEditCommits({
154155
if (options?.shouldSave && !options.shouldSave()) return;
155156

156157
const targetPath = selection.sourceFile || activeCompPath || "index.html";
158+
const completePersistence = <T>(result: T, changed: boolean): T => {
159+
if (options?.skipRefresh && changed) reseekPreviewRuntime(previewIframeRef.current);
160+
return result;
161+
};
157162

158163
const readResponse = await fetch(
159164
`/api/projects/${pid}/files/${encodeURIComponent(targetPath)}`,
@@ -201,7 +206,10 @@ export function useDomEditCommits({
201206
if (cutover.status === "committed") {
202207
// SDK handled it — its in-memory doc is already current, so do NOT
203208
// forceReload (that would echo-reload the session we just wrote).
204-
return { sourceFile: targetPath, version: cutover.version, changed: true };
209+
return completePersistence(
210+
{ sourceFile: targetPath, version: cutover.version, changed: true },
211+
true,
212+
);
205213
}
206214
}
207215

@@ -249,9 +257,12 @@ export function useDomEditCommits({
249257
throw new DomEditPersistUnresolvableError(targetPath);
250258
}
251259
warnDomEditPersistNoOp(selection, operations);
252-
return typeof patchData.path === "string" && typeof patchData.version === "string"
253-
? { sourceFile: patchData.path, version: patchData.version, changed: false }
254-
: undefined;
260+
return completePersistence(
261+
typeof patchData.path === "string" && typeof patchData.version === "string"
262+
? { sourceFile: patchData.path, version: patchData.version, changed: false }
263+
: undefined,
264+
false,
265+
);
255266
}
256267

257268
const patchedContent =
@@ -289,11 +300,14 @@ export function useDomEditCommits({
289300
if (!options?.skipRefresh) {
290301
reloadPreview();
291302
}
292-
return finalContent === patchedContent &&
293-
typeof patchData.path === "string" &&
294-
typeof patchData.version === "string"
295-
? { sourceFile: patchData.path, version: patchData.version, changed: true }
296-
: undefined;
303+
return completePersistence(
304+
finalContent === patchedContent &&
305+
typeof patchData.path === "string" &&
306+
typeof patchData.version === "string"
307+
? { sourceFile: patchData.path, version: patchData.version, changed: true }
308+
: undefined,
309+
true,
310+
);
297311
},
298312
[
299313
activeCompPath,
@@ -305,6 +319,7 @@ export function useDomEditCommits({
305319
showToast,
306320
forceReloadSdkSession,
307321
onTrySdkPersist,
322+
previewIframeRef,
308323
],
309324
);
310325

‎packages/studio/src/player/hooks/timelineSyncHydration.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ export function resolveReloadSeekTime(input: {
290290
return Math.min(target, input.duration);
291291
}
292292

293+
type SeekablePreview = Pick<PlaybackAdapter, "seek">;
294+
295+
/** Re-run the current frame even when the runtime already reports that time. */
296+
export function reseekPreviewAtTime(preview: SeekablePreview, time: number): void {
297+
const target = Number.isFinite(time) && time > 0 ? time : 0;
298+
preview.seek(target > 0.001 ? Math.max(0, target - 0.001) : 0.001);
299+
preview.seek(target);
300+
}
301+
293302
export function seekAdapterToRestorePoint(
294303
adapter: PlaybackAdapter,
295304
pendingSeekRef: { current: number | null },
@@ -303,8 +312,7 @@ export function seekAdapterToRestorePoint(
303312
});
304313
pendingSeekRef.current = null;
305314
if (storeSeek != null) usePlayerStore.getState().clearSeekRequest();
306-
adapter.seek(startTime > 0.001 ? Math.max(0, startTime - 0.001) : 0.001);
307-
adapter.seek(startTime);
315+
reseekPreviewAtTime(adapter, startTime);
308316
return startTime;
309317
}
310318

0 commit comments

Comments
 (0)