Skip to content

Commit aa9413b

Browse files
fix(studio): splitting a video in an untimed wrapper cuts the video, not the wrapper (#5486)
The split timed both wrapper copies and left both inner videos on the original window with the same id, so the two halves played together.
1 parent 5e6e58f commit aa9413b

2 files changed

Lines changed: 96 additions & 11 deletions

File tree

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ function setElementDuration(
383383
});
384384
}
385385

386+
/** True when the media has an authored window and the row's range (if given) is that window. */
387+
function isWindowOf(
388+
media: { start: number; duration: number },
389+
row?: { start: number; duration: number },
390+
): boolean {
391+
if (media.duration <= 0) return false;
392+
const near = (a: number, b: number) => Math.abs(a - b) < 0.001;
393+
return !row || (near(media.start, row.start) && near(media.duration, row.duration));
394+
}
395+
386396
// fallow-ignore-next-line complexity
387397
export function splitElementInHtml(
388398
source: string,
@@ -405,7 +415,20 @@ export function splitElementInHtml(
405415
const el = findTargetElement(document, target);
406416
if (!el || !isHTMLElement(el)) return { html: source, matched: false, newId: null };
407417

408-
const timing = resolveElementTiming(el);
418+
// An untimed wrapper whose row is its timed media's own window is a view of that
419+
// media: cut the media and leave the wrappers untimed. A GSAP scene keeps its own window.
420+
const timedMediaSelector = "video[data-start], audio[data-start]";
421+
const innerMedia =
422+
el.hasAttribute("data-start") ||
423+
resolveElementTiming(el).duration > 0 ||
424+
el.matches("[data-composition-src], [data-composition-id]")
425+
? null
426+
: el.querySelector(timedMediaSelector);
427+
const timedMedia =
428+
innerMedia && isWindowOf(resolveElementTiming(innerMedia), fallbackTiming) ? innerMedia : null;
429+
const timed = timedMedia ?? el;
430+
431+
const timing = resolveElementTiming(timed);
409432
let { start, duration } = timing;
410433
// GSAP-animated elements carry their timing in the script, not in data-* attrs,
411434
// so the source has no authored duration. Fall back to the store's (GSAP-derived)
@@ -432,34 +455,40 @@ export function splitElementInHtml(
432455
// Descendants carry their own data-hf-id; leaving them duplicates the id of
433456
// every nested node (e.g. an inner <span>), so strip them on the clone too.
434457
for (const node of clone.querySelectorAll("[data-hf-id]")) node.removeAttribute("data-hf-id");
435-
setElementDuration(clone, splitTime, secondDuration, fallbackTiming?.track);
458+
const clonedTimed = timedMedia ? clone.querySelector(timedMediaSelector)! : clone;
459+
if (timedMedia?.id) {
460+
const mediaId = nextUniqueId(document, `${timedMedia.id}-split`, new Set([newId]));
461+
duplicateCssRulesForId(document, timedMedia.id, mediaId);
462+
clonedTimed.setAttribute("id", mediaId);
463+
}
464+
setElementDuration(clonedTimed, splitTime, secondDuration, fallbackTiming?.track);
436465

437466
// Keep the "clip" class — the runtime uses it to control visibility
438467
// based on data-start/data-duration timing.
439468

440469
// A split creates two views over the same media source or sub-composition.
441470
// Even an untrimmed one needs an explicit zero in-point stamped on the first
442471
// half so the second half can advance from it instead of restarting at zero.
443-
const playbackStartAttr = el.hasAttribute("data-playback-start")
472+
const playbackStartAttr = timed.hasAttribute("data-playback-start")
444473
? "data-playback-start"
445-
: el.hasAttribute("data-media-start")
474+
: timed.hasAttribute("data-media-start")
446475
? "data-media-start"
447-
: el.matches("[data-composition-src], [data-composition-id]")
476+
: timed.matches("[data-composition-src], [data-composition-id]")
448477
? "data-playback-start"
449-
: el.matches("audio, video")
478+
: timed.matches("audio, video")
450479
? "data-media-start"
451480
: null;
452481
if (playbackStartAttr) {
453-
const readAttr = (name: string) => el.getAttribute(name);
454-
const authoredTrim = el.getAttribute(playbackStartAttr);
482+
const readAttr = (name: string) => timed.getAttribute(name);
483+
const authoredTrim = timed.getAttribute(playbackStartAttr);
455484
const currentTrim =
456485
authoredTrim !== null
457486
? readMediaOffsetSeconds(readAttr)
458487
: (fallbackTiming?.playbackStart ?? 0);
459488
const rate = readPlaybackRate(readAttr, fallbackTiming?.playbackRate);
460489
if (authoredTrim === null || Number(authoredTrim) !== currentTrim)
461-
el.setAttribute(playbackStartAttr, String(Math.round(currentTrim * 1000) / 1000));
462-
clone.setAttribute(
490+
timed.setAttribute(playbackStartAttr, String(Math.round(currentTrim * 1000) / 1000));
491+
clonedTimed.setAttribute(
463492
playbackStartAttr,
464493
String(Math.round((currentTrim + firstDuration * rate) * 1000) / 1000),
465494
);
@@ -473,7 +502,7 @@ export function splitElementInHtml(
473502

474503
// Trim the original element's duration. A GSAP element had no data-start; stamp
475504
// it so the runtime windows the first half (visibility selects on [data-start]).
476-
setElementDuration(el, start, firstDuration, fallbackTiming?.track);
505+
setElementDuration(timed, start, firstDuration, fallbackTiming?.track);
477506

478507
// Insert clone after original
479508
if (el.nextSibling) {

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,62 @@ describe("splitElementInHtml", () => {
222222
expect(document.getElementById("box")?.getAttribute("data-playback-start")).toBe("1.5");
223223
expect(document.getElementById("box-split")?.getAttribute("data-playback-start")).toBe("5.5");
224224
});
225+
226+
it("splits the timed video inside an untimed wrapper and leaves both wrappers untimed", () => {
227+
const wrapped = `<!DOCTYPE html><html><head><style>#v { object-fit: cover; }</style></head><body><div data-composition-id="root"><div id="wrap" class="frame"><video id="v" class="clip" src="a.mp4" data-start="1" data-duration="6"></video></div></div></body></html>`;
228+
// The requested wrapper id is the media's natural split id: both must still come out unique.
229+
const result = splitElementInHtml(wrapped, { id: "wrap" }, 3, "v-split", {
230+
start: 1,
231+
duration: 6,
232+
});
233+
const { document } = parseHTML(result.html);
234+
const wrappers = [...document.querySelectorAll("div.frame")];
235+
const [first, second] = [...document.querySelectorAll("video")];
236+
237+
expect(result.matched).toBe(true);
238+
expect(wrappers).toHaveLength(2);
239+
for (const wrapper of wrappers) {
240+
expect(wrapper.hasAttribute("data-start")).toBe(false);
241+
expect(wrapper.hasAttribute("data-duration")).toBe(false);
242+
}
243+
expect(first?.id).toBe("v");
244+
expect(second?.id).toBeTruthy();
245+
const ids = [...document.querySelectorAll("[id]")].map((node) => node.id);
246+
expect(new Set(ids).size).toBe(ids.length);
247+
expect(result.html).toContain(`#${second?.id} {`);
248+
expect(first?.getAttribute("data-duration")).toBe("2");
249+
expect(second?.getAttribute("data-start")).toBe("3");
250+
expect(second?.getAttribute("data-duration")).toBe("4");
251+
expect(second?.getAttribute("data-media-start")).toBe("2");
252+
});
253+
254+
it.each(['data-start="1"', 'data-duration="6"', 'data-composition-id="scene"'])(
255+
"cuts a wrapper with %s itself, not the video inside it",
256+
(wrapperAttrs) => {
257+
const wrapped = `<!DOCTYPE html><html><body><div data-composition-id="root"><div id="wrap" ${wrapperAttrs}><video id="v" src="a.mp4" data-start="1" data-duration="6"></video></div></div></body></html>`;
258+
const result = splitElementInHtml(wrapped, { id: "wrap" }, 3, "wrap-split", {
259+
start: 1,
260+
duration: 6,
261+
});
262+
const { document } = parseHTML(result.html);
263+
264+
expect(document.getElementById("wrap-split")?.hasAttribute("data-start")).toBe(true);
265+
expect(document.getElementById("v")?.getAttribute("data-duration")).toBe("6");
266+
},
267+
);
268+
269+
it("keeps cutting a GSAP scene on its own window when it holds a shorter timed video", () => {
270+
const scene = `<!DOCTYPE html><html><body><div data-composition-id="root"><div id="card"><video id="v" src="a.mp4" data-start="2" data-duration="3"></video></div></div></body></html>`;
271+
const result = splitElementInHtml(scene, { id: "card" }, 7, "card-split", {
272+
start: 0,
273+
duration: 10,
274+
});
275+
const { document } = parseHTML(result.html);
276+
277+
expect(result.matched).toBe(true);
278+
expect(document.getElementById("card")?.getAttribute("data-duration")).toBe("7");
279+
expect(document.getElementById("card-split")?.getAttribute("data-start")).toBe("7");
280+
});
225281
});
226282

227283
describe("wrapElementsInHtml / unwrapElementsFromHtml", () => {

0 commit comments

Comments
 (0)