Skip to content

Commit 477916c

Browse files
committed
fix(studio): label undo history with the track display row
The timeline track key is a fractional z-order sort key: an expanded sub-composition child gets `host.track + n / (siblings + 2)`. The undo history entry for the eye toggle interpolated that key directly, so hiding an expanded child recorded "Hide track 0.16666666666666666". Give the key-to-display-row conversion a single owner (timelineTrackDisplay.ts) and route both the track header labels and the history label through it, so the two cannot drift apart again. The raw key still routes the callbacks and lookups that need it. Adds a regression test that toggles a track keyed 1 / 6; a test on track 0 formats cleanly and proves nothing.
1 parent 1faa0cb commit 477916c

4 files changed

Lines changed: 96 additions & 4 deletions

File tree

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ describe("toggleTimelineTrackHidden", () => {
9595
expect(pendingRef.current).toEqual(new Set(["index.html", "scene.html"]));
9696
expect(timestampRef.current).toBeGreaterThan(0);
9797
expect(recordEdit).toHaveBeenCalledTimes(1);
98-
expect(recordEdit.mock.calls[0]?.[0]?.label).toBe("Hide track 0");
98+
// Display row, not the raw key: track 0 is the first row, so it reads "1",
99+
// the same number the track header announces for that row.
100+
expect(recordEdit.mock.calls[0]?.[0]?.label).toBe("Hide track 1");
99101
expect(Object.keys(recordEdit.mock.calls[0]?.[0]?.files ?? {}).sort()).toEqual([
100102
"index.html",
101103
"scene.html",
@@ -134,6 +136,64 @@ describe("toggleTimelineTrackHidden", () => {
134136

135137
expect(writes.get("index.html")).not.toContain("data-hidden");
136138
});
139+
140+
// An expanded sub-comp child gets a synthesized FRACTIONAL track key
141+
// (`display.track + n / (siblings + 2)`), so a label built from the raw key
142+
// reads "Hide track 0.16666666666666666" in the undo history. Track 0 would
143+
// format cleanly and prove nothing, hence 1 / 6.
144+
it("labels the undo entry with the display row, not the fractional track key", async () => {
145+
const files = new Map([
146+
["index.html", `<div id="child" data-start="0" data-duration="2"></div>`],
147+
]);
148+
stubProjectFiles(files);
149+
150+
const recordEdit = vi.fn();
151+
152+
await toggleTimelineTrackHidden({
153+
projectId: "project-1",
154+
activeCompPath: "index.html",
155+
timelineElements: [
156+
element({ id: "host", domId: "host", track: 0 }),
157+
element({ id: "child", domId: "child", track: 1 / 6 }),
158+
],
159+
track: 1 / 6,
160+
hidden: true,
161+
previewIframe: null,
162+
writeProjectFile: async () => {},
163+
recordEdit,
164+
domEditSaveTimestampRef: { current: 0 },
165+
pendingTimelineEditPathRef: { current: new Set() },
166+
});
167+
168+
expect(recordEdit.mock.calls[0]?.[0]?.label).toBe("Hide track 2");
169+
});
170+
171+
it("labels a show back with the display row too", async () => {
172+
const files = new Map([
173+
["index.html", `<div id="child" data-start="0" data-duration="2" data-hidden=""></div>`],
174+
]);
175+
stubProjectFiles(files);
176+
177+
const recordEdit = vi.fn();
178+
179+
await toggleTimelineTrackHidden({
180+
projectId: "project-1",
181+
activeCompPath: "index.html",
182+
timelineElements: [
183+
element({ id: "host", domId: "host", track: 0 }),
184+
element({ id: "child", domId: "child", track: 1 / 6, hidden: true }),
185+
],
186+
track: 1 / 6,
187+
hidden: false,
188+
previewIframe: null,
189+
writeProjectFile: async () => {},
190+
recordEdit,
191+
domEditSaveTimestampRef: { current: 0 },
192+
pendingTimelineEditPathRef: { current: new Set() },
193+
});
194+
195+
expect(recordEdit.mock.calls[0]?.[0]?.label).toBe("Show track 2");
196+
});
137197
});
138198

139199
describe("toggleTimelineElementHidden", () => {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useCallback } from "react";
22
import { usePlayerStore, type TimelineElement } from "../player";
33
import { useExpandedTimelineElements } from "../player/hooks/useExpandedTimelineElements";
4+
import { timelineTrackOrder, trackDisplayNumber } from "../player/components/timelineTrackDisplay";
45
import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
56
import { readTagSnippetByTarget, type PatchOperation } from "../utils/sourcePatcher";
67
import {
@@ -208,12 +209,15 @@ export async function toggleTimelineTrackHidden({
208209
domEditSaveTimestampRef,
209210
pendingTimelineEditPathRef,
210211
}: ToggleTimelineTrackHiddenInput): Promise<string[]> {
212+
// `track` is the fractional sort key the callback needs; the history entry is
213+
// read by a human, so it gets the display row instead.
214+
const displayNumber = trackDisplayNumber(timelineTrackOrder(timelineElements), track);
211215
return setElementsHidden({
212216
projectId,
213217
activeCompPath,
214218
elements: timelineElements.filter((element) => element.track === track),
215219
hidden,
216-
label: hidden ? `Hide track ${track}` : `Show track ${track}`,
220+
label: hidden ? `Hide track ${displayNumber}` : `Show track ${displayNumber}`,
217221
previewIframe,
218222
writeProjectFile,
219223
recordEdit,

‎packages/studio/src/player/components/TimelineLanes.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TimelineClipDiamonds } from "./TimelineClipDiamonds";
44
import { TimelinePropertyLanes } from "./TimelinePropertyLanes";
55
import { TimelineTrackHeader } from "./TimelineTrackHeader";
66
import { resolveTrackKeyframeClip } from "./useTimelineTrackLayout";
7+
import { trackDisplayNumber } from "./timelineTrackDisplay";
78
import { clipTimingStart } from "../../hooks/gsapShared";
89
import { getTimelineEditCapabilities, resolveBlockedTimelineEditIntent } from "./timelineEditing";
910
import { CLIP_Y, CLIP_HANDLE_W, TRACK_H, getTimelineRowHeight } from "./timelineLayout";
@@ -105,6 +106,7 @@ export function TimelineLanes({
105106
// in a virtualizer if editorial workflows ever push very high clip counts.
106107
// fallow-ignore-next-line complexity
107108
displayTrackOrder.map((trackNum, row) => {
109+
const displayNumber = trackDisplayNumber(displayTrackOrder, trackNum);
108110
const rowHeight = getTimelineRowHeight(row, rowHeights);
109111
const els = tracks.find(([t]) => t === trackNum)?.[1] ?? [];
110112
const ts = trackStyles.get(trackNum) ?? getTrackStyle("");
@@ -150,8 +152,10 @@ export function TimelineLanes({
150152
trackNumber={trackNum}
151153
// What gets announced. `trackNum` is a fractional z-order sort
152154
// key, so it stays out of every label and in every callback.
153-
trackDisplayNumber={row + 1}
154-
trackLabel={els[0]?.label ?? els[0]?.domId ?? els[0]?.id ?? `Track ${row + 1}`}
155+
trackDisplayNumber={displayNumber}
156+
trackLabel={
157+
els[0]?.label ?? els[0]?.domId ?? els[0]?.id ?? `Track ${displayNumber}`
158+
}
155159
lanesId={lanesId}
156160
contentOrigin={contentOrigin}
157161
keyframeClip={keyframeClip}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* The one owner of "what track number does the user see".
3+
*
4+
* `TimelineElement.track` is a z-order SORT key, not a row number: an expanded
5+
* sub-composition child gets a synthesized fractional key (`host.track + n /
6+
* (siblings + 2)`, see `useExpandedTimelineElements`), so putting it in a string
7+
* announces "Hide track 0.16666666666666666". Every user-visible track number,
8+
* whether it is rendered by a component or baked into an undo-history label,
9+
* routes through here; the raw key stays in callbacks and lookups only.
10+
*/
11+
12+
/** Ascending distinct track keys, the row order the timeline renders in. */
13+
export function timelineTrackOrder(elements: readonly { track: number }[]): number[] {
14+
return [...new Set(elements.map((element) => element.track))].sort((a, b) => a - b);
15+
}
16+
17+
/**
18+
* A track key's 1-based display row. A key not in `trackOrder` (a drag preview
19+
* onto a brand-new track, say) reads as the row it would land on at the end.
20+
*/
21+
export function trackDisplayNumber(trackOrder: readonly number[], track: number): number {
22+
const row = trackOrder.indexOf(track);
23+
return row < 0 ? trackOrder.length + 1 : row + 1;
24+
}

0 commit comments

Comments
 (0)