mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(studio): adding a track no longer remounts every clip on the timeline (#8068)
* test(studio): adding a track keeps every existing timeline clip mounted * fix(studio): adding a track no longer remounts every clip on the timeline
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import React, { act } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { afterAll, beforeAll, expect, it } from "vitest";
|
||||
import type { TimelineElement } from "../store/playerStore";
|
||||
import { normalizeToZones } from "./timelineZones";
|
||||
|
||||
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
||||
|
||||
class MockResizeObserver {
|
||||
constructor(private readonly callback: ResizeObserverCallback) {}
|
||||
observe(target: Element) {
|
||||
this.callback(
|
||||
[{ target, borderBoxSize: [{ inlineSize: 900, blockSize: 400 }] } as never],
|
||||
this as unknown as ResizeObserver,
|
||||
);
|
||||
}
|
||||
unobserve() {}
|
||||
disconnect() {}
|
||||
}
|
||||
|
||||
const originalResizeObserver = globalThis.ResizeObserver;
|
||||
const sizes = ["clientWidth", "clientHeight"] as const;
|
||||
const originals = sizes.map((name) => Object.getOwnPropertyDescriptor(HTMLElement.prototype, name));
|
||||
|
||||
beforeAll(() => {
|
||||
globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
|
||||
Object.defineProperty(HTMLElement.prototype, "clientWidth", {
|
||||
configurable: true,
|
||||
get: () => 900,
|
||||
});
|
||||
Object.defineProperty(HTMLElement.prototype, "clientHeight", {
|
||||
configurable: true,
|
||||
get: () => 400,
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
globalThis.ResizeObserver = originalResizeObserver;
|
||||
sizes.forEach((name, i) => {
|
||||
const original = originals[i];
|
||||
if (original) Object.defineProperty(HTMLElement.prototype, name, original);
|
||||
});
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
const clip = (id: string, track: number, start = 0): TimelineElement => ({
|
||||
id,
|
||||
label: id,
|
||||
tag: "div",
|
||||
start,
|
||||
duration: 5,
|
||||
track,
|
||||
});
|
||||
|
||||
async function settleUntil(ready: () => boolean) {
|
||||
for (let i = 0; i < 60 && !ready(); i++)
|
||||
await act(async () => new Promise<void>((done) => requestAnimationFrame(() => done())));
|
||||
}
|
||||
|
||||
/** Mounts the timeline on `first`, then shows `next`; returns which of `kept` got a new DOM node. */
|
||||
async function remountedAfter(first: TimelineElement[], next: TimelineElement[], kept: string[]) {
|
||||
const [{ Timeline }, { usePlayerStore }] = await Promise.all([
|
||||
import("./Timeline"),
|
||||
import("../store/playerStore"),
|
||||
]);
|
||||
usePlayerStore.setState({ duration: 60, timelineReady: true, elements: normalizeToZones(first) });
|
||||
const host = document.body.appendChild(document.createElement("div"));
|
||||
const root = createRoot(host);
|
||||
const node = (id: string) => host.querySelector<HTMLElement>(`[data-el-id="${id}"]`);
|
||||
try {
|
||||
await act(async () => root.render(React.createElement(Timeline)));
|
||||
await settleUntil(() => kept.every((id) => node(id)));
|
||||
const before = new Map(kept.map((id) => [id, node(id)]));
|
||||
expect([...before.values()].every(Boolean)).toBe(true);
|
||||
await act(async () => usePlayerStore.setState({ elements: normalizeToZones(next) }));
|
||||
await settleUntil(() => next.every(({ id }) => node(id)));
|
||||
return kept.filter((id) => node(id) !== before.get(id));
|
||||
} finally {
|
||||
act(() => root.unmount());
|
||||
usePlayerStore.getState().reset();
|
||||
}
|
||||
}
|
||||
|
||||
// An agent edit that adds a track re-zones every lane below it (normalizeToZones), shifting each clip's display
|
||||
// track by one. On a long film that remounted the whole timeline in one frame. The 30 s budget matches
|
||||
// Timeline.virtualization.test.
|
||||
it(
|
||||
"adding a track above the others keeps every existing clip mounted",
|
||||
{ timeout: 30_000 },
|
||||
async () => {
|
||||
const ids = ["a", "b", "c", "d", "e"];
|
||||
const authored = ids.map((id, i) => clip(id, i + 1));
|
||||
expect(await remountedAfter(authored, [clip("new", 0), ...authored], ids)).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it(
|
||||
"deleting a track's first clip keeps the rest of the timeline mounted",
|
||||
{ timeout: 30_000 },
|
||||
async () => {
|
||||
const first = [clip("x1", 0, 0), clip("x2", 0, 10), clip("y", 1), clip("z", 2)];
|
||||
expect(await remountedAfter(first, first.slice(1), ["x2", "y", "z"])).toEqual([]);
|
||||
},
|
||||
);
|
||||
@@ -31,6 +31,7 @@ import { TimelineTrackRow } from "./TimelineTrackRow";
|
||||
import { isTimelineClipActive } from "./useTimelineActiveClips";
|
||||
import { queryTimelineClipIndex } from "../lib/timelineClipIndex";
|
||||
import { getTimelineElementIdentity } from "../lib/timelineElementHelpers";
|
||||
import { timelineRows } from "./timelineRowKeys";
|
||||
import { timelineClipFocusId } from "./timelineNavigationIdentity";
|
||||
import { useTimelineKeyboardActor } from "./useTimelineKeyboardActor";
|
||||
import { TimelineTransitionOverlays } from "./TimelineTransitionOverlays";
|
||||
@@ -145,6 +146,7 @@ export function TimelineLanes({
|
||||
onDrillDown,
|
||||
});
|
||||
const clipActions = useRef<TimelineLaneClipActions>(null!);
|
||||
const rows = timelineRows(tracks);
|
||||
clipActions.current = {
|
||||
gestures: {
|
||||
pps,
|
||||
@@ -218,7 +220,7 @@ export function TimelineLanes({
|
||||
const logicalRow = trackLogicalRows[0];
|
||||
if (!logicalRow) return null;
|
||||
const rowHeight = rowGeometry.getRowHeight(row);
|
||||
const els = tracks.find(([t]) => t === trackNum)?.[1] ?? [];
|
||||
const els = rows.clips(trackNum);
|
||||
const renderElements = rowsVirtualized
|
||||
? queryTimelineClipIndex(
|
||||
clipIndex,
|
||||
@@ -302,7 +304,7 @@ export function TimelineLanes({
|
||||
: undefined;
|
||||
return (
|
||||
<TimelineTrackRow
|
||||
key={rowKey}
|
||||
key={rows.reactKey(trackNum)}
|
||||
index={row}
|
||||
rowKey={rowKey}
|
||||
logicalRow={logicalRow}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { expect, it } from "vitest";
|
||||
import type { TimelineElement } from "../store/playerStore";
|
||||
import { timelineRows } from "./timelineRowKeys";
|
||||
|
||||
const el = (id: string, track: number, authoredTrack?: number, tag = "div"): TimelineElement => ({
|
||||
id,
|
||||
tag,
|
||||
start: 0,
|
||||
duration: 1,
|
||||
track,
|
||||
...(authoredTrack === undefined ? {} : { authoredTrack }),
|
||||
});
|
||||
|
||||
it("keys rows by zone and authored track, numbering repeats and empty lanes apart, and finds each row's clips", () => {
|
||||
const rows = timelineRows([
|
||||
[0, [el("a", 0, 3)]],
|
||||
[1, [el("b", 1, 3)]],
|
||||
[2, [el("c", 2, 3, "audio")]],
|
||||
[3, []],
|
||||
]);
|
||||
expect([0, 1, 2, 3, 9].map(rows.reactKey)).toEqual([
|
||||
"visual:3",
|
||||
"visual:3#1",
|
||||
"audio:3",
|
||||
"lane:3",
|
||||
"lane:9",
|
||||
]);
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { TimelineElement } from "../store/playerStore";
|
||||
import { resolveElementTrack } from "../../utils/studioHelpers";
|
||||
import { classifyZone } from "./timelineZones";
|
||||
|
||||
type Tracks = readonly (readonly [number, readonly TimelineElement[]])[];
|
||||
const NO_CLIPS: readonly TimelineElement[] = [];
|
||||
|
||||
/** Each row's clips, and its React key: the clips' zone and authored track, which adding a track does not renumber
|
||||
* as it does display lanes. Rows sharing one (overlap sub-lanes, expanded children) take a #n suffix in row order. */
|
||||
export function timelineRows(tracks: Tracks) {
|
||||
const seen = new Map<string, number>();
|
||||
const keys = new Map<number, string>();
|
||||
const clips = new Map<number, readonly TimelineElement[]>();
|
||||
for (const [track, els] of tracks) {
|
||||
const base = els[0]
|
||||
? `${classifyZone(els[0])}:${resolveElementTrack(els[0])}`
|
||||
: `lane:${track}`;
|
||||
const repeat = seen.get(base) ?? 0;
|
||||
seen.set(base, repeat + 1);
|
||||
keys.set(track, repeat ? `${base}#${repeat}` : base);
|
||||
clips.set(track, els);
|
||||
}
|
||||
return {
|
||||
reactKey: (track: number) => keys.get(track) ?? `lane:${track}`,
|
||||
clips: (track: number) => clips.get(track) ?? NO_CLIPS,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user