Skip to content

Commit 0bbc9b2

Browse files
fix(studio-server): splitting a clip that starts after another clip cuts where it plays (#7172)
1 parent 845bd63 commit 0bbc9b2

2 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎packages/studio-server/src/helpers/sourceMutation.ts‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
readClipTiming,
1717
SPLIT_BOUNDARY_EPSILON_S,
1818
writeClipTiming,
19+
type ReferenceExpression,
1920
} from "@hyperframes/core/composition-contract";
2021
import { relinkSplitHalves } from "@hyperframes/core/media-link";
2122
import { parseStyleDecls, patchStyleAttrString } from "./sourceStyleMutation.js";
@@ -430,14 +431,16 @@ function resolveElementTiming(el: Element): {
430431
return { start: timing.start ?? 0, duration: timing.duration ?? 0 };
431432
}
432433

434+
const roundMs = (seconds: number) => Math.round(seconds * 1000) / 1000;
435+
433436
function setElementDuration(
434437
el: Element,
435-
start: number,
438+
start: number | ReferenceExpression,
436439
duration: number,
437440
trackIndex?: number,
438441
): void {
439442
writeClipTiming(el, {
440-
start: Math.round(start * 1000) / 1000,
443+
start: typeof start === "number" ? Math.round(start * 1000) / 1000 : start,
441444
duration: Math.round(duration * 1000) / 1000,
442445
...(trackIndex != null ? { trackIndex } : {}),
443446
});
@@ -490,6 +493,14 @@ export function splitElementInHtml(
490493

491494
const timing = resolveElementTiming(timed);
492495
let { start, duration } = timing;
496+
// A start that names another clip ("intro", "intro - 0.5") resolves to where that clip ends, which only the
497+
// client's resolved range knows; reading it as 0 cut the wrong span, or rewrote the clip to start at 0.
498+
const reference = readClipTiming(timed).startExpression;
499+
const startsAfter = reference?.kind === "reference" ? reference : null;
500+
if (startsAfter) {
501+
if (!fallbackTiming) return { html: source, matched: false, newId: null };
502+
start = fallbackTiming.start;
503+
}
493504
// GSAP-animated elements carry their timing in the script, not in data-* attrs,
494505
// so the source has no authored duration. Fall back to the store's (GSAP-derived)
495506
// range — the runtime windows visibility off data-start/data-duration regardless
@@ -525,7 +536,14 @@ export function splitElementInHtml(
525536
duplicateCssRulesForId(document, timedMedia.id, mediaId);
526537
clonedTimed.setAttribute("id", mediaId);
527538
}
528-
setElementDuration(clonedTimed, splitTime, secondDuration, fallbackTiming?.track);
539+
setElementDuration(
540+
clonedTimed,
541+
startsAfter
542+
? { ...startsAfter, offset: roundMs(startsAfter.offset + firstDuration) }
543+
: splitTime,
544+
secondDuration,
545+
fallbackTiming?.track,
546+
);
529547

530548
// Keep the "clip" class — the runtime uses it to control visibility
531549
// based on data-start/data-duration timing.
@@ -566,7 +584,7 @@ export function splitElementInHtml(
566584

567585
// Trim the original element's duration. A GSAP element had no data-start; stamp
568586
// it so the runtime windows the first half (visibility selects on [data-start]).
569-
setElementDuration(timed, start, firstDuration, fallbackTiming?.track);
587+
setElementDuration(timed, startsAfter ?? start, firstDuration, fallbackTiming?.track);
570588

571589
// Insert clone after original
572590
if (el.nextSibling) {

‎packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,73 @@ describe("splitElementInHtml — id CSS rules across repeated cuts", () => {
560560
expect(css).toContain(".clip:not(#a-cut){filter:blur(2px)}");
561561
});
562562
});
563+
564+
// A clip that starts after another ("b", "b + 1") has no start of its own in the file: where it plays is where b
565+
// ends, which only the studio's resolved range knows. Read as 0, a cut inside the clip's real time was refused, and
566+
// a cut before it was accepted and rewrote the clip to start at 0.
567+
describe("splitElementInHtml — a start that names another clip", () => {
568+
const after = (start: string) =>
569+
`<html><body><div data-composition-id="root"><div id="b" class="clip" data-start="0" data-duration="3"></div><div id="a" class="clip" data-start="${start}" data-duration="4"></div></div></body></html>`;
570+
const halves = (html: string) =>
571+
["a", "a-split"].map((id) => {
572+
const el = parseHTML(html).document.getElementById(id);
573+
return [el?.getAttribute("data-start"), el?.getAttribute("data-duration")];
574+
});
575+
576+
it("cuts at the time the studio gives and keeps both halves tied to the clip they follow", () => {
577+
const { html, matched } = splitElementInHtml(after("b"), { id: "a" }, 5, "a-split", {
578+
start: 3,
579+
duration: 4,
580+
});
581+
expect(matched).toBe(true);
582+
expect(halves(html)).toEqual([
583+
["b", "2"],
584+
["b + 2", "2"],
585+
]);
586+
});
587+
588+
it("keeps an offset: the second half starts the first half's length further after the clip", () => {
589+
const { html, matched } = splitElementInHtml(after("b + 1"), { id: "a" }, 5, "a-split", {
590+
start: 4,
591+
duration: 4,
592+
});
593+
expect(matched).toBe(true);
594+
expect(halves(html)).toEqual([
595+
["b + 1", "1"],
596+
["b + 2", "3"],
597+
]);
598+
});
599+
600+
it("leaves the file as it was for a cut before the clip's real start, instead of rewriting it to start at 0", () => {
601+
const source = after("b");
602+
const result = splitElementInHtml(source, { id: "a" }, 2, "a-split", { start: 3, duration: 4 });
603+
expect(result).toEqual({ html: source, matched: false, newId: null });
604+
});
605+
606+
it("refuses when it cannot know where the clip plays", () => {
607+
const source = after("b");
608+
expect(splitElementInHtml(source, { id: "a" }, 5, "a-split")).toEqual({
609+
html: source,
610+
matched: false,
611+
newId: null,
612+
});
613+
});
614+
});
615+
616+
describe("splitElementInHtml — a cut the file's own timing does not allow", () => {
617+
const clip = `<html><body><div data-composition-id="root"><div id="a" class="clip" data-start="2" data-duration="4"></div></div></body></html>`;
618+
const untouched = { html: clip, matched: false, newId: null };
619+
620+
it.each([
621+
["exactly at the clip's start", 2, { start: 2, duration: 4 }],
622+
["past the clip's end", 7, { start: 2, duration: 4 }],
623+
// The file is the truth: a studio that thinks the clip starts at 5 is out of date, and its cut at 6 is not cut.
624+
[
625+
"where the studio thinks the clip is but the file says otherwise",
626+
6,
627+
{ start: 5, duration: 4 },
628+
],
629+
])("leaves the file as it was for a cut %s", (_name, at, range) => {
630+
expect(splitElementInHtml(clip, { id: "a" }, at, "a-split", range)).toEqual(untouched);
631+
});
632+
});

0 commit comments

Comments
 (0)