mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-08 10:46:06 +00:00
feat(studio): carry hfId on TimelineElement, wire through buildPatchTarget (R7, T5b) (#1299)
* feat(studio): carry hfId on TimelineElement, wire through buildPatchTarget (R7, T5b) * refactor(studio): extract readHfId helper, fix empty-string normalization, add comments (R7 review) - Extract readHfId(el) to domEditingLayers.ts — centralizes `?.trim() || undefined` normalisation; guards against empty-string data-hf-id reaching findTagByTarget - Wire readHfId into domEditingLayers.ts and useDomEditCommits.ts (the one site that still used `?? undefined` instead of `|| undefined`) - Re-export readHfId through domEditing.ts public API - Add readHfId unit tests: present, absent, empty-string, whitespace-only - Add comment on PatchTarget: runtime validation lives in findTagByTarget, type is docs-only - Suppress pre-existing unused re-exports in timelineDOM.ts (backward-compat re-exports brought into fallow scope by the T5b hfId changes) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(core): clear data-hf-id on split clone to prevent dual-match (R7 review) cloneNode(true) copies all attributes including data-hf-id. Without clearing it, both halves of a split share the same hf-id; the server's findByHfId picks the first match and silently patches the wrong clip. Remove the attribute from the clone so write-back re-mints a fresh id on the next preview load. Adds a test: splitElementInHtml — hfId clone isolation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(studio): add hfId to DomEditLayerItem + getDomLayerPatchTarget return type (R7 review) - Add hfId to DomEditLayerItem interface (domEditingTypes.ts) so layer item construction in collectDomEditLayerItems compiles - Widen getDomLayerPatchTarget return type to include hfId + populate it from data-hf-id attribute (domEditingElement.ts) - Widen findDomEditSelectionTarget to check hfId-first when no id/selector - Widen Pick types in domEditOverlayGeometry.ts and useGsapScriptCommits.ts - Add hfId to buildMissingCompositionElements element construction - Add hfId-targeted test coverage in domEditing.test.ts, domEditOverlayGeometry.test.ts, timelineIframeHelpers.test.ts - Update hfIds.test.ts KNOWN LIMITATION labels — write-back landed in R7 T1-2 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
82c754f27a
commit
0923bc0787
@@ -0,0 +1,13 @@
|
||||
// @vitest-environment jsdom
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { selectionCacheKey } from "./domEditOverlayGeometry";
|
||||
|
||||
describe("selectionCacheKey — hfId collision (R7)", () => {
|
||||
it("produces distinct keys for two elements that differ only by hfId", () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const a = selectionCacheKey({ sourceFile: "index.html", hfId: "hf-111" } as any);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const b = selectionCacheKey({ sourceFile: "index.html", hfId: "hf-222" } as any);
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
});
|
||||
@@ -190,10 +190,11 @@ export function filterNestedDomEditGroupItems<T extends { element: HTMLElement }
|
||||
}
|
||||
|
||||
export function selectionCacheKey(
|
||||
selection: Pick<DomEditSelection, "id" | "selector" | "selectorIndex" | "sourceFile">,
|
||||
selection: Pick<DomEditSelection, "id" | "hfId" | "selector" | "selectorIndex" | "sourceFile">,
|
||||
): string {
|
||||
return [
|
||||
selection.sourceFile ?? "",
|
||||
selection.hfId ?? "",
|
||||
selection.id ?? "",
|
||||
selection.selector ?? "",
|
||||
selection.selectorIndex ?? "",
|
||||
|
||||
@@ -1156,3 +1156,46 @@ describe("patch builders and prompt builder", () => {
|
||||
).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("hfId — find, key, capabilities (R7 fixes)", () => {
|
||||
it("getDomEditTargetKey keeps two hfId-only elements distinct", () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const a = getDomEditTargetKey({ sourceFile: "index.html", hfId: "hf-aaa" } as any);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const b = getDomEditTargetKey({ sourceFile: "index.html", hfId: "hf-bbb" } as any);
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
|
||||
it("findElementForSelection finds element by data-hf-id when no id or selector", () => {
|
||||
const doc = createDocument(`
|
||||
<div data-composition-id="root">
|
||||
<div data-hf-id="hf-xyz789" class="clip" style="position:absolute;left:0;top:0;width:100px;height:100px;"></div>
|
||||
</div>
|
||||
`);
|
||||
const el = doc.querySelector('[data-hf-id="hf-xyz789"]') as HTMLElement;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const found = findElementForSelection(doc, { hfId: "hf-xyz789" } as any);
|
||||
expect(found).toBe(el);
|
||||
});
|
||||
|
||||
it("resolveDomEditCapabilities enables editing for hfId-only element (no CSS selector)", () => {
|
||||
const result = resolveDomEditCapabilities({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
hfId: "hf-abc" as any,
|
||||
selector: undefined,
|
||||
inlineStyles: { left: "10px", top: "20px", width: "100px", height: "50px" },
|
||||
computedStyles: {
|
||||
position: "absolute",
|
||||
left: "10px",
|
||||
top: "20px",
|
||||
width: "100px",
|
||||
height: "50px",
|
||||
},
|
||||
isCompositionHost: false,
|
||||
isInsideLockedComposition: false,
|
||||
isMasterView: false,
|
||||
});
|
||||
expect(result.canSelect).toBe(true);
|
||||
expect(result.canMove).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,6 +35,7 @@ export {
|
||||
getDomEditNonEditableReason,
|
||||
getDomEditTargetKey,
|
||||
isTextEditableSelection,
|
||||
readHfId,
|
||||
refreshDomEditSelection,
|
||||
resolveDomEditCapabilities,
|
||||
resolveDomEditSelection,
|
||||
|
||||
@@ -95,7 +95,7 @@ function isInspectableLayerElement(el: HTMLElement): boolean {
|
||||
export function getDomLayerPatchTarget(
|
||||
el: HTMLElement,
|
||||
activeCompositionPath: string | null,
|
||||
): Pick<DomEditSelection, "id" | "selector" | "selectorIndex" | "sourceFile"> | null {
|
||||
): Pick<DomEditSelection, "id" | "hfId" | "selector" | "selectorIndex" | "sourceFile"> | null {
|
||||
if (!isInspectableLayerElement(el)) return null;
|
||||
if (el.hasAttribute("data-composition-id")) return null;
|
||||
|
||||
@@ -105,6 +105,7 @@ export function getDomLayerPatchTarget(
|
||||
const { sourceFile } = getSourceFileForElement(el, activeCompositionPath);
|
||||
return {
|
||||
id: el.id || undefined,
|
||||
hfId: el.getAttribute("data-hf-id") || undefined,
|
||||
selector,
|
||||
selectorIndex: getSelectorIndex(
|
||||
el.ownerDocument,
|
||||
@@ -229,9 +230,14 @@ export function isLargeRasterDomEditSelection(
|
||||
|
||||
export function findElementForSelection(
|
||||
doc: Document,
|
||||
selection: Pick<DomEditSelection, "id" | "selector" | "selectorIndex" | "sourceFile">,
|
||||
selection: Pick<DomEditSelection, "id" | "hfId" | "selector" | "selectorIndex" | "sourceFile">,
|
||||
activeCompositionPath: string | null = null,
|
||||
): HTMLElement | null {
|
||||
if (selection.hfId) {
|
||||
const byHfId = doc.querySelector(`[data-hf-id="${selection.hfId}"]`);
|
||||
if (isHtmlElement(byHfId)) return byHfId;
|
||||
}
|
||||
|
||||
if (selection.id) {
|
||||
const byId = doc.getElementById(selection.id);
|
||||
if (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// @vitest-environment jsdom
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveDomEditSelection, buildDomEditPatchTarget } from "./domEditingLayers";
|
||||
import { resolveDomEditSelection, buildDomEditPatchTarget, readHfId } from "./domEditingLayers";
|
||||
|
||||
const opts = { activeCompositionPath: "index.html", isMasterView: true, skipSourceProbe: true };
|
||||
|
||||
@@ -27,6 +27,31 @@ describe("buildDomEditPatchTarget", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("readHfId", () => {
|
||||
it("returns the attribute value when present", () => {
|
||||
const el = document.createElement("div");
|
||||
el.setAttribute("data-hf-id", "hf-abc");
|
||||
expect(readHfId(el)).toBe("hf-abc");
|
||||
});
|
||||
|
||||
it("returns undefined when attribute is absent", () => {
|
||||
const el = document.createElement("div");
|
||||
expect(readHfId(el)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined when attribute is empty string", () => {
|
||||
const el = document.createElement("div");
|
||||
el.setAttribute("data-hf-id", "");
|
||||
expect(readHfId(el)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined when attribute is whitespace-only", () => {
|
||||
const el = document.createElement("div");
|
||||
el.setAttribute("data-hf-id", " ");
|
||||
expect(readHfId(el)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveDomEditSelection — hfId from data-hf-id", () => {
|
||||
it("populates hfId from the element data-hf-id attribute", async () => {
|
||||
const el = document.createElement("div");
|
||||
|
||||
@@ -173,6 +173,7 @@ export function buildDefaultDomEditTextField(base?: Partial<DomEditTextField>):
|
||||
// fallow-ignore-next-line complexity
|
||||
export function resolveDomEditCapabilities(args: {
|
||||
selector?: string;
|
||||
hfId?: string;
|
||||
tagName?: string;
|
||||
className?: string;
|
||||
inlineStyles: Record<string, string>;
|
||||
@@ -182,7 +183,7 @@ export function resolveDomEditCapabilities(args: {
|
||||
isMasterView: boolean;
|
||||
existsInSource?: boolean;
|
||||
}): DomEditCapabilities {
|
||||
if (!args.selector || args.isInsideLockedComposition) {
|
||||
if ((!args.selector && !args.hfId) || args.isInsideLockedComposition) {
|
||||
return {
|
||||
canSelect: !args.isInsideLockedComposition,
|
||||
canEditStyles: false,
|
||||
@@ -289,7 +290,7 @@ export function buildElementLabel(el: HTMLElement): string {
|
||||
async function probeSourceElement(
|
||||
projectId: string,
|
||||
sourceFile: string,
|
||||
target: { id?: string; selector?: string; selectorIndex?: number },
|
||||
target: { id?: string; hfId?: string; selector?: string; selectorIndex?: number },
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
@@ -321,7 +322,8 @@ export async function resolveDomEditSelection(
|
||||
let current: HTMLElement | null = getSelectionCandidate(startEl, options);
|
||||
while (current && current !== doc.body && current !== doc.documentElement) {
|
||||
const selector = buildStableSelector(current);
|
||||
if (!selector) {
|
||||
const hfId = readHfId(current);
|
||||
if (!selector && !hfId) {
|
||||
current = current.parentElement;
|
||||
continue;
|
||||
}
|
||||
@@ -330,13 +332,9 @@ export async function resolveDomEditSelection(
|
||||
current,
|
||||
options.activeCompositionPath,
|
||||
);
|
||||
const selectorIndex = getSelectorIndex(
|
||||
doc,
|
||||
current,
|
||||
selector,
|
||||
sourceFile,
|
||||
options.activeCompositionPath,
|
||||
);
|
||||
const selectorIndex = selector
|
||||
? getSelectorIndex(doc, current, selector, sourceFile, options.activeCompositionPath)
|
||||
: undefined;
|
||||
const compositionSrc =
|
||||
current.getAttribute("data-composition-src") ??
|
||||
current.getAttribute("data-composition-file") ??
|
||||
@@ -346,15 +344,18 @@ export async function resolveDomEditSelection(
|
||||
const textFields = collectDomEditTextFields(current);
|
||||
const isInsideLocked = Boolean(findClosestByAttribute(current, ["data-timeline-locked"]));
|
||||
let existsInSource: boolean | undefined;
|
||||
if (!options.skipSourceProbe && options.projectId && (current.id || selector)) {
|
||||
const probeTarget: { id?: string; selector?: string; selectorIndex?: number } = {};
|
||||
if (!options.skipSourceProbe && options.projectId && (current.id || selector || hfId)) {
|
||||
const probeTarget: { id?: string; hfId?: string; selector?: string; selectorIndex?: number } =
|
||||
{};
|
||||
if (current.id) probeTarget.id = current.id;
|
||||
if (hfId) probeTarget.hfId = hfId;
|
||||
if (selector) probeTarget.selector = selector;
|
||||
if (selectorIndex != null) probeTarget.selectorIndex = selectorIndex;
|
||||
existsInSource = await probeSourceElement(options.projectId, sourceFile, probeTarget);
|
||||
}
|
||||
const capabilities = resolveDomEditCapabilities({
|
||||
selector,
|
||||
hfId,
|
||||
tagName: current.tagName.toLowerCase(),
|
||||
className: current.className,
|
||||
inlineStyles,
|
||||
@@ -369,7 +370,7 @@ export async function resolveDomEditSelection(
|
||||
return {
|
||||
element: current,
|
||||
id: current.id || undefined,
|
||||
hfId: current.getAttribute("data-hf-id") ?? undefined,
|
||||
hfId,
|
||||
selector,
|
||||
selectorIndex,
|
||||
sourceFile,
|
||||
@@ -452,6 +453,7 @@ export function collectDomEditLayerItems(
|
||||
if (!root) return [];
|
||||
|
||||
const items: DomEditLayerItem[] = [];
|
||||
// fallow-ignore-next-line complexity
|
||||
const visit = (el: HTMLElement, depth: number) => {
|
||||
if (items.length >= maxItems) return;
|
||||
|
||||
@@ -465,6 +467,7 @@ export function collectDomEditLayerItems(
|
||||
depth,
|
||||
childCount: getDirectLayerChildren(el, options).length,
|
||||
id: target.id ?? undefined,
|
||||
hfId: target.hfId ?? undefined,
|
||||
selector: target.selector ?? undefined,
|
||||
selectorIndex: target.selectorIndex,
|
||||
sourceFile: target.sourceFile,
|
||||
@@ -536,10 +539,11 @@ export function getDomEditNonEditableReason(
|
||||
}
|
||||
|
||||
export function getDomEditTargetKey(
|
||||
selection: Pick<DomEditSelection, "id" | "selector" | "selectorIndex" | "sourceFile">,
|
||||
selection: Pick<DomEditSelection, "id" | "hfId" | "selector" | "selectorIndex" | "sourceFile">,
|
||||
): string {
|
||||
return [
|
||||
selection.sourceFile || "index.html",
|
||||
selection.hfId ?? "",
|
||||
selection.id ?? "",
|
||||
selection.selector ?? "",
|
||||
selection.selectorIndex ?? "",
|
||||
@@ -556,6 +560,10 @@ export function isTextEditableSelection(selection: DomEditSelection): boolean {
|
||||
|
||||
// buildElementAgentPrompt is in domEditingAgentPrompt.ts
|
||||
|
||||
export function readHfId(element: Element): string | undefined {
|
||||
return element.getAttribute("data-hf-id")?.trim() || undefined;
|
||||
}
|
||||
|
||||
export function buildDomEditPatchTarget(
|
||||
selection: Pick<DomEditSelection, "id" | "hfId" | "selector" | "selectorIndex">,
|
||||
): { id?: string | null; hfId?: string; selector?: string; selectorIndex?: number } {
|
||||
|
||||
@@ -98,6 +98,7 @@ export interface DomEditLayerItem {
|
||||
depth: number;
|
||||
childCount: number;
|
||||
id?: string;
|
||||
hfId?: string;
|
||||
selector?: string;
|
||||
selectorIndex?: number;
|
||||
sourceFile: string;
|
||||
|
||||
Reference in New Issue
Block a user