diff --git a/packages/studio/src/hooks/timelineEditingHelpers.test.ts b/packages/studio/src/hooks/timelineEditingHelpers.test.ts index b5aeb9d579..f0bcc99731 100644 --- a/packages/studio/src/hooks/timelineEditingHelpers.test.ts +++ b/packages/studio/src/hooks/timelineEditingHelpers.test.ts @@ -11,8 +11,11 @@ import { persistElementAttribute, persistTimelineBatchEdit, persistTimelineEdit, + readTargetAttribute, type PersistTimelineBatchChange, } from "./timelineEditingHelpers"; +import { ensureHfIds } from "@hyperframes/parsers/hf-ids"; +import { parseTimelineFromDOM } from "../player/lib/timelineDOM"; import type { TimelineElement } from "../player/store/playerStore"; import { readMediaOffsetSeconds } from "@hyperframes/parsers/media-duration"; import { usePlayerStore } from "../player/store/playerStore"; @@ -684,6 +687,51 @@ describe("persistElementAttribute", () => { }); }); +describe("a clip whose file has no ids yet", () => { + // Added outside Studio: no id, no class, and nothing has stamped the file since. The preview mints its hf-id. + const ON_DISK = `

Title
Title
{ + expect(hfId).toMatch(/^hf-/); + expect(readAttributeByTarget(ON_DISK, { hfId }, "volume")).toBe("0.5"); + expect(readTagSnippetByTarget(ON_DISK, { hfId })).toContain(`data-hf-id="${hfId}"`); + expect( + readAttributeByTarget(applyPatchByTarget(ON_DISK, { hfId }, op), { hfId }, "volume"), + ).toBe("0.9"); + }); + + it("leaves the file as it was when no element has the id, ids and all", () => { + expect(applyPatchByTarget(ON_DISK, { hfId: "hf-gone" }, op)).toBe(ON_DISK); + }); +}); + describe("file names with special characters", () => { const src = (value: string): PatchOperation => ({ type: "html-attribute", diff --git a/packages/studio/src/utils/sourcePatcher.ts b/packages/studio/src/utils/sourcePatcher.ts index 6bf2668f2e..a15b0c16a4 100644 --- a/packages/studio/src/utils/sourcePatcher.ts +++ b/packages/studio/src/utils/sourcePatcher.ts @@ -3,6 +3,7 @@ import { decodeAuthoredAttribute, HTML_ATTRIBUTE_ENTITIES, } from "@hyperframes/parsers"; +import { stampHfIdsInPlace } from "@hyperframes/parsers/hf-ids"; /** * Source Patcher — Maps visual property edits back to source HTML files. @@ -277,6 +278,12 @@ function findTagByClass(html: string, target: PatchTarget): TagMatch | null { : null; } +export function servedSource(html: string, target: PatchTarget): string { + if (!target.hfId || findTagByTarget(html, target)) return html; + const stamped = stampHfIdsInPlace(html); + return findTagByTarget(stamped, target) ? stamped : html; +} + export function findTagByTarget(html: string, target: PatchTarget): TagMatch | null { if (target.hfId) { const result = findTagByAttribute(html, "data-hf-id", target.hfId); @@ -304,7 +311,7 @@ export function readAttributeByTarget( target: PatchTarget, attr: string, ): string | undefined { - const match = findTagByTarget(html, target); + const match = findTagByTarget(servedSource(html, target), target); if (!match) return undefined; return readTagAttribute(match.tag, attr.startsWith("data-") ? attr : `data-${attr}`); @@ -316,7 +323,7 @@ export function readTagAttribute(tag: string, attr: string): string | undefined } export function readTagSnippetByTarget(html: string, target: PatchTarget): string | undefined { - const match = findTagByTarget(html, target); + const match = findTagByTarget(servedSource(html, target), target); return match?.tag; } @@ -454,22 +461,23 @@ export function applyPatch(html: string, elementId: string, op: PatchOperation): } export function applyPatchByTarget(html: string, target: PatchTarget, op: PatchOperation): string { + const source = servedSource(html, target); if (target.id) { - const patchedById = applyPatch(html, target.id, op); - if (patchedById !== html || !target.selector) { + const patchedById = applyPatch(source, target.id, op); + if (patchedById !== source || (!target.selector && !target.hfId)) { return patchedById; } } switch (op.type) { case "inline-style": - return patchInlineStyleByTarget(html, target, op.property, op.value); + return patchInlineStyleByTarget(source, target, op.property, op.value); case "attribute": - return patchAttributeByTarget(html, target, op.property, op.value); + return patchAttributeByTarget(source, target, op.property, op.value); case "html-attribute": - return patchHtmlAttributeByTarget(html, target, op.property, op.value); + return patchHtmlAttributeByTarget(source, target, op.property, op.value); case "text-content": - return op.value !== null ? patchTextContentByTarget(html, target, op.value) : html; + return op.value !== null ? patchTextContentByTarget(source, target, op.value) : html; default: return html; }