Skip to content

Commit 3dc3a9c

Browse files
fix(studio): clips inside nested compositions sit at their master time (#4681)
* fix(studio): clips inside nested compositions sit at their master time * fix(studio): nested clip starts read authored scene lengths as the runtime does * test(studio): drop a reference-start case the DOM fallback never reads * test(studio): a clip inside a referenced scene sits after its authored length * fix(studio): drag and trim a nested clip to where it is drawn * fix(studio): keep a root-time nested video's drag in master time * fix(studio): duplicate and keyframe edits on a nested clip use its own clock * fix(studio): nested clip drops stop at the host start and trims scale on its clock * fix(studio): a nested clip's head trim stops at its host's start * fix(studio): a dropped nested clip stops at its host's start before snapping
1 parent 5606b31 commit 3dc3a9c

31 files changed

Lines changed: 921 additions & 118 deletions

‎packages/core/src/mediaTiming.ts‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,31 @@ export function resolveAbsoluteMediaStartSeconds(input: {
2222
/** The one rule for a media element's root-timeline start; the runtime and the CLI both call it.
2323
* With no literal start, an auto-injected start, or a host at t<=0 there is nothing for the basis
2424
* to disambiguate, so the ordinary start resolution applies. */
25-
export function resolveMediaStartSeconds(input: {
26-
authoredStart: number | null;
27-
hostStart: number;
28-
hasAutoStart: boolean;
29-
basis?: string | null;
30-
ordinaryStart: () => number;
31-
}): number {
32-
if (input.hasAutoStart || input.authoredStart == null || input.hostStart <= 0) {
33-
return input.ordinaryStart();
34-
}
25+
export function resolveMediaStartSeconds(
26+
input: MediaStartInput & { ordinaryStart: () => number },
27+
): number {
28+
if (!hasLiteralNestedStart(input)) return input.ordinaryStart();
3529
return resolveAbsoluteMediaStartSeconds({
3630
authoredStart: input.authoredStart,
3731
hostStart: input.hostStart,
3832
basis: input.basis,
3933
});
4034
}
35+
36+
/** True when the authored start is already root time, so no host offset applies. */
37+
export function isRootGlobalMediaStart(input: MediaStartInput): boolean {
38+
return hasLiteralNestedStart(input) && readMediaStartBasis(input.basis) === "global";
39+
}
40+
41+
export interface MediaStartInput {
42+
authoredStart: number | null;
43+
hostStart: number;
44+
hasAutoStart: boolean;
45+
basis?: string | null;
46+
}
47+
48+
function hasLiteralNestedStart(
49+
input: MediaStartInput,
50+
): input is MediaStartInput & { authoredStart: number } {
51+
return !input.hasAutoStart && input.authoredStart != null && input.hostStart > 0;
52+
}

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ import {
1111
} from "./playbackRate";
1212
import { isMediaElement } from "./domRealm";
1313
import { parseStartExpression } from "./startExpression";
14-
import { MEDIA_START_BASIS_ATTR, resolveMediaStartSeconds } from "../mediaTiming";
14+
import {
15+
isRootGlobalMediaStart,
16+
MEDIA_START_BASIS_ATTR,
17+
resolveMediaStartSeconds,
18+
type MediaStartInput,
19+
} from "../mediaTiming";
1520

1621
export function createRuntimeStartTimeResolver(params: {
17-
timelineRegistry?: Record<string, RuntimeTimelineLike | undefined>;
22+
timelineRegistry?: Record<string, Pick<RuntimeTimelineLike, "duration"> | undefined>;
1823
includeAuthoredTimingAttrs?: boolean;
1924
/**
2025
* The document that reference lookups (`data-start="intro + 2"`) resolve
@@ -27,6 +32,8 @@ export function createRuntimeStartTimeResolver(params: {
2732
resolveStartForElement: (element: Element, fallback?: number) => number;
2833
resolveDurationForElement: (element: Element) => number | null;
2934
resolveMediaStartForElement: (element: Element) => number;
35+
resolveHostStartForElement: (element: Element) => number;
36+
isRootGlobalMediaStartForElement: (element: Element) => boolean;
3037
} {
3138
const timelineRegistry = params.timelineRegistry ?? {};
3239
const includeAuthoredTimingAttrs = params.includeAuthoredTimingAttrs ?? false;
@@ -185,23 +192,34 @@ export function createRuntimeStartTimeResolver(params: {
185192
* cache, WebAudio scheduling — must come through here, or the timeline the
186193
* editor draws stops matching the timeline that plays.
187194
*/
188-
const resolveMediaStartForElement = (element: Element): number => {
195+
const mediaStartInput = (element: Element): MediaStartInput => {
189196
const compositionRoot = element.closest("[data-composition-id]");
190-
const hostStart = compositionRoot ? resolveStartForElementInternal(compositionRoot, 0) : 0;
191-
return resolveMediaStartSeconds({
197+
return {
192198
authoredStart: parseStrictFiniteTimingNumber(element.getAttribute("data-start")),
193-
hostStart,
199+
hostStart: compositionRoot ? resolveStartForElementInternal(compositionRoot, 0) : 0,
194200
hasAutoStart: element.hasAttribute("data-hf-auto-start"),
195201
basis: element.getAttribute(MEDIA_START_BASIS_ATTR),
196-
ordinaryStart: () => resolveStartForElementInternal(element, hostStart),
202+
};
203+
};
204+
205+
const resolveMediaStartForElement = (element: Element): number => {
206+
const input = mediaStartInput(element);
207+
return resolveMediaStartSeconds({
208+
...input,
209+
ordinaryStart: () => resolveStartForElementInternal(element, input.hostStart),
197210
});
198211
};
199212

213+
const isRootGlobalMediaStartForElement = (element: Element): boolean =>
214+
isMediaElement(element) && isRootGlobalMediaStart(mediaStartInput(element));
215+
200216
return {
201217
resolveStartForElement: (element: Element, fallback = 0) =>
202218
resolveStartForElementInternal(element, Math.max(0, fallback)),
203219
resolveDurationForElement: (element: Element) => resolveDurationForElement(element),
204220
resolveMediaStartForElement,
221+
resolveHostStartForElement: (element: Element) => resolveHostOffsetForElement(element, 0),
222+
isRootGlobalMediaStartForElement,
205223
};
206224
}
207225

‎packages/studio/src/components/nle/useTimelineEditCallbacks.test.tsx‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,59 @@ function arrangeClickedCircle(): {
149149
return { circle, selection: selectionForElement(circle) };
150150
}
151151

152+
// main 0 > intro 2 > logo 3 > badge 1: rows are master time, tweens are local.
153+
function nestedKeyframedRow(id: string, start: number, duration: number, host: number) {
154+
const row: TimelineElement = {
155+
id,
156+
key: `compositions/${id}-parent.html#${id}`,
157+
domId: id,
158+
tag: "div",
159+
start,
160+
duration,
161+
track: 0,
162+
sourceFile: `compositions/${id}-parent.html`,
163+
parentCompositionStart: host,
164+
};
165+
const anim: GsapAnimation = {
166+
...authoredInteriorAnimation(),
167+
id: `${id}-to-position`,
168+
targetSelector: `#${id}`,
169+
position: start - host,
170+
resolvedStart: start - host,
171+
duration,
172+
};
173+
usePlayerStore.setState({
174+
elements: [row],
175+
gsapAnimations: new Map([[row.key!, [anim]]]),
176+
});
177+
return { row, anim };
178+
}
179+
180+
describe("keyframe drags on nested rows", () => {
181+
it.each([
182+
["logo", 5, 5, 2, 60],
183+
["badge", 6, 2, 5, 75],
184+
])("re-keys %s's middle keyframe inside its local tween", async (id, start, dur, host, pct) => {
185+
const { row, anim } = nestedKeyframedRow(id, start, dur, host);
186+
const view = renderCallbacks();
187+
await act(async () => {
188+
await view.callbacks.onMoveKeyframe?.(
189+
row.key!,
190+
{ percentage: 50, propertyGroup: "position", tweenPercentage: 50, animationId: anim.id },
191+
pct,
192+
);
193+
});
194+
expect(mocks.actions.handleGsapMoveKeyframe).toHaveBeenCalledWith(
195+
anim.id,
196+
50,
197+
pct,
198+
selectionForElement(row),
199+
);
200+
expect(mocks.actions.handleGsapResizeKeyframedTween).not.toHaveBeenCalled();
201+
view.unmount();
202+
});
203+
});
204+
152205
beforeEach(() => {
153206
vi.clearAllMocks();
154207
mocks.animations = [flatAnimation];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ describe("resolveClipTimingBasis", () => {
195195
it("rebases an expanded sub-comp child by its host mount", () => {
196196
// Expanded children carry host-ABSOLUTE display starts; the tweens they own are
197197
// still composition-local, so the basis is the child's local start.
198-
const pill = { id: "pill", domId: "pill", start: 8, duration: 4, expandedParentStart: 6 };
198+
const pill = { id: "pill", domId: "pill", start: 8, duration: 4, parentCompositionStart: 6 };
199199
expect(resolveClipTimingBasis("pill", "scene.html", [pill], [])).toEqual({
200200
elStart: 2,
201201
elDuration: 4,

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
resolveTweenStart,
1111
resolveTweenDuration,
1212
} from "../utils/globalTimeCompiler";
13+
import { toCompositionTime } from "../player/store/timelineElement";
1314

1415
// ── Types ─────────────────────────────────────────────────────────────────────
1516

@@ -165,14 +166,16 @@ export function resolveSelectorElementIds(
165166
}
166167

167168
/**
168-
* The clip start in the frame the element's OWN tweens are measured in. An
169-
* expanded sub-composition child sits on the master timeline at a host-absolute
170-
* `start`, but its tweens are parsed from its own source file and are local to
171-
* it, so the two must be brought into one frame before any clip-% math — or
172-
* every keyframe rebases to a percentage far outside the clip.
169+
* The clip start in the frame the element's OWN tweens are measured in. A nested
170+
* row sits on the master timeline at a master `start`, but its tweens are local
171+
* to its composition, so the two must be brought into one frame before any clip-%
172+
* math — or every keyframe rebases to a percentage far outside the clip.
173173
*/
174-
export function clipTimingStart(element: { start: number; expandedParentStart?: number }): number {
175-
return element.start - (element.expandedParentStart ?? 0);
174+
export function clipTimingStart(element: {
175+
start: number;
176+
parentCompositionStart?: number;
177+
}): number {
178+
return toCompositionTime(element, element.start);
176179
}
177180

178181
export function selectorFromSelection(selection: DomEditSelection): string | null {
@@ -476,8 +479,8 @@ export function toAbsoluteTime(tweenPos: number, tweenDur: number, percentage: n
476479
* timeline element's start is main-timeline absolute, so passing the raw element
477480
* start subtracted two different frames from each other: a host mounted at 1.5s
478481
* cached its 0s tween at -12%, and a clip-relative percentage can never be
479-
* negative. The composition's mount is `expandedParentStart` for an expanded
480-
* child, the parent composition clip's start otherwise, and 0 for a
482+
* negative. The composition's mount is the row's `parentCompositionStart` when
483+
* set, the parent composition clip's start otherwise, and 0 for a
481484
* root-composition element, whose start already IS the tween frame.
482485
*/
483486
export function resolveClipTimingBasis(
@@ -489,7 +492,7 @@ export function resolveClipTimingBasis(
489492
id: string;
490493
start: number;
491494
duration: number;
492-
expandedParentStart?: number;
495+
parentCompositionStart?: number;
493496
parentCompositionId?: string | null;
494497
}>,
495498
domClipChildren: ReadonlyArray<{ id: string; hostId: string }>,
@@ -502,8 +505,9 @@ export function resolveClipTimingBasis(
502505
const parent = parentId
503506
? elements.find((el) => el.domId === parentId || el.id === parentId)
504507
: undefined;
505-
const mount = direct.expandedParentStart ?? parent?.start;
506-
if (mount !== undefined) return { elStart: direct.start - mount, elDuration: direct.duration };
508+
if (direct.parentCompositionStart !== undefined)
509+
return { elStart: clipTimingStart(direct), elDuration: direct.duration };
510+
if (parent) return { elStart: direct.start - parent.start, elDuration: direct.duration };
507511
// No parent composition named, so this IS a main-timeline clip and its own
508512
// start is already the basis.
509513
if (!parentId) return { elStart: direct.start, elDuration: direct.duration };

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type TimelineElement, usePlayerStore } from "../player/store/playerStore";
2+
import { toAuthoredStart } from "../player/store/timelineElement";
23
import {
34
applyPatchByTarget,
45
findTagByTarget,
@@ -274,7 +275,7 @@ export function buildTimelineResizeTimingPatch(
274275
let patched = applyPatchByTarget(original, target, {
275276
type: "attribute",
276277
property: "start",
277-
value: formatTimelineAttributeNumber(updates.start),
278+
value: formatTimelineAttributeNumber(toAuthoredStart(element, updates.start)),
278279
});
279280
patched = applyPatchByTarget(patched, target, {
280281
type: "attribute",

‎packages/studio/src/hooks/useClipboard.source.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,64 @@ describe("copy order", () => {
336336
});
337337
});
338338

339+
describe("duplicate of a clip inside a sub-composition", () => {
340+
it("writes the copy at local time right after the original", async () => {
341+
// Host at 2 s, so the clip's local 1-3 s shows as a 3-5 s master row.
342+
const sub: TimelineElement = {
343+
id: SUB_HF_ID,
344+
hfId: SUB_HF_ID,
345+
tag: "h2",
346+
start: 3,
347+
duration: 2,
348+
track: 0,
349+
authoredTrack: 0,
350+
sourceFile: "compositions/sub.html",
351+
parentCompositionStart: 2,
352+
};
353+
usePlayerStore.setState({
354+
elements: [TITLE, sub],
355+
selectedElementId: sub.id,
356+
selectedElementIds: new Set([sub.id]),
357+
});
358+
const { clipboard, writes } = mountClipboard(null, SUB, (host) =>
359+
host.setAttribute("data-start", "2"),
360+
);
361+
await clipboard().handleDuplicate();
362+
const starts = [...(writes[0] ?? "").matchAll(/<h2[^>]*data-start="([^"]+)"/g)].map(
363+
(m) => m[1],
364+
);
365+
expect(starts.sort()).toEqual(["1", "3"]);
366+
});
367+
368+
it("moves the copy off a lane its own file already fills right after the original", async () => {
369+
// Host at 10 s: the clip is local 1-3 (master 11-13); its neighbour is local 3-5,
370+
// exactly where the copy lands, so the lane check must compare in local time.
371+
const sub: TimelineElement = {
372+
id: SUB_HF_ID,
373+
hfId: SUB_HF_ID,
374+
tag: "h2",
375+
start: 11,
376+
duration: 2,
377+
track: 0,
378+
authoredTrack: 0,
379+
sourceFile: "compositions/sub.html",
380+
parentCompositionStart: 10,
381+
};
382+
const neighbour: TimelineElement = { ...sub, id: "next", hfId: "next", start: 13 };
383+
usePlayerStore.setState({
384+
elements: [TITLE, sub, neighbour],
385+
selectedElementId: sub.id,
386+
selectedElementIds: new Set([sub.id]),
387+
});
388+
const { clipboard, writes } = mountClipboard(null, SUB, (host) =>
389+
host.setAttribute("data-start", "10"),
390+
);
391+
await clipboard().handleDuplicate();
392+
const copy = /<h2[^>]*data-start="3"[^>]*>/.exec(writes[0] ?? "")?.[0] ?? "";
393+
expect(copy).toContain('data-track-index="1"');
394+
});
395+
});
396+
339397
describe("a copy that fails", () => {
340398
it("leaves the previous copy on the clipboard", async () => {
341399
selectTitle();

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useCallback, useRef } from "react";
22
import { EXCLUDED_TAGS, mintHfId, walkCompositionDescendants } from "@hyperframes/parsers/hf-ids";
33
import type { TimelineElement } from "../player";
44
import { usePlayerStore } from "../player";
5+
import { toAuthoredStart } from "../player/store/timelineElement";
56
import type { DomEditSelection } from "../components/editor/domEditing";
67
import {
78
type ClipboardPayload,
@@ -416,14 +417,22 @@ export function useClipboard({
416417
if (!pid) return false;
417418

418419
const { elements } = targets;
419-
const targetPath = elements[0]?.sourceFile || activeCompPath || "index.html";
420-
const anchorTime = Math.max(...elements.map((el) => el.start + el.duration));
420+
const pathOf = (el: TimelineElement) => el.sourceFile || activeCompPath || "index.html";
421+
const targetPath = pathOf(elements[0]!);
422+
// The copy lands in targetPath's own clock, so anchor and lane check are local to it.
423+
const anchorTime = toAuthoredStart(
424+
elements[0]!,
425+
Math.max(...elements.map((el) => el.start + el.duration)),
426+
);
421427

422428
try {
423429
const clips = await readClips(targets);
424430
let ids: string[] = [];
425431
const duplicate = (originalContent: string) => {
426-
const liveElements = usePlayerStore.getState().elements;
432+
const liveElements = usePlayerStore
433+
.getState()
434+
.elements.filter((el) => pathOf(el) === targetPath)
435+
.map((el) => ({ ...el, start: toAuthoredStart(el, el.start) }));
427436
const pasted = pasteTimelineClips(originalContent, clips, anchorTime, liveElements);
428437
ids = pasted.ids;
429438
return extendRootDurationInSource(pasted.content, pasted.requiredEnd);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const rootElement: TimelineElement = {
2525
};
2626

2727
// An expanded sub-comp child: `start` is in MASTER coordinates (offset by the
28-
// host's master start), `sourceFile` is the sub-comp, and `expandedParentStart`
28+
// host's master start), `sourceFile` is the sub-comp, and `parentCompositionStart`
2929
// is that host master start. Its authored time in the file is start - basis.
3030
const expandedChild: TimelineElement = {
3131
id: "child-clip",
@@ -35,7 +35,7 @@ const expandedChild: TimelineElement = {
3535
track: 1,
3636
domId: "child-clip",
3737
sourceFile: SUBCOMP_FILE,
38-
expandedParentStart: 2,
38+
parentCompositionStart: 2,
3939
};
4040

4141
interface SplitRequest {
@@ -119,13 +119,13 @@ describe("useRazorSplit — sub-comp coordinate rebasing", () => {
119119
expect(harness.splitRequests).toHaveLength(1);
120120
const req = harness.splitRequests[0];
121121
expect(req.path).toBe(SUBCOMP_FILE);
122-
expect(req.splitTime).toBe(3); // 5 - expandedParentStart(2), NOT 5
122+
expect(req.splitTime).toBe(3); // 5 - parentCompositionStart(2), NOT 5
123123
expect(req.elementStart).toBe(0); // 2 - 2, NOT the master start 2
124124
expect(req.elementDuration).toBe(6);
125125
});
126126

127127
it("leaves a root-level clip's coordinates unchanged", async () => {
128-
// Master split time T = 4; no expandedParentStart, so nothing is rebased.
128+
// Master split time T = 4; no parentCompositionStart, so nothing is rebased.
129129
await act(async () => {
130130
await harness.singleRef.current!(rootElement, 4);
131131
});
@@ -154,7 +154,7 @@ describe("useRazorSplit — sub-comp coordinate rebasing", () => {
154154
expect(rootReq.splitTime).toBe(3);
155155
expect(rootReq.elementStart).toBe(0);
156156

157-
// Expanded child: rebased by its OWN expandedParentStart, not the root's.
157+
// Expanded child: rebased by its OWN parentCompositionStart, not the root's.
158158
expect(childReq.splitTime).toBe(1); // 3 - 2
159159
expect(childReq.elementStart).toBe(0); // 2 - 2
160160
});

0 commit comments

Comments
 (0)