From cb072eb047efafe9749ae81613ccbb8036be3e21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 14 Nov 2026 02:48:59 +0000 Subject: [PATCH] fix(studio): edit clips added to a project outside Studio (#7686) * fix(studio): edit clips added to a project outside Studio A clip written into a composition after its ids were last stamped (a hand edit, an outside agent) has a data-hf-id only in the served preview. The timeline's client-side patch looked that id up in the file's raw bytes, so Hide, Mute, move, resize and attribute saves failed with "Unable to patch timeline element ... (by hf-id; no file on the clip)". Resolve the target against the file stamped in place the way the preview stamps it, as the server's own saves and the GSAP save already do. The save then persists that id. * fix(studio): save volume on several clips added outside Studio The multi-clip attribute save (gain or volume on a selection) patched each clip in the file's raw bytes, so a clip with no ids on disk was skipped and the save reported success with nothing written. The by-target read and patch helpers now resolve a clip by the id the preview served it with, so every caller finds it. The multi-clip save goes through the same batch patch as the other timeline saves, which refuses a clip the file does not hold; the error reaches the person instead of a silent no-op. The preview picker finds the file holding an unstamped element the same way. --- .../src/hooks/timelineEditingHelpers.test.ts | 48 +++++++++++++++++++ .../src/hooks/timelineEditingHelpers.ts | 2 + .../hooks/timelineElementsAttribute.test.ts | 46 ++++++++++++++++++ .../src/hooks/timelineElementsAttribute.ts | 30 ++++++------ .../src/hooks/timelineTrackVisibility.test.ts | 34 +++++++++++++ .../studio/src/hooks/useElementPicker.test.ts | 11 ++++- packages/studio/src/hooks/useElementPicker.ts | 6 ++- .../studio/src/utils/sourcePatcher.test.ts | 21 ++++++++ packages/studio/src/utils/sourcePatcher.ts | 24 ++++++---- 9 files changed, 194 insertions(+), 28 deletions(-) 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 = `
`; + const served = () => { + const doc = document.implementation.createHTMLDocument(""); + doc.body.innerHTML = ensureHfIds(ON_DISK); + const [clip] = parseTimelineFromDOM(doc, 10); + if (!clip?.hfId) throw new Error("expected the served clip to carry an hf-id"); + return { hfId: clip.hfId }; + }; + + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it("saves an attribute on it by the id the preview served it with", async () => { + vi.stubGlobal( + "fetch", + vi.fn(async () => new Response(JSON.stringify({ content: ON_DISK }))), + ); + const target = served(); + const writes: string[] = []; + + await persistElementAttribute({ + projectId: "p", + targetPath: "index.html", + patchTarget: target, + attr: "data-volume", + value: "0.9", + label: "Set volume", + writeProjectFile: async (_path, content) => void writes.push(content), + recordEdit: vi.fn(async () => {}), + pendingTimelineEditPathRef: { current: new Set() }, + onFileRead: vi.fn(), + patchLive: vi.fn(), + }); + + expect(readTargetAttribute(writes[0] ?? "", target, "data-volume")).toBe("0.9"); + }); + + it("reads its attribute by that id", () => { + expect(readTargetAttribute(ON_DISK, served(), "data-volume")).toBe("0.5"); + }); +}); + describe("persistElementAttribute — unwind value", () => { /** * The unwind has to restore the value on DISK, not the one in the preview. diff --git a/packages/studio/src/hooks/timelineEditingHelpers.ts b/packages/studio/src/hooks/timelineEditingHelpers.ts index 1e38641aa9..f66f72a004 100644 --- a/packages/studio/src/hooks/timelineEditingHelpers.ts +++ b/packages/studio/src/hooks/timelineEditingHelpers.ts @@ -7,6 +7,7 @@ import { findTagByTarget, readAttributeByTarget, readTagSnippetByTarget, + servedSource, type PatchOperation, } from "../utils/sourcePatcher"; import { @@ -420,6 +421,7 @@ export function patchTimelineChangesInSource( for (const { element, buildPatches } of changes) { const target = buildPatchTarget(element); if (!target) throw new Error(`Timeline element ${element.id} is missing a patchable target`); + current = servedSource(current, target); // Resolve first: a member already at its target values patches to the same string, a missing one must throw. if (!findTagByTarget(current, target)) { throw new Error( diff --git a/packages/studio/src/hooks/timelineElementsAttribute.test.ts b/packages/studio/src/hooks/timelineElementsAttribute.test.ts index 781dd10566..becffda1c7 100644 --- a/packages/studio/src/hooks/timelineElementsAttribute.test.ts +++ b/packages/studio/src/hooks/timelineElementsAttribute.test.ts @@ -5,6 +5,8 @@ import { describe, expect, it, vi } from "vitest"; Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", { configurable: true, value: true }); import type { TimelineElement } from "../player"; +import { ensureHfIds } from "@hyperframes/parsers/hf-ids"; +import { parseTimelineFromDOM } from "../player/lib/timelineDOM"; const saved = vi.hoisted(() => ({ inputs: [] as Array> })); vi.mock("../utils/studioFileHistory", () => ({ @@ -68,6 +70,50 @@ describe("useSetElementsAttribute", () => { expect(out).toMatch(/id="b"[^>]*data-volume="2"/); }); + it("saves clips whose file has no ids yet by the ids the preview served them with, and refuses a clip the file lacks", async () => { + saved.inputs.length = 0; + // Added outside Studio: no id, no class, and nothing has stamped the file since. + const onDisk = `
`; + const doc = document.implementation.createHTMLDocument(""); + doc.body.innerHTML = ensureHfIds(onDisk); + const served = parseTimelineFromDOM(doc, 10); + expect(served.map((element) => element.hfId).every(Boolean)).toBe(true); + let write: ReturnType | null = null; + function Probe() { + write = useSetElementsAttribute({ + projectIdRef: { current: "p1" }, + activeCompPath: "index.html", + showToast: vi.fn(), + writeProjectFile: vi.fn(async () => {}), + recordEdit: vi.fn(async () => {}), + previewIframeRef: { current: null }, + pendingTimelineEditPathRef: { current: new Set() }, + }); + return null; + } + const root = createRoot(document.createElement("div")); + act(() => root.render(createElement(Probe))); + await write?.( + served.map((element) => ({ element, value: "0.3" })), + "data-volume", + "Audio Gain", + ); + await write?.( + [{ element: { ...served[0]!, hfId: "hf-gone" }, value: "0.3" }], + "data-volume", + "Audio Gain", + ); + act(() => root.unmount()); + const build = (n: number) => { + const files = saved.inputs[n]?.["files"]; + const fn = typeof files === "object" && files !== null && Reflect.get(files, "index.html"); + if (typeof fn !== "function") throw new Error("no builder for index.html"); + return (html: string) => String(fn(html)); + }; + expect(build(0)(onDisk).match(/data-volume="0\.3"/g)).toHaveLength(2); + expect(() => build(1)(onDisk)).toThrow("Unable to patch timeline element"); + }); + it("refuses while recording, like the single-clip write", async () => { saved.inputs.length = 0; const showToast = vi.fn(); diff --git a/packages/studio/src/hooks/timelineElementsAttribute.ts b/packages/studio/src/hooks/timelineElementsAttribute.ts index 9254339ce9..406156d923 100644 --- a/packages/studio/src/hooks/timelineElementsAttribute.ts +++ b/packages/studio/src/hooks/timelineElementsAttribute.ts @@ -4,9 +4,10 @@ import { syncStoredElementAttribute } from "../player/lib/automationStoreSync"; import { saveProjectFilesWithHistory } from "../utils/studioFileHistory"; import { applyPatchByTarget } from "../utils/sourcePatcher"; import { - buildPatchTarget, findTimelineElementInIframe, + patchTimelineChangesInSource, readFileContent, + type PersistTimelineBatchChange, } from "./timelineEditingHelpers"; import type { UseTimelineElementVisibilityEditingInput } from "./timelineTrackVisibility"; import { projectForTimelineSave, type TimelineEditOutcome } from "./timelineEditPermission"; @@ -15,18 +16,20 @@ type ElementAttributeEdits = Parameters< NonNullable >[0]; -type PatchTarget = NonNullable>; - function editsByFile( edits: ElementAttributeEdits, + attr: string, activeCompPath: string | null, -): Map> { - const byFile = new Map>(); +): Map { + const byFile = new Map(); for (const { element, value } of edits) { - const target = buildPatchTarget(element); - if (!target) throw new Error("A clip has no id to save it by"); const path = element.sourceFile || activeCompPath || "index.html"; - byFile.set(path, [...(byFile.get(path) ?? []), { target, value }]); + const change: PersistTimelineBatchChange = { + element, + buildPatches: (html, target) => + applyPatchByTarget(html, target, { type: "attribute", property: attr, value }), + }; + byFile.set(path, [...(byFile.get(path) ?? []), change]); } return byFile; } @@ -70,16 +73,11 @@ export function useSetElementsAttribute({ if (typeof projectId !== "string") return projectId; if (edits.length === 0) return { status: "saved" }; try { - const byFile = editsByFile(edits, activeCompPath); + const byFile = editsByFile(edits, attr, activeCompPath); const files = Object.fromEntries( - [...byFile].map(([path, patches]) => [ + [...byFile].map(([path, changes]) => [ path, - (before: string) => - patches.reduce( - (html, { target, value }) => - applyPatchByTarget(html, target, { type: "attribute", property: attr, value }), - before, - ), + (before: string) => patchTimelineChangesInSource(before, path, changes), ]), ); for (const path of byFile.keys()) pendingTimelineEditPathRef.current.add(path); diff --git a/packages/studio/src/hooks/timelineTrackVisibility.test.ts b/packages/studio/src/hooks/timelineTrackVisibility.test.ts index 800a224f3d..98c52537f5 100644 --- a/packages/studio/src/hooks/timelineTrackVisibility.test.ts +++ b/packages/studio/src/hooks/timelineTrackVisibility.test.ts @@ -3,6 +3,8 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { usePlayerStore, type TimelineElement } from "../player"; import { resolveAudioGroups } from "@hyperframes/core/audio-groups"; +import { ensureHfIds } from "@hyperframes/parsers/hf-ids"; +import { parseTimelineFromDOM } from "../player/lib/timelineDOM"; import { readTagSnippetByTarget } from "../utils/sourcePatcher"; import { createAudioGroupAndAssignMembers } from "./timelineAudioGroupCreate"; import { toggleTimelineElementHidden, toggleTimelineTrackHidden } from "./timelineTrackVisibility"; @@ -302,6 +304,38 @@ describe("toggleTimelineTrackHidden", () => { }); describe("toggleTimelineElementHidden", () => { + it("hides a clip whose file has no ids yet, by the id the preview served it with", async () => { + // A clip added outside Studio: no id, no class, and nothing has stamped the file since. + const onDisk = `

Title

`; + stubProjectFiles(new Map([["index.html", onDisk]])); + const iframe = document.createElement("iframe"); + document.body.append(iframe); + const doc = iframe.contentDocument; + if (!doc) throw new Error("expected iframe document"); + doc.body.innerHTML = ensureHfIds(onDisk); + const [clip] = parseTimelineFromDOM(doc, 10); + if (!clip?.hfId) throw new Error("expected the served clip to carry an hf-id"); + const writes = new Map(); + + await toggleTimelineElementHidden({ + projectId: "project-1", + activeCompPath: "index.html", + timelineElements: [clip], + elementKey: clip.key ?? clip.id, + hidden: true, + previewIframe: iframe, + writeProjectFile: async (path, content) => { + writes.set(path, content); + }, + recordEdit: vi.fn(), + pendingTimelineEditPathRef: { current: new Set() }, + }); + + expect(readTagSnippetByTarget(writes.get("index.html") ?? "", { hfId: clip.hfId })).toContain( + 'data-hidden=""', + ); + }); + it("persists data-hidden for only the selected element and updates the player store", async () => { const iframe = document.createElement("iframe"); document.body.append(iframe); diff --git a/packages/studio/src/hooks/useElementPicker.test.ts b/packages/studio/src/hooks/useElementPicker.test.ts index 05192c119c..af4ba1ab46 100644 --- a/packages/studio/src/hooks/useElementPicker.test.ts +++ b/packages/studio/src/hooks/useElementPicker.test.ts @@ -8,10 +8,11 @@ import { useElementPicker } from "./useElementPicker"; import { serveEditingRequests } from "../player/hooks/timelinePlayerTestHarness"; // As the host leaves it on disk: hf-ids pinned, no element ids. -const SAVED = ensureHfIds(` +const UNSTAMPED = `

Title

-
`); +`; +const SAVED = ensureHfIds(UNSTAMPED); let root: Root | null = null; afterEach(() => { @@ -354,6 +355,12 @@ describe("an edit to a picked element without an id", () => { ); }); + it("writes the edit into a file nothing has stamped yet, by the id the preview served", () => { + const { picker, synced } = mountPicker({ "index.html": UNSTAMPED }); + act(() => picker().setStyle("color", "red")); + expect(synced[0]?.["index.html"]).toMatch(/

]*style="color: red"[^>]*>Title<\/h1>/); + }); + it("writes nothing when no saved file holds the element", () => { const { picker, synced } = mountPicker({ "index.html": "
other
" }); act(() => picker().setStyle("color", "red")); diff --git a/packages/studio/src/hooks/useElementPicker.ts b/packages/studio/src/hooks/useElementPicker.ts index b466ebd1fa..3398e8090d 100644 --- a/packages/studio/src/hooks/useElementPicker.ts +++ b/packages/studio/src/hooks/useElementPicker.ts @@ -4,7 +4,7 @@ import { resolveSourceFile, applyPatch, applyPatchByTarget, - findTagByTarget, + readTagSnippetByTarget, type PatchOperation, } from "../utils/sourcePatcher"; import { @@ -554,7 +554,9 @@ function patchByHfId( ownFile: string, op: PatchOperation, ): SourcePatch | null { - const matches = Object.keys(files).filter((file) => findTagByTarget(files[file] ?? "", { hfId })); + const matches = Object.keys(files).filter( + (file) => readTagSnippetByTarget(files[file] ?? "", { hfId }) !== undefined, + ); const path = matches.includes(ownFile) ? ownFile : matches.length === 1 ? matches[0] : undefined; const before = path ? files[path] : undefined; return path && before ? { path, before, after: applyPatchByTarget(before, { hfId }, op) } : null; diff --git a/packages/studio/src/utils/sourcePatcher.test.ts b/packages/studio/src/utils/sourcePatcher.test.ts index cecb72e63b..0417c673a8 100644 --- a/packages/studio/src/utils/sourcePatcher.test.ts +++ b/packages/studio/src/utils/sourcePatcher.test.ts @@ -7,6 +7,7 @@ import { readTagSnippetByTarget, type PatchOperation, } from "./sourcePatcher"; +import { ensureHfIds } from "@hyperframes/parsers/hf-ids"; describe("applyPatchByTarget", () => { it("removes all duplicate boolean attributes so unmuting cannot reveal another", () => { @@ -679,6 +680,26 @@ describe("T3 — hfId targeting (spec for R1)", () => { }); }); +describe("a file nothing has stamped yet", () => { + // Added outside Studio: no id, no class. The preview serves it with an hf-id the file does not hold. + const ON_DISK = `

Title

`; + const hfId = /

{ + 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; }