Skip to content

Write HTML. Render video. Built for agents.

fix(studio): persist honors prepareContent again so audio edits keep their content step

Vance Ingalls76c4fc7b3cf1
diff --git a/packages/studio/src/hooks/canvasQuickSaves.test.tsx b/packages/studio/src/hooks/canvasQuickSaves.test.tsx
index 93d2b0f120..73d460ccab 100644
--- a/packages/studio/src/hooks/canvasQuickSaves.test.tsx
+++ b/packages/studio/src/hooks/canvasQuickSaves.test.tsx
@@ -197,6 +197,26 @@ describe("canvas edit commits share the file queue", () => {
expect(project.wholeFileWrites(), "the font rides the edit's own patch").toBe(0);
});
+ it("writes a batch's prepared content over the patch and saves it once", async () => {
+ const project = fakeProject();
+ const { hook, selection, toasts } = mountCanvasCommits(project);
+ project.release();
+
+ const saved = await hook.handleDomAttributeBatchCommit(
+ selection,
+ [{ type: "attribute", property: "data-note", value: "1" }],
+ {
+ label: "Edit with prepared content",
+ prepareContent: (html) => html.replace("</head>", '<meta data-prepared="1"></head>'),
+ },
+ );
+
+ expect(saved).toBe(true);
+ expect(toasts).toEqual([]);
+ expect(project.read().match(/data-prepared="1"/g)).toHaveLength(1);
+ expect(project.wholeFileWrites()).toBe(1);
+ });
+
it("lands a canvas commit started while a timeline save is writing", async () => {
const project = fakeProject();
const { hook, toasts } = mountCanvasCommits(project);
diff --git a/packages/studio/src/hooks/domEditCommitTypes.ts b/packages/studio/src/hooks/domEditCommitTypes.ts
index 248865ed6f..0f5a34c3ab 100644
--- a/packages/studio/src/hooks/domEditCommitTypes.ts
+++ b/packages/studio/src/hooks/domEditCommitTypes.ts
@@ -56,6 +56,7 @@ export type PersistDomEditOperations = (
skipRefresh?: boolean;
deferRender?: boolean;
importedFont?: ImportedFontAsset;
+ prepareContent?: (html: string, sourceFile: string) => string;
shouldSave?: () => boolean;
},
) => Promise<DomEditPersistOutcome | undefined>;
diff --git a/packages/studio/src/hooks/useDomEditCommitsHelpers.ts b/packages/studio/src/hooks/useDomEditCommitsHelpers.ts
index 01d36afef8..e9db2c69ef 100644
--- a/packages/studio/src/hooks/useDomEditCommitsHelpers.ts
+++ b/packages/studio/src/hooks/useDomEditCommitsHelpers.ts
@@ -197,3 +197,26 @@ export async function postPatchElement(
}
return (await response.json()) as PatchElementResponse;
}
+
+/** Writes `prepare`'s embellishment over the server's patch; returns what the file ends up holding. */
+export async function writePreparedContent(
+ targetPath: string,
+ patchedContent: string,
+ prepare: (html: string, sourceFile: string) => string,
+ writeProjectFile: (path: string, content: string, expectedContent?: string) => Promise<void>,
+ showToast: ShowToast,
+): Promise<string> {
+ const preparedContent = prepare(patchedContent, targetPath);
+ if (preparedContent === patchedContent) return patchedContent;
+ try {
+ await writeProjectFile(targetPath, preparedContent, patchedContent);
+ return preparedContent;
+ } catch (error) {
+ // The patch already landed on disk; keep it rather than revert a committed change.
+ showToast(
+ `Saved, but couldn't finish updating ${targetPath}: ${getErrorDetail(error)}`,
+ "error",
+ );
+ return patchedContent;
+ }
+}
diff --git a/packages/studio/src/hooks/useDomEditPersist.ts b/packages/studio/src/hooks/useDomEditPersist.ts
index 9dba97502a..cdbb5f7255 100644
--- a/packages/studio/src/hooks/useDomEditPersist.ts
+++ b/packages/studio/src/hooks/useDomEditPersist.ts
@@ -9,7 +9,11 @@ import {
DomEditPersistUnresolvableError,
warnDomEditPersistNoOp,
} from "./domEditPersistFailure";
-import { formatUnsafeFieldList, postPatchElement } from "./useDomEditCommitsHelpers";
+import {
+ formatUnsafeFieldList,
+ postPatchElement,
+ writePreparedContent,
+} from "./useDomEditCommitsHelpers";
import { importedFontFaceCssFor } from "../utils/studioFontHelpers";
import { countStudioManualEditSave } from "../components/editor/manualEditsDom";
import type { CutoverResult } from "../utils/sdkCutover";
@@ -108,9 +112,10 @@ export function useDomEditPersist({
});
}
- // An imported font takes the server patch, which writes its @font-face with the edit; the SDK
- // serializes only the patched DOM. The SDK re-reads in the file queue; this read is its fallback.
- if (onTrySdkPersist && !font) {
+ // An imported font or a prepareContent takes the server patch: the server writes the @font-face
+ // with the edit and prepareContent embellishes the patched file, while the SDK serializes only
+ // the patched DOM. The SDK re-reads in the file queue; this read is its fallback.
+ if (onTrySdkPersist && !font && !options?.prepareContent) {
const originalContent = await readTarget();
if (originalContent === null) return;
const cutover = await onTrySdkPersist(selection, operations, originalContent, targetPath, {
@@ -134,7 +139,8 @@ export function useDomEditPersist({
coalesceKey: options?.coalesceKey,
coalesceMs: options?.coalesceMs,
};
- // Read, server patch and history hold the file's queue, so no save lands between them.
+ const prepare = options?.prepareContent;
+ // Read, server patch, follow-up write and history hold the file's queue, so no save lands between them.
const saved = await serializeStudioFileMutations(writeProjectFile, [targetPath], async () => {
const originalContent = await readTarget();
if (originalContent === null) return null;
@@ -143,7 +149,15 @@ export function useDomEditPersist({
const patchedContent =
typeof patchData.content === "string" ? patchData.content : originalContent;
- const finalContent = patchedContent;
+ const finalContent = prepare
+ ? await writePreparedContent(
+ targetPath,
+ patchedContent,
+ prepare,
+ writeProjectFile,
+ showToast,
+ )
+ : patchedContent;
await editHistory.recordEdit({
...history,