Skip to content

Commit 7090021

Browse files
vanceingallsclaude
andauthored
feat(studio): audio gain on G, speed in clip names, additive selection and partner clamp (8/8) (#4821)
* feat(studio): show clip speed in the clip name as [150%] or [ramp] Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(studio): one fx badge per clip replaces the per-tool and link badges Grey with no effects, white with any. The tooltip lists look, voice, other fx, crop and ducking; a click opens the clip menu. The volume badge stays when volume is off 100%. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(studio): premiere-style audio gain dialog on G Set Gain to, Adjust Gain by, Normalize Max Peak to, Normalize All Peaks to and Normalize loudness to -16 LUFS, with the selection's peak amplitude read from the peak map. Writes data-volume on every selected clip with sound as one undo step. The clip menu's Normalize loudness becomes Audio Gain... (G). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): use a theme blue for the audio gain OK button Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): keep shortcuts out of the audio gain modal; refuse while recording Keys pressed inside an aria-modal dialog no longer reach the global hotkeys, so Delete on the focused OK button cannot delete the clips the dialog edits. The multi-clip write refuses during a recording like the single-clip one. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): offer link for any unlinked video and audio pair Link no longer requires the same file and identical timing, matching Premiere. Same-file pairs get a sync origin so a trimmed or moved audio shows its offset; different files link without one. Merge back is disabled until the pair is in sync. Linked moves keep the offset, and a trim only carries to a partner whose edge is at the same time. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): shift and cmd-click add timeline clips to the selection A modifier click on a clip replaced the selection, so a video and an audio could never both be selected and Link never appeared. Shift, Cmd and Ctrl now toggle the clip (and its link partners unless Alt) in the selection; Shift no longer turns a clip click into a time-range popover. Right-clicking a lone unlinked video or audio offers Link when exactly one opposite-kind clip from the same file sits on another track. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): keep audio inside its partner video and make cmd-click add in every host A modifier click passed the toggle as an argument to an optional onSelectElement call, so hosts without that handler (desktop) never ran it. An audio clip linked to a video, or sharing its sync origin, now stays inside that video's span while dragged or trimmed, Alt edits included, unless the video moves or trims in the same gesture. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): hold the drag actor at the partner-video clamp The dragged audio's ghost followed the pointer past the video's end while the landing stayed clamped; it now stops where the clip will land. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(studio): preview a look on hover in the clip menu before applying it Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(studio): trim comments the clip-link names already carry Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): unlinking clears the timeline selection After Unlink both clips stayed selected, so the next edge trim ran the multi-select group resize and the former partner still followed. Unlink from the menu, the shortcut or an Alt edit now leaves nothing selected. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(sdk): keep a linked partner's end when a trim changes the grabbed duration Copying the grabbed clip's duration to a partner that starts later pushed the partner's end past the clip both ended on. The partner's duration is now the shared new end minus its own start. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): keep every moving detached audio inside its video, after placement too The containment clamp ran before collision placement and only for the grabbed clip, so a drop pushed past an obstacle, or audio carried along in a multi-selection, could land outside its video. The allowed shift is now the intersection over every moving audio, and a placement outside it is rejected. Link partners are matched within their source file. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(studio): carry the normalize refusal and save outcomes into the gain dialog Normalize moved from the clip menu into the Audio Gain dialog here, so the volume-lane refusal and the saved/refused/failed checks go with it: a lane owned clip is refused, and a failed single or multi-clip save rejects instead of reporting success. Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(studio): trim comments the gain and multiselect layer added Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(sdk): split the linked partner's duration out of partnerTiming Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(sdk): refuse a linked trim whose new end would cross the partner's start With linked video [2,8] and audio [3,8], setTiming(video, {duration: 0.5}) computed a partner duration of -0.5, skipped it, and committed the video alone, leaving the audio outside its video. The partner edits are now planned before any mutation; a non-positive partner duration throws ("Linked audio would start after the new end — unlink or trim the audio first.") and can() reports E_LINKED_PARTNER_CROSSED, so nothing is written and the undo stack is untouched. In Studio the refusal fails the cutover (never declines into the legacy path) and the timeline resize toast shows the reason. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(sdk): key a linked edit's grabbed baselines by composition scope planLinkedTiming stored each target's timing under its raw data-link id, while linkedPartnerIds scopes link ids to their host composition. One setTiming naming a root video and a nested video that both use lk-1 kept only the nested baseline, so the root audio was skipped and left past its trimmed video. Baselines are now keyed by host scope and link id, the same rule linkedPartnerIds uses, for both the partner trim and the crossing refusal. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(sdk): run the two-scope grabbed-baseline cases on an inline composition too Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 6388845 commit 7090021

68 files changed

Lines changed: 2392 additions & 392 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/sdk/src/engine/mutate.ts‎

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
JsonPatchOp,
1818
} from "../types.js";
1919
import type { ParsedDocument } from "./model.js";
20-
import { MEDIA_LINK_ATTR } from "@hyperframes/core/media-link";
20+
import { MEDIA_LINK_ATTR, linkScopeOf } from "@hyperframes/core/media-link";
2121
import { idsToUnlink, linkedPartnerIds } from "./linkedTiming.js";
2222
import {
2323
resolveScoped,
@@ -298,16 +298,96 @@ function applySetTiming(
298298
const own = handleSetTiming(parsed, ids, timing);
299299
return concatResults(own, handleSetAttribute(parsed, unlink, MEDIA_LINK_ATTR, null));
300300
}
301-
const partners =
302-
op.start === undefined && op.duration === undefined
303-
? []
304-
: linkedPartnerIds(parsed.document, ids);
305-
const own = handleSetTiming(parsed, ids, timing);
306-
if (partners.length === 0) return own;
307-
return concatResults(
308-
own,
309-
handleSetTiming(parsed, partners, { start: op.start, duration: op.duration }),
310-
);
301+
const plan = planLinkedTiming(parsed, ids, timing);
302+
if (plan.refusal) throw new Error(plan.refusal);
303+
let result = handleSetTiming(parsed, ids, timing);
304+
for (const partner of plan.partners) {
305+
result = concatResults(result, handleSetTiming(parsed, [partner.id], partner.timing));
306+
}
307+
return result;
308+
}
309+
310+
type PartnerEdit = { id: HfId; timing: { start?: number; duration?: number } };
311+
312+
function planLinkedTiming(
313+
parsed: ParsedDocument,
314+
ids: HfId[],
315+
timing: { start?: number; duration?: number },
316+
): { partners: PartnerEdit[]; refusal: string | null } {
317+
if (timing.start === undefined && timing.duration === undefined) {
318+
return { partners: [], refusal: null };
319+
}
320+
const grabbedFor = grabbedBaselines(parsed, ids);
321+
const partners: PartnerEdit[] = [];
322+
for (const id of linkedPartnerIds(parsed.document, ids)) {
323+
const el = resolveScoped(parsed.document, id);
324+
const grabbed = el ? grabbedFor(el) : undefined;
325+
if (!el || !grabbed) continue;
326+
const partnerEdit = partnerTiming(grabbed, readClipTiming(el), timing);
327+
if (partnerEdit.duration !== undefined && partnerEdit.duration <= 0) {
328+
const tag = el.tagName.toLowerCase();
329+
return {
330+
partners: [],
331+
refusal: `Linked ${tag} would start after the new end — unlink or trim the ${tag} first.`,
332+
};
333+
}
334+
partners.push({ id, timing: partnerEdit });
335+
}
336+
return { partners, refusal: null };
337+
}
338+
339+
type ClipWindow = { start: number | null; duration: number | null };
340+
341+
function grabbedBaselines(
342+
parsed: ParsedDocument,
343+
ids: HfId[],
344+
): (partner: Element) => ClipWindow | undefined {
345+
const byScope = new Map<Element | null, Map<string, ClipWindow>>();
346+
for (const id of ids) {
347+
const el = resolveScoped(parsed.document, id);
348+
const link = el?.getAttribute(MEDIA_LINK_ATTR);
349+
if (!el || !link) continue;
350+
const byLink = byScope.get(linkScopeOf(el)) ?? new Map<string, ClipWindow>();
351+
byLink.set(link, readClipTiming(el));
352+
byScope.set(linkScopeOf(el), byLink);
353+
}
354+
return (partner) => {
355+
const link = partner.getAttribute(MEDIA_LINK_ATTR);
356+
return link ? byScope.get(linkScopeOf(partner))?.get(link) : undefined;
357+
};
358+
}
359+
const ALIGN_EPSILON_S = 1e-3;
360+
361+
/**
362+
* A partner follows the edit without resyncing: a start change shifts it by the
363+
* same delta (keeping any offset); a duration change carries over only when the
364+
* partner's end sat at the edited clip's end.
365+
*/
366+
function partnerTiming(
367+
grabbed: ClipWindow,
368+
partner: ClipWindow,
369+
edit: { start?: number; duration?: number },
370+
): { start?: number; duration?: number } {
371+
const timing: { start?: number; duration?: number } = {};
372+
const [gStart, pStart] = [grabbed.start ?? 0, partner.start ?? 0];
373+
if (edit.start !== undefined) timing.start = pStart + (edit.start - gStart);
374+
const duration = partnerDuration(grabbed, partner, edit, timing.start ?? pStart);
375+
if (duration !== undefined) timing.duration = duration;
376+
return timing;
377+
}
378+
379+
function partnerDuration(
380+
grabbed: ClipWindow,
381+
partner: ClipWindow,
382+
edit: { start?: number; duration?: number },
383+
partnerStart: number,
384+
): number | undefined {
385+
if (edit.duration === undefined) return undefined;
386+
const grabbedStart = grabbed.start ?? 0;
387+
const grabbedEnd = grabbedStart + (grabbed.duration ?? 0);
388+
const partnerEnd = (partner.start ?? 0) + (partner.duration ?? 0);
389+
if (Math.abs(partnerEnd - grabbedEnd) >= ALIGN_EPSILON_S) return undefined;
390+
return (edit.start ?? grabbedStart) + edit.duration - partnerStart;
311391
}
312392

313393
export function applyOp(parsed: ParsedDocument, op: EditOp): MutationResult {
@@ -1667,7 +1747,11 @@ export function validateOp(parsed: ParsedDocument, op: EditOp): CanResult {
16671747
`Element(s) not found: ${missing.join(", ")}.`,
16681748
"Verify the id against comp.getElements() or comp.find().",
16691749
);
1670-
return CAN_OK;
1750+
const refusal =
1751+
op.type === "setTiming" && op.linked !== false
1752+
? planLinkedTiming(parsed, ids, op).refusal
1753+
: null;
1754+
return refusal ? canErr("E_LINKED_PARTNER_CROSSED", refusal) : CAN_OK;
16711755
}
16721756
case "addElement": {
16731757
if (op.parent !== null && resolveScoped(parsed.document, op.parent) === null)

‎packages/sdk/src/session.linked.test.ts‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ describe("setTiming on linked clips", () => {
3636
expect(attr(html, "hf-talk-audio", "data-duration")).toBe("6");
3737
});
3838

39+
it("moves a misaligned pair by the same delta, preserving the offset", async () => {
40+
const html = LINKED_HTML.replace(
41+
'src="talk.mp4" data-link="lk-1" data-start="2"',
42+
'src="talk.mp4" data-link="lk-1" data-start="3"',
43+
);
44+
const comp = await openComposition(html);
45+
comp.setTiming("hf-talk", { start: 5 });
46+
const out = comp.serialize();
47+
expect(attr(out, "hf-talk", "data-start")).toBe("5");
48+
expect(attr(out, "hf-talk-audio", "data-start")).toBe("6");
49+
});
50+
51+
it("keeps a common end when partners start at different times", async () => {
52+
const offset = LINKED_HTML.replace(
53+
'data-hf-id="hf-talk-audio" src="talk.mp4" data-link="lk-1" data-start="2" data-duration="6"',
54+
'data-hf-id="hf-talk-audio" src="talk.mp4" data-link="lk-1" data-start="3" data-duration="5"',
55+
);
56+
const comp = await openComposition(offset);
57+
comp.setTiming("hf-talk", { duration: 4 });
58+
const html = comp.serialize();
59+
expect(attr(html, "hf-talk", "data-duration")).toBe("4");
60+
expect(attr(html, "hf-talk-audio", "data-start")).toBe("3");
61+
expect(attr(html, "hf-talk-audio", "data-duration")).toBe("3");
62+
});
63+
3964
it("{ linked: false } edits one member and unlinks the pair", async () => {
4065
const comp = await openComposition(LINKED_HTML);
4166
comp.setTiming("hf-talk", { start: 5 }, { linked: false });
@@ -127,4 +152,100 @@ describe("setTiming on linked clips", () => {
127152
);
128153
expect(comp.serialize()).toBe((await openComposition(html)).serialize());
129154
});
155+
156+
describe("a duration edit whose new end would cross a linked partner's start", () => {
157+
const OFFSET_HTML = LINKED_HTML.replace(
158+
'data-hf-id="hf-talk-audio" src="talk.mp4" data-link="lk-1" data-start="2" data-duration="6"',
159+
'data-hf-id="hf-talk-audio" src="talk.mp4" data-link="lk-1" data-start="3" data-duration="5"',
160+
);
161+
162+
it.each([1, 0.5])("refuses duration %s atomically, before any mutation", async (duration) => {
163+
const comp = await openComposition(OFFSET_HTML);
164+
comp.setTiming("hf-music", { start: 1 });
165+
const before = comp.serialize();
166+
expect(comp.can({ type: "setTiming", target: "hf-talk", duration })).toMatchObject({
167+
ok: false,
168+
code: "E_LINKED_PARTNER_CROSSED",
169+
});
170+
expect(() => comp.setTiming("hf-talk", { duration })).toThrow(
171+
/linked audio would start after the new end/i,
172+
);
173+
expect(comp.serialize()).toBe(before);
174+
comp.undo();
175+
expect(attr(comp.serialize(), "hf-music", "data-start")).toBe("0");
176+
expect(comp.canUndo()).toBe(false);
177+
});
178+
179+
it("trims the partner to the shared end while that end stays after its start", async () => {
180+
const comp = await openComposition(OFFSET_HTML);
181+
expect(comp.can({ type: "setTiming", target: "hf-talk", duration: 1.5 }).ok).toBe(true);
182+
comp.setTiming("hf-talk", { duration: 1.5 });
183+
const html = comp.serialize();
184+
expect(attr(html, "hf-talk", "data-duration")).toBe("1.5");
185+
expect(attr(html, "hf-talk-audio", "data-start")).toBe("3");
186+
expect(attr(html, "hf-talk-audio", "data-duration")).toBe("0.5");
187+
});
188+
189+
it("allows the same edit once the pair is unlinked", async () => {
190+
const comp = await openComposition(OFFSET_HTML);
191+
comp.setTiming("hf-talk", { duration: 0.5 }, { linked: false });
192+
const html = comp.serialize();
193+
expect(attr(html, "hf-talk", "data-duration")).toBe("0.5");
194+
expect(attr(html, "hf-talk-audio", "data-duration")).toBe("5");
195+
});
196+
});
197+
198+
describe.each([
199+
{ host: 'data-composition-id="child" data-composition-file="child.html"', cv: "hf-host/hf-cv" },
200+
{ host: 'data-composition-id="child"', cv: "hf-cv" },
201+
])(
202+
"one edit naming linked clips in two compositions that reuse a link id ($host)",
203+
({ host, cv }) => {
204+
const TWO_SCOPES_HTML = `
205+
<div data-hf-id="hf-stage" data-hf-root style="width:1280px;height:720px" data-duration="10">
206+
<video data-hf-id="hf-v" src="talk.mp4" muted data-link="lk-1" data-start="2" data-duration="6" data-track-index="0"></video>
207+
<audio data-hf-id="hf-a" src="talk.mp4" data-link="lk-1" data-start="2" data-duration="6" data-track-index="1"></audio>
208+
<div data-hf-id="hf-host" ${host} data-start="0" data-duration="10" data-track-index="2">
209+
<video data-hf-id="hf-cv" src="b.mp4" muted data-link="lk-1" data-start="0" data-duration="4" data-track-index="0"></video>
210+
<audio data-hf-id="hf-ca" src="b.mp4" data-link="lk-1" data-start="0" data-duration="4" data-track-index="1"></audio>
211+
</div>
212+
</div>`.trim();
213+
214+
it("trims each audio against its own video's baseline", async () => {
215+
const comp = await openComposition(TWO_SCOPES_HTML);
216+
comp.dispatch({ type: "setTiming", target: ["hf-v", cv], duration: 0.5 });
217+
const html = comp.serialize();
218+
expect([attr(html, "hf-v", "data-start"), attr(html, "hf-v", "data-duration")]).toEqual([
219+
"2",
220+
"0.5",
221+
]);
222+
expect([attr(html, "hf-a", "data-start"), attr(html, "hf-a", "data-duration")]).toEqual([
223+
"2",
224+
"0.5",
225+
]);
226+
expect([attr(html, "hf-cv", "data-start"), attr(html, "hf-cv", "data-duration")]).toEqual([
227+
"0",
228+
"0.5",
229+
]);
230+
expect([attr(html, "hf-ca", "data-start"), attr(html, "hf-ca", "data-duration")]).toEqual([
231+
"0",
232+
"0.5",
233+
]);
234+
});
235+
236+
it("refuses against the scoped baseline when one scope's audio would cross its video's end", async () => {
237+
const crossing = TWO_SCOPES_HTML.replace(
238+
'data-hf-id="hf-a" src="talk.mp4" data-link="lk-1" data-start="2" data-duration="6"',
239+
'data-hf-id="hf-a" src="talk.mp4" data-link="lk-1" data-start="3" data-duration="5"',
240+
);
241+
const comp = await openComposition(crossing);
242+
const before = comp.serialize();
243+
const op = { type: "setTiming" as const, target: ["hf-v", cv], duration: 0.5 };
244+
expect(comp.can(op)).toMatchObject({ ok: false, code: "E_LINKED_PARTNER_CROSSED" });
245+
expect(() => comp.dispatch(op)).toThrow(/linked audio would start after the new end/i);
246+
expect(comp.serialize()).toBe(before);
247+
expect(comp.canUndo()).toBe(false);
248+
});
249+
},
250+
);
130251
});

‎packages/studio/src/components/editor/mediaLinkEdits.test.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
pickDetachedAudioTrack,
1111
removeElementInSource,
1212
setLinkInSource,
13+
sharesSourceFile,
1314
} from "./mediaLinkEdits";
1415

1516
const automation = JSON.stringify({
@@ -192,6 +193,15 @@ describe("linkInSource", () => {
192193
});
193194
});
194195

196+
describe("linkInSource without a sync origin", () => {
197+
it("writes only data-link", () => {
198+
const src = '<video id="v"></video><audio id="a"></audio>';
199+
expect(linkInSource(src, [{ id: "v" }, { id: "a" }], { syncOrigin: false })).toBe(
200+
'<video id="v" data-link="lk-1"></video><audio id="a" data-link="lk-1"></audio>',
201+
);
202+
});
203+
});
204+
195205
describe("setLinkInSource", () => {
196206
it("writes and removes data-link on every target", () => {
197207
const src = '<video id="v"></video><audio id="a"></audio>';
@@ -248,9 +258,12 @@ describe("predicates", () => {
248258
expect(findMergePair(v, [v, el("a", "audio", { src: "other.mp4" })])).toBeNull();
249259
});
250260

251-
it("links exactly one video and one audio of the same file and timing", () => {
261+
it("links exactly one unlinked video and one audio, whatever their timing or file", () => {
252262
expect(canLinkPair([el("v", "video"), el("a", "audio")])).toBe(true);
253-
expect(canLinkPair([el("v", "video"), el("a", "audio", { start: 3 })])).toBe(false);
263+
expect(canLinkPair([el("v", "video"), el("a", "audio", { start: 3, duration: 2 })])).toBe(true);
264+
expect(canLinkPair([el("v", "video"), el("a", "audio", { src: "other.mp3" })])).toBe(true);
265+
expect(canLinkPair([el("v", "video", { link: "x" }), el("a", "audio")])).toBe(false);
266+
expect(canLinkPair([el("v", "video"), el("a", "audio", { link: "y" })])).toBe(false);
254267
expect(canLinkPair([el("v", "video"), el("w", "video")])).toBe(false);
255268
expect(canLinkPair([el("v", "video", { link: "x" }), el("a", "audio", { link: "x" })])).toBe(
256269
false,
@@ -266,16 +279,16 @@ describe("predicates compare the whole asset path", () => {
266279
const v = el("v", "video", { muted: true, ...one });
267280
const a = el("a", "audio", two);
268281
expect(findMergePair(v, [v, a])).toBeNull();
269-
expect(canLinkPair([el("v", "video", one), el("a", "audio", two)])).toBe(false);
282+
expect(sharesSourceFile([el("v", "video", one), el("a", "audio", two)])).toBe(false);
270283
});
271284

272285
it("resolves each src against its own source file, and links only inside one", () => {
273286
const v = el("v", "video", { src: "../assets/talk.mp4", sourceFile: "scenes/a.html" });
274287
const a = el("a", "audio", { src: "./assets/talk.mp4", sourceFile: "index.html" });
275288
expect(canLinkPair([v, a])).toBe(false);
276-
expect(
277-
canLinkPair([v, { ...a, src: "../assets/./talk.mp4", sourceFile: "scenes/a.html" }]),
278-
).toBe(true);
289+
expect(sharesSourceFile([v, a])).toBe(false);
290+
const sameFileAudio = { ...a, src: "../assets/./talk.mp4", sourceFile: "scenes/a.html" };
291+
expect(sharesSourceFile([v, sameFileAudio])).toBe(true);
279292
});
280293

281294
it("does not take a same-id link from another source file as the merge partner", () => {

‎packages/studio/src/components/editor/mediaLinkEdits.ts‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { HF_AUDIO_AUTOMATION_ATTR } from "@hyperframes/core/audio-automation";
2-
import { MEDIA_LINK_ATTR, SYNC_ORIGIN_ATTR, mintLinkId } from "@hyperframes/core/media-link";
2+
import {
3+
MEDIA_LINK_ATTR,
4+
SYNC_ORIGIN_ATTR,
5+
mintLinkId,
6+
sourceZeroTime,
7+
} from "@hyperframes/core/media-link";
38
import { sameCompositionScope, type TimelineElement } from "../../player/store/timelineElement";
49
import { mediaAssetIdentity, sharesLinkGroup } from "../../player/components/audioClipLink";
510
import {
@@ -62,14 +67,17 @@ export function setLinkInSource(
6267
);
6368
}
6469

65-
/** Link the targets under one freshly minted id, which is also their sync origin. */
66-
export function linkInSource(source: string, targets: readonly PatchTarget[]): string {
70+
/** Link the targets under one fresh id, also their sync origin when they share a source file. */
71+
export function linkInSource(
72+
source: string,
73+
targets: readonly PatchTarget[],
74+
options: { syncOrigin: boolean } = { syncOrigin: true },
75+
): string {
6776
const linkId = mintLinkIdForSource(source);
68-
return targets.reduce(
69-
(html, target) =>
70-
applyOps(html, target, [dataOp(LINK_PROPERTY, linkId), dataOp(SYNC_ORIGIN_PROPERTY, linkId)]),
71-
source,
72-
);
77+
const ops = options.syncOrigin
78+
? [dataOp(LINK_PROPERTY, linkId), dataOp(SYNC_ORIGIN_PROPERTY, linkId)]
79+
: [dataOp(LINK_PROPERTY, linkId)];
80+
return targets.reduce((html, target) => applyOps(html, target, ops), source);
7381
}
7482

7583
/** Remove one element (open tag through its matching close tag) and its line's indentation. */
@@ -242,15 +250,28 @@ export function findMergePair<T extends TimedElement>(
242250
return video.muted === true ? { video, audio } : null;
243251
}
244252

245-
/** Exactly one video and one audio, same file, identical timing, not already linked together. */
253+
/** One video and one audio in one composition, neither linked: timing and file don't matter. */
246254
export function canLinkPair(selected: readonly TimedElement[]): boolean {
247255
if (selected.length !== 2) return false;
248256
const [a, b] = selected;
249-
if (!a || !b) return false;
257+
if (!a || !b || a.link || b.link) return false;
250258
const tags = new Set([tagOf(a), tagOf(b)]);
251-
if (!tags.has("video") || !tags.has("audio")) return false;
252-
if (sharesLinkGroup(a, b)) return false;
253-
return sameAssetInScope(a, b) && hasIdenticalTiming(a, b);
259+
return tags.has("video") && tags.has("audio") && sameCompositionScope(a, b);
260+
}
261+
262+
export function sharesSourceFile(selected: readonly TimedElement[]): boolean {
263+
const [a, b] = selected;
264+
return selected.length === 2 && !!a && !!b && sameAssetInScope(a, b);
265+
}
266+
267+
export function isPairInSync(a: TimedElement, b: TimedElement): boolean {
268+
const timing = (el: TimedElement) => ({
269+
start: el.start,
270+
mediaStart: el.playbackStart ?? 0,
271+
playbackRate: el.playbackRate ?? 1,
272+
});
273+
const [x, y] = [timing(a), timing(b)];
274+
return near(x.playbackRate, y.playbackRate) && near(sourceZeroTime(x), sourceZeroTime(y));
254275
}
255276

256277
type TrackedElement = Pick<TimelineElement, "tag" | "track" | "start" | "duration"> & {

0 commit comments

Comments
 (0)