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.
This commit is contained in:
Miguel Ángel
2026-11-14 02:48:59 +00:00
committed by GitHub
parent 798c241e4d
commit cb072eb047
9 changed files with 194 additions and 28 deletions
@@ -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 = `<div data-composition-id="main" data-start="0" data-duration="10"><img src="a.png" data-start="1" data-duration="3" data-volume="0.5"></div>`;
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.
@@ -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(
@@ -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<Record<string, unknown>> }));
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 = `<div data-composition-id="main" data-start="0" data-duration="10"><audio src="a.mp3" data-start="0" data-duration="4" data-track-index="0"></audio><audio src="b.mp3" data-start="4" data-duration="4" data-track-index="1"></audio></div>`;
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<typeof useSetElementsAttribute> | 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<string>() },
});
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();
@@ -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<TimelineEditCallbacks["onSetElementsAttributeQuiet"]>
>[0];
type PatchTarget = NonNullable<ReturnType<typeof buildPatchTarget>>;
function editsByFile(
edits: ElementAttributeEdits,
attr: string,
activeCompPath: string | null,
): Map<string, Array<{ target: PatchTarget; value: string | null }>> {
const byFile = new Map<string, Array<{ target: PatchTarget; value: string | null }>>();
): Map<string, PersistTimelineBatchChange[]> {
const byFile = new Map<string, PersistTimelineBatchChange[]>();
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);
@@ -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 = `<div data-composition-id="main" data-start="0" data-duration="10"><p data-start="1" data-duration="3" data-track-index="0">Title</p></div>`;
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<string, string>();
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);
@@ -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(`<!doctype html><html><body>
const UNSTAMPED = `<!doctype html><html><body>
<div data-composition-id="main" data-start="0" data-duration="10">
<h1 class="clip" data-start="2" data-duration="3" data-track-index="0">Title</h1>
</div></body></html>`);
</div></body></html>`;
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(/<h1 [^>]*style="color: red"[^>]*>Title<\/h1>/);
});
it("writes nothing when no saved file holds the element", () => {
const { picker, synced } = mountPicker({ "index.html": "<div>other</div>" });
act(() => picker().setStyle("color", "red"));
@@ -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;
@@ -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 = `<div data-composition-id="main"><p data-start="1" data-volume="0.5">Title</p></div>`;
const hfId = /<p data-hf-id="([^"]+)"/.exec(ensureHfIds(ON_DISK))?.[1] ?? "";
const op: PatchOperation = { type: "attribute", property: "volume", value: "0.9" };
it("reads, finds and patches the element by its served hf-id", () => {
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",
+16 -8
View File
@@ -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;
}