Skip to content

Commit d972915

Browse files
committed
feat(studio,core): populate hfId in DomEditSelection + widen MutationTarget (R7, T5a)
1 parent 04c77fa commit d972915

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

packages/core/src/studio-api/routes/files.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ function resolveFileMutationContext(c: RouteContext, adapter: StudioApiAdapter,
8484
return resolveProjectPath(c, adapter, (id) => `/projects/${id}/file-mutations/${operation}/`);
8585
}
8686

87-
type MutationTarget = { id?: string | null; selector?: string; selectorIndex?: number };
87+
type MutationTarget = {
88+
id?: string | null;
89+
hfId?: string;
90+
selector?: string;
91+
selectorIndex?: number;
92+
};
8893

8994
/** Write `next` to `absPath` only if it differs from `original`, returning a standardized change response. */
9095
function writeIfChanged(
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @vitest-environment jsdom
2+
import { describe, expect, it } from "vitest";
3+
import { resolveDomEditSelection } from "./domEditingLayers";
4+
5+
const opts = { activeCompositionPath: "index.html", isMasterView: true, skipSourceProbe: true };
6+
7+
describe("resolveDomEditSelection — hfId from data-hf-id", () => {
8+
it("populates hfId from the element data-hf-id attribute", async () => {
9+
const el = document.createElement("div");
10+
el.id = "hero";
11+
el.setAttribute("data-hf-id", "hf-x7k2");
12+
document.body.appendChild(el);
13+
14+
const selection = await resolveDomEditSelection(el, opts);
15+
document.body.removeChild(el);
16+
17+
expect(selection?.hfId).toBe("hf-x7k2");
18+
});
19+
20+
it("leaves hfId undefined when element has no data-hf-id", async () => {
21+
const el = document.createElement("div");
22+
el.id = "no-hfid-el";
23+
document.body.appendChild(el);
24+
25+
const selection = await resolveDomEditSelection(el, opts);
26+
document.body.removeChild(el);
27+
28+
expect(selection?.hfId).toBeUndefined();
29+
});
30+
});

packages/studio/src/components/editor/domEditingLayers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ export async function resolveDomEditSelection(
369369
return {
370370
element: current,
371371
id: current.id || undefined,
372+
hfId: current.getAttribute("data-hf-id") ?? undefined,
372373
selector,
373374
selectorIndex,
374375
sourceFile,

0 commit comments

Comments
 (0)