Skip to content

Commit 76c4fc7

Browse files
vanceingallsclaude
andcommitted
fix(studio): persist honors prepareContent again so audio edits keep their content step
#4869 dropped prepareContent from the persist options and the server path while #4813's attribute batch commit (mediaAudioEdits) still passes it, so main fails typecheck and the audio edit would lose its prepared content. Restore the option, take the server patch when it is set, and write the prepared content over the patch under the same file queue. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent d4756f5 commit 76c4fc7

4 files changed

Lines changed: 64 additions & 6 deletions

File tree

‎packages/studio/src/hooks/canvasQuickSaves.test.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ describe("canvas edit commits share the file queue", () => {
197197
expect(project.wholeFileWrites(), "the font rides the edit's own patch").toBe(0);
198198
});
199199

200+
it("writes a batch's prepared content over the patch and saves it once", async () => {
201+
const project = fakeProject();
202+
const { hook, selection, toasts } = mountCanvasCommits(project);
203+
project.release();
204+
205+
const saved = await hook.handleDomAttributeBatchCommit(
206+
selection,
207+
[{ type: "attribute", property: "data-note", value: "1" }],
208+
{
209+
label: "Edit with prepared content",
210+
prepareContent: (html) => html.replace("</head>", '<meta data-prepared="1"></head>'),
211+
},
212+
);
213+
214+
expect(saved).toBe(true);
215+
expect(toasts).toEqual([]);
216+
expect(project.read().match(/data-prepared="1"/g)).toHaveLength(1);
217+
expect(project.wholeFileWrites()).toBe(1);
218+
});
219+
200220
it("lands a canvas commit started while a timeline save is writing", async () => {
201221
const project = fakeProject();
202222
const { hook, toasts } = mountCanvasCommits(project);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export type PersistDomEditOperations = (
5656
skipRefresh?: boolean;
5757
deferRender?: boolean;
5858
importedFont?: ImportedFontAsset;
59+
prepareContent?: (html: string, sourceFile: string) => string;
5960
shouldSave?: () => boolean;
6061
},
6162
) => Promise<DomEditPersistOutcome | undefined>;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,26 @@ export async function postPatchElement(
197197
}
198198
return (await response.json()) as PatchElementResponse;
199199
}
200+
201+
/** Writes `prepare`'s embellishment over the server's patch; returns what the file ends up holding. */
202+
export async function writePreparedContent(
203+
targetPath: string,
204+
patchedContent: string,
205+
prepare: (html: string, sourceFile: string) => string,
206+
writeProjectFile: (path: string, content: string, expectedContent?: string) => Promise<void>,
207+
showToast: ShowToast,
208+
): Promise<string> {
209+
const preparedContent = prepare(patchedContent, targetPath);
210+
if (preparedContent === patchedContent) return patchedContent;
211+
try {
212+
await writeProjectFile(targetPath, preparedContent, patchedContent);
213+
return preparedContent;
214+
} catch (error) {
215+
// The patch already landed on disk; keep it rather than revert a committed change.
216+
showToast(
217+
`Saved, but couldn't finish updating ${targetPath}: ${getErrorDetail(error)}`,
218+
"error",
219+
);
220+
return patchedContent;
221+
}
222+
}

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
DomEditPersistUnresolvableError,
1010
warnDomEditPersistNoOp,
1111
} from "./domEditPersistFailure";
12-
import { formatUnsafeFieldList, postPatchElement } from "./useDomEditCommitsHelpers";
12+
import {
13+
formatUnsafeFieldList,
14+
postPatchElement,
15+
writePreparedContent,
16+
} from "./useDomEditCommitsHelpers";
1317
import { importedFontFaceCssFor } from "../utils/studioFontHelpers";
1418
import { countStudioManualEditSave } from "../components/editor/manualEditsDom";
1519
import type { CutoverResult } from "../utils/sdkCutover";
@@ -108,9 +112,10 @@ export function useDomEditPersist({
108112
});
109113
}
110114

111-
// An imported font takes the server patch, which writes its @font-face with the edit; the SDK
112-
// serializes only the patched DOM. The SDK re-reads in the file queue; this read is its fallback.
113-
if (onTrySdkPersist && !font) {
115+
// An imported font or a prepareContent takes the server patch: the server writes the @font-face
116+
// with the edit and prepareContent embellishes the patched file, while the SDK serializes only
117+
// the patched DOM. The SDK re-reads in the file queue; this read is its fallback.
118+
if (onTrySdkPersist && !font && !options?.prepareContent) {
114119
const originalContent = await readTarget();
115120
if (originalContent === null) return;
116121
const cutover = await onTrySdkPersist(selection, operations, originalContent, targetPath, {
@@ -134,7 +139,8 @@ export function useDomEditPersist({
134139
coalesceKey: options?.coalesceKey,
135140
coalesceMs: options?.coalesceMs,
136141
};
137-
// Read, server patch and history hold the file's queue, so no save lands between them.
142+
const prepare = options?.prepareContent;
143+
// Read, server patch, follow-up write and history hold the file's queue, so no save lands between them.
138144
const saved = await serializeStudioFileMutations(writeProjectFile, [targetPath], async () => {
139145
const originalContent = await readTarget();
140146
if (originalContent === null) return null;
@@ -143,7 +149,15 @@ export function useDomEditPersist({
143149

144150
const patchedContent =
145151
typeof patchData.content === "string" ? patchData.content : originalContent;
146-
const finalContent = patchedContent;
152+
const finalContent = prepare
153+
? await writePreparedContent(
154+
targetPath,
155+
patchedContent,
156+
prepare,
157+
writeProjectFile,
158+
showToast,
159+
)
160+
: patchedContent;
147161

148162
await editHistory.recordEdit({
149163
...history,

0 commit comments

Comments
 (0)