Skip to content

Commit a340ed3

Browse files
fix(studio): keep subcomposition timelines open during playback (#3382)
* fix(studio): keep subcomposition timelines open during playback * fix(studio): address timeline playback review feedback
1 parent 7a8f8a0 commit a340ed3

2 files changed

Lines changed: 72 additions & 28 deletions

File tree

‎packages/studio/src/player/hooks/useExpandedTimelineElements.test.ts‎

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { describe, expect, it } from "vitest";
1+
// @vitest-environment happy-dom
2+
3+
import React, { act, useEffect } from "react";
4+
import { createRoot } from "react-dom/client";
5+
import { afterEach, describe, expect, it } from "vitest";
26
import {
37
buildExpandedElements,
48
resolveTimelineExpansionRawId,
9+
useExpandedTimelineElements,
510
} from "./useExpandedTimelineElements";
611
import { buildTimelineElementKey } from "../lib/timelineElementHelpers";
7-
import type { TimelineElement } from "../store/playerStore";
12+
import { usePlayerStore, type TimelineElement } from "../store/playerStore";
813
import type { ClipManifestClip } from "../lib/playbackTypes";
914

15+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
16+
1017
const clip = (over: Partial<ClipManifestClip>): ClipManifestClip => ({
1118
id: "x",
1219
label: "x",
@@ -31,6 +38,19 @@ const el = (over: Partial<TimelineElement>): TimelineElement => ({
3138
...over,
3239
});
3340

41+
function TimelineExpansionHarness({ onValue }: { onValue: (value: TimelineElement[]) => void }) {
42+
const value = useExpandedTimelineElements();
43+
useEffect(() => {
44+
onValue(value);
45+
}, [onValue, value]);
46+
return null;
47+
}
48+
49+
afterEach(() => {
50+
document.body.innerHTML = "";
51+
usePlayerStore.getState().reset();
52+
});
53+
3454
describe("buildExpandedElements", () => {
3555
it("rebases a 1-level child onto its sub-comp host (start + sourceFile)", () => {
3656
// host s3 at absolute 16 → stats-panel.html; children live in that file.
@@ -357,7 +377,7 @@ describe("buildExpandedElements", () => {
357377
it("keeps the host row present at every playhead position (keyframe lane repro)", () => {
358378
// Live repro with no drag at all: seek 0 gave 3 diamonds, seek 7.68 gave 0,
359379
// seek 0.2 gave 3. Diamonds render per row from keyframeCache.get(elementKey),
360-
// so the whole lane went with the host row whenever the paused drill-in
380+
// so the whole lane went with the host row whenever the store-time drill-in
361381
// substituted it for its children.
362382
const elements = [
363383
el({ id: "scene", domId: "scene", key: "index.html#scene", start: 0, duration: 12 }),
@@ -371,7 +391,6 @@ describe("buildExpandedElements", () => {
371391
for (const currentTime of [0, 7.68, 0.2]) {
372392
const rawId = resolveTimelineExpansionRawId({
373393
selectedElementId: null,
374-
isPlaying: false,
375394
currentTime,
376395
manifest,
377396
parentMap,
@@ -424,21 +443,20 @@ describe("buildExpandedElements", () => {
424443
});
425444

426445
describe("resolveTimelineExpansionRawId", () => {
427-
it("returns null when paused inside a childless top-level clip", () => {
446+
it("returns null inside a childless top-level clip", () => {
428447
const manifest = [clip({ id: "title", start: 0, duration: 4 })];
429448

430449
expect(
431450
resolveTimelineExpansionRawId({
432451
selectedElementId: null,
433-
isPlaying: false,
434452
currentTime: 2,
435453
manifest,
436454
parentMap: new Map(),
437455
}),
438456
).toBeNull();
439457
});
440458

441-
it("auto-expands an active composition with children when paused and nothing is selected", () => {
459+
it("auto-expands an active composition with children when nothing is selected", () => {
442460
const manifest = [
443461
clip({ id: "scene", start: 1, duration: 5 }),
444462
clip({ id: "headline", start: 1.5, duration: 2 }),
@@ -448,7 +466,6 @@ describe("resolveTimelineExpansionRawId", () => {
448466
expect(
449467
resolveTimelineExpansionRawId({
450468
selectedElementId: null,
451-
isPlaying: false,
452469
currentTime: 2,
453470
manifest,
454471
parentMap,
@@ -468,7 +485,6 @@ describe("resolveTimelineExpansionRawId", () => {
468485
expect(
469486
resolveTimelineExpansionRawId({
470487
selectedElementId: null,
471-
isPlaying: false,
472488
currentTime: 12,
473489
manifest,
474490
parentMap,
@@ -491,15 +507,14 @@ describe("resolveTimelineExpansionRawId", () => {
491507
expect(
492508
resolveTimelineExpansionRawId({
493509
selectedElementId: null,
494-
isPlaying: false,
495510
currentTime: 5,
496511
manifest,
497512
parentMap,
498513
}),
499514
).toBe("second");
500515
});
501516

502-
it("auto-expands the innermost active nested composition when paused", () => {
517+
it("auto-expands the innermost active nested composition", () => {
503518
const manifest = [
504519
clip({ id: "outer", start: 0, duration: 10 }),
505520
clip({ id: "inner", start: 2, duration: 5 }),
@@ -513,15 +528,14 @@ describe("resolveTimelineExpansionRawId", () => {
513528
expect(
514529
resolveTimelineExpansionRawId({
515530
selectedElementId: null,
516-
isPlaying: false,
517531
currentTime: 3.5,
518532
manifest,
519533
parentMap,
520534
}),
521535
).toBe("inner");
522536
});
523537

524-
it("does not auto-expand an active composition while playing", () => {
538+
it("resolves an active composition from the current store time", () => {
525539
const manifest = [
526540
clip({ id: "scene", start: 0, duration: 5 }),
527541
clip({ id: "headline", start: 1, duration: 2 }),
@@ -531,15 +545,49 @@ describe("resolveTimelineExpansionRawId", () => {
531545
expect(
532546
resolveTimelineExpansionRawId({
533547
selectedElementId: null,
534-
isPlaying: true,
535548
currentTime: 2,
536549
manifest,
537550
parentMap,
538551
}),
539-
).toBeNull();
552+
).toBe("scene");
553+
});
554+
555+
it("keeps inline children visible when the master timeline is playing", () => {
556+
const elements = [
557+
el({ id: "scene", domId: "scene", key: "index.html#scene", start: 0, duration: 5 }),
558+
];
559+
const manifest = [
560+
clip({ id: "scene", start: 0, duration: 5, compositionSrc: "scene.html" }),
561+
clip({ id: "headline", start: 1, duration: 2 }),
562+
];
563+
const parentMap = new Map([["headline", "scene"]]);
564+
let rows: TimelineElement[] | undefined;
565+
const host = document.createElement("div");
566+
document.body.append(host);
567+
const root = createRoot(host);
568+
569+
act(() => {
570+
usePlayerStore.setState({
571+
elements,
572+
clipManifest: manifest,
573+
clipParentMap: parentMap,
574+
currentTime: 2,
575+
isPlaying: true,
576+
});
577+
root.render(
578+
React.createElement(TimelineExpansionHarness, {
579+
onValue: (value) => (rows = value),
580+
}),
581+
);
582+
});
583+
584+
expect(usePlayerStore.getState().isPlaying).toBe(true);
585+
expect(rows?.map((row) => row.domId ?? row.id)).toEqual(["scene", "headline"]);
586+
587+
act(() => root.unmount());
540588
});
541589

542-
it("keeps selected elements ahead of paused active composition auto-expansion", () => {
590+
it("keeps selected elements ahead of active composition auto-expansion", () => {
543591
const manifest = [
544592
clip({ id: "scene", start: 0, duration: 6 }),
545593
clip({ id: "headline", start: 1, duration: 2 }),
@@ -553,7 +601,6 @@ describe("resolveTimelineExpansionRawId", () => {
553601
expect(
554602
resolveTimelineExpansionRawId({
555603
selectedElementId: "caption",
556-
isPlaying: false,
557604
currentTime: 1.5,
558605
manifest,
559606
parentMap,

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ function resolveRawId(
3535

3636
interface TimelineExpansionRawIdInput {
3737
selectedElementId: string | null;
38-
isPlaying: boolean;
3938
currentTime: number;
4039
manifest: ClipManifestClip[];
4140
parentMap: Map<string, string>;
@@ -97,14 +96,12 @@ function findActiveExpandableCompositionId(
9796

9897
export function resolveTimelineExpansionRawId({
9998
selectedElementId,
100-
isPlaying,
10199
currentTime,
102100
manifest,
103101
parentMap,
104102
}: TimelineExpansionRawIdInput): string | null {
105103
const selectedRawId = resolveRawId(selectedElementId, manifest, parentMap);
106104
if (selectedRawId) return selectedRawId;
107-
if (isPlaying) return null;
108105
return findActiveExpandableCompositionId(currentTime, manifest, parentMap);
109106
}
110107

@@ -334,7 +331,7 @@ export function buildExpandedElements(
334331
: (el.key ?? el.id) === parentKey;
335332

336333
// ADDITIVE drill-in: the host row stays and its children are appended under
337-
// it. Expansion is also triggered by the playhead alone (paused auto-expand),
334+
// it. Expansion is also triggered by the playhead alone (active auto-expand),
338335
// so substituting the host row made it vanish on an ordinary seek, and with
339336
// it the host's keyframe lane, since diamonds render per row from
340337
// `keyframeCache.get(elementKey)`. The synthetic fractional lanes above sit
@@ -356,13 +353,14 @@ export function useExpandedTimelineElements(): TimelineElement[] {
356353
const clipParentMap = usePlayerStore((s) => s.clipParentMap);
357354
const domClipChildren = usePlayerStore((s) => s.domClipChildren);
358355
const selectedElementId = usePlayerStore((s) => s.selectedElementId);
359-
const isPlaying = usePlayerStore((s) => s.isPlaying);
360356
const currentTime = usePlayerStore((s) => s.currentTime);
361357

362-
// Resolve which raw clip drives expansion. This reads currentTime (for paused
363-
// auto-expand) so it re-runs each scrub tick, but it's a cheap manifest scan and
364-
// its RESULT only changes when the playhead crosses a composition boundary. Keying
365-
// the expensive build below on these ids (not raw currentTime) avoids re-allocating
358+
// Resolve which raw clip drives expansion from the store's committed playhead
359+
// time. The RAF loop keeps live time out of React and Zustand during playback,
360+
// so this target deliberately stays stable until the loop commits or a seek /
361+
// pause updates the store. The scan re-runs on scrub ticks, but its RESULT only
362+
// changes when the playhead crosses a composition boundary. Keying the expensive
363+
// build below on these ids (not raw currentTime) avoids re-allocating
366364
// expandedElements — and cascading TimelineClip re-renders — on every tick.
367365
const { rawId, selectedRawId } = useMemo(() => {
368366
if (!clipManifest || clipManifest.length === 0 || clipParentMap.size === 0) {
@@ -371,14 +369,13 @@ export function useExpandedTimelineElements(): TimelineElement[] {
371369
return {
372370
rawId: resolveTimelineExpansionRawId({
373371
selectedElementId,
374-
isPlaying,
375372
currentTime,
376373
manifest: clipManifest,
377374
parentMap: clipParentMap,
378375
}),
379376
selectedRawId: resolveRawId(selectedElementId, clipManifest, clipParentMap),
380377
};
381-
}, [clipManifest, clipParentMap, selectedElementId, isPlaying, currentTime]);
378+
}, [clipManifest, clipParentMap, selectedElementId, currentTime]);
382379

383380
return useMemo(() => {
384381
if (!clipManifest || clipManifest.length === 0 || clipParentMap.size === 0) {

0 commit comments

Comments
 (0)