From 0bbc9b2943ad6d97edd4acd9b50bb8d648e208d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 4 Nov 2026 16:57:09 +0000 Subject: [PATCH] fix(studio-server): splitting a clip that starts after another clip cuts where it plays (#7172) --- .../src/helpers/sourceMutation.ts | 26 +++++-- .../sourceMutationSplitAndGroup.test.ts | 70 +++++++++++++++++++ 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/packages/studio-server/src/helpers/sourceMutation.ts b/packages/studio-server/src/helpers/sourceMutation.ts index 1b26d9856b..9d5413d689 100644 --- a/packages/studio-server/src/helpers/sourceMutation.ts +++ b/packages/studio-server/src/helpers/sourceMutation.ts @@ -16,6 +16,7 @@ import { readClipTiming, SPLIT_BOUNDARY_EPSILON_S, writeClipTiming, + type ReferenceExpression, } from "@hyperframes/core/composition-contract"; import { relinkSplitHalves } from "@hyperframes/core/media-link"; import { parseStyleDecls, patchStyleAttrString } from "./sourceStyleMutation.js"; @@ -430,14 +431,16 @@ function resolveElementTiming(el: Element): { return { start: timing.start ?? 0, duration: timing.duration ?? 0 }; } +const roundMs = (seconds: number) => Math.round(seconds * 1000) / 1000; + function setElementDuration( el: Element, - start: number, + start: number | ReferenceExpression, duration: number, trackIndex?: number, ): void { writeClipTiming(el, { - start: Math.round(start * 1000) / 1000, + start: typeof start === "number" ? Math.round(start * 1000) / 1000 : start, duration: Math.round(duration * 1000) / 1000, ...(trackIndex != null ? { trackIndex } : {}), }); @@ -490,6 +493,14 @@ export function splitElementInHtml( const timing = resolveElementTiming(timed); let { start, duration } = timing; + // A start that names another clip ("intro", "intro - 0.5") resolves to where that clip ends, which only the + // client's resolved range knows; reading it as 0 cut the wrong span, or rewrote the clip to start at 0. + const reference = readClipTiming(timed).startExpression; + const startsAfter = reference?.kind === "reference" ? reference : null; + if (startsAfter) { + if (!fallbackTiming) return { html: source, matched: false, newId: null }; + start = fallbackTiming.start; + } // GSAP-animated elements carry their timing in the script, not in data-* attrs, // so the source has no authored duration. Fall back to the store's (GSAP-derived) // range — the runtime windows visibility off data-start/data-duration regardless @@ -525,7 +536,14 @@ export function splitElementInHtml( duplicateCssRulesForId(document, timedMedia.id, mediaId); clonedTimed.setAttribute("id", mediaId); } - setElementDuration(clonedTimed, splitTime, secondDuration, fallbackTiming?.track); + setElementDuration( + clonedTimed, + startsAfter + ? { ...startsAfter, offset: roundMs(startsAfter.offset + firstDuration) } + : splitTime, + secondDuration, + fallbackTiming?.track, + ); // Keep the "clip" class — the runtime uses it to control visibility // based on data-start/data-duration timing. @@ -566,7 +584,7 @@ export function splitElementInHtml( // Trim the original element's duration. A GSAP element had no data-start; stamp // it so the runtime windows the first half (visibility selects on [data-start]). - setElementDuration(timed, start, firstDuration, fallbackTiming?.track); + setElementDuration(timed, startsAfter ?? start, firstDuration, fallbackTiming?.track); // Insert clone after original if (el.nextSibling) { diff --git a/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts b/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts index 8f8c589ac5..282f6a2371 100644 --- a/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts +++ b/packages/studio-server/src/helpers/sourceMutationSplitAndGroup.test.ts @@ -560,3 +560,73 @@ describe("splitElementInHtml — id CSS rules across repeated cuts", () => { expect(css).toContain(".clip:not(#a-cut){filter:blur(2px)}"); }); }); + +// A clip that starts after another ("b", "b + 1") has no start of its own in the file: where it plays is where b +// ends, which only the studio's resolved range knows. Read as 0, a cut inside the clip's real time was refused, and +// a cut before it was accepted and rewrote the clip to start at 0. +describe("splitElementInHtml — a start that names another clip", () => { + const after = (start: string) => + `
`; + const halves = (html: string) => + ["a", "a-split"].map((id) => { + const el = parseHTML(html).document.getElementById(id); + return [el?.getAttribute("data-start"), el?.getAttribute("data-duration")]; + }); + + it("cuts at the time the studio gives and keeps both halves tied to the clip they follow", () => { + const { html, matched } = splitElementInHtml(after("b"), { id: "a" }, 5, "a-split", { + start: 3, + duration: 4, + }); + expect(matched).toBe(true); + expect(halves(html)).toEqual([ + ["b", "2"], + ["b + 2", "2"], + ]); + }); + + it("keeps an offset: the second half starts the first half's length further after the clip", () => { + const { html, matched } = splitElementInHtml(after("b + 1"), { id: "a" }, 5, "a-split", { + start: 4, + duration: 4, + }); + expect(matched).toBe(true); + expect(halves(html)).toEqual([ + ["b + 1", "1"], + ["b + 2", "3"], + ]); + }); + + it("leaves the file as it was for a cut before the clip's real start, instead of rewriting it to start at 0", () => { + const source = after("b"); + const result = splitElementInHtml(source, { id: "a" }, 2, "a-split", { start: 3, duration: 4 }); + expect(result).toEqual({ html: source, matched: false, newId: null }); + }); + + it("refuses when it cannot know where the clip plays", () => { + const source = after("b"); + expect(splitElementInHtml(source, { id: "a" }, 5, "a-split")).toEqual({ + html: source, + matched: false, + newId: null, + }); + }); +}); + +describe("splitElementInHtml — a cut the file's own timing does not allow", () => { + const clip = `
`; + const untouched = { html: clip, matched: false, newId: null }; + + it.each([ + ["exactly at the clip's start", 2, { start: 2, duration: 4 }], + ["past the clip's end", 7, { start: 2, duration: 4 }], + // 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. + [ + "where the studio thinks the clip is but the file says otherwise", + 6, + { start: 5, duration: 4 }, + ], + ])("leaves the file as it was for a cut %s", (_name, at, range) => { + expect(splitElementInHtml(clip, { id: "a" }, at, "a-split", range)).toEqual(untouched); + }); +});