From 5e6068e94a82d35815611cbecfa8830c3441a348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 22 Nov 2026 07:24:10 +0000 Subject: [PATCH] fix(studio): a reloaded preview shows the saved grade, not one an undo took back (#8455) * test(studio): a dragged grade saves to the file once * fix(studio): a colour grade made in the property panel saves to the file * test(studio): the grade panel follows an undo of a saved grade * fix(studio): the grade panel follows an undo or redo of the grade * test(studio): the grade-save panel test lives in its own file * fix(studio): a reloaded preview shows the saved grade, not one an undo took back * refactor(studio): grade saves live in their own hook --- .../editor/useColorGradingController.test.ts | 66 ++++++++++++++++++- .../editor/useColorGradingController.ts | 5 +- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/packages/studio/src/components/editor/useColorGradingController.test.ts b/packages/studio/src/components/editor/useColorGradingController.test.ts index 070962bd2a..56f466b910 100644 --- a/packages/studio/src/components/editor/useColorGradingController.test.ts +++ b/packages/studio/src/components/editor/useColorGradingController.test.ts @@ -810,8 +810,12 @@ describe("useColorGradingController after the preview iframe is replaced", () => const initialElement = makeElement({ dataAttributes: { "color-grading": JSON.stringify({ preset: "bright-pop", intensity: 1 }) }, }); - const { root } = renderHook(vi.fn(), initialElement, ref); + const { root, getState } = renderHook(vi.fn(), initialElement, ref); act(() => vi.advanceTimersByTime(100)); + // A drag not saved yet: the one grade a new frame cannot read from the file. + const draft = normalizeHfColorGrading({ adjust: { exposure: 1.5 } }); + if (!draft) throw new Error("expected a grading"); + act(() => getState().commitColorGrading(draft)); const postToA = vi.spyOn(a.contentWindow, "postMessage"); const postToB = vi.spyOn(b.contentWindow, "postMessage"); @@ -859,6 +863,66 @@ describe("useColorGradingController after the preview iframe is replaced", () => vi.useRealTimers(); }); + it("sends a reloaded preview no grade once the grade is saved, so an undo's file grade shows", () => { + vi.useFakeTimers(); + const { contentWindow, iframe } = createPreviewFrame(); + const onSetAttributeQuiet = vi.fn( + (_attr: string, _value: string | null, onSettled?: (ok: boolean) => void) => + onSettled?.(true), + ); + const before = makeElement({ + dataAttributes: { "color-grading": '{"adjust":{"exposure":0.1}}' }, + }); + const { root, getState, rerenderWithElement } = renderHook(onSetAttributeQuiet, before, { + current: iframe, + }); + const brighter = normalizeHfColorGrading({ adjust: { exposure: 1.5 } }); + if (!brighter) throw new Error("expected a grading"); + act(() => getState().commitColorGrading(brighter)); + act(() => vi.advanceTimersByTime(400)); + rerenderWithElement( + makeElement({ dataAttributes: { "color-grading": onSetAttributeQuiet.mock.calls[0]![1] } }), + ); + const postMessage = vi.spyOn(contentWindow, "postMessage"); + // An undo wrote the file back to 0.1; the reloaded preview is ready before the timeline says so. + const ready = { source: "hf-preview", type: "ready", ...runtimeProtocolMetadata(30) }; + act(() => { + window.dispatchEvent(new MessageEvent("message", { data: ready, source: contentWindow })); + }); + + expect(colorGradingMessages(postMessage.mock.calls)).toEqual([]); + act(() => root.unmount()); + vi.useRealTimers(); + }); + + it("keeps a drag on a preview reloaded before the drag is saved, and while it is being saved", () => { + vi.useFakeTimers(); + const { contentWindow, iframe } = createPreviewFrame(); + const before = makeElement({ + dataAttributes: { "color-grading": '{"adjust":{"exposure":0.1}}' }, + }); + const { root, getState } = renderHook(vi.fn(), before, { current: iframe }); + const brighter = normalizeHfColorGrading({ adjust: { exposure: 1.5 } }); + if (!brighter) throw new Error("expected a grading"); + const ready = { source: "hf-preview", type: "ready", ...runtimeProtocolMetadata(30) }; + const reloadedGrade = () => { + const postMessage = vi.spyOn(contentWindow, "postMessage"); + act(() => { + window.dispatchEvent(new MessageEvent("message", { data: ready, source: contentWindow })); + }); + const sent = colorGradingMessages(postMessage.mock.calls).at(-1)?.grading; + postMessage.mockRestore(); + return sent; + }; + + act(() => getState().commitColorGrading(brighter)); + expect(reloadedGrade(), "waiting to save").toMatchObject({ adjust: { exposure: 1.5 } }); + act(() => vi.advanceTimersByTime(400)); + expect(reloadedGrade(), "being saved").toMatchObject({ adjust: { exposure: 1.5 } }); + act(() => root.unmount()); + vi.useRealTimers(); + }); + it("keeps an edit that is still waiting to save over an older value from the element", () => { vi.useFakeTimers(); const before = makeElement({ diff --git a/packages/studio/src/components/editor/useColorGradingController.ts b/packages/studio/src/components/editor/useColorGradingController.ts index a7f0522c23..91d3e25349 100644 --- a/packages/studio/src/components/editor/useColorGradingController.ts +++ b/packages/studio/src/components/editor/useColorGradingController.ts @@ -378,7 +378,10 @@ export function useColorGradingController({ const refreshAndReplay = () => { const nextGrading = latestGradingRef.current; const active = isHfColorGradingActive(nextGrading); - if (active) postColorGrading(nextGrading); + const notInTheFileYet = + serializeHfColorGrading(nextGrading) !== + serializeHfColorGrading(confirmedGradingRef.current); + if (notInTheFileYet) postColorGrading(nextGrading); postCompare(compareEnabledRef.current && active); scheduleRuntimeStatusRefresh(); };