11import { buildProjectApiPath } from "../utils/projectRouting" ;
2- import { useCallback , useRef } from "react" ;
2+ import { useCallback } from "react" ;
33import { findUnsafeDomPatchValues } from "@hyperframes/core/studio-api/finite-mutation" ;
44import { FONT_EXT } from "../utils/mediaTypes" ;
5-
6- import { trackStudioEvent } from "../utils/studioTelemetry" ;
75import { primaryFontFamilyValue } from "../utils/studioFontHelpers" ;
86import { StudioSaveHttpError , trackStudioSaveFailure } from "../utils/studioSaveDiagnostics" ;
9- import { buildDomEditPatchTarget , type DomEditSelection } from "../components/editor/domEditing" ;
7+ import type { DomEditSelection } from "../components/editor/domEditing" ;
108import { fontFamilyFromAssetPath , type ImportedFontAsset } from "../components/editor/fontAssets" ;
11- import type { CommitDomEditPatchBatches , PersistDomEditOperations } from "./domEditCommitTypes" ;
9+ import type { CommitDomEditPatchBatches } from "./domEditCommitTypes" ;
1210import type { PatchOperation } from "../utils/sourcePatcher" ;
13- import {
14- DomEditPersistUnsafeValueError ,
15- DomEditPersistUnresolvableError ,
16- warnDomEditPersistNoOp ,
17- } from "./domEditPersistFailure" ;
11+ import { DomEditPersistUnsafeValueError } from "./domEditPersistFailure" ;
12+ import { useDomEditPersist , type RecordEditInput } from "./useDomEditPersist" ;
1813import { useDomEditPositionPatchCommit } from "./useDomEditPositionPatchCommit" ;
1914import { useDomEditTextCommits } from "./useDomEditTextCommits" ;
2015import { useDomGeometryCommits } from "./useDomGeometryCommits" ;
@@ -24,19 +19,9 @@ import {
2419 batchesAreInlineStyleOnly ,
2520 formatUnsafeFieldList ,
2621 patchElementBatches ,
27- postPatchElement ,
28- writePreparedContent ,
2922} from "./useDomEditCommitsHelpers" ;
3023import type { CutoverResult } from "../utils/sdkCutover" ;
31- import { reseekPreviewRuntime } from "./timelineTrackVisibility" ;
3224import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator" ;
33- import { readProjectFileContent } from "../utils/studioFileHistory" ;
34- interface RecordEditInput {
35- label : string ;
36- coalesceKey ?: string ;
37- coalesceMs ?: number ;
38- files : Record < string , { before : string ; after : string } > ;
39- }
4025
4126export interface UseDomEditCommitsParams {
4227 activeCompPath : string | null ;
@@ -132,168 +117,18 @@ export function useDomEditCommits({
132117 [ fileTree , projectId , importedFontAssetsRef ] ,
133118 ) ;
134119
135- const reportedUnresolvableRef = useRef ( new Set < string > ( ) ) ;
136-
137- // fallow-ignore-next-line complexity
138- const performPersistDomEditOperations = useCallback (
139- // fallow-ignore-next-line complexity
140- async (
141- selection : DomEditSelection ,
142- operations : PatchOperation [ ] ,
143- options : Parameters < PersistDomEditOperations > [ 2 ] ,
144- expectedProjectId : string ,
145- ) => {
146- if ( projectIdRef . current !== expectedProjectId ) {
147- throw new Error ( "Active project changed before the edit could be saved" ) ;
148- }
149- const pid = expectedProjectId ;
150- if ( options ?. shouldSave && ! options . shouldSave ( ) ) return ;
151-
152- const targetPath = selection . sourceFile || activeCompPath || "index.html" ;
153- const completePersistence = < T > ( result : T , changed : boolean ) : T => {
154- if ( options ?. skipRefresh && changed ) reseekPreviewRuntime ( previewIframeRef . current ) ;
155- return result ;
156- } ;
157-
158- const readTarget = async ( ) : Promise < string | null > => {
159- const content = await readProjectFileContent ( pid , targetPath ) ;
160- if ( projectIdRef . current !== expectedProjectId ) {
161- throw new Error ( "Active project changed before the edit could be saved" ) ;
162- }
163- return options ?. shouldSave && ! options . shouldSave ( ) ? null : content ;
164- } ;
165-
166- // Validate layout values BEFORE any persist path runs. The SDK cutover
167- // path (onTrySdkPersist) returns early on success, so leaving this check
168- // after it let invalid numeric values bypass the guard whenever the
169- // cutover flag was on.
170- const patchTarget = buildDomEditPatchTarget ( selection ) ;
171- const patchBody = { target : patchTarget , operations } ;
172- const unsafeFields = findUnsafeDomPatchValues ( patchBody ) ;
173- if ( unsafeFields . length > 0 ) {
174- const fields = formatUnsafeFieldList ( unsafeFields ) ;
175- showToast ( "Couldn't save edit because it contains invalid layout values" , "error" ) ;
176- throw new DomEditPersistUnsafeValueError ( `DOM patch contains unsafe values: ${ fields } ` , {
177- alreadyToasted : true ,
178- } ) ;
179- }
180-
181- // Skip the SDK path when prepareContent is set (e.g. @font-face injection
182- // for a custom font): sdkCutoverPersist serializes only the patched DOM
183- // and would drop the injected content. Let the server path run prepareContent.
184- // The SDK joins the file queue itself and re-reads there; this read is only its fallback.
185- if ( onTrySdkPersist && ! options ?. prepareContent ) {
186- const originalContent = await readTarget ( ) ;
187- if ( originalContent === null ) return ;
188- const cutover = await onTrySdkPersist ( selection , operations , originalContent , targetPath , {
189- label : options ?. label ,
190- coalesceKey : options ?. coalesceKey ,
191- skipRefresh : options ?. skipRefresh ,
192- } ) ;
193- if ( cutover . status === "failed" ) throw cutover . error ;
194- if ( cutover . status === "committed" ) {
195- // SDK handled it — its in-memory doc is already current, so do NOT
196- // forceReload (that would echo-reload the session we just wrote).
197- return completePersistence (
198- { sourceFile : targetPath , version : cutover . version , changed : true } ,
199- true ,
200- ) ;
201- }
202- }
203-
204- const history = {
205- label : options ?. label ?? "Edit layer" ,
206- coalesceKey : options ?. coalesceKey ,
207- coalesceMs : options ?. coalesceMs ,
208- } ;
209- const prepare = options ?. prepareContent ;
210- // Read, server patch, follow-up write and history hold the file's queue, so no save lands between them.
211- const saved = await serializeStudioFileMutations ( writeProjectFile , [ targetPath ] , async ( ) => {
212- const originalContent = await readTarget ( ) ;
213- if ( originalContent === null ) return null ;
214- const patchData = await postPatchElement ( pid , targetPath , patchBody , showToast ) ;
215- if ( ! patchData . changed ) return { patchData, patchedContent : null , finalContent : null } ;
216-
217- const patchedContent =
218- typeof patchData . content === "string" ? patchData . content : originalContent ;
219- const finalContent = prepare
220- ? await writePreparedContent (
221- targetPath ,
222- patchedContent ,
223- prepare ,
224- writeProjectFile ,
225- showToast ,
226- )
227- : patchedContent ;
228-
229- await editHistory . recordEdit ( {
230- ...history ,
231- files : { [ targetPath ] : { before : originalContent , after : finalContent } } ,
232- } ) ;
233- return { patchData, patchedContent, finalContent } ;
234- } ) ;
235- if ( saved === null ) return ;
236- const { patchData, patchedContent, finalContent } = saved ;
237-
238- if ( finalContent === null ) {
239- if ( patchData . matched === false ) {
240- const targetKey = selection . selector ?? selection . id ?? "selection" ;
241- if ( ! reportedUnresolvableRef . current . has ( targetKey ) ) {
242- reportedUnresolvableRef . current . add ( targetKey ) ;
243- trackStudioEvent ( "save_skipped_unresolvable" , {
244- target_id : selection . id ?? undefined ,
245- target_selector : selection . selector ?? undefined ,
246- target_source_file : selection . sourceFile ?? undefined ,
247- composition : activeCompPath ?? undefined ,
248- } ) ;
249- }
250- throw new DomEditPersistUnresolvableError ( targetPath ) ;
251- }
252- warnDomEditPersistNoOp ( selection , operations ) ;
253- return completePersistence (
254- typeof patchData . path === "string" && typeof patchData . version === "string"
255- ? { sourceFile : patchData . path , version : patchData . version , changed : false }
256- : undefined ,
257- false ,
258- ) ;
259- }
260- forceReloadSdkSession ?.( ) ;
261-
262- if ( ! options ?. skipRefresh ) {
263- reloadPreview ( ) ;
264- }
265- return completePersistence (
266- finalContent === patchedContent &&
267- typeof patchData . path === "string" &&
268- typeof patchData . version === "string"
269- ? { sourceFile : patchData . path , version : patchData . version , changed : true }
270- : undefined ,
271- true ,
272- ) ;
273- } ,
274- [
275- activeCompPath ,
276- editHistory ,
277- writeProjectFile ,
278- projectIdRef ,
279- reloadPreview ,
280- showToast ,
281- forceReloadSdkSession ,
282- onTrySdkPersist ,
283- previewIframeRef ,
284- ] ,
285- ) ;
286-
287- const persistDomEditOperations : PersistDomEditOperations = useCallback (
288- ( selection , operations , options ) => {
289- const expectedProjectId = projectIdRef . current ;
290- if ( ! expectedProjectId ) return Promise . reject ( new Error ( "No active project" ) ) ;
291- return queueDomEditSave ( ( ) =>
292- performPersistDomEditOperations ( selection , operations , options , expectedProjectId ) ,
293- ) ;
294- } ,
295- [ performPersistDomEditOperations , projectIdRef , queueDomEditSave ] ,
296- ) ;
120+ const persistDomEditOperations = useDomEditPersist ( {
121+ activeCompPath,
122+ previewIframeRef,
123+ showToast,
124+ queueDomEditSave,
125+ writeProjectFile,
126+ editHistory,
127+ projectIdRef,
128+ reloadPreview,
129+ forceReloadSdkSession,
130+ onTrySdkPersist,
131+ } ) ;
297132
298133 const commitDomEditPatchBatches : CommitDomEditPatchBatches = useCallback (
299134 ( batches , options ) => {
0 commit comments