fix(studio): copy, duplicate and audio lane updates work in a sealed preview (#8192)

* fix(studio): copy, duplicate and audio lane updates work in a sealed preview

* fix(studio): a sealed copy keeps its starting selection, and a stale lane read is dropped

* fix(studio): a saved lane value outlasts an older sealed read, and cut deletes in its own project
This commit is contained in:
Miguel Ángel
2026-11-18 17:15:05 +00:00
committed by GitHub
parent e5566b3dc9
commit 30d4cd26c6
9 changed files with 409 additions and 84 deletions
@@ -14,6 +14,8 @@ import type { DomEditSelection } from "../components/editor/domEditing";
import type { PersistDomEditOperations } from "./domEditCommitTypes";
import { commitDomStyles } from "./domStyleCommit";
import { useDomEditAttributeCommits } from "./useDomEditAttributeCommits";
import { usePlayerStore } from "../player/store/playerStore";
import { syncStoredAutomationFromFrame } from "../player/lib/automationStoreSync";
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -158,6 +160,33 @@ describe("DOM edit commits in a sealed preview", () => {
expect(card.getAttribute("title")).toBe("Card");
});
it("records a sealed automation save in the timeline's store", async () => {
const { iframe, selection } = await sealedCard(`<audio id="card" data-volume="1"></audio>`);
const clip = { id: "card", key: "card", tag: "audio", start: 0, duration: 4, track: 1 };
usePlayerStore.setState({ elements: [{ ...clip, domId: "card", volume: 1 }] });
const commits = attributeCommits(
iframe,
selection,
vi.fn(async () => undefined),
);
await act(() => commits.handleDomAttributeQuietCommit("volume", "0.25"));
await vi.waitFor(() => expect(usePlayerStore.getState().elements[0]?.volume).toBe(0.25));
usePlayerStore.getState().reset();
});
it("drops a sealed lane read when the frame loads before it answers", async () => {
const { iframe } = await sealedCard(`<audio id="card" data-volume="0.25"></audio>`);
const clip = { id: "card", key: "card", tag: "audio", start: 0, duration: 4, track: 1 };
usePlayerStore.setState({ elements: [{ ...clip, domId: "card", volume: 1 }] });
syncStoredAutomationFromFrame(iframe);
iframe.dispatchEvent(new Event("load"));
await settle();
expect(usePlayerStore.getState().elements[0]?.volume).toBe(1);
syncStoredAutomationFromFrame(iframe);
await vi.waitFor(() => expect(usePlayerStore.getState().elements[0]?.volume).toBe(0.25));
usePlayerStore.getState().reset();
});
it("never puts back a value over a newer edit that saved", async () => {
const { card, iframe, selection } = await sealedCard(
`<div id="card" title="original">Hi</div>`,
@@ -117,7 +117,7 @@ const GROUP_ATTR_TO_MIRROR: Record<
*
* Invalidating the parse cache is necessary but not sufficient — it only
* ensures the NEXT parse is honest, and a live attribute patch does not cause
* one. Same reason `commitDataAttribute` carries `syncStoredAutomationFromPreview`.
* one. Same reason `commitDataAttribute` carries `syncStoredAutomationFromFrame`.
*/
function syncStoredGroupAttribute(groupId: string, attr: string, value: string | null): void {
const toPatch = GROUP_ATTR_TO_MIRROR[attr];
@@ -159,7 +159,18 @@ export function findTimelineElementInIframe(
): Element | null {
try {
const doc = iframe?.contentDocument;
if (!doc) return null;
return doc ? findTimelineElementInDocument(doc, element, activeCompositionPath) : null;
} catch {
return null;
}
}
export function findTimelineElementInDocument(
doc: Document,
element: TimelineElement,
activeCompositionPath: string | null,
): Element | null {
try {
if (element.kind === "composition" && element.compositionSrc) {
return findElementForTimelineElement(doc, element, {
activeCompositionPath,
@@ -0,0 +1,151 @@
// @vitest-environment happy-dom
import React, { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { ensureHfIds } from "@hyperframes/parsers/hf-ids";
import { afterEach, expect, it, vi } from "vitest";
import { useClipboard } from "./useClipboard";
import { usePlayerStore, type TimelineElement } from "../player";
import type { DomEditSelection } from "../components/editor/domEditing";
import { sealedPreviewFrame } from "../player/lib/sealedPreviewFrame.test-helpers";
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const SAVED = `<!doctype html><html><body>
<div data-composition-id="main" data-start="0" data-duration="10">
<h1 id="title" class="clip" data-start="2" data-duration="3" data-track-index="0">Title</h1>
<h2 id="other" class="clip" data-start="6" data-duration="2" data-track-index="1">Other</h2>
</div></body></html>`;
let root: Root | null = null;
afterEach(() => {
act(() => root?.unmount());
root = null;
vi.unstubAllGlobals();
usePlayerStore.getState().reset();
document.body.innerHTML = "";
});
/** A sealed preview of SAVED, with the runtime's hide on the clip, and the saved file served. */
function sealedClipboard(domSelection: DomEditSelection | null) {
const preview = new DOMParser().parseFromString(ensureHfIds(SAVED), "text/html");
document.body.innerHTML = preview.body.innerHTML;
document.getElementById("title")!.style.cssText = "visibility: hidden; display: none";
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response(JSON.stringify({ content: SAVED }))),
);
const { iframe } = sealedPreviewFrame();
const writes: string[] = [];
const deleted: string[] = [];
const view = { projectId: "p" };
let api: ReturnType<typeof useClipboard> | null = null;
function Harness() {
api = useClipboard({
projectId: view.projectId,
activeCompPath: "index.html",
domEditSelectionRef: { current: domSelection },
showToast: () => {},
writeProjectFile: async (_path: string, content: string) => void writes.push(content),
recordEdit: async () => {},
reloadPreview: () => {},
handleTimelineElementsDelete: async (elements) =>
void deleted.push(...elements.map((el) => el.id)),
handleDomEditElementDelete: async () => {},
previewIframeRef: { current: iframe },
waitForPendingDomEditSaves: () => Promise.resolve(),
});
return null;
}
root = createRoot(document.createElement("div"));
act(() => root?.render(React.createElement(Harness)));
const openProject = (projectId: string) => {
view.projectId = projectId;
act(() => root?.render(React.createElement(Harness)));
};
return { clipboard: () => api as ReturnType<typeof useClipboard>, writes, deleted, openProject };
}
const clipOf = (id: string, tag: string, start: number, track: number): TimelineElement => ({
id,
domId: id,
tag,
start,
duration: 2,
track,
authoredTrack: track,
sourceFile: "index.html",
});
const CLIPS = [clipOf("title", "h1", 2, 0), clipOf("other", "h2", 6, 1)];
function selectClip(id: string) {
usePlayerStore.setState({ selectedElementId: id, selectedElementIds: new Set([id]) });
}
function selectTitleClip() {
usePlayerStore.setState({ elements: CLIPS });
selectClip("title");
}
it("duplicates a timeline clip from a sealed preview, as saved", async () => {
selectTitleClip();
const { clipboard, writes } = sealedClipboard(null);
expect(await clipboard().handleDuplicate()).toBe(true);
expect(writes[0]?.match(/>Title<\/h1>/g)).toHaveLength(2);
expect(writes[0]).not.toContain("display: none");
});
it("copies a timeline clip from a sealed preview, as saved", async () => {
selectTitleClip();
const { clipboard, writes } = sealedClipboard(null);
expect(clipboard().handleCopy()).toBe(true);
await clipboard().handlePaste();
expect(writes[0]).toContain(">Title</h1>");
expect(writes[0]).not.toContain("display: none");
});
it("copies a canvas selection from a sealed preview, as saved", async () => {
const selection = {
id: "title",
selector: "#title",
sourceFile: "index.html",
} as DomEditSelection;
const { clipboard, writes } = sealedClipboard(selection);
expect(clipboard().handleCopy()).toBe(true);
await clipboard().handlePaste();
expect(writes[0]).toContain(">Title</h1>");
expect(writes[0]).not.toContain("display: none");
});
it("leaves the clipboard as it was when a sealed preview lacks the element", async () => {
const selection = { id: "gone", selector: "#gone", sourceFile: "index.html" } as DomEditSelection;
const { clipboard, writes } = sealedClipboard(selection);
clipboard().handleCopy();
await clipboard().handlePaste();
expect(writes).toEqual([]);
});
it("cuts, and duplicates, the clip selected when the key was pressed, not one selected meanwhile", async () => {
selectTitleClip();
const { clipboard, writes, deleted } = sealedClipboard(null);
const cut = clipboard().handleCut();
selectClip("other");
expect(await cut).toBe(true);
expect(deleted).toEqual(["title"]);
await clipboard().handlePaste();
expect(writes[0]?.match(/>Title<\/h1>/g)).toHaveLength(2);
selectClip("title");
const duplicate = clipboard().handleDuplicate();
selectClip("other");
expect(await duplicate).toBe(true);
expect(writes[1]?.match(/>Title<\/h1>/g)).toHaveLength(2);
expect(writes[1]?.match(/>Other<\/h2>/g)).toHaveLength(1);
});
it("deletes nothing when another project opened while a sealed cut waited", async () => {
selectTitleClip();
const { clipboard, deleted, openProject } = sealedClipboard(null);
const cut = clipboard().handleCut();
openProject("q");
expect(await cut).toBe(false);
expect(deleted).toEqual([]);
});
+109 -69
View File
@@ -18,7 +18,8 @@ import { extendRootDurationInSource } from "../utils/rootDuration";
import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
import { formatTimelineAttributeNumber } from "../player/components/timelineEditing";
import { findElementForSelection } from "../components/editor/domEditingElement";
import { findTimelineElementInIframe, readFileContent } from "./timelineEditingHelpers";
import { findTimelineElementInDocument, readFileContent } from "./timelineEditingHelpers";
import { previewDocumentCopy, previewFrameOpaque } from "../player/lib/previewChannel";
import { buildTimelineElementKey } from "../player/lib/timelineElementHelpers";
import { timeRangesOverlap } from "../player/components/timelineCollision";
import {
@@ -93,20 +94,47 @@ function clipToast(verbPast: string, clipCount: number): string {
return clipCount > 1 ? `${verbPast} ${clipCount} clips` : `${verbPast} clip`;
}
function getSelectedDomElement(
iframeRef: React.MutableRefObject<HTMLIFrameElement | null>,
selection: DomEditSelection,
activeCompositionPath: string | null,
): Element | null {
/** A sealed frame's document is read off a copy of it, so it comes later. */
function inPreviewDocument<T>(
iframe: HTMLIFrameElement | null,
find: (doc: Document | null) => T,
): T | Promise<T> {
if (iframe && previewFrameOpaque(iframe)) return previewDocumentCopy(iframe).then(find);
let doc: Document | null = null;
try {
doc = iframeRef.current?.contentDocument ?? null;
doc = iframe?.contentDocument ?? null;
} catch {
return null;
doc = null;
}
if (!doc) return null;
return find(doc);
}
return findElementForSelection(doc, selection, activeCompositionPath);
type CopyTarget<T> = T | { refused: string } | null;
const refusal = (target: unknown): target is { refused: string } =>
typeof target === "object" && target !== null && "refused" in target;
type Toast = (message: string, tone?: "error" | "info") => void;
function foundOrToast<T>(found: CopyTarget<T>, showToast: Toast): T | null {
if (!refusal(found)) return found;
showToast(found.refused, "info");
return null;
}
/** A miss found now toasts and copies nothing; a sealed frame's arrives later, as the copy's error. */
function copyFound<T>(
found: CopyTarget<T> | Promise<CopyTarget<T>>,
copy: (target: T) => Promise<ClipboardPayload>,
showToast: Toast,
): Promise<ClipboardPayload> | null {
if (found instanceof Promise)
return found.then((target) => {
if (target === null) throw new Error("Nothing selected to copy.");
if (refusal(target)) throw new Error(target.refused);
return copy(target);
});
const target = foundOrToast(found, showToast);
return target === null ? null : copy(target);
}
function savedMarkupElseLive(saved: Document, live: Element, sourceFile: string): string {
@@ -269,8 +297,7 @@ export function useClipboard({
// After any save still in flight on the file, so a copy right after an edit takes the edit.
const readSaved = useCallback(
async (path: string): Promise<string> => {
const pid = projectIdRef.current;
async (path: string, pid: string | null): Promise<string> => {
if (!pid) throw new Error("No project is open.");
// Only the order matters here; a failed save already shows its own banner.
await waitForPendingDomEditSaves().catch(() => {});
@@ -281,34 +308,33 @@ export function useClipboard({
[waitForPendingDomEditSaves, writeProjectFile],
);
// Resolved through findTimelineElementInIframe, the same composition-aware
// Resolved through findTimelineElementInDocument, the same composition-aware
// lookup every other timeline editor uses — unlike findElementForSelection,
// it can address a composition-instance clip's root.
const findSelectedClips = useCallback((): {
elements: TimelineElement[];
lives: Element[];
} | null => {
const selected = getSelectedElements();
if (selected.length === 0) return null;
const lives: Element[] = [];
for (const element of selected) {
const live = findTimelineElementInIframe(previewIframeRef.current, element, activeCompPath);
if (!live) {
showToast(`Unable to copy "${element.label ?? element.id}".`, "info");
return null;
const findSelectedClips = useCallback(
(
selected: TimelineElement[],
doc: Document | null,
): CopyTarget<{ elements: TimelineElement[]; lives: Element[] }> => {
if (selected.length === 0) return null;
const lives: Element[] = [];
for (const element of selected) {
const live = doc && findTimelineElementInDocument(doc, element, activeCompPath);
if (!live) return { refused: `Unable to copy "${element.label ?? element.id}".` };
lives.push(live);
}
lives.push(live);
}
return { elements: selected, lives };
}, [activeCompPath, previewIframeRef, showToast]);
return { elements: selected, lives };
},
[activeCompPath],
);
const readClips = useCallback(
async (targets: {
elements: TimelineElement[];
lives: Element[];
}): Promise<TimelineClipboardClip[]> => {
async (
targets: { elements: TimelineElement[]; lives: Element[] },
pid: string | null,
): Promise<TimelineClipboardClip[]> => {
const paths = targets.elements.map((el) => el.sourceFile || activeCompPath || "index.html");
const markup = await readSavedMarkup(readSaved, paths, targets.lives);
const markup = await readSavedMarkup((path) => readSaved(path, pid), paths, targets.lives);
return targets.elements.map((element, index) => ({
html: markup[index] as string,
start: element.start,
@@ -322,41 +348,50 @@ export function useClipboard({
[activeCompPath, readSaved],
);
// The gesture's selection and project, taken before a sealed frame's read: the person can move on meanwhile.
const copyTimelineSelection = useCallback((): Promise<ClipboardPayload> | null => {
const targets = findSelectedClips();
if (!targets) return null;
const sourceFile = targets.elements[0]?.sourceFile || activeCompPath || "index.html";
return readClips(targets).then((clips) => {
showToast(clips.length > 1 ? `Copied ${clips.length} clips` : "Copied clip", "info");
return {
kind: "timeline-clip",
clips,
sourceFile,
projectId: projectIdRef.current ?? undefined,
copiedInComposition: activeCompPath || "index.html",
};
});
}, [activeCompPath, findSelectedClips, readClips, showToast]);
const selected = getSelectedElements();
const pid = projectIdRef.current;
const found = inPreviewDocument(previewIframeRef.current, (doc) =>
findSelectedClips(selected, doc),
);
const copy = (targets: { elements: TimelineElement[]; lives: Element[] }) => {
const sourceFile = targets.elements[0]?.sourceFile || activeCompPath || "index.html";
return readClips(targets, pid).then((clips): ClipboardPayload => {
showToast(clips.length > 1 ? `Copied ${clips.length} clips` : "Copied clip", "info");
return {
kind: "timeline-clip",
clips,
sourceFile,
projectId: pid ?? undefined,
copiedInComposition: activeCompPath || "index.html",
};
});
};
return copyFound(found, copy, showToast);
}, [activeCompPath, findSelectedClips, previewIframeRef, readClips, showToast]);
const copyDomSelection = useCallback(
(domSelection: DomEditSelection): Promise<ClipboardPayload> | null => {
const live = getSelectedDomElement(previewIframeRef, domSelection, activeCompPath);
if (!live) {
showToast("Unable to copy this element.", "info");
return null;
}
const sourceFile = domSelection.sourceFile || activeCompPath || "index.html";
return readSaved(sourceFile).then((content) => {
showToast("Copied element", "info");
return {
kind: "dom-element",
html: savedMarkupElseLive(parseSavedSource(content), live, sourceFile),
sourceFile,
projectId: projectIdRef.current ?? undefined,
originSelector: domSelection.selector,
originSelectorIndex: domSelection.selectorIndex,
};
const pid = projectIdRef.current;
const found = inPreviewDocument(previewIframeRef.current, (doc) => {
const live = doc && findElementForSelection(doc, domSelection, activeCompPath);
return live ?? { refused: "Unable to copy this element." };
});
const sourceFile = domSelection.sourceFile || activeCompPath || "index.html";
const copy = (live: Element) =>
readSaved(sourceFile, pid).then((content): ClipboardPayload => {
showToast("Copied element", "info");
return {
kind: "dom-element",
html: savedMarkupElseLive(parseSavedSource(content), live, sourceFile),
sourceFile,
projectId: pid ?? undefined,
originSelector: domSelection.selector,
originSelectorIndex: domSelection.selectorIndex,
};
});
return copyFound(found, copy, showToast);
},
[activeCompPath, previewIframeRef, readSaved, showToast],
);
@@ -456,10 +491,13 @@ export function useClipboard({
// selection's end or start here, the playhead or the copy's own time there).
const handleDuplicate = useCallback(
async (options?: InPlace): Promise<boolean> => {
const targets = findSelectedClips();
if (!targets) return false;
const selected = getSelectedElements();
const pid = projectIdRef.current;
if (!pid) return false;
const found = await inPreviewDocument(previewIframeRef.current, (doc) =>
findSelectedClips(selected, doc),
);
const targets = foundOrToast(found, showToast);
if (!targets || !pid || projectIdRef.current !== pid) return false;
const { elements } = targets;
const pathOf = (el: TimelineElement) => el.sourceFile || activeCompPath || "index.html";
@@ -473,7 +511,7 @@ export function useClipboard({
);
try {
const clips = await readClips(targets);
const clips = await readClips(targets, pid);
let ids: string[] = [];
const duplicate = (originalContent: string) => {
const liveElements = usePlayerStore
@@ -513,6 +551,7 @@ export function useClipboard({
[
activeCompPath,
findSelectedClips,
previewIframeRef,
readClips,
recordEdit,
reloadPreview,
@@ -524,8 +563,9 @@ export function useClipboard({
const handleCut = useCallback(async (): Promise<boolean> => {
const selected = getSelectedElements();
const domSelection = domEditSelectionRef.current;
const pid = projectIdRef.current;
const copied = copyToClipboard();
if (!copied || !(await copied)) return false;
if (!copied || !(await copied) || projectIdRef.current !== pid) return false;
if (selected.length > 0) {
// One call for the whole selection, not one per element: the batched
@@ -11,7 +11,7 @@ import {
reportDomEditPersistFailure,
} from "./domEditPersistFailure";
import { bumpDomEditCommitMapVersion, runDomEditCommit } from "./domEditCommitRunner";
import { syncStoredAutomationFromPreview } from "../player/lib/automationStoreSync";
import { syncStoredAutomationFromFrame } from "../player/lib/automationStoreSync";
import { HF_AUDIO_GROUP_ATTR, HF_AUDIO_GROUP_TAG } from "@hyperframes/core/audio-groups";
import { invalidateGroupInfoCache } from "../player/lib/timelineGroupInfo";
import { editSealedSelection } from "../components/editor/previewSelection";
@@ -215,7 +215,7 @@ export function useDomEditAttributeCommits({
// the audio while the lane went on drawing what it had, until a reload.
// One sink here rather than a sync in each writer, because three of them
// shipped without one.
syncStoredAutomationFromPreview(previewIframeRef.current?.contentDocument ?? null);
syncStoredAutomationFromFrame(previewIframeRef.current);
},
onSettled: options.onSettled,
onFinally: isLatestCommit.release,
@@ -318,7 +318,7 @@ export function useDomEditAttributeCommits({
resync: () => {
refreshDomEditSelectionFromPreview(selection);
if (options.syncAutomation) {
syncStoredAutomationFromPreview(previewIframeRef.current?.contentDocument ?? null);
syncStoredAutomationFromFrame(previewIframeRef.current);
}
},
onSettled: (ok) => {
@@ -482,7 +482,7 @@ export function useDomEditAttributeCommits({
// the audio while the lane went on drawing what it had, until a reload.
// One sink here rather than a sync in each writer, because three of them
// shipped without one.
syncStoredAutomationFromPreview(previewIframeRef.current?.contentDocument ?? null);
syncStoredAutomationFromFrame(previewIframeRef.current);
},
onFinally: isLatestCommit.release,
});
@@ -30,7 +30,10 @@ import {
import { settleNestedReads } from "../utils/gsapSoftReload";
import { readProjectFileContent } from "../utils/studioFileHistory";
import { usePlayerStore } from "../player";
import { syncStoredAutomationFromPreview } from "../player/lib/automationStoreSync";
import {
syncStoredAutomationFromFrame,
syncStoredAutomationFromPreview,
} from "../player/lib/automationStoreSync";
/** The restore payload the undo/redo preview-sync consumes (from the history store). */
interface HistoryPreviewRestore {
@@ -123,6 +126,11 @@ async function clearLegacyStudioMotionFile(
// ── Hook ──
const projectFileReader = (projectId: string | null) => (path: string) =>
projectId
? readProjectFileContent(projectId, path)
: Promise.reject(new Error("No project is open to read nested files from."));
export function usePreviewPersistence({
showToast,
readOptionalProjectFile: _readOptionalProjectFile,
@@ -226,16 +234,12 @@ export function usePreviewPersistence({
// attributes onto the live DOM and re-runs the timeline at the SAME playhead,
// falling back to reloadPreview for anything structural (split/delete undo),
// multi-file, sub-comp, or a permanent soft-reload failure.
const projectId = usePlayerStore.getState().timelineProjectId;
const nestedFiles = await settleNestedReads(
readUndoNestedFiles(
previewIframeRef.current,
activeCompPathRef.current,
restore.files,
(path) =>
projectId
? readProjectFileContent(projectId, path)
: Promise.reject(new Error("No project is open to read nested files from.")),
projectFileReader(usePlayerStore.getState().timelineProjectId),
),
);
const held = heldPreviewDoc();
@@ -260,7 +264,7 @@ export function usePreviewPersistence({
// player store keeps its own copy and that copy is what the automation lanes
// draw — so without this an undone envelope edit stayed invisible until a
// reload. The full path above waits for the reloaded preview to report instead.
syncStoredAutomationFromPreview(previewIframeRef.current?.contentDocument ?? null);
syncStoredAutomationFromFrame(previewIframeRef.current);
},
[previewIframeRef, activeCompPathRef, reloadPreview, heldPreviewDoc],
);
@@ -1,6 +1,10 @@
// @vitest-environment happy-dom
import { beforeEach, describe, expect, it } from "vitest";
import { syncStoredAutomationFromPreview, syncStoredElementAttribute } from "./automationStoreSync";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
syncStoredAutomationFromFrame,
syncStoredAutomationFromPreview,
syncStoredElementAttribute,
} from "./automationStoreSync";
import { usePlayerStore, type TimelineElement } from "../store/playerStore";
const TWO_POINTS = '{"version":1,"lanes":[{"target":"volume","points":[{"t":0,"v":1}]}]}';
@@ -28,8 +32,26 @@ function previewWith(attrs: Record<string, string>): Document {
return doc;
}
// A sealed frame's document copy, answered when the test says.
const copies = vi.hoisted(() => [] as Array<(doc: Document) => void>);
vi.mock("./previewChannel", async (importOriginal) => ({
...(await importOriginal<typeof import("./previewChannel")>()),
previewFrameOpaque: (iframe: HTMLIFrameElement) => iframe.dataset.sealed === "true",
previewDocumentCopy: () => new Promise<Document>((resolve) => copies.push(resolve)),
}));
function frame(sealed: boolean, doc?: Document): HTMLIFrameElement {
const iframe = document.createElement("iframe");
iframe.dataset.sealed = String(sealed);
if (doc) Object.defineProperty(iframe, "contentDocument", { value: doc });
return iframe;
}
const settle = () => new Promise((resolve) => setTimeout(resolve));
beforeEach(() => {
usePlayerStore.getState().reset();
copies.length = 0;
});
describe("syncStoredElementAttribute", () => {
@@ -163,3 +185,50 @@ describe("syncStoredAutomationFromPreview", () => {
expect(stored?.audioGroupFxChain).toBe(chain);
});
});
describe("syncStoredAutomationFromFrame", () => {
beforeEach(() =>
usePlayerStore.setState({ elements: [el({ volume: 1 })], timelineProjectId: "a" }),
);
const volume = () => usePlayerStore.getState().elements[0]?.volume;
it("records a sealed frame's copy once it arrives", async () => {
syncStoredAutomationFromFrame(frame(true));
copies[0]!(previewWith({ "data-volume": "0.2" }));
await settle();
expect(volume()).toBe(0.2);
});
it("keeps the later of two sealed reads when the earlier answers last", async () => {
syncStoredAutomationFromFrame(frame(true));
syncStoredAutomationFromFrame(frame(true));
copies[1]!(previewWith({ "data-volume": "0.8" }));
copies[0]!(previewWith({ "data-volume": "0.2" }));
await settle();
expect(volume()).toBe(0.8);
});
it("drops a sealed read once a live sync ran after it", async () => {
syncStoredAutomationFromFrame(frame(true));
syncStoredAutomationFromFrame(frame(false, previewWith({ "data-volume": "0.8" })));
copies[0]!(previewWith({ "data-volume": "0.2" }));
await settle();
expect(volume()).toBe(0.8);
});
it("drops a sealed read once the timeline saved a newer value", async () => {
syncStoredAutomationFromFrame(frame(true));
syncStoredElementAttribute(el(), "data-volume", "0.8");
copies[0]!(previewWith({ "data-volume": "0.2" }));
await settle();
expect(volume()).toBe(0.8);
});
it("drops a sealed read once another project is open", async () => {
syncStoredAutomationFromFrame(frame(true));
usePlayerStore.setState({ timelineProjectId: "b" });
copies[0]!(previewWith({ "data-volume": "0.2" }));
await settle();
expect(volume()).toBe(1);
});
});
@@ -30,6 +30,10 @@ import {
resolveMediaElement,
} from "./timelineElementHelpers";
import { elementVolume, parseStoredVolume } from "./storedVolume";
import { previewDocumentCopy, previewFrameOpaque } from "./previewChannel";
/** Every sync and saved-value write: a sealed read lands only if none ran after it began. */
let syncs = 0;
/**
* Re-read every element's automation, FX-chain, fade and volume attributes from the preview
@@ -67,6 +71,7 @@ function syncedFields(doc: Document, element: TimelineElement, node: Element) {
}
export function syncStoredAutomationFromPreview(doc: Document | null | undefined): void {
syncs += 1;
if (!doc) return;
const findNode = previewElementFinder(doc);
usePlayerStore.setState((state) => {
@@ -86,6 +91,21 @@ export function syncStoredAutomationFromPreview(doc: Document | null | undefined
});
}
/**
* `syncStoredAutomationFromPreview` for a preview frame. A sealed one is read off a copy, which lands
* only if no other sync or saved write ran and the project is the one read; a frame load drops the read.
*/
export function syncStoredAutomationFromFrame(iframe: HTMLIFrameElement | null | undefined): void {
if (!iframe) return;
if (!previewFrameOpaque(iframe)) return syncStoredAutomationFromPreview(iframe.contentDocument);
const read = ++syncs;
const project = usePlayerStore.getState().timelineProjectId;
void previewDocumentCopy(iframe).then((doc) => {
if (read === syncs && usePlayerStore.getState().timelineProjectId === project)
syncStoredAutomationFromPreview(doc);
});
}
/** Discovery's rule (applyFadeMetadataFromElement): a fade of 0 is no fade. */
const storedFade = (seconds: number) => (seconds > 0 ? seconds : undefined);
@@ -106,6 +126,7 @@ export function syncStoredElementAttribute(
): void {
const fields = STORED_FIELD[attr]?.(value);
if (!fields) return;
syncs += 1;
const key = getTimelineElementIdentity(target);
const same = (element: TimelineElement) =>
(Object.keys(fields) as (keyof TimelineElement)[]).every((k) => element[k] === fields[k]);