Skip to content

Commit d94708e

Browse files
fix(core): keep nested scenes in place during a drag when the root has no timeline (heygen-com#5115)
* fix(core): keep nested scenes in place during a drag when the root has no timeline * fix(core): reuse the missing-root composite so a late root timeline still binds * fix(core): count a nested scene in its composite length so a rebind keeps it at the playhead * fix(core): rebuild a held composite whose length went stale so the player length stays right * test(core): reuse the no-root-timeline loader for the stale-length case
1 parent b50dead commit d94708e

2 files changed

Lines changed: 155 additions & 9 deletions

File tree

‎packages/core/src/runtime/init.subCompositionPlacement.test.ts‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import gsap from "gsap";
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import { initSandboxRuntimeModular } from "./init";
44
import type { RuntimeTimelineLike } from "./types";
5+
import { STUDIO_MANUAL_EDIT_GESTURE_ATTR } from "../editing/draftMarkers";
56
import { resetRuntimeFixtureDom } from "./runtimeSeekFixture.test-helpers";
67

78
// Pins where a running runtime places a sub-composition's timeline, against what a fresh load does.
@@ -14,6 +15,7 @@ describe("runtime sub-composition placement", () => {
1415
afterEach(() => {
1516
window.__hfRuntimeTeardown?.();
1617
vi.restoreAllMocks();
18+
vi.unstubAllGlobals();
1719
delete window.__player;
1820
delete window.__playerReady;
1921
delete window.__hf;
@@ -98,6 +100,123 @@ describe("runtime sub-composition placement", () => {
98100
expect(intro.startTime()).toBe(2);
99101
});
100102

103+
/** Transport frames on demand; the clock moves a second per frame, past the change-service rate limit. */
104+
function stubFrames() {
105+
const frames = new Map<number, FrameRequestCallback>();
106+
let next = 0;
107+
vi.stubGlobal("requestAnimationFrame", (cb: FrameRequestCallback) => {
108+
frames.set(++next, cb);
109+
return next;
110+
});
111+
vi.stubGlobal("cancelAnimationFrame", (id: number) => frames.delete(id));
112+
let now = Date.now();
113+
vi.spyOn(Date, "now").mockImplementation(() => now);
114+
return async (count: number) => {
115+
for (let frame = 1; frame <= count; frame++) {
116+
now += 1000;
117+
await Promise.resolve();
118+
const due = [...frames.values()];
119+
frames.clear();
120+
for (const run of due) run(frame * 16);
121+
}
122+
};
123+
}
124+
125+
/** A root with no timeline of its own around one scene clip, whose timeline slides `#s` from 0 to 100 over 4 s. */
126+
function loadWithoutRootTimeline(rootMarkup = "", rootLength = ` data-duration="10"`) {
127+
document.body.innerHTML =
128+
`<div data-composition-id="main" data-root="true"${rootLength}>${rootMarkup}` +
129+
`<div id="host" class="clip" data-composition-id="scene" data-start="1" data-duration="4">` +
130+
`<div id="s"></div></div></div>`;
131+
const scene = gsap
132+
.timeline({ paused: true })
133+
.to("#s", { x: 100, duration: 4, ease: "none" }, 0);
134+
window.__timelines = { scene } as unknown as Record<string, RuntimeTimelineLike>;
135+
initSandboxRuntimeModular();
136+
return scene;
137+
}
138+
139+
it("keeps a nested scene where the playhead left it during a drag when the root has no timeline", async () => {
140+
const runFrames = stubFrames();
141+
loadWithoutRootTimeline();
142+
window.__player?.seek(4);
143+
expect(shownX()).toBe(75);
144+
145+
// Every transform GSAP writes while the drag is held, including ones a later write in the same tick hides.
146+
const shown: string[] = [];
147+
const writes = new MutationObserver((records) =>
148+
shown.push(...records.map((r) => r.oldValue ?? "")),
149+
);
150+
writes.observe(document.getElementById("s")!, {
151+
attributes: true,
152+
attributeFilter: ["style"],
153+
attributeOldValue: true,
154+
});
155+
// A Studio press marks the element; a timing edit then wakes the transport's rebind mid-drag.
156+
document.getElementById("s")!.setAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR, "gesture-1:move");
157+
document.getElementById("host")!.setAttribute("data-duration", "4");
158+
await runFrames(30);
159+
160+
shown.push(
161+
...writes.takeRecords().map((r) => r.oldValue ?? ""),
162+
document.getElementById("s")!.getAttribute("style") ?? "",
163+
);
164+
writes.disconnect();
165+
const xs = shown.map((style) => /translate(?:3d)?\(([-\d.]+)/.exec(style)?.[1]).filter(Boolean);
166+
expect(xs.length).toBeGreaterThan(0);
167+
expect(new Set(xs)).toEqual(new Set(["75"]));
168+
});
169+
170+
it("keeps a dragged scene where the playhead left it when another scene registers mid-drag", async () => {
171+
const runFrames = stubFrames();
172+
loadWithoutRootTimeline(
173+
`<div id="later" class="clip" data-composition-id="later" data-start="0" data-duration="2"><div id="l"></div></div>`,
174+
);
175+
window.__player?.seek(4);
176+
document.getElementById("s")!.setAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR, "gesture-1:move");
177+
(window.__timelines as Record<string, unknown>).later = gsap
178+
.timeline({ paused: true })
179+
.to("#l", { x: 100, duration: 2, ease: "none" }, 0);
180+
await runFrames(30);
181+
182+
expect(shownX()).toBe(75);
183+
});
184+
185+
it("reports a scene's new length after a script extends it, when the root declares none", async () => {
186+
const runFrames = stubFrames();
187+
const scene = loadWithoutRootTimeline("", "");
188+
await runFrames(3);
189+
window.__player?.seek(2);
190+
scene.to({}, { duration: 8 }, 0);
191+
await runFrames(5);
192+
193+
expect(window.__player?.getDuration()).toBe(9);
194+
});
195+
196+
it("binds a root timeline that registers after the runtime started on its children", async () => {
197+
const runFrames = stubFrames();
198+
loadWithoutRootTimeline(`<div id="r"></div>`);
199+
const root = gsap
200+
.timeline({ paused: true })
201+
.to("#r", { x: 100, duration: 10, ease: "none" }, 0);
202+
(window.__timelines as Record<string, unknown>).main = root;
203+
await runFrames(30);
204+
205+
window.__player?.seek(5);
206+
expect(Number(gsap.getProperty("#r", "x"))).toBe(50);
207+
expect(shownX()).toBe(100);
208+
});
209+
210+
it("places a scene at its host's new start on a rebind when the root has no timeline", () => {
211+
const scene = loadWithoutRootTimeline();
212+
document.getElementById("host")!.setAttribute("data-start", "4");
213+
window.__hfForceTimelineRebind?.();
214+
215+
expect(scene.startTime()).toBe(4);
216+
window.__player?.seek(5);
217+
expect(shownX()).toBe(25);
218+
});
219+
101220
it("leaves an unmoved scene in place on a rebind, whatever its start rounds to", () => {
102221
const { root, scene } = load("0.33333333");
103222
const remove = vi.spyOn(root, "remove");

‎packages/core/src/runtime/init.ts‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,8 @@ export function initSandboxRuntimeModular(): void {
13751375

13761376
// Sub-composition timelines the runtime nested into the root: the host composition id, and where.
13771377
const autoNested = new WeakMap<object, { hostId: string; parent: unknown; at: number }>();
1378+
// The composite that holds the children of a root with no timeline, and how many it holds.
1379+
let missingRootComposite: { timeline: RuntimeTimelineLike; size: number } | null = null;
13781380

13791381
const resolveRootTimelineFromDocument = (): TimelineResolution => {
13801382
const timelines = (window.__timelines ?? {}) as Record<string, RuntimeTimelineLike | undefined>;
@@ -1430,16 +1432,25 @@ export function initSandboxRuntimeModular(): void {
14301432
parent.add(candidate.timeline, at);
14311433
autoNested.set(candidate.timeline, { hostId: candidate.compositionId, parent, at });
14321434
};
1433-
const followHostStart = (
1435+
/** Placed by the runtime in `root` and still there; under another root a re-run script owns its place. */
1436+
const placedIn = (
14341437
root: RuntimeTimelineLike,
14351438
candidate: { compositionId: string; timeline: RuntimeTimelineLike },
1436-
): void => {
1439+
) => {
14371440
const nested = candidate.timeline as RuntimeTimelineLike & RuntimeTimelineChildLike;
14381441
const placed = autoNested.get(nested);
1439-
// Under another root, a re-run script placed it, and owns that place as on a fresh load.
1440-
if (!placed || placed.parent !== root || nested.parent !== (root as unknown)) return;
1441-
if (Math.abs(placed.at - resolveCompositionStartSeconds(candidate.compositionId)) < 1e-6)
1442-
return;
1442+
return placed && placed.parent === root && nested.parent === (root as unknown)
1443+
? placed
1444+
: null;
1445+
};
1446+
const atHostStart = (placed: { at: number }, candidate: { compositionId: string }) =>
1447+
Math.abs(placed.at - resolveCompositionStartSeconds(candidate.compositionId)) < 1e-6;
1448+
const followHostStart = (
1449+
root: RuntimeTimelineLike,
1450+
candidate: { compositionId: string; timeline: RuntimeTimelineLike },
1451+
): void => {
1452+
const placed = placedIn(root, candidate);
1453+
if (!placed || atHostStart(placed, candidate)) return;
14431454
// GSAP takes a child out of its parent before adding it; the rebind's unpause aligns it to the root.
14441455
nestAtHostStart(root, candidate);
14451456
};
@@ -1454,6 +1465,8 @@ export function initSandboxRuntimeModular(): void {
14541465
if (!gsapApi || typeof gsapApi.timeline !== "function") return null;
14551466
const compositeTimeline = gsapApi.timeline({ paused: true }) as RuntimeTimelineLike;
14561467
for (const candidate of candidates) nestAtHostStart(compositeTimeline, candidate);
1468+
// Before any duration read: GSAP leaves paused children out of the duration it caches.
1469+
ensureChildCandidatesActive(nestedCandidates(compositeTimeline, candidates));
14571470
return compositeTimeline;
14581471
};
14591472
const createDurationFloorTimeline = (
@@ -1632,7 +1645,6 @@ export function initSandboxRuntimeModular(): void {
16321645
const compositeTimeline = createCompositeTimelineFromCandidates(rootChildCandidates);
16331646
const compositeDurationSeconds = getTimelineDurationSeconds(compositeTimeline);
16341647
if (compositeTimeline && isUsableTimelineDuration(compositeDurationSeconds)) {
1635-
ensureChildCandidatesActive(nestedCandidates(compositeTimeline, rootChildCandidates));
16361648
return {
16371649
timeline: compositeTimeline,
16381650
selectedTimelineIds,
@@ -1786,10 +1798,25 @@ export function initSandboxRuntimeModular(): void {
17861798
}
17871799
if (rootChildCandidates.length > 0) {
17881800
const selectedTimelineIds = rootChildCandidates.map((candidate) => candidate.compositionId);
1789-
const compositeTimeline = createCompositeTimelineFromCandidates(rootChildCandidates);
1801+
// A rebuild re-parents every child at time 0 and paints it there, under a paused drag too,
1802+
// so the same children, each still at its host's start, keep the composite they sit in.
1803+
const held = missingRootComposite;
1804+
const reused =
1805+
held?.size === rootChildCandidates.length &&
1806+
isUsableTimelineDuration(getTimelineDurationSeconds(held.timeline)) &&
1807+
rootChildCandidates.every((candidate) => {
1808+
const placed = placedIn(held.timeline, candidate);
1809+
return placed !== null && atHostStart(placed, candidate);
1810+
})
1811+
? held.timeline
1812+
: null;
1813+
const compositeTimeline =
1814+
reused ?? createCompositeTimelineFromCandidates(rootChildCandidates);
17901815
const compositeDurationSeconds = getTimelineDurationSeconds(compositeTimeline);
17911816
if (compositeTimeline) {
1792-
ensureChildCandidatesActive(nestedCandidates(compositeTimeline, rootChildCandidates));
1817+
if (!reused) {
1818+
missingRootComposite = { timeline: compositeTimeline, size: rootChildCandidates.length };
1819+
}
17931820
return {
17941821
timeline: compositeTimeline,
17951822
selectedTimelineIds,

0 commit comments

Comments
 (0)