@hyperframes/studio 0.9.61 → 0.9.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/player.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Player
3
- } from "./chunk-3KCEIESB.js";
3
+ } from "./chunk-ZUDVUI7K.js";
4
4
  import "./chunk-JTUKMELR.js";
5
5
  import "./chunk-OOGQ5KCC.js";
6
6
  import "./chunk-MLKGABMK.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperframes/studio",
3
- "version": "0.9.61",
3
+ "version": "0.9.63",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -64,12 +64,12 @@
64
64
  "dockview-react": "8.3.1",
65
65
  "gsap": "^3.13.0",
66
66
  "mediabunny": "^1.66.0",
67
- "@hyperframes/core": "0.9.61",
68
- "@hyperframes/parsers": "0.9.61",
69
- "@hyperframes/sdk": "0.9.61",
70
- "@hyperframes/shader-transitions": "0.9.61",
71
- "@hyperframes/studio-server": "0.9.61",
72
- "@hyperframes/player": "0.9.61"
67
+ "@hyperframes/parsers": "0.9.63",
68
+ "@hyperframes/sdk": "0.9.63",
69
+ "@hyperframes/player": "0.9.63",
70
+ "@hyperframes/core": "0.9.63",
71
+ "@hyperframes/shader-transitions": "0.9.63",
72
+ "@hyperframes/studio-server": "0.9.63"
73
73
  },
74
74
  "devDependencies": {
75
75
  "@tailwindcss/vite": "^4.3.3",
@@ -86,7 +86,7 @@
86
86
  "vite": "^6.4.5",
87
87
  "vitest": "^4.1.12",
88
88
  "zustand": "^5.0.0",
89
- "@hyperframes/producer": "0.9.61"
89
+ "@hyperframes/producer": "0.9.63"
90
90
  },
91
91
  "peerDependencies": {
92
92
  "react": "19",
@@ -19,6 +19,7 @@ import {
19
19
  } from "@hyperframes/core/audio-gain";
20
20
  import type { StereoLevel } from "@hyperframes/core/runtime/levelTap";
21
21
  import { usePlayerStore } from "../../player";
22
+ import { previewFrameOpaque, previewRequest } from "../../player/lib/previewChannel";
22
23
  import { clampNumber } from "../../utils/studioHelpers";
23
24
  import { useAudioMetersVisible } from "../../utils/audioMeterVisibility";
24
25
  import { EXPORT_PEAK_CEILING } from "../../utils/exportPeakCeiling";
@@ -135,6 +136,55 @@ function readHook(iframe: HTMLIFrameElement | null | undefined): AudioMeterHook
135
136
  }
136
137
  }
137
138
 
139
+ const isFields = (value: unknown): value is Record<string, unknown> =>
140
+ typeof value === "object" && value !== null;
141
+ const stereo = (value: unknown): StereoLevel | undefined =>
142
+ isFields(value) && Number.isFinite(value.l) && Number.isFinite(value.r)
143
+ ? { l: value.l as number, r: value.r as number }
144
+ : undefined;
145
+
146
+ function meterLevels(value: unknown): Levels | undefined {
147
+ if (!isFields(value) || !isFields(value.groups)) return undefined;
148
+ const master = stereo(value.master);
149
+ if (!master) return undefined;
150
+ const groups = Object.entries(value.groups).flatMap(([id, level]) => {
151
+ const read = stereo(level);
152
+ return read ? [[id, read] as const] : [];
153
+ });
154
+ return { master, groups: Object.fromEntries(groups) };
155
+ }
156
+
157
+ /** Read interval for a sealed preview's taps: about 30 a second, near the analyser's own window. */
158
+ const SEALED_METER_READ_MS = 33;
159
+
160
+ function createSealedMeter() {
161
+ let frame: HTMLIFrameElement | null = null;
162
+ let levels: Levels | undefined;
163
+ let busy = false;
164
+ let askedAt = -Infinity;
165
+ const stop = () => {
166
+ if (frame) void previewRequest(frame, "audio-meter", { on: false });
167
+ frame = null;
168
+ levels = undefined;
169
+ };
170
+ const read = (iframe: HTMLIFrameElement, now: number): Levels | undefined => {
171
+ if (iframe !== frame) stop();
172
+ frame = iframe;
173
+ if (busy || now - askedAt < SEALED_METER_READ_MS) return levels;
174
+ busy = true;
175
+ askedAt = now;
176
+ void previewRequest(iframe, "audio-meter", { on: true })
177
+ .then((reply) => {
178
+ if (frame === iframe) levels = meterLevels(reply?.levels);
179
+ })
180
+ .finally(() => {
181
+ busy = false;
182
+ });
183
+ return levels;
184
+ };
185
+ return { read, stop };
186
+ }
187
+
138
188
  /** 0 dB is bottom 100% plus 1px down, so the tick stays inside overflow-hidden.
139
189
  * A calc() bottom does not stick as an inline style on Windows happy-dom. */
140
190
  function paintPeak(el: HTMLElement | null, peak: number): void {
@@ -218,10 +268,20 @@ function useMeterLoop(
218
268
  let active: AudioMeterHook | null = null;
219
269
  let last = performance.now();
220
270
  const state = new Map<string | null, Pair>();
271
+ const sealed = createSealedMeter();
272
+ const readLevels = (now: number): Levels | undefined => {
273
+ const iframe = previewIframeRef?.current;
274
+ if (iframe && previewFrameOpaque(iframe)) {
275
+ active = followMeterHook(active, null);
276
+ return sealed.read(iframe, now);
277
+ }
278
+ sealed.stop();
279
+ active = followMeterHook(active, readHook(iframe));
280
+ return active?.read();
281
+ };
221
282
  const tick = (now: number) => {
222
283
  raf = requestAnimationFrame(tick);
223
- active = followMeterHook(active, readHook(previewIframeRef?.current));
224
- const levels = active?.read();
284
+ const levels = readLevels(now);
225
285
  const dt = now - last;
226
286
  last = now;
227
287
  evictGoneMeterState(state, new Set(stripsRef.current.map((s) => s.id)));
@@ -235,6 +295,7 @@ function useMeterLoop(
235
295
  raf = requestAnimationFrame(tick);
236
296
  return () => {
237
297
  cancelAnimationFrame(raf);
298
+ sealed.stop();
238
299
  try {
239
300
  active?.stop();
240
301
  } catch {
@@ -0,0 +1,254 @@
1
+ // @vitest-environment happy-dom
2
+
3
+ import { act, createElement } from "react";
4
+ import { createRoot, type Root } from "react-dom/client";
5
+ import { afterEach, beforeEach, expect, it, vi } from "vitest";
6
+ // The frame's real runtime agent, serving the opaque frame from this document.
7
+ import { installMarkedEditingAgent } from "../../../../core/src/runtime/editingAgent.test-helpers";
8
+ import { usePlayerStore, type TimelineElement } from "../../player";
9
+ import { ClipMenuAudioItems } from "../../player/components/clipMenuAudioItems";
10
+ import { usePreviewIframeStore } from "../../player/store/previewIframeStore";
11
+ import { TimelineEditProvider } from "../../contexts/TimelineEditContext";
12
+ import { createAudioGroupAndAssignMembers } from "../../hooks/timelineAudioGroupCreate";
13
+ import { mintGroupId } from "../editor/useFxCarveGrouping";
14
+ import { detachedFakeIframe } from "../../player/hooks/timelinePlayerTestHarness";
15
+ import { useAudioMetersVisible } from "../../utils/audioMeterVisibility";
16
+ import { useSetAudioGroupAttribute } from "../../hooks/timelineAudioGroupVolume";
17
+ import { previewDocumentCopy } from "../../player/lib/previewChannel";
18
+ import { AudioMeterStrip } from "./AudioMeterStrip";
19
+
20
+ (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
21
+
22
+ type MeterWindow = Window & { __hf?: Record<string, unknown> };
23
+
24
+ const closers: Array<() => void> = [];
25
+ const roots: Root[] = [];
26
+ let frames: FrameRequestCallback[] = [];
27
+ const tick = () => {
28
+ const run = frames;
29
+ frames = [];
30
+ act(() => run.forEach((cb) => cb(performance.now())));
31
+ };
32
+
33
+ beforeEach(() => {
34
+ frames = [];
35
+ vi.stubGlobal("requestAnimationFrame", (cb: FrameRequestCallback) => frames.push(cb));
36
+ vi.stubGlobal("cancelAnimationFrame", () => {});
37
+ useAudioMetersVisible.setState({ visible: true });
38
+ });
39
+ afterEach(() => {
40
+ act(() => roots.splice(0).forEach((root) => root.unmount()));
41
+ closers.splice(0).forEach((close) => close());
42
+ delete (window as MeterWindow).__hf;
43
+ vi.unstubAllGlobals();
44
+ usePlayerStore.getState().reset();
45
+ usePreviewIframeStore.setState({ iframe: null });
46
+ document.body.innerHTML = "";
47
+ });
48
+
49
+ /** A frame Studio cannot read, whose runtime is the real editing agent over this document. */
50
+ function opaqueFrame() {
51
+ const frame: Record<string, unknown> = {};
52
+ const iframe = detachedFakeIframe(frame);
53
+ Object.defineProperty(iframe, "contentDocument", { value: null });
54
+ Object.defineProperty(frame, "document", {
55
+ get: () => {
56
+ throw new DOMException("blocked", "SecurityError");
57
+ },
58
+ });
59
+ const parent = {
60
+ postMessage: (data: unknown, _origin: string, ports: MessagePort[]) =>
61
+ window.dispatchEvent(
62
+ new MessageEvent("message", { source: frame as unknown as Window, data, ports }),
63
+ ),
64
+ } as unknown as Window;
65
+ const agent = installMarkedEditingAgent(parent);
66
+ frame.postMessage = (data: unknown) =>
67
+ window.dispatchEvent(new MessageEvent("message", { source: parent, data }));
68
+ closers.push(() => agent.dispose());
69
+ return iframe;
70
+ }
71
+
72
+ const member = (domId: string): TimelineElement => ({
73
+ id: domId,
74
+ key: `compositions/voices.html#${domId}`,
75
+ domId,
76
+ tag: "audio",
77
+ start: 0,
78
+ duration: 5,
79
+ track: 0,
80
+ audioGroup: "vo",
81
+ audioGroupVolume: 1,
82
+ });
83
+
84
+ const VOICES = `<section data-composition-id="voices">
85
+ <audio id="voice-1" data-audio-group="vo"></audio>
86
+ <hf-audio-group id="vo" data-volume="1"></hf-audio-group>
87
+ </section>`;
88
+
89
+ function stubProjectFiles(files: Map<string, string>) {
90
+ vi.stubGlobal(
91
+ "fetch",
92
+ vi.fn(async (input: RequestInfo | URL) => {
93
+ const url = String(input);
94
+ const content = files.get(decodeURIComponent(url.slice(url.lastIndexOf("/") + 1)));
95
+ return new Response(JSON.stringify({ content }), {
96
+ status: content === undefined ? 404 : 200,
97
+ headers: { "Content-Type": "application/json" },
98
+ });
99
+ }),
100
+ );
101
+ }
102
+
103
+ it("meter moves + group volume sealed", async () => {
104
+ document.body.innerHTML = `<main data-composition-id="root">
105
+ <div id="voices-host" data-composition-id="voices-host" data-composition-file="compositions/voices.html">${VOICES}</div>
106
+ </main>`;
107
+ const groupEl = document.getElementById("vo")!;
108
+ const meter = {
109
+ start: vi.fn(),
110
+ stop: vi.fn(),
111
+ read: vi.fn(() => ({ master: { l: 1, r: 1 }, groups: { vo: { l: 1, r: 1 } } })),
112
+ };
113
+ (window as MeterWindow).__hf = { audioMeter: meter };
114
+ usePlayerStore.getState().setElements([member("voice-1")]);
115
+ const iframe = opaqueFrame();
116
+
117
+ const host = document.createElement("div");
118
+ document.body.append(host);
119
+ const root = createRoot(host);
120
+ roots.push(root);
121
+ act(() => root.render(createElement(AudioMeterStrip, { previewIframeRef: { current: iframe } })));
122
+ const masks = [...host.querySelectorAll<HTMLElement>("[data-testid=meter-mask]")];
123
+ expect(masks).toHaveLength(4);
124
+ await vi.waitFor(() => {
125
+ tick();
126
+ expect(masks.map((mask) => mask.style.height)).toEqual(["0%", "0%", "0%", "0%"]);
127
+ });
128
+ expect(meter.start).toHaveBeenCalled();
129
+ act(() => roots.splice(0).forEach((r) => r.unmount()));
130
+ await vi.waitFor(() => expect(meter.stop).toHaveBeenCalled());
131
+
132
+ stubProjectFiles(new Map([["compositions/voices.html", VOICES]]));
133
+ const writes = new Map<string, string>();
134
+ const held: { setter: ReturnType<typeof useSetAudioGroupAttribute> | null } = { setter: null };
135
+ function Probe() {
136
+ held.setter = useSetAudioGroupAttribute({
137
+ projectIdRef: { current: "project-1" },
138
+ activeCompPath: "index.html",
139
+ showToast: vi.fn(),
140
+ writeProjectFile: async (path, content) => void writes.set(path, content),
141
+ recordEdit: async () => {},
142
+ pendingTimelineEditPathRef: { current: new Set() },
143
+ previewIframeRef: { current: iframe },
144
+ });
145
+ return null;
146
+ }
147
+ const probeRoot = createRoot(document.createElement("div"));
148
+ roots.push(probeRoot);
149
+ act(() => probeRoot.render(createElement(Probe)));
150
+ const setter = held.setter!;
151
+
152
+ setter.setLive("vo", "data-volume", "0.5");
153
+ setter.setLive("vo", "data-volume", "0.4");
154
+ await vi.waitFor(() => expect(groupEl.getAttribute("data-volume")).toBe("0.4"));
155
+ expect(usePlayerStore.getState().elements[0]?.audioGroupVolume).toBeCloseTo(0.4, 6);
156
+
157
+ await expect(setter.setQuiet("vo", "data-volume", "0.25", "Set volume")).resolves.toEqual({
158
+ status: "saved",
159
+ });
160
+ expect([...writes.keys()]).toEqual(["compositions/voices.html"]);
161
+ expect(writes.get("compositions/voices.html")).toContain(
162
+ '<hf-audio-group id="vo" data-volume="0.25">',
163
+ );
164
+ await vi.waitFor(() => expect(groupEl.getAttribute("data-volume")).toBe("0.25"));
165
+ });
166
+
167
+ it("groups clips in a sealed preview, and unwinds the preview when the save fails", async () => {
168
+ document.body.innerHTML = `<main data-composition-id="root">
169
+ <audio id="voice-1" data-audio-group="old"></audio><audio id="voice-2"></audio>
170
+ </main>`;
171
+ const iframe = opaqueFrame();
172
+ const files = new Map([["index.html", document.body.innerHTML]]);
173
+ stubProjectFiles(files);
174
+ const elements = ["voice-1", "voice-2"].map((domId) => ({
175
+ ...member(domId),
176
+ key: `index.html#${domId}`,
177
+ audioGroup: undefined,
178
+ }));
179
+ const input = {
180
+ projectId: "project-1",
181
+ activeCompPath: "index.html",
182
+ elements,
183
+ groupId: "vo",
184
+ groupLabel: "Voice",
185
+ previewIframe: iframe,
186
+ recordEdit: async () => {},
187
+ pendingTimelineEditPathRef: { current: new Set<string>() },
188
+ };
189
+ const groupOf = (id: string) => document.getElementById(id)!.getAttribute("data-audio-group");
190
+
191
+ await expect(
192
+ createAudioGroupAndAssignMembers({
193
+ ...input,
194
+ writeProjectFile: async () => {
195
+ expect([groupOf("voice-1"), groupOf("voice-2")]).toEqual(["vo", "vo"]);
196
+ expect(document.querySelector("hf-audio-group#vo")?.getAttribute("data-label")).toBe(
197
+ "Voice",
198
+ );
199
+ throw new Error("disk full");
200
+ },
201
+ }),
202
+ ).rejects.toThrow("disk full");
203
+ expect([groupOf("voice-1"), groupOf("voice-2")]).toEqual(["old", null]);
204
+ expect(document.getElementById("vo")).toBeNull();
205
+
206
+ await createAudioGroupAndAssignMembers({ ...input, writeProjectFile: async () => {} });
207
+ expect([groupOf("voice-1"), groupOf("voice-2")]).toEqual(["vo", "vo"]);
208
+ expect(document.querySelector("hf-audio-group#vo")).not.toBeNull();
209
+ });
210
+
211
+ it("reads a sealed preview's groups and ids off an inert copy", async () => {
212
+ document.body.innerHTML = `<audio id="voiceover"></audio><audio id="music" src="bed.wav"></audio>`;
213
+ const copy = await previewDocumentCopy(opaqueFrame());
214
+ expect(copy).not.toBeNull();
215
+ expect(copy).not.toBe(document);
216
+ expect(mintGroupId(copy!)).toBe("voiceover-2");
217
+ expect(copy!.baseURI).toBe(document.baseURI);
218
+ });
219
+
220
+ it("offers Duck under voice for a bed in a sealed preview", async () => {
221
+ document.body.innerHTML = `<main data-composition-id="root">
222
+ <audio id="music" src="bed.wav" data-start="0" data-duration="10"></audio>
223
+ <audio id="voiceover" src="vo.wav" data-start="1" data-duration="2"></audio>
224
+ </main>`;
225
+ const iframe = opaqueFrame();
226
+ usePreviewIframeStore.setState({ iframe });
227
+ usePlayerStore.getState().beginTimelineSession("project-1");
228
+ const bed: TimelineElement = {
229
+ id: "music",
230
+ domId: "music",
231
+ tag: "audio",
232
+ start: 0,
233
+ duration: 10,
234
+ track: 0,
235
+ };
236
+ const host = document.createElement("div");
237
+ document.body.append(host);
238
+ const root = createRoot(host);
239
+ roots.push(root);
240
+ const value = {
241
+ onSetElementAttributeQuiet: vi.fn(async () => ({ status: "saved" as const })),
242
+ onNotice: vi.fn(),
243
+ };
244
+ act(() =>
245
+ root.render(
246
+ createElement(
247
+ TimelineEditProvider,
248
+ { value },
249
+ createElement(ClipMenuAudioItems, { part: "duck", element: bed, onClose: () => {} }),
250
+ ),
251
+ ),
252
+ );
253
+ await vi.waitFor(() => expect(host.textContent).toContain("Duck under voice"));
254
+ });
@@ -1,4 +1,10 @@
1
1
  import { describe, expect, it, vi } from "vitest";
2
+ import {
3
+ beginStudioPendingEdit,
4
+ paintBackNewestStudioPendingEdit,
5
+ setStudioPendingEditsSettled,
6
+ trackStudioPendingEdit,
7
+ } from "../../utils/studioPendingEdits";
2
8
  import { runLaneZGesture, runZLaneGesture } from "./zLaneGesture";
3
9
 
4
10
  const durable = { durable: true, allMatched: true, changed: true };
@@ -174,3 +180,64 @@ describe("runZLaneGesture", () => {
174
180
  expect(next).toBe(true);
175
181
  });
176
182
  });
183
+
184
+ // fails without the fix: the z write settled before the lane mirror began, which closed the gesture's undo step.
185
+ describe("one undo step per gesture", () => {
186
+ it.each([
187
+ [
188
+ "z then lanes",
189
+ (first: () => Promise<void>, second: () => Promise<boolean>) =>
190
+ runZLaneGesture({ commitZ: async () => void (await first()), mirror: second }),
191
+ ],
192
+ [
193
+ "lanes then z",
194
+ (first: () => Promise<void>, second: () => Promise<boolean>) =>
195
+ runLaneZGesture({
196
+ commitLane: async () => (await first(), true),
197
+ commitZ: async () => void (await second()),
198
+ }),
199
+ ],
200
+ ])(
201
+ "a layer reorder (%s) is one undo step: Studio's edits stay pending between its two writes",
202
+ async (_, gesture) => {
203
+ const settled = vi.fn();
204
+ setStudioPendingEditsSettled(settled);
205
+ const secondWrite = deferred<boolean>();
206
+ try {
207
+ const done = gesture(
208
+ () => trackStudioPendingEdit(Promise.resolve()) as Promise<void>,
209
+ () => trackStudioPendingEdit(secondWrite.promise) as Promise<boolean>,
210
+ );
211
+ await new Promise((resolve) => setTimeout(resolve, 0));
212
+ expect(settled).not.toHaveBeenCalled();
213
+ secondWrite.resolve(true);
214
+ await done;
215
+ await new Promise((resolve) => setTimeout(resolve, 0));
216
+ expect(settled).toHaveBeenCalledTimes(1);
217
+ } finally {
218
+ secondWrite.resolve(true);
219
+ setStudioPendingEditsSettled(null);
220
+ }
221
+ },
222
+ );
223
+
224
+ it("Cmd+Z during a lane drag's save still paints that drag back at once", async () => {
225
+ const revert = vi.fn(() => () => {});
226
+ const save = deferred<void>();
227
+ const done = runLaneZGesture({
228
+ commitLane: async () => {
229
+ beginStudioPendingEdit(revert).settle(save.promise);
230
+ await save.promise;
231
+ return true;
232
+ },
233
+ commitZ: async () => {},
234
+ });
235
+ try {
236
+ expect(paintBackNewestStudioPendingEdit()).not.toBeNull();
237
+ expect(revert).toHaveBeenCalledTimes(1);
238
+ } finally {
239
+ save.resolve();
240
+ await done;
241
+ }
242
+ });
243
+ });
@@ -1,4 +1,5 @@
1
1
  import type { DomEditPatchBatchesResult } from "../../hooks/domEditCommitTypes";
2
+ import { trackStudioPendingEdit } from "../../utils/studioPendingEdits";
2
3
 
3
4
  /**
4
5
  * Run one COMPLETE z→lane gesture — the z-index persist followed by its
@@ -27,6 +28,8 @@ let gestureTail: Promise<void> | null = null;
27
28
  /** The single ordering owner for every gesture that crosses z/lane persistence. */
28
29
  export function serializeZLaneGesture<Result>(run: () => Promise<Result>): Promise<Result> {
29
30
  const previous = gestureTail;
31
+ let release!: () => void;
32
+ trackStudioPendingEdit(new Promise<void>((resolve) => (release = resolve)));
30
33
  // Preserve synchronous optimistic updates for the first gesture. A queued
31
34
  // gesture starts only after the preceding gesture fully settled.
32
35
  let gesture: Promise<Result>;
@@ -39,6 +42,7 @@ export function serializeZLaneGesture<Result>(run: () => Promise<Result>): Promi
39
42
  gesture = Promise.reject(error);
40
43
  }
41
44
  }
45
+ void gesture.then(release, release);
42
46
  const tail = gesture.then(
43
47
  () => undefined,
44
48
  () => undefined,
@@ -12,6 +12,8 @@ import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
12
12
  import { HF_AUDIO_GROUP_ATTR, HF_AUDIO_GROUP_TAG } from "@hyperframes/core/audio-groups";
13
13
  import { runtimeAudioId } from "../player/lib/timelineElementHelpers";
14
14
  import { invalidateGroupInfoCache } from "../player/lib/timelineGroupInfo";
15
+ import { previewFrameOpaque, previewRequest } from "../player/lib/previewChannel";
16
+ import { findOpaqueTimelineElement } from "../components/editor/previewSelection";
15
17
  import { readTagSnippetByTarget, type PatchOperation } from "../utils/sourcePatcher";
16
18
  import {
17
19
  findTimelineElementInIframe,
@@ -146,6 +148,55 @@ function patchLiveGroupElement(
146
148
  return true;
147
149
  }
148
150
 
151
+ /** The members' and the group element's live writes, as `patchLiveAudioGroupState` and
152
+ * `patchLiveGroupElement` make them; returns their unwind. */
153
+ function patchLiveGroup(
154
+ iframe: HTMLIFrameElement | null,
155
+ elements: readonly TimelineElement[],
156
+ groupId: string,
157
+ groupLabel: string | undefined,
158
+ activeCompPath: string | null,
159
+ ): () => void {
160
+ const priorGroups = captureAudioGroupState(iframe, elements, activeCompPath);
161
+ patchLiveAudioGroupState(iframe, elements, groupId, activeCompPath);
162
+ const createdLiveGroupElement = patchLiveGroupElement(iframe, groupId, groupLabel);
163
+ return () => {
164
+ patchLiveAudioGroupState(iframe, elements, null, activeCompPath, priorGroups);
165
+ if (createdLiveGroupElement) {
166
+ iframe?.contentDocument?.getElementById(groupId)?.remove();
167
+ }
168
+ };
169
+ }
170
+
171
+ /** The same writes in a sealed preview, run by its runtime on the members it resolves. */
172
+ async function patchSealedGroup(
173
+ iframe: HTMLIFrameElement,
174
+ elements: readonly TimelineElement[],
175
+ groupId: string,
176
+ groupLabel: string | undefined,
177
+ activeCompPath: string | null,
178
+ ): Promise<() => Promise<void>> {
179
+ const lookup = { activeCompositionPath: activeCompPath, isMasterView: true };
180
+ const found = await Promise.all(
181
+ elements.map((element) => findOpaqueTimelineElement(iframe, element, lookup)),
182
+ );
183
+ const handles = found.flatMap((element) => (element ? [element.handle] : []));
184
+ const assigned = await previewRequest(iframe, "assign-audio-group", {
185
+ members: handles.map((handle) => ({ handle, group: groupId })),
186
+ element: { id: groupId, label: groupLabel ?? null, present: true },
187
+ });
188
+ return async () => {
189
+ if (!assigned) return;
190
+ await previewRequest(iframe, "assign-audio-group", {
191
+ members: handles.map((handle, i) => {
192
+ const prior = assigned.previous[i];
193
+ return { handle, group: typeof prior === "string" && prior ? prior : null };
194
+ }),
195
+ element: assigned.changed ? { id: groupId, label: null, present: false } : null,
196
+ });
197
+ };
198
+ }
199
+
149
200
  interface CreateAudioGroupAndAssignMembersInput {
150
201
  projectId: string;
151
202
  activeCompPath: string | null;
@@ -188,9 +239,10 @@ export async function createAudioGroupAndAssignMembers({
188
239
  throw new Error(`Invalid audio group id ${JSON.stringify(groupId)}`);
189
240
  }
190
241
 
191
- const priorGroups = captureAudioGroupState(previewIframe, elements, activeCompPath);
192
- patchLiveAudioGroupState(previewIframe, elements, groupId, activeCompPath);
193
- const createdLiveGroupElement = patchLiveGroupElement(previewIframe, groupId, groupLabel);
242
+ const unwindLive =
243
+ previewIframe && previewFrameOpaque(previewIframe)
244
+ ? await patchSealedGroup(previewIframe, elements, groupId, groupLabel, activeCompPath)
245
+ : patchLiveGroup(previewIframe, elements, groupId, groupLabel, activeCompPath);
194
246
  reseekPreviewRuntime(previewIframe);
195
247
 
196
248
  const groupOperation: PatchOperation = {
@@ -220,6 +272,7 @@ export async function createAudioGroupAndAssignMembers({
220
272
  label: groupLabel
221
273
  ? `Group ${elements.length} clips as ${groupLabel}`
222
274
  : `Group ${elements.length} voice clips`,
275
+ // fallow-ignore-next-line code-duplication
223
276
  files,
224
277
  readFile: (path) => readFileContent(projectId, path),
225
278
  writeFile: writeProjectFile,
@@ -233,10 +286,7 @@ export async function createAudioGroupAndAssignMembers({
233
286
  // Mirrors setElementsHidden's failure path: the optimistic live patch
234
287
  // already ran, so a save failure has to be unwound or the preview shows a
235
288
  // grouping that never made it to disk.
236
- patchLiveAudioGroupState(previewIframe, elements, null, activeCompPath, priorGroups);
237
- if (createdLiveGroupElement) {
238
- previewIframe?.contentDocument?.getElementById(groupId)?.remove();
239
- }
289
+ await unwindLive();
240
290
  reseekPreviewRuntime(previewIframe);
241
291
  throw error;
242
292
  }