Skip to content

Commit 7caf4b8

Browse files
authored
feat(studio): drag automation segments (#3465)
* feat(studio): drag automation segments * fix(studio): clear clip selection for group effects * fix(studio): replace clip selection with audio bus * fix(studio): make audio bus selection authoritative
1 parent 21dc4ae commit 7caf4b8

12 files changed

Lines changed: 703 additions & 74 deletions

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { SelectElementOptions, TimelineElement } from "../player";
2+
import { HF_AUDIO_GROUP_TAG } from "@hyperframes/core/audio-groups";
23
import { findMatchingTimelineElementId, findTimelineIdByAncestor } from "../utils/studioHelpers";
34
import type { DomEditSelection } from "../components/editor/domEditing";
45
import { logSelect } from "../utils/selectDebug";
@@ -60,9 +61,17 @@ export function announceTimelineSelection(
6061
anchor,
6162
anchorPublished: anchor != null && publishedMembers.has(anchor),
6263
});
63-
// A canvas target can be editable without owning a timeline row. Preserve that
64-
// canvas-only selection when the timeline has nothing truthful to represent.
65-
if (!timelineAnchor) return;
64+
// A canvas target can be editable without owning a timeline row. Most such
65+
// targets live inside a clip and must not erase its timeline context. A mixer
66+
// bus is different: it is itself the editing target, so its title replaces any
67+
// selected clips even though the bus has no clip row of its own.
68+
if (!timelineAnchor) {
69+
if (primary.tagName.toLowerCase() === HF_AUDIO_GROUP_TAG) {
70+
setTimelineSelectionSet(new Set());
71+
setSelectedTimelineElementId(null);
72+
}
73+
return;
74+
}
6675
// A late async primary that already belongs to the live set must preserve the
6776
// group. A fresh single click does not belong to it, so publish the singleton
6877
// first; otherwise `preserveSet` clears the set and sync wipes the canvas.

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,121 @@ describe("useDomSelection — Variables tab preservation", () => {
130130
});
131131
});
132132

133+
describe("useDomSelection — canvas-only targets replace timeline clips", () => {
134+
beforeEach(() => {
135+
deferreds.clear();
136+
usePlayerStore.getState().clearSelection();
137+
});
138+
afterEach(() => {
139+
deferreds.clear();
140+
usePlayerStore.getState().clearSelection();
141+
});
142+
143+
it("deselects every clip when an audio bus is selected", () => {
144+
const store = usePlayerStore.getState();
145+
store.setSelectedElementId("voice-1");
146+
store.setSelectedElementIds(new Set(["voice-1", "voice-2"]));
147+
148+
const bus = document.createElement("hf-audio-group");
149+
bus.id = "voiceover";
150+
const harness = renderHarness({
151+
rightPanelTab: "design",
152+
setRightPanelTab: vi.fn(),
153+
iframe: null,
154+
timelineElements: [
155+
{ id: "voice-1", tag: "audio", start: 0, duration: 1, track: 0 },
156+
{ id: "voice-2", tag: "audio", start: 1, duration: 1, track: 1 },
157+
],
158+
setSelectedTimelineElementId: usePlayerStore.getState().setSelectedElementId,
159+
setTimelineSelectionSet: usePlayerStore.getState().setSelectedElementIds,
160+
});
161+
162+
act(() => harness.current().applyDomSelection(makeSelection("Voiceover", bus)));
163+
164+
expect(harness.current().domEditSelection?.id).toBe("voiceover");
165+
expect(usePlayerStore.getState().selectedElementId).toBeNull();
166+
expect(usePlayerStore.getState().selectedElementIds).toEqual(new Set());
167+
harness.cleanup();
168+
});
169+
170+
it("lets a bus supersede a clip selection that is still resolving", async () => {
171+
const iframe = document.createElement("iframe");
172+
document.body.append(iframe);
173+
const doc = iframe.contentDocument!;
174+
const clipNode = doc.createElement("audio");
175+
clipNode.id = "voice-1";
176+
const busNode = doc.createElement("hf-audio-group");
177+
busNode.id = "voiceover";
178+
doc.body.append(clipNode, busNode);
179+
180+
const clip: TimelineElement = {
181+
id: "voice-1",
182+
domId: "voice-1",
183+
tag: "audio",
184+
start: 0,
185+
duration: 1,
186+
track: 0,
187+
};
188+
const bus: TimelineElement = {
189+
id: "voiceover",
190+
domId: "voiceover",
191+
tag: "audio",
192+
start: 0,
193+
duration: 10,
194+
track: -0.5,
195+
};
196+
const harness = renderHarness({
197+
rightPanelTab: "design",
198+
setRightPanelTab: vi.fn(),
199+
iframe,
200+
// The bus is a synthetic row target, not a clip in the store.
201+
timelineElements: [clip],
202+
setSelectedTimelineElementId: usePlayerStore.getState().setSelectedElementId,
203+
setTimelineSelectionSet: usePlayerStore.getState().setSelectedElementIds,
204+
});
205+
206+
let pendingClip = Promise.resolve();
207+
let pendingBus = Promise.resolve();
208+
act(() => {
209+
pendingClip = harness.current().handleTimelineElementSelect(clip);
210+
pendingBus = harness.current().handleTimelineElementSelect(bus);
211+
});
212+
await act(async () => {
213+
deferreds.get("voiceover")?.resolve();
214+
await pendingBus;
215+
deferreds.get("voice-1")?.resolve();
216+
await pendingClip;
217+
});
218+
219+
expect(harness.current().domEditSelection?.id).toBe("voiceover");
220+
expect(usePlayerStore.getState().selectedElementId).toBeNull();
221+
expect(usePlayerStore.getState().selectedElementIds).toEqual(new Set());
222+
harness.cleanup();
223+
iframe.remove();
224+
});
225+
226+
it("preserves clip context for a non-bus canvas-only selection", () => {
227+
const store = usePlayerStore.getState();
228+
store.setSelectedElementId("voice-1");
229+
const decoration = document.createElement("div");
230+
decoration.id = "decoration";
231+
const harness = renderHarness({
232+
rightPanelTab: "design",
233+
setRightPanelTab: vi.fn(),
234+
iframe: null,
235+
timelineElements: [{ id: "voice-1", tag: "audio", start: 0, duration: 1, track: 0 }],
236+
setSelectedTimelineElementId: usePlayerStore.getState().setSelectedElementId,
237+
setTimelineSelectionSet: usePlayerStore.getState().setSelectedElementIds,
238+
});
239+
240+
act(() => harness.current().applyDomSelection(makeSelection("Decoration", decoration)));
241+
242+
expect(usePlayerStore.getState().selectedElementId).toBe("voice-1");
243+
expect(usePlayerStore.getState().selectedElementIds).toEqual(new Set(["voice-1"]));
244+
harness.cleanup();
245+
});
246+
});
247+
133248
describe("useDomSelection — timeline-select race guard", () => {
134249
beforeEach(() => deferreds.clear());
135250
afterEach(() => deferreds.clear());
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/** The base automation envelope plus the heavier segment grab affordance. */
2+
3+
import type { AutomationRange, HfAutomationLane } from "@hyperframes/core/audio-automation";
4+
import { envelopeSegmentPath } from "./automationLaneGeometry";
5+
6+
interface AutomationEnvelopePathsProps {
7+
path: string;
8+
lane: HfAutomationLane;
9+
range: AutomationRange;
10+
accentColor: string;
11+
activeSegment: number | null;
12+
xOf(t: number): number;
13+
yOf(v: number): number;
14+
}
15+
16+
export function AutomationEnvelopePaths({
17+
path,
18+
lane,
19+
range,
20+
accentColor,
21+
activeSegment,
22+
xOf,
23+
yOf,
24+
}: AutomationEnvelopePathsProps) {
25+
const activePath =
26+
activeSegment === null
27+
? null
28+
: envelopeSegmentPath({ lane, range, index: activeSegment, xOf, yOf });
29+
30+
return (
31+
<>
32+
<path
33+
data-automation-envelope=""
34+
d={path}
35+
fill="none"
36+
stroke={accentColor}
37+
strokeWidth={1.5}
38+
opacity={lane.points.length === 0 ? 0.35 : 0.95}
39+
/>
40+
{activePath ? (
41+
<path
42+
data-automation-segment-active={activeSegment ?? undefined}
43+
d={activePath}
44+
fill="none"
45+
stroke={accentColor}
46+
strokeWidth={3}
47+
strokeLinecap="round"
48+
opacity={1}
49+
pointerEvents="none"
50+
/>
51+
) : null}
52+
</>
53+
);
54+
}

‎packages/studio/src/player/components/TimelineAutomationLane.test.tsx‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { MAX_AUDIO_GAIN } from "@hyperframes/core/audio-gain";
1010
import {
1111
normalizeAutomation,
1212
resolveAutomationRange,
13+
sampleAutomationLane,
1314
VOLUME_RANGE,
1415
type HfAutomation,
1516
} from "@hyperframes/core/audio-automation";
@@ -668,6 +669,118 @@ describe("TimelineAutomationLane point visibility", () => {
668669
});
669670
});
670671

672+
describe("TimelineAutomationLane segment drag", () => {
673+
const four: HfAutomation = {
674+
version: 1,
675+
lanes: [
676+
{
677+
target: "volume",
678+
points: [
679+
{ t: 0, v: 1 },
680+
{ t: 1, v: 0.8 },
681+
{ t: 2, v: 0.6 },
682+
{ t: 3.5, v: 0.2 },
683+
],
684+
},
685+
],
686+
};
687+
688+
const previewedPoints = (props: { onPreview: ReturnType<typeof vi.fn> }) =>
689+
props.onPreview.mock.calls.at(-1)?.[0].lanes[0].points as {
690+
t: number;
691+
v: number;
692+
viaX?: number;
693+
viaY?: number;
694+
}[];
695+
696+
it("thickens the segment and offers a grab cursor only within its hit proximity", () => {
697+
const { container, svg } = mount(ramp);
698+
const envelope = container.querySelector<SVGPathElement>("[data-automation-envelope]");
699+
expect(envelope?.getAttribute("stroke-width")).toBe("1.5");
700+
701+
fire(svg, "pointermove", at(2, 0.5));
702+
const active = container.querySelector<SVGPathElement>("[data-automation-segment-active]");
703+
expect(active).not.toBeNull();
704+
expect(active?.getAttribute("stroke-width")).toBe("3");
705+
expect(svg.style.cursor).toBe("grab");
706+
707+
// Same time span, but far enough above the drawn ramp to be background.
708+
fire(svg, "pointermove", at(2, 0.9));
709+
expect(container.querySelector("[data-automation-segment-active]")).toBeNull();
710+
expect(svg.style.cursor).toBe("crosshair");
711+
});
712+
713+
it("moves both segment endpoints by the same time and value delta", () => {
714+
const { svg, props } = mount(four);
715+
// Midpoint of the segment from (1, .8) to (2, .6).
716+
fire(svg, "pointerdown", { ...at(1.5, 0.7), buttons: 1 });
717+
fire(svg, "pointermove", { ...at(2, 0.5), buttons: 1 });
718+
719+
const points = previewedPoints(props);
720+
expect(points[0]).toEqual({ t: 0, v: 1 });
721+
expect(points[1]!.t).toBeCloseTo(1.5, 2);
722+
expect(points[2]!.t).toBeCloseTo(2.5, 2);
723+
expect(points[1]!.v).toBeCloseTo(0.6, 2);
724+
expect(points[2]!.v).toBeCloseTo(0.4, 2);
725+
expect(points[3]).toEqual({ t: 3.5, v: 0.2 });
726+
});
727+
728+
it("treats a press outside the line's proximity as a background range drag", () => {
729+
const onRangeSelect = vi.fn();
730+
const { svg, props } = mount(four, { onRangeSelect });
731+
fire(svg, "pointerdown", { ...at(1.5, 0.95), buttons: 1 });
732+
fire(svg, "pointermove", { ...at(2.5, 0.95), buttons: 1 });
733+
expect(onRangeSelect).toHaveBeenCalled();
734+
expect(props.onPreview).not.toHaveBeenCalled();
735+
});
736+
737+
it("preserves the segment's curve while translating its endpoints", () => {
738+
const curved: HfAutomation = {
739+
version: 1,
740+
lanes: [
741+
{
742+
target: "volume",
743+
points: [
744+
{ t: 0, v: 1 },
745+
{ t: 1, v: 0.8, viaX: 0.4, viaY: 0.7 },
746+
{ t: 2, v: 0.6 },
747+
{ t: 3.5, v: 0.2 },
748+
],
749+
},
750+
],
751+
};
752+
const { svg, props } = mount(curved);
753+
const lineValue = sampleAutomationLane(curved.lanes[0]!, 1.7, "linear");
754+
fire(svg, "pointerdown", { ...at(1.7, lineValue), buttons: 1 });
755+
fire(svg, "pointermove", { ...at(2.1, lineValue - 0.1), buttons: 1 });
756+
const points = previewedPoints(props);
757+
expect(points[1]?.viaX).toBe(0.4);
758+
expect(points[1]?.viaY).toBe(0.7);
759+
});
760+
761+
it("stops both endpoints together before the next breakpoint", () => {
762+
const { svg, props } = mount(four);
763+
fire(svg, "pointerdown", { ...at(1.5, 0.7), buttons: 1 });
764+
fire(svg, "pointermove", { ...at(4, 0.7), buttons: 1, altKey: true });
765+
const points = previewedPoints(props);
766+
expect(points[2]!.t).toBeLessThan(points[3]!.t);
767+
expect(points[3]!.t - points[2]!.t).toBeCloseTo(0.001, 4);
768+
expect(points[2]!.t - points[1]!.t).toBeCloseTo(1, 4);
769+
});
770+
771+
it("previews every move and persists the segment once on release", () => {
772+
const { svg, props } = mount(four);
773+
fire(svg, "pointerdown", { ...at(1.5, 0.7), buttons: 1 });
774+
for (const t of [1.7, 1.9, 2.1]) {
775+
fire(svg, "pointermove", { ...at(t, 0.6), buttons: 1 });
776+
}
777+
expect(props.onPreview).toHaveBeenCalledTimes(3);
778+
expect(props.onCommit).not.toHaveBeenCalled();
779+
fire(svg, "pointerup", { ...at(2.1, 0.6), buttons: 0 });
780+
expect(props.onCommit).toHaveBeenCalledTimes(1);
781+
});
782+
});
783+
671784
/** `mount`, plus the re-render a real store update causes — the persisted
672785
* automation and the new selection coming back down as props. */
673786
const mountRerenderable = (automation: HfAutomation, over: Record<string, unknown> = {}) => {

0 commit comments

Comments
 (0)