From 62f212aacf850fde063b06ee2a7cff70eef04c0c Mon Sep 17 00:00:00 2001 From: Vance Ingalls Date: Fri, 1 May 2026 12:04:04 -0700 Subject: [PATCH] feat: persist studio manual edits via manifest --- .../src/studio-api/routes/thumbnail.test.ts | 29 +- .../core/src/studio-api/routes/thumbnail.ts | 11 +- .../producer/src/services/fileServer.test.ts | 34 + packages/producer/src/services/fileServer.ts | 7 +- .../src/services/renderOrchestrator.ts | 4 + packages/studio/src/App.tsx | 433 ++++--- .../components/editor/DomEditOverlay.test.ts | 127 ++ .../src/components/editor/DomEditOverlay.tsx | 403 +++++-- .../src/components/editor/PropertyPanel.tsx | 59 - .../src/components/editor/domEditing.test.ts | 98 +- .../src/components/editor/domEditing.ts | 163 +-- .../src/components/editor/manualEdits.test.ts | 577 +++++++++ .../src/components/editor/manualEdits.ts | 1042 +++++++++++++++++ .../editor/manualEditsRenderScript.test.ts | 188 +++ .../editor/manualEditsRenderScript.ts | 273 +++++ .../src/components/renders/RenderQueue.tsx | 22 +- .../hooks/usePersistentEditHistory.test.ts | 1 + .../src/hooks/usePersistentEditHistory.ts | 5 +- .../player/hooks/useTimelinePlayer.test.ts | 1 + packages/studio/src/utils/clipboard.test.ts | 1 + packages/studio/vite.config.ts | 256 +++- 21 files changed, 3296 insertions(+), 438 deletions(-) create mode 100644 packages/studio/src/components/editor/DomEditOverlay.test.ts create mode 100644 packages/studio/src/components/editor/manualEdits.test.ts create mode 100644 packages/studio/src/components/editor/manualEdits.ts create mode 100644 packages/studio/src/components/editor/manualEditsRenderScript.test.ts create mode 100644 packages/studio/src/components/editor/manualEditsRenderScript.ts diff --git a/packages/core/src/studio-api/routes/thumbnail.test.ts b/packages/core/src/studio-api/routes/thumbnail.test.ts index e225d4d066..0c2697aa8c 100644 --- a/packages/core/src/studio-api/routes/thumbnail.test.ts +++ b/packages/core/src/studio-api/routes/thumbnail.test.ts @@ -1,6 +1,6 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { Hono } from "hono"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { registerThumbnailRoutes } from "./thumbnail"; @@ -151,22 +151,27 @@ describe("registerThumbnailRoutes", () => { ); }); - it("preserves an explicit zero seek time", async () => { + it("keeps changed studio manual edits separated in the disk cache", async () => { const adapter = createAdapter(); + const project = await adapter.resolveProject("demo"); + if (!project) throw new Error("missing project"); const app = new Hono(); registerThumbnailRoutes(app, adapter); - const response = await app.request( - "http://localhost/projects/demo/thumbnail/index.html?t=0&format=png", - ); + const indexPath = join(project.dir, "index.html"); + writeFileSync(indexPath, `
`); + const manualEditsDir = join(project.dir, ".hyperframes"); + mkdirSync(manualEditsDir, { recursive: true }); + const manualEditsPath = join(manualEditsDir, "studio-manual-edits.json"); + writeFileSync(manualEditsPath, `{"version":1,"edits":[]}`); - expect(response.status).toBe(200); - expect(adapter.generateThumbnail).toHaveBeenCalledWith( - expect.objectContaining({ - compPath: "index.html", - seekTime: 0, - format: "png", - }), + await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=test"); + writeFileSync( + manualEditsPath, + `{"version":1,"edits":[{"kind":"rotation","target":{"sourceFile":"index.html","id":"card"},"angle":30}]}`, ); + await app.request("http://localhost/projects/demo/thumbnail/index.html?t=2&v=test"); + + expect(adapter.generateThumbnail).toHaveBeenCalledTimes(2); }); }); diff --git a/packages/core/src/studio-api/routes/thumbnail.ts b/packages/core/src/studio-api/routes/thumbnail.ts index 34c1187e25..5f9c746504 100644 --- a/packages/core/src/studio-api/routes/thumbnail.ts +++ b/packages/core/src/studio-api/routes/thumbnail.ts @@ -1,9 +1,11 @@ import type { Hono } from "hono"; import { existsSync, readFileSync, writeFileSync, mkdirSync, statSync } from "node:fs"; import { join } from "node:path"; +import { createHash } from "node:crypto"; import type { StudioApiAdapter } from "../types.js"; const THUMBNAIL_CACHE_VERSION = "v3"; +const STUDIO_MANUAL_EDITS_PATH = ".hyperframes/studio-manual-edits.json"; export function registerThumbnailRoutes(api: Hono, adapter: StudioApiAdapter): void { api.get("/projects/:id/thumbnail/*", async (c) => { @@ -47,6 +49,13 @@ export function registerThumbnailRoutes(api: Hono, adapter: StudioApiAdapter): v if (hMatch?.[1]) compH = parseInt(hMatch[1]); } } + const manualEditsFile = join(project.dir, STUDIO_MANUAL_EDITS_PATH); + let manualEditsKey = ""; + if (existsSync(manualEditsFile)) { + const manualEditsContent = readFileSync(manualEditsFile, "utf-8"); + manualEditsKey = `_${createHash("sha1").update(manualEditsContent).digest("hex").slice(0, 16)}`; + sourceMtime = Math.max(sourceMtime, Math.round(statSync(manualEditsFile).mtimeMs)); + } const previewUrl = compPath === "index.html" @@ -61,7 +70,7 @@ export function registerThumbnailRoutes(api: Hono, adapter: StudioApiAdapter): v const urlVersionKey = urlVersion ? `_${urlVersion.replace(/[^a-zA-Z0-9_-]+/g, "_").slice(0, 32)}` : ""; - const cacheKey = `${THUMBNAIL_CACHE_VERSION}${urlVersionKey}_${format}_${compPath.replace(/\//g, "_")}_${compW}x${compH}_${sourceMtime}_${seekTime.toFixed(2)}${selectorKey}.${format === "png" ? "png" : "jpg"}`; + const cacheKey = `${THUMBNAIL_CACHE_VERSION}${urlVersionKey}${manualEditsKey}_${format}_${compPath.replace(/\//g, "_")}_${compW}x${compH}_${sourceMtime}_${seekTime.toFixed(2)}${selectorKey}.${format === "png" ? "png" : "jpg"}`; const cachePath = join(cacheDir, cacheKey); if (existsSync(cachePath)) { return new Response(new Uint8Array(readFileSync(cachePath)), { diff --git a/packages/producer/src/services/fileServer.test.ts b/packages/producer/src/services/fileServer.test.ts index 426dfb31dc..643fc34dcf 100644 --- a/packages/producer/src/services/fileServer.test.ts +++ b/packages/producer/src/services/fileServer.test.ts @@ -153,6 +153,40 @@ describe("isPathInside", () => { }); describe("createFileServer", () => { + it("appends additional body scripts after configured body scripts", async () => { + const projectDir = mkdtempSync(join(tmpdir(), "hf-file-server-extra-body-")); + + try { + writeFileSync( + join(projectDir, "index.html"), + "", + ); + + const server = await createFileServer({ + projectDir, + preHeadScripts: [], + headScripts: [], + bodyScripts: ["window.__bodyScript = true;"], + additionalBodyScripts: ["window.__additionalBodyScript = true;"], + }); + + try { + const response = await fetch(`${server.url}/index.html`); + const html = await response.text(); + + expect(response.status).toBe(200); + expect(html.indexOf("window.__bodyScript = true;")).toBeGreaterThanOrEqual(0); + expect(html.indexOf("window.__additionalBodyScript = true;")).toBeGreaterThan( + html.indexOf("window.__bodyScript = true;"), + ); + } finally { + server.close(); + } + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }); + it("serves asset files through project-root symlinked directories", async () => { const workspaceDir = mkdtempSync(join(tmpdir(), "hf-file-server-symlink-assets-")); const adsDir = join(workspaceDir, "Ads"); diff --git a/packages/producer/src/services/fileServer.ts b/packages/producer/src/services/fileServer.ts index 2f96c6e9a0..bc36072c36 100644 --- a/packages/producer/src/services/fileServer.ts +++ b/packages/producer/src/services/fileServer.ts @@ -420,6 +420,8 @@ export interface FileServerOptions { headScripts?: string[]; /** Scripts injected before of index.html. Default: render mode extension. */ bodyScripts?: string[]; + /** Scripts injected after bodyScripts before of index.html. */ + additionalBodyScripts?: string[]; /** Strip embedded runtime scripts from HTML before injection. Default: true. */ stripEmbeddedRuntime?: boolean; } @@ -442,7 +444,10 @@ export function createFileServer(options: FileServerOptions): Promise, render mode in const headScripts = options.headScripts ?? [getVerifiedHyperframeRuntimeSource()]; - const bodyScripts = options.bodyScripts ?? [RENDER_MODE_SCRIPT, HF_BRIDGE_SCRIPT]; + const bodyScripts = [ + ...(options.bodyScripts ?? [RENDER_MODE_SCRIPT, HF_BRIDGE_SCRIPT]), + ...(options.additionalBodyScripts ?? []), + ]; const app = new Hono(); diff --git a/packages/producer/src/services/renderOrchestrator.ts b/packages/producer/src/services/renderOrchestrator.ts index c7026c55de..ca3d423df2 100644 --- a/packages/producer/src/services/renderOrchestrator.ts +++ b/packages/producer/src/services/renderOrchestrator.ts @@ -260,6 +260,8 @@ export interface RenderConfig { producerConfig?: EngineConfig; /** Custom logger. Defaults to console-based defaultLogger. */ logger?: ProducerLogger; + /** Extra scripts injected after the render runtime before in index.html. */ + renderBodyScripts?: string[]; /** Override CRF for the video encoder. Mutually exclusive with `videoBitrate`. */ crf?: number; /** Target video bitrate (e.g. "10M"). Mutually exclusive with `crf`. */ @@ -2169,6 +2171,7 @@ export async function executeRenderJob( compiledDir: join(workDir, "compiled"), port: 0, preHeadScripts: [VIRTUAL_TIME_SHIM], + additionalBodyScripts: job.config.renderBodyScripts, }); assertNotAborted(); @@ -2631,6 +2634,7 @@ export async function executeRenderJob( compiledDir: join(workDir, "compiled"), port: 0, preHeadScripts: [VIRTUAL_TIME_SHIM], + additionalBodyScripts: job.config.renderBodyScripts, }); assertNotAborted(); } diff --git a/packages/studio/src/App.tsx b/packages/studio/src/App.tsx index f6d6270c50..606a08f619 100644 --- a/packages/studio/src/App.tsx +++ b/packages/studio/src/App.tsx @@ -73,13 +73,11 @@ import { import { DomEditOverlay } from "./components/editor/DomEditOverlay"; import { buildDefaultDomEditTextField, - buildDomEditDetachPatchOperations, - buildDomEditMovePatchOperations, - buildDomEditResizePatchOperations, buildDomEditStylePatchOperation, buildDomEditTextPatchOperation, buildElementAgentPrompt, findElementForSelection, + getDomEditTargetKey, isTextEditableSelection, serializeDomEditTextFields, resolveDomEditCapabilities, @@ -87,6 +85,16 @@ import { type DomEditTextField, type DomEditSelection, } from "./components/editor/domEditing"; +import { + STUDIO_MANUAL_EDITS_PATH, + applyStudioManualEditManifest, + installStudioManualEditSeekReapply, + parseStudioManualEditManifest, + serializeStudioManualEditManifest, + upsertStudioBoxSizeEdit, + upsertStudioPathOffsetEdit, + upsertStudioRotationEdit, +} from "./components/editor/manualEdits"; import { saveProjectFilesWithHistory } from "./utils/studioFileHistory"; interface EditingFile { @@ -290,14 +298,6 @@ function getHistoryShortcutLabel(action: "undo" | "redo"): string { return action === "undo" ? `${modifier}+Z` : `${modifier}+Shift+Z`; } -function getDomEditCoalesceKey( - selection: Pick, - action: "move" | "resize", -): string { - const target = selection.id || selection.selector || "selection"; - return `${action}:${selection.sourceFile || "index.html"}:${target}`; -} - function findMatchingTimelineElementId( selection: Pick< DomEditSelection, @@ -399,37 +399,6 @@ function isResizeStyleProperty(property: string): boolean { return property === "width" || property === "height"; } -function getDomDetachCoordinateRoot(element: HTMLElement): HTMLElement { - const offsetParent = element.offsetParent; - if (offsetParent instanceof HTMLElement) return offsetParent; - - let current = element.parentElement; - while (current) { - if (current.hasAttribute("data-composition-id")) return current; - current = current.parentElement; - } - - return element.ownerDocument.body; -} - -function measureDomDetachRect(element: HTMLElement): { - left: number; - top: number; - width: number; - height: number; -} { - const root = getDomDetachCoordinateRoot(element); - const rect = element.getBoundingClientRect(); - const rootRect = root.getBoundingClientRect(); - - return { - left: rect.left - rootRect.left + root.scrollLeft, - top: rect.top - rootRect.top + root.scrollTop, - width: rect.width, - height: rect.height, - }; -} - function getDomSelectionClickKey( selection: Pick, ): string { @@ -785,6 +754,7 @@ export function StudioApp() { const lastBlockedDomMoveToastAtRef = useRef(0); const importedFontAssetsRef = useRef([]); const previewHotkeyWindowRef = useRef(null); + const previewHistoryHotkeyCleanupRef = useRef<(() => void) | null>(null); const panelDragRef = useRef<{ side: "left" | "right"; startX: number; @@ -1106,6 +1076,20 @@ export function StudioApp() { const lastPreviewClickRef = useRef<{ key: string; at: number } | null>(null); const domEditSaveTimestampRef = useRef(0); const domTextCommitVersionRef = useRef(0); + const domEditSaveQueueRef = useRef(Promise.resolve()); + + const queueDomEditSave = useCallback((save: () => Promise) => { + const queuedSave = domEditSaveQueueRef.current.catch(() => undefined).then(save); + domEditSaveQueueRef.current = queuedSave.then( + () => undefined, + () => undefined, + ); + return queuedSave; + }, []); + + const waitForPendingDomEditSaves = useCallback(async () => { + await domEditSaveQueueRef.current.catch(() => undefined); + }, []); // Listen for external file changes (user editing HTML outside the editor). // In dev: use Vite HMR. In embedded/production: use SSE from /api/events. @@ -1200,6 +1184,16 @@ export function StudioApp() { } }, []); + const readOptionalProjectFile = useCallback(async (path: string): Promise => { + const pid = projectIdRef.current; + if (!pid) throw new Error("No active project"); + const response = await fetch(`/api/projects/${pid}/files/${encodeURIComponent(path)}`); + if (response.status === 404) return ""; + if (!response.ok) throw new Error(`Failed to read ${path}`); + const data = (await response.json()) as { content?: string }; + return typeof data.content === "string" ? data.content : ""; + }, []); + const handleContentChange = useCallback( (content: string) => { const pid = projectIdRef.current; @@ -1420,6 +1414,7 @@ export function StudioApp() { const currentTime = usePlayerStore.getState().currentTime; setCaptureFrameTime(currentTime); + await waitForPendingDomEditSaves(); const href = buildFrameCaptureUrl({ projectId, compositionPath: activeCompPath, @@ -1446,7 +1441,7 @@ export function StudioApp() { showToast(message); } }, - [activeCompPath, projectId, showToast], + [activeCompPath, projectId, showToast, waitForPendingDomEditSaves], ); const handleTimelineElementDelete = useCallback( @@ -1573,10 +1568,8 @@ export function StudioApp() { if (now - lastBlockedDomMoveToastAtRef.current < 1500) return; lastBlockedDomMoveToastAtRef.current = now; showToast( - selection.capabilities.canDetachFromLayout - ? "This layer is controlled by layout. Use Make movable in the panel to detach it." - : (selection.capabilities.reasonIfDisabled ?? - "This element can’t be moved directly from the preview."), + selection.capabilities.reasonIfDisabled ?? + "This element can’t be adjusted directly from the preview.", "info", ); }, @@ -1607,10 +1600,82 @@ export function StudioApp() { applyDomSelection(null, { revealPanel: false }); }, [applyDomSelection]); + const readHistoryProjectFile = useCallback( + async (path: string): Promise => { + return path === STUDIO_MANUAL_EDITS_PATH + ? readOptionalProjectFile(path) + : readProjectFile(path); + }, + [readOptionalProjectFile, readProjectFile], + ); + + const writeHistoryProjectFile = useCallback( + async (path: string, content: string): Promise => { + await writeProjectFile(path, content); + if (path === STUDIO_MANUAL_EDITS_PATH) { + domEditSaveTimestampRef.current = Date.now(); + } + }, + [writeProjectFile], + ); + + const applyStudioManualEditsToPreview = useCallback( + async (iframe: HTMLIFrameElement | null = previewIframeRef.current) => { + if (!iframe) return; + let doc: Document | null = null; + try { + doc = iframe.contentDocument; + } catch { + return; + } + if (!doc) return; + const previewDoc = doc; + + const content = await readOptionalProjectFile(STUDIO_MANUAL_EDITS_PATH).catch(() => ""); + const manifest = parseStudioManualEditManifest(content); + const applyManifest = () => { + applyStudioManualEditManifest(previewDoc, manifest, activeCompPath); + }; + const applyAndInstallSeekHooks = () => { + applyManifest(); + if (iframe.contentWindow) { + installStudioManualEditSeekReapply(iframe.contentWindow, applyManifest); + } + }; + + const win = iframe.contentWindow; + applyAndInstallSeekHooks(); + win?.requestAnimationFrame?.(applyAndInstallSeekHooks); + win?.setTimeout?.(applyAndInstallSeekHooks, 80); + win?.setTimeout?.(applyAndInstallSeekHooks, 250); + win?.setTimeout?.(applyAndInstallSeekHooks, 500); + win?.setTimeout?.(applyAndInstallSeekHooks, 1000); + win?.setTimeout?.(applyAndInstallSeekHooks, 2000); + }, + [activeCompPath, readOptionalProjectFile], + ); + + const syncHistoryPreviewAfterApply = useCallback( + async (paths: string[] | undefined) => { + const changedPaths = paths ?? []; + const manualManifestOnly = + changedPaths.length > 0 && changedPaths.every((path) => path === STUDIO_MANUAL_EDITS_PATH); + + if (manualManifestOnly) { + await applyStudioManualEditsToPreview(previewIframeRef.current); + return; + } + + setRefreshKey((key) => key + 1); + }, + [applyStudioManualEditsToPreview], + ); + const handleUndo = useCallback(async () => { + await waitForPendingDomEditSaves(); const result = await editHistory.undo({ - readFile: readProjectFile, - writeFile: writeProjectFile, + readFile: readHistoryProjectFile, + writeFile: writeHistoryProjectFile, }); if (!result.ok && result.reason === "content-mismatch") { showToast("File changed outside Studio. Undo history was not applied.", "info"); @@ -1618,15 +1683,24 @@ export function StudioApp() { } if (result.ok && result.label) { clearDomSelection(); - setRefreshKey((key) => key + 1); + await syncHistoryPreviewAfterApply(result.paths); showToast(`Undid ${result.label}`, "info"); } - }, [clearDomSelection, editHistory, readProjectFile, showToast, writeProjectFile]); + }, [ + clearDomSelection, + editHistory, + readHistoryProjectFile, + showToast, + syncHistoryPreviewAfterApply, + waitForPendingDomEditSaves, + writeHistoryProjectFile, + ]); const handleRedo = useCallback(async () => { + await waitForPendingDomEditSaves(); const result = await editHistory.redo({ - readFile: readProjectFile, - writeFile: writeProjectFile, + readFile: readHistoryProjectFile, + writeFile: writeHistoryProjectFile, }); if (!result.ok && result.reason === "content-mismatch") { showToast("File changed outside Studio. Redo history was not applied.", "info"); @@ -1634,35 +1708,76 @@ export function StudioApp() { } if (result.ok && result.label) { clearDomSelection(); - setRefreshKey((key) => key + 1); + await syncHistoryPreviewAfterApply(result.paths); showToast(`Redid ${result.label}`, "info"); } - }, [clearDomSelection, editHistory, readProjectFile, showToast, writeProjectFile]); + }, [ + clearDomSelection, + editHistory, + readHistoryProjectFile, + showToast, + syncHistoryPreviewAfterApply, + waitForPendingDomEditSaves, + writeHistoryProjectFile, + ]); const handleUndoRef = useRef(handleUndo); const handleRedoRef = useRef(handleRedo); handleUndoRef.current = handleUndo; handleRedoRef.current = handleRedo; + const handleHistoryHotkey = useCallback((event: KeyboardEvent) => { + if (!(event.metaKey || event.ctrlKey)) return; + if (shouldIgnoreHistoryShortcut(event.target)) return; + const key = event.key.toLowerCase(); + if (key === "z" && !event.shiftKey) { + event.preventDefault(); + void handleUndoRef.current(); + return; + } + if ((key === "z" && event.shiftKey) || (event.ctrlKey && !event.metaKey && key === "y")) { + event.preventDefault(); + void handleRedoRef.current(); + } + }, []); + // eslint-disable-next-line no-restricted-syntax useEffect(() => { - const handler = (event: KeyboardEvent) => { - if (!(event.metaKey || event.ctrlKey)) return; - if (shouldIgnoreHistoryShortcut(event.target)) return; - const key = event.key.toLowerCase(); - if (key === "z" && !event.shiftKey) { - event.preventDefault(); - void handleUndoRef.current(); - return; + window.addEventListener("keydown", handleHistoryHotkey, true); + return () => window.removeEventListener("keydown", handleHistoryHotkey, true); + }, [handleHistoryHotkey]); + + const syncPreviewHistoryHotkey = useCallback( + (iframe: HTMLIFrameElement | null) => { + previewHistoryHotkeyCleanupRef.current?.(); + previewHistoryHotkeyCleanupRef.current = null; + + const win = iframe?.contentWindow ?? null; + let doc: Document | null = null; + try { + doc = iframe?.contentDocument ?? null; + } catch { + doc = null; } - if ((key === "z" && event.shiftKey) || (event.ctrlKey && !event.metaKey && key === "y")) { - event.preventDefault(); - void handleRedoRef.current(); - } - }; - window.addEventListener("keydown", handler); - return () => window.removeEventListener("keydown", handler); - }, []); + if (!win && !doc) return; + + win?.addEventListener("keydown", handleHistoryHotkey, true); + doc?.addEventListener("keydown", handleHistoryHotkey, true); + previewHistoryHotkeyCleanupRef.current = () => { + win?.removeEventListener("keydown", handleHistoryHotkey, true); + doc?.removeEventListener("keydown", handleHistoryHotkey, true); + }; + }, + [handleHistoryHotkey], + ); + + useEffect( + () => () => { + previewHistoryHotkeyCleanupRef.current?.(); + previewHistoryHotkeyCleanupRef.current = null; + }, + [], + ); const buildDomSelectionFromTarget = useCallback( (target: HTMLElement, options?: { preferClipAncestor?: boolean }) => { @@ -1818,86 +1933,98 @@ export function StudioApp() { [activeCompPath, editHistory.recordEdit, writeProjectFile], ); - const handleDomMoveCommit = useCallback( - async (selection: DomEditSelection, next: { left: number; top: number }) => { - await persistDomEditOperations( - selection, - [ - ...buildDomEditMovePatchOperations(next.left, next.top), - ...buildOppositeEdgePatchOperations(selection, "both"), - ], - { - skipRefresh: true, + const handleDomPathOffsetCommit = useCallback( + async (selection: DomEditSelection, next: { x: number; y: number }) => { + const save = async () => { + const originalContent = await readOptionalProjectFile(STUDIO_MANUAL_EDITS_PATH); + const manifest = parseStudioManualEditManifest(originalContent); + const nextContent = serializeStudioManualEditManifest( + upsertStudioPathOffsetEdit(manifest, selection, next), + ); + + if (nextContent === originalContent) return; + + const pid = projectIdRef.current; + if (!pid) throw new Error("No active project"); + await saveProjectFilesWithHistory({ + projectId: pid, label: "Move layer", - coalesceKey: getDomEditCoalesceKey(selection, "move"), - }, - ); + kind: "manual", + coalesceKey: `path-offset:${getDomEditTargetKey(selection)}`, + files: { [STUDIO_MANUAL_EDITS_PATH]: nextContent }, + readFile: async () => originalContent, + writeFile: writeProjectFile, + recordEdit: editHistory.recordEdit, + }); + domEditSaveTimestampRef.current = Date.now(); + }; + + await queueDomEditSave(save); }, - [persistDomEditOperations], + [editHistory.recordEdit, queueDomEditSave, readOptionalProjectFile, writeProjectFile], ); - const handleDomResizeCommit = useCallback( + const handleDomBoxSizeCommit = useCallback( async (selection: DomEditSelection, next: { width: number; height: number }) => { - if (shouldDetachOppositeEdges(selection)) { - selection.element.style.right = "auto"; - selection.element.style.bottom = "auto"; - } - await persistDomEditOperations( - selection, - [ - ...buildDomEditResizePatchOperations(next.width, next.height), - ...buildOppositeEdgePatchOperations(selection, "both"), - ], - { - skipRefresh: true, - label: "Resize layer", - coalesceKey: getDomEditCoalesceKey(selection, "resize"), - }, - ); + const save = async () => { + const originalContent = await readOptionalProjectFile(STUDIO_MANUAL_EDITS_PATH); + const manifest = parseStudioManualEditManifest(originalContent); + const nextContent = serializeStudioManualEditManifest( + upsertStudioBoxSizeEdit(manifest, selection, next), + ); + + if (nextContent === originalContent) return; + + const pid = projectIdRef.current; + if (!pid) throw new Error("No active project"); + await saveProjectFilesWithHistory({ + projectId: pid, + label: "Resize layer box", + kind: "manual", + coalesceKey: `box-size:${getDomEditTargetKey(selection)}`, + files: { [STUDIO_MANUAL_EDITS_PATH]: nextContent }, + readFile: async () => originalContent, + writeFile: writeProjectFile, + recordEdit: editHistory.recordEdit, + }); + domEditSaveTimestampRef.current = Date.now(); + }; + + await queueDomEditSave(save); }, - [persistDomEditOperations], + [editHistory.recordEdit, queueDomEditSave, readOptionalProjectFile, writeProjectFile], ); - const handleDomDetachFromLayout = useCallback(async () => { - const selection = domEditSelection; - if (!selection?.capabilities.canDetachFromLayout) return; + const handleDomRotationCommit = useCallback( + async (selection: DomEditSelection, next: { angle: number }) => { + const save = async () => { + const originalContent = await readOptionalProjectFile(STUDIO_MANUAL_EDITS_PATH); + const manifest = parseStudioManualEditManifest(originalContent); + const nextContent = serializeStudioManualEditManifest( + upsertStudioRotationEdit(manifest, selection, next), + ); - const doc = previewIframeRef.current?.contentDocument; - const element = doc - ? findElementForSelection(doc, selection, selection.sourceFile) - : selection.element; - if (!element) { - showToast("Could not find the selected layer in the preview.", "info"); - return; - } + if (nextContent === originalContent) return; - const rect = measureDomDetachRect(element); - const operations = buildDomEditDetachPatchOperations(rect); + const pid = projectIdRef.current; + if (!pid) throw new Error("No active project"); + await saveProjectFilesWithHistory({ + projectId: pid, + label: "Rotate layer", + kind: "manual", + coalesceKey: `rotation:${getDomEditTargetKey(selection)}`, + files: { [STUDIO_MANUAL_EDITS_PATH]: nextContent }, + readFile: async () => originalContent, + writeFile: writeProjectFile, + recordEdit: editHistory.recordEdit, + }); + domEditSaveTimestampRef.current = Date.now(); + }; - for (const operation of operations) { - element.style.setProperty(operation.property, operation.value); - } - - await persistDomEditOperations(selection, operations, { - skipRefresh: true, - label: "Make layer movable", - }); - - const refreshed = doc ? findElementForSelection(doc, selection, selection.sourceFile) : element; - if (refreshed) { - const nextSelection = buildDomSelectionFromTarget(refreshed); - if (nextSelection) { - applyDomSelection(nextSelection, { revealPanel: false }); - } - } - showToast("Layer detached from layout. You can move it now.", "info"); - }, [ - applyDomSelection, - buildDomSelectionFromTarget, - domEditSelection, - persistDomEditOperations, - showToast, - ]); + await queueDomEditSave(save); + }, + [editHistory.recordEdit, queueDomEditSave, readOptionalProjectFile, writeProjectFile], + ); const handleDomStyleCommit = useCallback( async (property: string, value: string) => { @@ -2185,10 +2312,11 @@ export function StudioApp() { previewIframeRef.current = iframe; setPreviewIframe(iframe); syncPreviewTimelineHotkey(iframe); + syncPreviewHistoryHotkey(iframe); consoleErrorsRef.current = []; setConsoleErrors(null); }, - [syncPreviewTimelineHotkey], + [syncPreviewHistoryHotkey, syncPreviewTimelineHotkey], ); const handlePreviewCanvasMouseDown = useCallback( @@ -2316,12 +2444,16 @@ export function StudioApp() { }; attachErrorCapture(); + syncPreviewHistoryHotkey(previewIframe); + void applyStudioManualEditsToPreview(previewIframe); syncSelectionFromDocument(); const handleLoad = () => { consoleErrorsRef.current = []; setConsoleErrors(null); attachErrorCapture(); + syncPreviewHistoryHotkey(previewIframe); + void applyStudioManualEditsToPreview(previewIframe); syncSelectionFromDocument(); }; @@ -2332,9 +2464,11 @@ export function StudioApp() { }, [ activeCompPath, applyDomSelection, + applyStudioManualEditsToPreview, buildDomSelectionFromTarget, captionEditMode, previewIframe, + syncPreviewHistoryHotkey, ]); // eslint-disable-next-line no-restricted-syntax @@ -3020,13 +3154,14 @@ export function StudioApp() { selection={ !rightCollapsed && rightPanelTab === "design" ? domEditSelection : null } - allowCanvasMovement={false} + allowCanvasMovement onCanvasMouseDown={handlePreviewCanvasMouseDown} onCanvasDoubleClick={handlePreviewCanvasDoubleClick} onSelectedDoubleClick={handleSelectedOverlayDoubleClick} onBlockedMove={handleBlockedDomMove} - onMoveCommit={handleDomMoveCommit} - onResizeCommit={handleDomResizeCommit} + onPathOffsetCommit={handleDomPathOffsetCommit} + onBoxSizeCommit={handleDomBoxSizeCommit} + onRotationCommit={handleDomRotationCommit} /> ) } @@ -3109,13 +3244,10 @@ export function StudioApp() { onSetTextFieldStyle={handleDomTextFieldStyleCommit} onAddTextField={handleDomAddTextField} onRemoveTextField={handleDomRemoveTextField} - onDetachFromLayout={handleDomDetachFromLayout} onAskAgent={handleAskAgent} - onCopyAgentInstruction={handleAgentModalSubmit} onImportAssets={handleImportFiles} fontAssets={fontAssets} onImportFonts={handleImportFonts} - allowLayoutDetach={false} /> ) : ( - renderQueue.startRender({ format, quality, resolution }) - } + onStartRender={async (format, quality, resolution) => { + await waitForPendingDomEditSaves(); + await renderQueue.startRender({ format, quality, resolution }); + }} isRendering={renderQueue.isRendering} /> )} diff --git a/packages/studio/src/components/editor/DomEditOverlay.test.ts b/packages/studio/src/components/editor/DomEditOverlay.test.ts new file mode 100644 index 0000000000..69f38dc4b7 --- /dev/null +++ b/packages/studio/src/components/editor/DomEditOverlay.test.ts @@ -0,0 +1,127 @@ +import { describe, expect, it } from "vitest"; +import { + focusDomEditOverlayElement, + resolveDomEditResizeGesture, + resolveDomEditRotationGesture, +} from "./DomEditOverlay"; + +describe("focusDomEditOverlayElement", () => { + it("focuses the canvas overlay without scrolling", () => { + const calls: Array = []; + focusDomEditOverlayElement({ + focus: (options?: FocusOptions) => calls.push(options), + }); + + expect(calls).toEqual([{ preventScroll: true }]); + }); +}); + +describe("resolveDomEditResizeGesture", () => { + it("resizes width and height independently by default", () => { + expect( + resolveDomEditResizeGesture({ + originWidth: 240, + originHeight: 120, + actualWidth: 240, + actualHeight: 120, + scaleX: 1, + scaleY: 1, + dx: 30, + dy: 12, + uniform: false, + }), + ).toEqual({ + overlayWidth: 270, + overlayHeight: 132, + width: 270, + height: 132, + }); + }); + + it("snaps width and height to the same value when Shift is held", () => { + expect( + resolveDomEditResizeGesture({ + originWidth: 240, + originHeight: 120, + actualWidth: 240, + actualHeight: 120, + scaleX: 1, + scaleY: 1, + dx: 30, + dy: 12, + uniform: true, + }), + ).toEqual({ + overlayWidth: 270, + overlayHeight: 270, + width: 270, + height: 270, + }); + }); + + it("uses the dominant pointer delta for uniform shrink", () => { + expect( + resolveDomEditResizeGesture({ + originWidth: 300, + originHeight: 180, + actualWidth: 300, + actualHeight: 180, + scaleX: 1, + scaleY: 1, + dx: 8, + dy: -40, + uniform: true, + }), + ).toMatchObject({ + width: 260, + height: 260, + }); + }); +}); + +describe("resolveDomEditRotationGesture", () => { + it("rotates by the pointer angle around the element center", () => { + expect( + resolveDomEditRotationGesture({ + centerX: 0, + centerY: 0, + startX: 0, + startY: -10, + currentX: 10, + currentY: 0, + actualAngle: 5, + snap: false, + }), + ).toEqual({ angle: 95 }); + }); + + it("uses the shortest delta across the 180 degree boundary", () => { + expect( + resolveDomEditRotationGesture({ + centerX: 0, + centerY: 0, + startX: -10, + startY: 1.76, + currentX: -10, + currentY: -1.76, + actualAngle: 0, + snap: false, + }).angle, + ).toBeCloseTo(20, 1); + }); + + it("snaps to 15 degree increments when requested", () => { + expect( + resolveDomEditRotationGesture({ + centerX: 0, + centerY: 0, + startX: 10, + startY: 0, + currentX: 10, + currentY: 3.25, + actualAngle: 0, + snap: true, + }), + ).toEqual({ angle: 15 }); + }); +}); diff --git a/packages/studio/src/components/editor/DomEditOverlay.tsx b/packages/studio/src/components/editor/DomEditOverlay.tsx index 56b1304aa7..14845e6ce2 100644 --- a/packages/studio/src/components/editor/DomEditOverlay.tsx +++ b/packages/studio/src/components/editor/DomEditOverlay.tsx @@ -1,6 +1,27 @@ import { memo, useMemo, useRef, useState, type RefObject } from "react"; import { useMountEffect } from "../../hooks/useMountEffect"; import { type DomEditSelection, findElementForSelection } from "./domEditing"; +import { + applyStudioBoxSize, + applyStudioBoxSizeDraft, + applyStudioPathOffset, + applyStudioPathOffsetDraft, + applyStudioRotation, + applyStudioRotationDraft, + beginStudioManualEditGesture, + captureStudioBoxSize, + captureStudioRotation, + endStudioManualEditGesture, + isStudioManualEditGestureCurrent, + readStudioBoxSize, + readStudioPathOffset, + readStudioRotation, + restoreStudioBoxSize, + restoreStudioPathOffset, + restoreStudioRotation, + type StudioBoxSizeSnapshot, + type StudioRotationSnapshot, +} from "./manualEdits"; interface OverlayRect { left: number; @@ -22,14 +43,15 @@ interface DomEditOverlayProps { onCanvasDoubleClick: (event: React.MouseEvent) => void; onSelectedDoubleClick: () => void; onBlockedMove: (selection: DomEditSelection) => void; - onMoveCommit: ( + onPathOffsetCommit: ( selection: DomEditSelection, - next: { left: number; top: number }, + next: { x: number; y: number }, ) => Promise | void; - onResizeCommit: ( + onBoxSizeCommit: ( selection: DomEditSelection, next: { width: number; height: number }, ) => Promise | void; + onRotationCommit: (selection: DomEditSelection, next: { angle: number }) => Promise | void; } function toOverlayRect( @@ -61,9 +83,11 @@ function toOverlayRect( }; } -type GestureKind = "drag" | "resize"; +type GestureKind = "drag" | "resize" | "rotate"; const BLOCKED_MOVE_THRESHOLD_PX = 4; +const MIN_RESIZE_EDGE_PX = 20; const OVERLAY_RECT_EPSILON_PX = 0.5; +const ROTATION_SNAP_DEGREES = 15; function rectsEqual(a: OverlayRect | null, b: OverlayRect | null): boolean { if (a === b) return true; @@ -89,31 +113,111 @@ function selectionCacheKey( ].join("|"); } -function restoreInlineStyle( - element: HTMLElement, - property: "left" | "top" | "width" | "height", - value: string, -) { - if (value) element.style.setProperty(property, value); - else element.style.removeProperty(property); +type FocusableDomEditOverlay = { + focus(options?: FocusOptions): void; +}; + +export function focusDomEditOverlayElement(element: FocusableDomEditOverlay | null): void { + element?.focus({ preventScroll: true }); } -interface GestureState { - kind: GestureKind; - startX: number; - startY: number; - initialStyleLeft: string; - initialStyleTop: string; - originLeft: number; - originTop: number; +export function resolveDomEditResizeGesture(input: { originWidth: number; originHeight: number; - actualLeft: number; - actualTop: number; actualWidth: number; actualHeight: number; scaleX: number; scaleY: number; + dx: number; + dy: number; + uniform: boolean; +}): { overlayWidth: number; overlayHeight: number; width: number; height: number } { + const scaleX = input.scaleX > 0 ? input.scaleX : 1; + const scaleY = input.scaleY > 0 ? input.scaleY : 1; + + if (input.uniform) { + const deltaX = input.dx / scaleX; + const deltaY = input.dy / scaleY; + const delta = Math.abs(deltaX) >= Math.abs(deltaY) ? deltaX : deltaY; + const side = Math.max(1, Math.max(input.actualWidth, input.actualHeight) + delta); + return { + overlayWidth: Math.max(MIN_RESIZE_EDGE_PX, side * scaleX), + overlayHeight: Math.max(MIN_RESIZE_EDGE_PX, side * scaleY), + width: side, + height: side, + }; + } + + return { + overlayWidth: Math.max(MIN_RESIZE_EDGE_PX, input.originWidth + input.dx), + overlayHeight: Math.max(MIN_RESIZE_EDGE_PX, input.originHeight + input.dy), + width: Math.max(1, input.actualWidth + input.dx / scaleX), + height: Math.max(1, input.actualHeight + input.dy / scaleY), + }; +} + +function pointerAngleDegrees(centerX: number, centerY: number, x: number, y: number): number { + return (Math.atan2(y - centerY, x - centerX) * 180) / Math.PI; +} + +function normalizeAngleDelta(delta: number): number { + return ((((delta + 180) % 360) + 360) % 360) - 180; +} + +function roundAngle(angle: number): number { + return Math.round(angle * 10) / 10; +} + +export function resolveDomEditRotationGesture(input: { + centerX: number; + centerY: number; + startX: number; + startY: number; + currentX: number; + currentY: number; + actualAngle: number; + snap: boolean; +}): { angle: number } { + const startAngle = pointerAngleDegrees(input.centerX, input.centerY, input.startX, input.startY); + const currentAngle = pointerAngleDegrees( + input.centerX, + input.centerY, + input.currentX, + input.currentY, + ); + const delta = normalizeAngleDelta(currentAngle - startAngle); + const angle = input.actualAngle + delta; + return { + angle: input.snap + ? Math.round(angle / ROTATION_SNAP_DEGREES) * ROTATION_SNAP_DEGREES + : roundAngle(angle), + }; +} + +interface GestureState { + kind: GestureKind; + mode: "path-offset" | "box-size" | "rotation"; + startX: number; + startY: number; + centerX: number; + centerY: number; + initialStyleTranslate: string; + initialRotation: StudioRotationSnapshot; + initialOffsetX: string; + initialOffsetY: string; + initialBoxSize: StudioBoxSizeSnapshot; + originLeft: number; + originTop: number; + originWidth: number; + originHeight: number; + actualOffsetX: number; + actualOffsetY: number; + actualWidth: number; + actualHeight: number; + actualRotation: number; + scaleX: number; + scaleY: number; + manualEditDragToken?: string; } interface BlockedMoveState { @@ -131,8 +235,9 @@ export const DomEditOverlay = memo(function DomEditOverlay({ onCanvasDoubleClick, onSelectedDoubleClick, onBlockedMove, - onMoveCommit, - onResizeCommit, + onPathOffsetCommit, + onBoxSizeCommit, + onRotationCommit, }: DomEditOverlayProps) { const overlayRef = useRef(null); const boxRef = useRef(null); @@ -147,10 +252,12 @@ export const DomEditOverlay = memo(function DomEditOverlay({ selectionRef.current = selection; const overlayRectRef = useRef(overlayRect); overlayRectRef.current = overlayRect; - const onMoveCommitRef = useRef(onMoveCommit); - onMoveCommitRef.current = onMoveCommit; - const onResizeCommitRef = useRef(onResizeCommit); - onResizeCommitRef.current = onResizeCommit; + const onPathOffsetCommitRef = useRef(onPathOffsetCommit); + onPathOffsetCommitRef.current = onPathOffsetCommit; + const onBoxSizeCommitRef = useRef(onBoxSizeCommit); + onBoxSizeCommitRef.current = onBoxSizeCommit; + const onRotationCommitRef = useRef(onRotationCommit); + onRotationCommitRef.current = onRotationCommit; const onBlockedMoveRef = useRef(onBlockedMove); onBlockedMoveRef.current = onBlockedMove; @@ -189,7 +296,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({ const sel = selectionRef.current; const iframe = iframeRef.current; const overlayEl = overlayRef.current; - if (!sel || !iframe || !overlayEl) { + if (!iframe || !overlayEl) { resolvedElementRef.current = null; clearOverlayRect(); return; @@ -198,6 +305,12 @@ export const DomEditOverlay = memo(function DomEditOverlay({ const doc = iframe.contentDocument; if (!doc) return; + if (!sel) { + resolvedElementRef.current = null; + clearOverlayRect(); + return; + } + const el = resolveElement(doc, sel); if (!el) { clearOverlayRect(); @@ -223,14 +336,25 @@ export const DomEditOverlay = memo(function DomEditOverlay({ const sel = selectionRef.current; const rect = overlayRectRef.current; const box = boxRef.current; + const overlayEl = overlayRef.current; if (!sel || !rect || !box) return; - - const left = Number.parseFloat(sel.computedStyles.left ?? ""); - const top = Number.parseFloat(sel.computedStyles.top ?? ""); - const width = Number.parseFloat(sel.computedStyles.width ?? ""); - const height = Number.parseFloat(sel.computedStyles.height ?? ""); - if (!Number.isFinite(left) || !Number.isFinite(top)) return; - if (kind === "resize" && !Number.isFinite(width) && !Number.isFinite(height)) return; + const mode: GestureState["mode"] = + kind === "rotate" ? "rotation" : kind === "drag" ? "path-offset" : "box-size"; + if (kind === "drag" && !sel.capabilities.canApplyManualOffset) return; + if (kind === "resize" && !sel.capabilities.canApplyManualSize) return; + if (kind === "rotate" && !sel.capabilities.canApplyManualRotation) return; + if (kind === "resize" && (!Number.isFinite(rect.width) || !Number.isFinite(rect.height))) { + return; + } + const offset = readStudioPathOffset(sel.element); + const size = readStudioBoxSize(sel.element); + const rotation = readStudioRotation(sel.element); + const actualWidth = size.width > 0 ? size.width : rect.width / rect.scaleX; + const actualHeight = size.height > 0 ? size.height : rect.height / rect.scaleY; + const manualEditDragToken = beginStudioManualEditGesture(sel.element); + const overlayBounds = overlayEl?.getBoundingClientRect(); + const centerX = (overlayBounds?.left ?? 0) + rect.left + rect.width / 2; + const centerY = (overlayBounds?.top ?? 0) + rect.top + rect.height / 2; e.preventDefault(); e.stopPropagation(); @@ -240,20 +364,28 @@ export const DomEditOverlay = memo(function DomEditOverlay({ gestureRef.current = { kind, + mode, startX: e.clientX, startY: e.clientY, - initialStyleLeft: sel.element.style.left, - initialStyleTop: sel.element.style.top, + centerX, + centerY, + initialStyleTranslate: sel.element.style.getPropertyValue("translate"), + initialRotation: captureStudioRotation(sel.element), + initialOffsetX: sel.element.style.getPropertyValue("--hf-studio-offset-x"), + initialOffsetY: sel.element.style.getPropertyValue("--hf-studio-offset-y"), + initialBoxSize: captureStudioBoxSize(sel.element), originLeft: rect.left, originTop: rect.top, originWidth: rect.width, originHeight: rect.height, - actualLeft: left, - actualTop: top, - actualWidth: Number.isFinite(width) ? width : 0, - actualHeight: Number.isFinite(height) ? height : 0, + actualOffsetX: offset.x, + actualOffsetY: offset.y, + actualWidth, + actualHeight, + actualRotation: rotation.angle, scaleX: rect.scaleX, scaleY: rect.scaleY, + manualEditDragToken, }; }; @@ -278,20 +410,45 @@ export const DomEditOverlay = memo(function DomEditOverlay({ const dx = e.clientX - g.startX; const dy = e.clientY - g.startY; + if (g.kind === "rotate") { + const nextRotation = resolveDomEditRotationGesture({ + centerX: g.centerX, + centerY: g.centerY, + startX: g.startX, + startY: g.startY, + currentX: e.clientX, + currentY: e.clientY, + actualAngle: g.actualRotation, + snap: e.shiftKey, + }); + applyStudioRotationDraft(sel.element, nextRotation); + return; + } + if (g.kind === "drag") { const nextBoxLeft = g.originLeft + dx; const nextBoxTop = g.originTop + dy; box.style.left = `${nextBoxLeft}px`; box.style.top = `${nextBoxTop}px`; - sel.element.style.left = `${Math.round(g.actualLeft + dx / g.scaleX)}px`; - sel.element.style.top = `${Math.round(g.actualTop + dy / g.scaleY)}px`; + applyStudioPathOffsetDraft(sel.element, { + x: g.actualOffsetX + dx / g.scaleX, + y: g.actualOffsetY + dy / g.scaleY, + }); } else { - const newW = Math.max(20, g.originWidth + dx); - const newH = Math.max(20, g.originHeight + dy); - box.style.width = `${newW}px`; - box.style.height = `${newH}px`; - sel.element.style.width = `${Math.round(g.actualWidth + dx / g.scaleX)}px`; - sel.element.style.height = `${Math.round(g.actualHeight + dy / g.scaleY)}px`; + const nextSize = resolveDomEditResizeGesture({ + originWidth: g.originWidth, + originHeight: g.originHeight, + actualWidth: g.actualWidth, + actualHeight: g.actualHeight, + scaleX: g.scaleX, + scaleY: g.scaleY, + dx, + dy, + uniform: e.shiftKey, + }); + box.style.width = `${nextSize.overlayWidth}px`; + box.style.height = `${nextSize.overlayHeight}px`; + applyStudioBoxSizeDraft(sel.element, nextSize); } }; @@ -311,8 +468,12 @@ export const DomEditOverlay = memo(function DomEditOverlay({ const movedDistance = Math.hypot(e.clientX - g.startX, e.clientY - g.startY); if (g.kind === "drag" && movedDistance < BLOCKED_MOVE_THRESHOLD_PX) { - restoreInlineStyle(sel.element, "left", g.initialStyleLeft); - restoreInlineStyle(sel.element, "top", g.initialStyleTop); + restoreStudioPathOffset(sel.element, { + translate: g.initialStyleTranslate, + x: g.initialOffsetX, + y: g.initialOffsetY, + }); + endStudioManualEditGesture(sel.element, g.manualEditDragToken); if (box) { box.style.left = `${g.originLeft}px`; box.style.top = `${g.originTop}px`; @@ -324,26 +485,71 @@ export const DomEditOverlay = memo(function DomEditOverlay({ return; } - if (g.kind === "drag") { - const finalLeft = Number.parseFloat(sel.element.style.left) || g.actualLeft; - const finalTop = Number.parseFloat(sel.element.style.top) || g.actualTop; - void Promise.resolve(onMoveCommitRef.current(sel, { left: finalLeft, top: finalTop })).catch( - () => { - sel.element.style.left = `${Math.round(g.actualLeft)}px`; - sel.element.style.top = `${Math.round(g.actualTop)}px`; - }, - ); + if (g.kind === "resize" && movedDistance < BLOCKED_MOVE_THRESHOLD_PX) { + restoreStudioBoxSize(sel.element, g.initialBoxSize); + endStudioManualEditGesture(sel.element, g.manualEditDragToken); + if (box) { + box.style.width = `${g.originWidth}px`; + box.style.height = `${g.originHeight}px`; + } + return; + } + + if (g.kind === "rotate" && movedDistance < BLOCKED_MOVE_THRESHOLD_PX) { + restoreStudioRotation(sel.element, g.initialRotation); + endStudioManualEditGesture(sel.element, g.manualEditDragToken); + return; + } + + if (g.kind === "rotate") { + const finalRotation = readStudioRotation(sel.element); + applyStudioRotation(sel.element, finalRotation); + void Promise.resolve(onRotationCommitRef.current(sel, finalRotation)) + .catch(() => { + if ( + g.manualEditDragToken && + isStudioManualEditGestureCurrent(sel.element, g.manualEditDragToken) + ) { + restoreStudioRotation(sel.element, g.initialRotation); + } + }) + .finally(() => { + endStudioManualEditGesture(sel.element, g.manualEditDragToken); + }); + } else if (g.kind === "drag") { + const finalOffset = readStudioPathOffset(sel.element); + applyStudioPathOffset(sel.element, finalOffset); + void Promise.resolve(onPathOffsetCommitRef.current(sel, finalOffset)) + .catch(() => { + if ( + g.manualEditDragToken && + isStudioManualEditGestureCurrent(sel.element, g.manualEditDragToken) + ) { + restoreStudioPathOffset(sel.element, { + translate: g.initialStyleTranslate, + x: g.initialOffsetX, + y: g.initialOffsetY, + }); + } + }) + .finally(() => { + endStudioManualEditGesture(sel.element, g.manualEditDragToken); + }); } else { - const finalW = Number.parseFloat(sel.element.style.width) || g.actualWidth; - const finalH = Number.parseFloat(sel.element.style.height) || g.actualHeight; - void Promise.resolve(onResizeCommitRef.current(sel, { width: finalW, height: finalH })).catch( - () => { - if (g.actualWidth > 0) sel.element.style.width = `${Math.round(g.actualWidth)}px`; - else sel.element.style.removeProperty("width"); - if (g.actualHeight > 0) sel.element.style.height = `${Math.round(g.actualHeight)}px`; - else sel.element.style.removeProperty("height"); - }, - ); + const finalSize = readStudioBoxSize(sel.element); + applyStudioBoxSize(sel.element, finalSize); + void Promise.resolve(onBoxSizeCommitRef.current(sel, finalSize)) + .catch(() => { + if ( + g.manualEditDragToken && + isStudioManualEditGestureCurrent(sel.element, g.manualEditDragToken) + ) { + restoreStudioBoxSize(sel.element, g.initialBoxSize); + } + }) + .finally(() => { + endStudioManualEditGesture(sel.element, g.manualEditDragToken); + }); } }; @@ -377,6 +583,24 @@ export const DomEditOverlay = memo(function DomEditOverlay({ }; const clearPointerState = () => { + const g = gestureRef.current; + const sel = selectionRef.current; + if (g?.mode === "path-offset" && sel) { + restoreStudioPathOffset(sel.element, { + translate: g.initialStyleTranslate, + x: g.initialOffsetX, + y: g.initialOffsetY, + }); + endStudioManualEditGesture(sel.element, g.manualEditDragToken); + } + if (g?.mode === "box-size" && sel) { + restoreStudioBoxSize(sel.element, g.initialBoxSize); + endStudioManualEditGesture(sel.element, g.manualEditDragToken); + } + if (g?.mode === "rotation" && sel) { + restoreStudioRotation(sel.element, g.initialRotation); + endStudioManualEditGesture(sel.element, g.manualEditDragToken); + } blockedMoveRef.current = null; gestureRef.current = null; rafPausedRef.current = false; @@ -386,7 +610,10 @@ export const DomEditOverlay = memo(function DomEditOverlay({
focusDomEditOverlayElement(event.currentTarget)} onMouseDown={handleOverlayMouseDown} onDoubleClick={handleOverlayDoubleClick} onPointerMove={onPointerMove} @@ -395,6 +622,31 @@ export const DomEditOverlay = memo(function DomEditOverlay({ > {selection && overlayRect && ( <> + {allowCanvasMovement && selection.capabilities.canApplyManualRotation && ( +
+
+
+ )}
{ if (!allowCanvasMovement) return; - if (selection.capabilities.canMove) { + if (selection.capabilities.canApplyManualOffset) { startGesture("drag", e); return; } @@ -427,7 +682,7 @@ export const DomEditOverlay = memo(function DomEditOverlay({ onDoubleClick={onSelectedDoubleClick} > {/* Resize handle — bottom-right corner */} - {allowCanvasMovement && selection.capabilities.canResize && ( + {allowCanvasMovement && selection.capabilities.canApplyManualSize && (
void; onAddTextField: (afterFieldKey?: string) => string | Promise | null; onRemoveTextField: (fieldKey: string) => void; - onDetachFromLayout: () => void; onAskAgent: () => void; - onCopyAgentInstruction: (instruction: string) => void | Promise; onImportAssets?: (files: FileList) => Promise; fontAssets?: ImportedFontAsset[]; onImportFonts?: (files: FileList | File[]) => Promise; - allowLayoutDetach?: boolean; } const FIELD = @@ -117,17 +114,6 @@ interface FontOption { const COLOR_PICKER_SIZE = { width: 292, height: 386 }; -function buildMakeMovableAgentInstruction(reason: string): string { - return [ - "Make this selected HyperFrames element movable in Studio.", - `Studio blocked direct move/resize because: ${reason}`, - "Refactor only this element safely.", - "Preserve its current visual position, timing, and sibling layout intent where possible.", - "If it is transform-driven, replace transform-based positioning with absolute pixel geometry.", - "If layout flow owns it, detach conservatively with position: absolute, px left/top/width/height, and margin: 0.", - ].join(" "); -} - function colorFromCss(value: string): ParsedColor { return parseCssColor(value) ?? { red: 0, green: 0, blue: 0, alpha: 1 }; } @@ -1979,13 +1965,10 @@ export const PropertyPanel = memo(function PropertyPanel({ onSetTextFieldStyle, onAddTextField, onRemoveTextField, - onDetachFromLayout, onAskAgent, - onCopyAgentInstruction, onImportAssets, fontAssets = [], onImportFonts, - allowLayoutDetach = true, }: PropertyPanelProps) { const styles = element?.computedStyles ?? EMPTY_STYLES; const selectionColors = useMemo(() => collectSelectionColors(styles), [styles]); @@ -2037,12 +2020,6 @@ export const PropertyPanel = memo(function PropertyPanel({ const clipContent = ["hidden", "clip"].includes((styles.overflow ?? "").trim()); const sourceLabel = element.id ? `#${element.id}` : element.selector; const showEditableSections = element.capabilities.canEditStyles; - const disabledMoveReason = - allowLayoutDetach && - element.capabilities.reasonIfDisabled && - !element.capabilities.canDetachFromLayout - ? element.capabilities.reasonIfDisabled - : null; const handleFillModeChange = (nextMode: string) => { setPreferredFillMode(nextMode); @@ -2116,42 +2093,6 @@ export const PropertyPanel = memo(function PropertyPanel({ onCommit={(next) => onSetStyle("height", next)} />
- {disabledMoveReason && ( -
-
-
{disabledMoveReason}
-
Copy a targeted prompt so an agent can refactor this layer safely.
-
- -
- )} - {allowLayoutDetach && element.capabilities.canDetachFromLayout && ( -
-
-
- This layer is controlled by layout. -
-
Detaches from flex/grid flow and preserves current visual position.
-
- -
- )} {showEditableSections && isFlex && ( diff --git a/packages/studio/src/components/editor/domEditing.test.ts b/packages/studio/src/components/editor/domEditing.test.ts index 9a7be202cf..713116167d 100644 --- a/packages/studio/src/components/editor/domEditing.test.ts +++ b/packages/studio/src/components/editor/domEditing.test.ts @@ -1,11 +1,11 @@ import { describe, expect, it } from "vitest"; import { Window } from "happy-dom"; import { - buildDomEditMovePatchOperations, - buildDomEditResizePatchOperations, buildDomEditStylePatchOperation, buildElementAgentPrompt, findElementForSelection, + getDomEditNonEditableReason, + getDomEditTargetKey, isTextEditableSelection, serializeDomEditTextFields, type DomEditSelection, @@ -47,7 +47,9 @@ describe("resolveDomEditCapabilities", () => { canEditStyles: true, canMove: true, canResize: true, - canDetachFromLayout: false, + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, reasonIfDisabled: undefined, }); }); @@ -75,8 +77,10 @@ describe("resolveDomEditCapabilities", () => { canEditStyles: true, canMove: false, canResize: false, - canDetachFromLayout: true, - reasonIfDisabled: "This layer is controlled by layout.", + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, + reasonIfDisabled: undefined, }); }); @@ -104,7 +108,9 @@ describe("resolveDomEditCapabilities", () => { ).toMatchObject({ canMove: false, canResize: false, - canDetachFromLayout: false, + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, }); }); @@ -132,7 +138,7 @@ describe("resolveDomEditCapabilities", () => { ).toMatchObject({ canMove: true, canResize: true, - canDetachFromLayout: false, + canApplyManualOffset: true, }); }); @@ -217,8 +223,10 @@ describe("resolveDomEditSelection", () => { canEditStyles: false, canMove: true, canResize: true, - canDetachFromLayout: false, - reasonIfDisabled: undefined, + canApplyManualOffset: false, + canApplyManualSize: false, + canApplyManualRotation: false, + reasonIfDisabled: "Drill into the composition to edit its internal layers.", }); }); @@ -373,23 +381,60 @@ describe("resolveDomEditSelection", () => { expect(selection?.textFields.map((field) => field.tagName)).toEqual(["strong", "span"]); expect(selection?.textFields.map((field) => field.value)).toEqual(["", ""]); }); + + it("explains anonymous child elements that resolve to an editable parent", () => { + const document = createDocument(` +
+
+ Headline +
+
+ `); + + const child = document.querySelector("strong") as HTMLElement; + const selection = resolveDomEditSelection(child, { + activeCompositionPath: null, + isMasterView: false, + preferClipAncestor: false, + }); + + expect(selection?.id).toBe("card"); + expect(getDomEditNonEditableReason(child, selection)).toBe("Selection resolves to Card"); + }); + + it("does not mark an element as non-editable when Studio can edit it directly", () => { + const document = createDocument(` +
+
Editable
+
+ `); + + const element = document.getElementById("card") as HTMLElement; + const selection = resolveDomEditSelection(element, { + activeCompositionPath: null, + isMasterView: false, + }); + + expect(getDomEditNonEditableReason(element, selection)).toBeNull(); + }); + + it("keeps duplicate class targets distinct for history keys", () => { + const first = getDomEditTargetKey({ + sourceFile: "index.html", + selector: ".card", + selectorIndex: 0, + }); + const second = getDomEditTargetKey({ + sourceFile: "index.html", + selector: ".card", + selectorIndex: 1, + }); + + expect(first).not.toBe(second); + }); }); describe("patch builders and prompt builder", () => { - it("builds move patch operations for left/top", () => { - expect(buildDomEditMovePatchOperations(140.4, 82.1)).toEqual([ - { type: "inline-style", property: "left", value: "140px" }, - { type: "inline-style", property: "top", value: "82px" }, - ]); - }); - - it("builds resize patch operations for width/height", () => { - expect(buildDomEditResizePatchOperations(301.6, 210.1)).toEqual([ - { type: "inline-style", property: "width", value: "302px" }, - { type: "inline-style", property: "height", value: "210px" }, - ]); - }); - it("builds style patch operations", () => { expect(buildDomEditStylePatchOperation("background-color", "rgb(15, 23, 42)")).toEqual({ type: "inline-style", @@ -444,6 +489,9 @@ describe("patch builders and prompt builder", () => { canEditStyles: true, canMove: true, canResize: true, + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, }, } satisfies DomEditSelection; @@ -490,7 +538,9 @@ describe("patch builders and prompt builder", () => { canEditStyles: true, canMove: true, canResize: true, - canDetachFromLayout: false, + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, }, } satisfies DomEditSelection; diff --git a/packages/studio/src/components/editor/domEditing.ts b/packages/studio/src/components/editor/domEditing.ts index cfbce4673f..6544fe8258 100644 --- a/packages/studio/src/components/editor/domEditing.ts +++ b/packages/studio/src/components/editor/domEditing.ts @@ -35,9 +35,13 @@ const CURATED_STYLE_PROPERTIES = [ export interface DomEditCapabilities { canSelect: boolean; canEditStyles: boolean; + /** Directly editable authored left/top style fields. Canvas drag uses manual edits instead. */ canMove: boolean; + /** Directly editable authored width/height style fields. Canvas resize uses manual edits instead. */ canResize: boolean; - canDetachFromLayout: boolean; + canApplyManualOffset: boolean; + canApplyManualSize: boolean; + canApplyManualRotation: boolean; reasonIfDisabled?: string; } @@ -119,54 +123,6 @@ function isIdentityTransform(value: string | undefined): boolean { return values.every((part, index) => Math.abs(part - identity[index]) < 0.0001); } -function isClipClassName(className: string | undefined): boolean { - return Boolean(className?.split(/\s+/).includes("clip")); -} - -function isInlineTextTag(tagName: string | undefined): boolean { - return Boolean( - tagName && - ["a", "b", "em", "i", "small", "span", "strong", "sub", "sup"].includes(tagName.toLowerCase()), - ); -} - -function isBlockishTag(tagName: string | undefined): boolean { - return Boolean( - tagName && - [ - "article", - "aside", - "canvas", - "div", - "figure", - "footer", - "header", - "img", - "main", - "section", - "svg", - "video", - ].includes(tagName.toLowerCase()), - ); -} - -function isBlockishDisplay(display: string | undefined): boolean { - return Boolean( - display && - [ - "block", - "flex", - "flow-root", - "grid", - "inline-block", - "inline-flex", - "inline-grid", - "list-item", - "table", - ].includes(display), - ); -} - function isTextBearingTag(tagName: string): boolean { return ["div", "span", "p", "strong", "h1", "h2", "h3", "h4", "h5", "h6"].includes(tagName); } @@ -442,7 +398,9 @@ export function resolveDomEditCapabilities(args: { canEditStyles: false, canMove: false, canResize: false, - canDetachFromLayout: false, + canApplyManualOffset: false, + canApplyManualSize: false, + canApplyManualRotation: false, reasonIfDisabled: "Studio could not resolve a stable patch target for this element.", }; } @@ -461,21 +419,13 @@ export function resolveDomEditCapabilities(args: { !hasTransformDrivenGeometry; const canResize = canMove && (width != null || height != null); - const isBlockishLayer = - args.isCompositionHost || - isClipClassName(args.className) || - isBlockishTag(args.tagName) || - isBlockishDisplay(args.computedStyles.display); - const canDetachFromLayout = - !canMove && - !hasTransformDrivenGeometry && - isBlockishLayer && - (!isInlineTextTag(args.tagName) || isClipClassName(args.className)); - const reasonIfDisabled = !canMove - ? canDetachFromLayout - ? "This layer is controlled by layout." - : "Direct move/resize is limited to absolute or fixed elements with px geometry and no transform-driven layout." - : undefined; + const canApplyManualGeometry = !args.isCompositionHost; + const canApplyManualOffset = canApplyManualGeometry; + const canApplyManualSize = canApplyManualGeometry; + const canApplyManualRotation = canApplyManualGeometry; + const reasonIfDisabled = canApplyManualGeometry + ? undefined + : "Drill into the composition to edit its internal layers."; if (args.isCompositionHost && args.isMasterView) { return { @@ -483,7 +433,9 @@ export function resolveDomEditCapabilities(args: { canEditStyles: false, canMove, canResize, - canDetachFromLayout, + canApplyManualOffset, + canApplyManualSize, + canApplyManualRotation, reasonIfDisabled, }; } @@ -493,7 +445,9 @@ export function resolveDomEditCapabilities(args: { canEditStyles: true, canMove, canResize, - canDetachFromLayout, + canApplyManualOffset, + canApplyManualSize, + canApplyManualRotation, reasonIfDisabled, }; } @@ -585,6 +539,49 @@ export function refreshDomEditSelection( : null; } +export function getDomEditTargetKey( + selection: Pick, +): string { + return [ + selection.sourceFile || "index.html", + selection.id ?? "", + selection.selector ?? "", + selection.selectorIndex ?? "", + ].join("|"); +} + +function hasSupportedDirectEdit(capabilities: DomEditCapabilities): boolean { + return ( + capabilities.canEditStyles || + capabilities.canMove || + capabilities.canResize || + capabilities.canApplyManualOffset || + capabilities.canApplyManualSize || + capabilities.canApplyManualRotation + ); +} + +export function getDomEditNonEditableReason( + element: HTMLElement, + selection: DomEditSelection | null, +): string | null { + if (!selection) { + return "No stable source target"; + } + + if (selection.element !== element) { + return selection.isCompositionHost + ? "Nested composition boundary" + : `Selection resolves to ${selection.label}`; + } + + if (!hasSupportedDirectEdit(selection.capabilities)) { + return selection.capabilities.reasonIfDisabled ?? "No supported direct edits"; + } + + return null; +} + export function findElementForSelection( doc: Document, selection: Pick, @@ -624,38 +621,6 @@ export function findElementForSelection( return matches[0] ?? null; } -export function buildDomEditMovePatchOperations(left: number, top: number): PatchOperation[] { - return [ - { type: "inline-style", property: "left", value: `${Math.round(left)}px` }, - { type: "inline-style", property: "top", value: `${Math.round(top)}px` }, - ]; -} - -export function buildDomEditResizePatchOperations(width: number, height: number): PatchOperation[] { - return [ - { type: "inline-style", property: "width", value: `${Math.round(width)}px` }, - { type: "inline-style", property: "height", value: `${Math.round(height)}px` }, - ]; -} - -export function buildDomEditDetachPatchOperations(rect: { - left: number; - top: number; - width: number; - height: number; -}): PatchOperation[] { - return [ - { type: "inline-style", property: "position", value: "absolute" }, - { type: "inline-style", property: "left", value: `${Math.round(rect.left)}px` }, - { type: "inline-style", property: "top", value: `${Math.round(rect.top)}px` }, - { type: "inline-style", property: "width", value: `${Math.round(rect.width)}px` }, - { type: "inline-style", property: "height", value: `${Math.round(rect.height)}px` }, - { type: "inline-style", property: "margin", value: "0" }, - { type: "inline-style", property: "right", value: "auto" }, - { type: "inline-style", property: "bottom", value: "auto" }, - ]; -} - export function buildDomEditStylePatchOperation(property: string, value: string): PatchOperation { return { type: "inline-style", diff --git a/packages/studio/src/components/editor/manualEdits.test.ts b/packages/studio/src/components/editor/manualEdits.test.ts new file mode 100644 index 0000000000..1790cd9723 --- /dev/null +++ b/packages/studio/src/components/editor/manualEdits.test.ts @@ -0,0 +1,577 @@ +import { describe, expect, it, vi } from "vitest"; +import { Window } from "happy-dom"; +import type { DomEditSelection } from "./domEditing"; +import { + STUDIO_OFFSET_X_PROP, + STUDIO_OFFSET_Y_PROP, + STUDIO_ROTATION_PROP, + STUDIO_WIDTH_PROP, + applyStudioBoxSize, + applyStudioBoxSizeDraft, + applyStudioManualEditManifest, + applyStudioPathOffset, + applyStudioPathOffsetDraft, + applyStudioRotation, + applyStudioRotationDraft, + beginStudioManualEditGesture, + captureStudioBoxSize, + captureStudioRotation, + emptyStudioManualEditManifest, + endStudioManualEditGesture, + installStudioManualEditSeekReapply, + parseStudioManualEditManifest, + readStudioBoxSize, + readStudioPathOffset, + readStudioRotation, + restoreStudioBoxSize, + restoreStudioRotation, + serializeStudioManualEditManifest, + upsertStudioBoxSizeEdit, + upsertStudioPathOffsetEdit, + upsertStudioRotationEdit, +} from "./manualEdits"; + +function createDocument(markup: string): Document { + const window = new Window(); + window.document.body.innerHTML = markup; + return window.document; +} + +function createSelection(): DomEditSelection { + return { + element: {} as HTMLElement, + id: "card", + selector: "#card", + selectorIndex: undefined, + sourceFile: "index.html", + compositionPath: "index.html", + compositionSrc: undefined, + isCompositionHost: false, + label: "Card", + tagName: "div", + boundingBox: { x: 0, y: 0, width: 100, height: 100 }, + textContent: null, + dataAttributes: {}, + inlineStyles: {}, + computedStyles: {}, + textFields: [], + capabilities: { + canSelect: true, + canEditStyles: true, + canMove: false, + canResize: false, + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, + }, + }; +} + +function mockBoundingRect(element: HTMLElement, width: number, height: number): void { + element.getBoundingClientRect = () => + ({ + x: 0, + y: 0, + left: 0, + top: 0, + right: width, + bottom: height, + width, + height, + toJSON: () => ({}), + }) as DOMRect; +} + +describe("studio manual edits", () => { + it("upserts path offsets by stable target", () => { + const manifest = upsertStudioPathOffsetEdit( + emptyStudioManualEditManifest(), + createSelection(), + { + x: 12.4, + y: 30.6, + }, + ); + const updated = upsertStudioPathOffsetEdit(manifest, createSelection(), { + x: 20, + y: 42, + }); + + expect(updated.edits).toHaveLength(1); + expect(updated.edits[0]).toMatchObject({ + kind: "path-offset", + target: { sourceFile: "index.html", selector: "#card", id: "card" }, + x: 20, + y: 42, + }); + }); + + it("upserts box sizes without replacing path offsets for the same target", () => { + const selection = createSelection(); + const manifest = upsertStudioPathOffsetEdit(emptyStudioManualEditManifest(), selection, { + x: 12, + y: 30, + }); + const updated = upsertStudioBoxSizeEdit(manifest, selection, { + width: 240.4, + height: 120.6, + }); + const resized = upsertStudioBoxSizeEdit(updated, selection, { + width: 260, + height: 140, + }); + + expect(resized.edits).toHaveLength(2); + expect(resized.edits).toEqual( + expect.arrayContaining([ + expect.objectContaining({ kind: "path-offset", x: 12, y: 30 }), + expect.objectContaining({ kind: "box-size", width: 260, height: 140 }), + ]), + ); + }); + + it("upserts rotations without replacing other manual edits for the same target", () => { + const selection = createSelection(); + const manifest = upsertStudioPathOffsetEdit(emptyStudioManualEditManifest(), selection, { + x: 12, + y: 30, + }); + const resized = upsertStudioBoxSizeEdit(manifest, selection, { + width: 240, + height: 120, + }); + const rotated = upsertStudioRotationEdit(resized, selection, { angle: 32.34 }); + const updated = upsertStudioRotationEdit(rotated, selection, { angle: -14.96 }); + + expect(updated.edits).toHaveLength(3); + expect(updated.edits).toEqual( + expect.arrayContaining([ + expect.objectContaining({ kind: "path-offset", x: 12, y: 30 }), + expect.objectContaining({ kind: "box-size", width: 240, height: 120 }), + expect.objectContaining({ kind: "rotation", angle: -15 }), + ]), + ); + }); + + it("round-trips valid manifest entries and drops invalid entries", () => { + const content = serializeStudioManualEditManifest({ + version: 1, + edits: [ + { + kind: "path-offset", + target: { sourceFile: "index.html", selector: "#card", id: "card" }, + x: 10, + y: 20, + }, + { + kind: "box-size", + target: { sourceFile: "index.html", selector: "#card", id: "card" }, + width: 320, + height: 180, + }, + { + kind: "rotation", + target: { sourceFile: "index.html", selector: "#card", id: "card" }, + angle: 22.5, + }, + ], + }); + + expect(parseStudioManualEditManifest(content).edits).toHaveLength(3); + expect(parseStudioManualEditManifest('{ "edits": [{ "kind": "path-offset" }] }').edits).toEqual( + [], + ); + }); + + it("applies offsets through CSS translate longhand", () => { + const document = createDocument(`
`); + const card = document.getElementById("card") as HTMLElement; + + applyStudioPathOffset(card, { x: 14, y: -8 }); + + expect(readStudioPathOffset(card)).toEqual({ x: 14, y: -8 }); + expect(card.style.getPropertyValue(STUDIO_OFFSET_X_PROP)).toBe("14px"); + expect(card.style.getPropertyValue(STUDIO_OFFSET_Y_PROP)).toBe("-8px"); + expect(card.style.getPropertyValue("translate")).toContain(STUDIO_OFFSET_X_PROP); + }); + + it("applies box sizes through CSS dimensions and flex sizing overrides", () => { + const document = createDocument(` +
+
+
+ `); + const card = document.getElementById("card") as HTMLElement; + mockBoundingRect(card, 160, 90); + + applyStudioBoxSize(card, { width: 240, height: 135 }); + + expect(readStudioBoxSize(card)).toEqual({ width: 240, height: 135 }); + expect(card.style.getPropertyValue(STUDIO_WIDTH_PROP)).toBe("240px"); + expect(card.style.getPropertyValue("width")).toBe("240px"); + expect(card.style.getPropertyValue("height")).toBe("135px"); + expect(card.style.getPropertyValue("flex-basis")).toBe("240px"); + expect(card.style.getPropertyValue("flex-grow")).toBe("0"); + expect(card.style.getPropertyValue("flex-shrink")).toBe("0"); + expect(card.style.getPropertyValue("box-sizing")).toBe("border-box"); + expect(card.style.getPropertyValue("scale")).toBe(""); + + applyStudioBoxSizeDraft(card, { width: 260, height: 150 }); + expect(readStudioBoxSize(card)).toEqual({ width: 260, height: 150 }); + expect(card.style.getPropertyValue("width")).toBe("260px"); + expect(card.style.getPropertyValue("height")).toBe("150px"); + expect(card.style.getPropertyValue("flex-basis")).toBe("260px"); + + const snapshot = captureStudioBoxSize(card); + applyStudioBoxSizeDraft(card, { width: 280, height: 160 }); + restoreStudioBoxSize(card, snapshot); + expect(readStudioBoxSize(card)).toEqual({ width: 260, height: 150 }); + expect(card.style.getPropertyValue("width")).toBe("260px"); + expect(card.style.getPropertyValue("height")).toBe("150px"); + expect(card.style.getPropertyValue("flex-basis")).toBe("260px"); + }); + + it("applies rotations through CSS rotate longhand", () => { + const document = createDocument(`
`); + const card = document.getElementById("card") as HTMLElement; + + applyStudioRotation(card, { angle: 24.24 }); + + expect(readStudioRotation(card)).toEqual({ angle: 24.2 }); + expect(card.style.getPropertyValue(STUDIO_ROTATION_PROP)).toBe("24.2deg"); + expect(card.style.getPropertyValue("rotate")).toContain(STUDIO_ROTATION_PROP); + expect(card.style.getPropertyValue("rotate")).toContain("8deg"); + + applyStudioRotationDraft(card, { angle: -12.26 }); + expect(readStudioRotation(card)).toEqual({ angle: -12.3 }); + expect(card.style.getPropertyValue("rotate")).toBe("calc(8deg + -12.3deg)"); + + const snapshot = captureStudioRotation(card); + applyStudioRotationDraft(card, { angle: 45 }); + restoreStudioRotation(card, snapshot); + expect(readStudioRotation(card)).toEqual({ angle: -12.3 }); + expect(card.style.getPropertyValue("rotate")).toBe("calc(8deg + -12.3deg)"); + }); + + it("uses height for flex-basis inside column flex containers", () => { + const document = createDocument(` +
+
+
+ `); + const card = document.getElementById("card") as HTMLElement; + + applyStudioBoxSize(card, { width: 240, height: 135 }); + + expect(card.style.getPropertyValue("width")).toBe("240px"); + expect(card.style.getPropertyValue("height")).toBe("135px"); + expect(card.style.getPropertyValue("flex-basis")).toBe("135px"); + }); + + it("uses additive CSS translate without mutating GSAP tweens during path-offset moves", () => { + const document = createDocument(`
`); + const card = document.getElementById("card") as HTMLElement; + const getTweensOf = vi.fn(); + const getProperty = vi.fn(); + const set = vi.fn(); + const tickerTick = vi.fn(); + const tween = { + vars: { x: 0, y: 10, startAt: { x: -240, y: -20 } }, + targets: () => [card], + invalidate: vi.fn(), + parent: { + time: () => 1.25, + totalTime: vi.fn(), + invalidate: vi.fn(), + }, + _startAt: { + vars: { x: -240, y: -20 }, + invalidate: vi.fn(), + }, + }; + + ( + document.defaultView as unknown as { + gsap: { + getTweensOf: () => Array; + getProperty: (_target: Element, property: string) => unknown; + set: (_target: Element, vars: Record) => void; + ticker: { tick: () => void }; + }; + } + ).gsap = { + getTweensOf, + getProperty, + set, + ticker: { tick: tickerTick }, + }; + + applyStudioPathOffset(card, { x: 30, y: -12 }); + + expect(tween.vars).toMatchObject({ + x: 0, + y: 10, + startAt: { x: -240, y: -20 }, + }); + expect(tween._startAt.vars).toEqual({ x: -240, y: -20 }); + expect(readStudioPathOffset(card)).toEqual({ x: 30, y: -12 }); + expect(card.style.getPropertyValue("translate")).toContain(STUDIO_OFFSET_X_PROP); + expect(getTweensOf).not.toHaveBeenCalled(); + expect(getProperty).not.toHaveBeenCalled(); + expect(set).not.toHaveBeenCalled(); + expect(tickerTick).not.toHaveBeenCalled(); + + beginStudioManualEditGesture(card); + applyStudioPathOffsetDraft(card, { x: 35, y: -6 }); + + expect(readStudioPathOffset(card)).toEqual({ x: 35, y: -6 }); + expect(card.style.getPropertyValue("translate")).toBe("35px -6px"); + expect(tween.vars).toMatchObject({ + x: 0, + y: 10, + startAt: { x: -240, y: -20 }, + }); + expect(tween._startAt.vars).toEqual({ x: -240, y: -20 }); + expect(tickerTick).not.toHaveBeenCalled(); + + applyStudioPathOffset(card, { x: 35, y: -6 }); + endStudioManualEditGesture(card); + + expect(tween.vars).toMatchObject({ + x: 0, + y: 10, + startAt: { x: -240, y: -20 }, + }); + expect(tween._startAt.vars).toEqual({ x: -240, y: -20 }); + expect(card.style.getPropertyValue("translate")).toContain(STUDIO_OFFSET_X_PROP); + + expect( + applyStudioManualEditManifest(document, emptyStudioManualEditManifest(), "index.html"), + ).toBe(0); + expect(tween.vars).toMatchObject({ + x: 0, + y: 10, + startAt: { x: -240, y: -20 }, + }); + expect(tween._startAt.vars).toEqual({ x: -240, y: -20 }); + expect(card.style.getPropertyValue(STUDIO_OFFSET_X_PROP)).toBe(""); + expect(card.style.getPropertyValue(STUDIO_OFFSET_Y_PROP)).toBe(""); + expect(card.style.getPropertyValue("translate")).toBe(""); + }); + + it("applies manifest offsets to matching preview elements", () => { + const document = createDocument(`
`); + const manifest = parseStudioManualEditManifest(`{ + "version": 1, + "edits": [ + { + "kind": "path-offset", + "target": { "sourceFile": "index.html", "selector": "#card", "id": "card" }, + "x": 32, + "y": 18 + } + ] + }`); + + expect(applyStudioManualEditManifest(document, manifest, "index.html")).toBe(1); + expect(readStudioPathOffset(document.getElementById("card") as HTMLElement)).toEqual({ + x: 32, + y: 18, + }); + }); + + it("resolves manifest targets within the matching source file", () => { + const document = createDocument(` +
+
+
+
+
+
+
+ `); + const htmlElement = document.defaultView?.HTMLElement; + if (!htmlElement) throw new Error("HTMLElement fixture missing"); + const cards = Array.from(document.getElementsByTagName("*")).filter( + (element): element is HTMLElement => element instanceof htmlElement && element.id === "card", + ); + const rootCard = cards[0]; + const nestedCard = cards[1]; + const tiles = Array.from(document.getElementsByTagName("*")).filter( + (element): element is HTMLElement => + element instanceof htmlElement && element.classList.contains("tile"), + ); + const nestedSecondTile = tiles[2]; + if (!rootCard || !nestedCard || !nestedSecondTile) { + throw new Error("source-scoped fixture missing"); + } + + const manifest = parseStudioManualEditManifest(`{ + "version": 1, + "edits": [ + { + "kind": "path-offset", + "target": { + "sourceFile": "scenes/nested.html", + "selector": "#card", + "id": "card" + }, + "x": 48, + "y": 16 + }, + { + "kind": "box-size", + "target": { + "sourceFile": "scenes/nested.html", + "selector": ".tile", + "selectorIndex": 1 + }, + "width": 220, + "height": 80 + } + ] + }`); + + expect(applyStudioManualEditManifest(document, manifest, "index.html")).toBe(2); + expect(readStudioPathOffset(rootCard)).toEqual({ x: 0, y: 0 }); + expect(readStudioPathOffset(nestedCard)).toEqual({ x: 48, y: 16 }); + expect(readStudioBoxSize(nestedSecondTile)).toEqual({ width: 220, height: 80 }); + }); + + it("applies and clears manifest box sizes while restoring authored inline size", () => { + const document = createDocument(` +
+
+
+ `); + const manifest = parseStudioManualEditManifest(`{ + "version": 1, + "edits": [ + { + "kind": "box-size", + "target": { "sourceFile": "index.html", "selector": "#card", "id": "card" }, + "width": 320, + "height": 180 + } + ] + }`); + const card = document.getElementById("card") as HTMLElement; + mockBoundingRect(card, 160, 90); + + expect(applyStudioManualEditManifest(document, manifest, "index.html")).toBe(1); + expect(readStudioBoxSize(card)).toEqual({ width: 320, height: 180 }); + expect(card.style.getPropertyValue("width")).toBe("320px"); + expect(card.style.getPropertyValue("height")).toBe("180px"); + expect(card.style.getPropertyValue("flex-basis")).toBe("320px"); + + expect( + applyStudioManualEditManifest(document, emptyStudioManualEditManifest(), "index.html"), + ).toBe(0); + expect(readStudioBoxSize(card)).toEqual({ width: 0, height: 0 }); + expect(card.style.getPropertyValue("width")).toBe("160px"); + expect(card.style.getPropertyValue("height")).toBe("90px"); + expect(card.style.getPropertyValue("flex-basis")).toBe(""); + expect(card.style.getPropertyValue("flex-grow")).toBe(""); + expect(card.style.getPropertyValue("flex-shrink")).toBe(""); + expect(card.style.getPropertyValue("scale")).toBe(""); + }); + + it("applies and clears manifest rotations while restoring authored inline rotation", () => { + const document = createDocument(`
`); + const manifest = parseStudioManualEditManifest(`{ + "version": 1, + "edits": [ + { + "kind": "rotation", + "target": { "sourceFile": "index.html", "selector": "#card", "id": "card" }, + "angle": 37.5 + } + ] + }`); + const card = document.getElementById("card") as HTMLElement; + + expect(applyStudioManualEditManifest(document, manifest, "index.html")).toBe(1); + expect(readStudioRotation(card)).toEqual({ angle: 37.5 }); + expect(card.style.getPropertyValue("rotate")).toContain(STUDIO_ROTATION_PROP); + + expect( + applyStudioManualEditManifest(document, emptyStudioManualEditManifest(), "index.html"), + ).toBe(0); + expect(readStudioRotation(card)).toEqual({ angle: 0 }); + expect(card.style.getPropertyValue("rotate")).toBe("8deg"); + }); + + it("clears stale preview offsets that are no longer in the manifest", () => { + const document = createDocument(`
`); + const card = document.getElementById("card") as HTMLElement; + + applyStudioPathOffset(card, { x: 24, y: 12 }); + expect(readStudioPathOffset(card)).toEqual({ x: 24, y: 12 }); + + expect( + applyStudioManualEditManifest(document, emptyStudioManualEditManifest(), "index.html"), + ).toBe(0); + + expect(readStudioPathOffset(card)).toEqual({ x: 0, y: 0 }); + expect(card.style.getPropertyValue(STUDIO_OFFSET_X_PROP)).toBe(""); + expect(card.style.getPropertyValue(STUDIO_OFFSET_Y_PROP)).toBe(""); + expect(card.style.getPropertyValue("translate")).toBe(""); + }); + + it("does not replay the manifest over an active manual edit gesture", () => { + const document = createDocument(`
`); + const card = document.getElementById("card") as HTMLElement; + const manifest = parseStudioManualEditManifest(`{ + "version": 1, + "edits": [ + { + "kind": "path-offset", + "target": { "sourceFile": "index.html", "selector": "#card", "id": "card" }, + "x": 8, + "y": 4 + } + ] + }`); + + applyStudioPathOffset(card, { x: 40, y: 24 }); + const firstToken = beginStudioManualEditGesture(card); + const secondToken = beginStudioManualEditGesture(card); + endStudioManualEditGesture(card, firstToken); + + expect(applyStudioManualEditManifest(document, manifest, "index.html")).toBe(0); + expect(readStudioPathOffset(card)).toEqual({ x: 40, y: 24 }); + + endStudioManualEditGesture(card, secondToken); + expect(applyStudioManualEditManifest(document, manifest, "index.html")).toBe(1); + expect(readStudioPathOffset(card)).toEqual({ x: 8, y: 4 }); + }); + + it("reapplies the latest preview manifest after wrapped seeks", () => { + const window = new Window(); + const previewWindow = window as unknown as Parameters< + typeof installStudioManualEditSeekReapply + >[0] & { + __player: Record; + }; + previewWindow.__player = { + seek: () => {}, + }; + + let applied = 0; + expect( + installStudioManualEditSeekReapply(previewWindow, () => { + applied += 1; + }), + ).toBe(true); + (previewWindow.__player.seek as (time: number) => void)(1); + expect(applied).toBe(1); + + expect( + installStudioManualEditSeekReapply(previewWindow, () => { + applied += 10; + }), + ).toBe(true); + (previewWindow.__player.seek as (time: number) => void)(2); + expect(applied).toBe(11); + }); +}); diff --git a/packages/studio/src/components/editor/manualEdits.ts b/packages/studio/src/components/editor/manualEdits.ts new file mode 100644 index 0000000000..45dc6cdfc5 --- /dev/null +++ b/packages/studio/src/components/editor/manualEdits.ts @@ -0,0 +1,1042 @@ +import type { DomEditSelection } from "./domEditing"; + +export const STUDIO_MANUAL_EDITS_PATH = ".hyperframes/studio-manual-edits.json"; +export const STUDIO_OFFSET_X_PROP = "--hf-studio-offset-x"; +export const STUDIO_OFFSET_Y_PROP = "--hf-studio-offset-y"; +export const STUDIO_WIDTH_PROP = "--hf-studio-width"; +export const STUDIO_HEIGHT_PROP = "--hf-studio-height"; +export const STUDIO_ROTATION_PROP = "--hf-studio-rotation"; +const STUDIO_PATH_OFFSET_ATTR = "data-hf-studio-path-offset"; +const STUDIO_MANUAL_EDIT_GESTURE_ATTR = "data-hf-studio-manual-edit-gesture"; +const STUDIO_BOX_SIZE_ATTR = "data-hf-studio-box-size"; +const STUDIO_ROTATION_ATTR = "data-hf-studio-rotation"; +const STUDIO_ORIGINAL_WIDTH_ATTR = "data-hf-studio-original-width"; +const STUDIO_ORIGINAL_HEIGHT_ATTR = "data-hf-studio-original-height"; +const STUDIO_ORIGINAL_MIN_WIDTH_ATTR = "data-hf-studio-original-min-width"; +const STUDIO_ORIGINAL_MIN_HEIGHT_ATTR = "data-hf-studio-original-min-height"; +const STUDIO_ORIGINAL_MAX_WIDTH_ATTR = "data-hf-studio-original-max-width"; +const STUDIO_ORIGINAL_MAX_HEIGHT_ATTR = "data-hf-studio-original-max-height"; +const STUDIO_ORIGINAL_FLEX_BASIS_ATTR = "data-hf-studio-original-flex-basis"; +const STUDIO_ORIGINAL_FLEX_GROW_ATTR = "data-hf-studio-original-flex-grow"; +const STUDIO_ORIGINAL_FLEX_SHRINK_ATTR = "data-hf-studio-original-flex-shrink"; +const STUDIO_ORIGINAL_BOX_SIZING_ATTR = "data-hf-studio-original-box-sizing"; +const STUDIO_ORIGINAL_SCALE_ATTR = "data-hf-studio-original-scale"; +const STUDIO_ORIGINAL_TRANSFORM_ORIGIN_ATTR = "data-hf-studio-original-transform-origin"; +const STUDIO_ORIGINAL_DISPLAY_ATTR = "data-hf-studio-original-display"; +const STUDIO_ORIGINAL_ROTATE_ATTR = "data-hf-studio-original-rotate"; +const STUDIO_MANUAL_EDITS_APPLY_PROP = "__hfStudioManualEditsApply"; +const STUDIO_MANUAL_EDITS_WRAPPED_SEEK_PROP = "__hfStudioManualEditsWrapped"; +let studioManualEditGestureId = 0; + +export interface StudioManualEditTarget { + sourceFile: string; + selector?: string; + selectorIndex?: number; + id?: string; +} + +export interface StudioPathOffsetEdit { + kind: "path-offset"; + target: StudioManualEditTarget; + x: number; + y: number; + updatedAt?: string; +} + +export interface StudioBoxSizeEdit { + kind: "box-size"; + target: StudioManualEditTarget; + width: number; + height: number; + updatedAt?: string; +} + +export interface StudioRotationEdit { + kind: "rotation"; + target: StudioManualEditTarget; + angle: number; + updatedAt?: string; +} + +export type StudioManualEdit = StudioPathOffsetEdit | StudioBoxSizeEdit | StudioRotationEdit; + +export interface StudioManualEditManifest { + version: 1; + edits: StudioManualEdit[]; +} + +type StudioManualEditSeekWindow = Window & { + __hf?: Record; + __player?: Record; + __timeline?: Record; + __timelines?: Record>; + __hfStudioManualEditsApply?: () => void; +}; + +export function emptyStudioManualEditManifest(): StudioManualEditManifest { + return { version: 1, edits: [] }; +} + +function finiteNumber(value: unknown): number | null { + return typeof value === "number" && Number.isFinite(value) ? value : null; +} + +function parsePathOffsetEdit(value: unknown): StudioPathOffsetEdit | null { + if (!value || typeof value !== "object") return null; + const record = value as Record; + if (record.kind !== "path-offset") return null; + const target = record.target; + if (!target || typeof target !== "object") return null; + const targetRecord = target as Record; + const sourceFile = typeof targetRecord.sourceFile === "string" ? targetRecord.sourceFile : ""; + if (!sourceFile) return null; + + const selector = typeof targetRecord.selector === "string" ? targetRecord.selector : undefined; + const id = typeof targetRecord.id === "string" ? targetRecord.id : undefined; + if (!selector && !id) return null; + + const x = finiteNumber(record.x); + const y = finiteNumber(record.y); + if (x == null || y == null) return null; + + return { + kind: "path-offset", + target: { + sourceFile, + selector, + selectorIndex: finiteNumber(targetRecord.selectorIndex) ?? undefined, + id, + }, + x, + y, + updatedAt: typeof record.updatedAt === "string" ? record.updatedAt : undefined, + }; +} + +function parseBoxSizeEdit(value: unknown): StudioBoxSizeEdit | null { + if (!value || typeof value !== "object") return null; + const record = value as Record; + if (record.kind !== "box-size") return null; + const target = record.target; + if (!target || typeof target !== "object") return null; + const targetRecord = target as Record; + const sourceFile = typeof targetRecord.sourceFile === "string" ? targetRecord.sourceFile : ""; + if (!sourceFile) return null; + + const selector = typeof targetRecord.selector === "string" ? targetRecord.selector : undefined; + const id = typeof targetRecord.id === "string" ? targetRecord.id : undefined; + if (!selector && !id) return null; + + const width = finiteNumber(record.width); + const height = finiteNumber(record.height); + if (width == null || height == null || width <= 0 || height <= 0) return null; + + return { + kind: "box-size", + target: { + sourceFile, + selector, + selectorIndex: finiteNumber(targetRecord.selectorIndex) ?? undefined, + id, + }, + width, + height, + updatedAt: typeof record.updatedAt === "string" ? record.updatedAt : undefined, + }; +} + +function parseRotationEdit(value: unknown): StudioRotationEdit | null { + if (!value || typeof value !== "object") return null; + const record = value as Record; + if (record.kind !== "rotation") return null; + const target = record.target; + if (!target || typeof target !== "object") return null; + const targetRecord = target as Record; + const sourceFile = typeof targetRecord.sourceFile === "string" ? targetRecord.sourceFile : ""; + if (!sourceFile) return null; + + const selector = typeof targetRecord.selector === "string" ? targetRecord.selector : undefined; + const id = typeof targetRecord.id === "string" ? targetRecord.id : undefined; + if (!selector && !id) return null; + + const angle = finiteNumber(record.angle); + if (angle == null) return null; + + return { + kind: "rotation", + target: { + sourceFile, + selector, + selectorIndex: finiteNumber(targetRecord.selectorIndex) ?? undefined, + id, + }, + angle, + updatedAt: typeof record.updatedAt === "string" ? record.updatedAt : undefined, + }; +} + +function parseManualEdit(value: unknown): StudioManualEdit | null { + return parsePathOffsetEdit(value) ?? parseBoxSizeEdit(value) ?? parseRotationEdit(value); +} + +export function parseStudioManualEditManifest(content: string): StudioManualEditManifest { + if (!content.trim()) return emptyStudioManualEditManifest(); + + try { + const parsed = JSON.parse(content) as unknown; + if (!parsed || typeof parsed !== "object") return emptyStudioManualEditManifest(); + const edits = (parsed as { edits?: unknown }).edits; + if (!Array.isArray(edits)) return emptyStudioManualEditManifest(); + return { + version: 1, + edits: edits.map(parseManualEdit).filter((edit): edit is StudioManualEdit => edit !== null), + }; + } catch { + return emptyStudioManualEditManifest(); + } +} + +export function serializeStudioManualEditManifest(manifest: StudioManualEditManifest): string { + return `${JSON.stringify(manifest, null, 2)}\n`; +} + +function selectionTarget(selection: DomEditSelection): StudioManualEditTarget { + return { + sourceFile: selection.sourceFile || "index.html", + selector: selection.selector, + selectorIndex: selection.selectorIndex, + id: selection.id ?? undefined, + }; +} + +function targetKey(target: StudioManualEditTarget): string { + return [ + target.sourceFile, + target.id ?? "", + target.selector ?? "", + target.selectorIndex ?? "", + ].join("|"); +} + +function roundRotationAngle(angle: number): number { + return Math.round(angle * 10) / 10; +} + +export function upsertStudioPathOffsetEdit( + manifest: StudioManualEditManifest, + selection: DomEditSelection, + offset: { x: number; y: number }, +): StudioManualEditManifest { + const target = selectionTarget(selection); + const key = targetKey(target); + const nextEdit: StudioPathOffsetEdit = { + kind: "path-offset", + target, + x: Math.round(offset.x), + y: Math.round(offset.y), + updatedAt: new Date().toISOString(), + }; + + const edits = manifest.edits.filter( + (edit) => edit.kind !== "path-offset" || targetKey(edit.target) !== key, + ); + edits.push(nextEdit); + return { version: 1, edits }; +} + +export function upsertStudioBoxSizeEdit( + manifest: StudioManualEditManifest, + selection: DomEditSelection, + size: { width: number; height: number }, +): StudioManualEditManifest { + const target = selectionTarget(selection); + const key = targetKey(target); + const nextEdit: StudioBoxSizeEdit = { + kind: "box-size", + target, + width: Math.round(Math.max(1, size.width)), + height: Math.round(Math.max(1, size.height)), + updatedAt: new Date().toISOString(), + }; + + const edits = manifest.edits.filter( + (edit) => edit.kind !== "box-size" || targetKey(edit.target) !== key, + ); + edits.push(nextEdit); + return { version: 1, edits }; +} + +export function upsertStudioRotationEdit( + manifest: StudioManualEditManifest, + selection: DomEditSelection, + rotation: { angle: number }, +): StudioManualEditManifest { + const target = selectionTarget(selection); + const key = targetKey(target); + const nextEdit: StudioRotationEdit = { + kind: "rotation", + target, + angle: roundRotationAngle(rotation.angle), + updatedAt: new Date().toISOString(), + }; + + const edits = manifest.edits.filter( + (edit) => edit.kind !== "rotation" || targetKey(edit.target) !== key, + ); + edits.push(nextEdit); + return { version: 1, edits }; +} + +function readPxCustomProperty(element: HTMLElement, property: string): number { + const value = Number.parseFloat(element.style.getPropertyValue(property)); + return Number.isFinite(value) ? value : 0; +} + +function readDegreeCustomProperty(element: HTMLElement, property: string): number { + const value = Number.parseFloat(element.style.getPropertyValue(property)); + return Number.isFinite(value) ? value : 0; +} + +export function readStudioPathOffset(element: HTMLElement): { x: number; y: number } { + return { + x: readPxCustomProperty(element, STUDIO_OFFSET_X_PROP), + y: readPxCustomProperty(element, STUDIO_OFFSET_Y_PROP), + }; +} + +export function readStudioBoxSize(element: HTMLElement): { width: number; height: number } { + return { + width: readPxCustomProperty(element, STUDIO_WIDTH_PROP), + height: readPxCustomProperty(element, STUDIO_HEIGHT_PROP), + }; +} + +export function readStudioRotation(element: HTMLElement): { angle: number } { + return { + angle: readDegreeCustomProperty(element, STUDIO_ROTATION_PROP), + }; +} + +export function beginStudioManualEditGesture(element: HTMLElement): string { + studioManualEditGestureId += 1; + const token = `gesture-${studioManualEditGestureId}`; + element.setAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR, token); + return token; +} + +export function endStudioManualEditGesture(element: HTMLElement, token?: string): void { + if (token && element.getAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR) !== token) return; + element.removeAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR); +} + +function isStudioManualEditGestureActive(element: HTMLElement): boolean { + return element.hasAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR); +} + +export function isStudioManualEditGestureCurrent(element: HTMLElement, token: string): boolean { + return element.getAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR) === token; +} + +function writeStudioPathOffsetVars(element: HTMLElement, offset: { x: number; y: number }): void { + element.setAttribute(STUDIO_PATH_OFFSET_ATTR, "true"); + element.style.setProperty(STUDIO_OFFSET_X_PROP, `${Math.round(offset.x)}px`); + element.style.setProperty(STUDIO_OFFSET_Y_PROP, `${Math.round(offset.y)}px`); +} + +function writeStudioBoxSizeVars( + element: HTMLElement, + size: { width: number; height: number }, +): void { + if (!element.hasAttribute(STUDIO_BOX_SIZE_ATTR)) { + element.setAttribute(STUDIO_ORIGINAL_WIDTH_ATTR, element.style.getPropertyValue("width")); + element.setAttribute(STUDIO_ORIGINAL_HEIGHT_ATTR, element.style.getPropertyValue("height")); + element.setAttribute( + STUDIO_ORIGINAL_MIN_WIDTH_ATTR, + element.style.getPropertyValue("min-width"), + ); + element.setAttribute( + STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, + element.style.getPropertyValue("min-height"), + ); + element.setAttribute( + STUDIO_ORIGINAL_MAX_WIDTH_ATTR, + element.style.getPropertyValue("max-width"), + ); + element.setAttribute( + STUDIO_ORIGINAL_MAX_HEIGHT_ATTR, + element.style.getPropertyValue("max-height"), + ); + element.setAttribute( + STUDIO_ORIGINAL_FLEX_BASIS_ATTR, + element.style.getPropertyValue("flex-basis"), + ); + element.setAttribute( + STUDIO_ORIGINAL_FLEX_GROW_ATTR, + element.style.getPropertyValue("flex-grow"), + ); + element.setAttribute( + STUDIO_ORIGINAL_FLEX_SHRINK_ATTR, + element.style.getPropertyValue("flex-shrink"), + ); + element.setAttribute( + STUDIO_ORIGINAL_BOX_SIZING_ATTR, + element.style.getPropertyValue("box-sizing"), + ); + element.setAttribute(STUDIO_ORIGINAL_SCALE_ATTR, element.style.getPropertyValue("scale")); + element.setAttribute( + STUDIO_ORIGINAL_TRANSFORM_ORIGIN_ATTR, + element.style.getPropertyValue("transform-origin"), + ); + element.setAttribute(STUDIO_ORIGINAL_DISPLAY_ATTR, element.style.getPropertyValue("display")); + } + + element.setAttribute(STUDIO_BOX_SIZE_ATTR, "true"); + element.style.setProperty(STUDIO_WIDTH_PROP, `${Math.round(Math.max(1, size.width))}px`); + element.style.setProperty(STUDIO_HEIGHT_PROP, `${Math.round(Math.max(1, size.height))}px`); +} + +function writeStudioRotationVars(element: HTMLElement, rotation: { angle: number }): void { + if (!element.hasAttribute(STUDIO_ROTATION_ATTR)) { + element.setAttribute(STUDIO_ORIGINAL_ROTATE_ATTR, element.style.getPropertyValue("rotate")); + } + + element.setAttribute(STUDIO_ROTATION_ATTR, "true"); + element.style.setProperty(STUDIO_ROTATION_PROP, `${roundRotationAngle(rotation.angle)}deg`); +} + +function isSimpleRotateAngle(value: string): boolean { + return /^-?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|rad|turn|grad)$/.test(value.trim()); +} + +function composeStudioRotationValue(element: HTMLElement, rotationValue: string): string { + const original = element.getAttribute(STUDIO_ORIGINAL_ROTATE_ATTR)?.trim(); + if (!original || original === "none" || !isSimpleRotateAngle(original)) { + return rotationValue; + } + return `calc(${original} + ${rotationValue})`; +} + +function safeComputedStyleProperty(element: HTMLElement, property: string): string { + try { + return ( + element.ownerDocument.defaultView?.getComputedStyle(element).getPropertyValue(property) ?? "" + ); + } catch { + return ""; + } +} + +function readStyleOrComputed(element: HTMLElement, property: string): string { + return element.style.getPropertyValue(property) || safeComputedStyleProperty(element, property); +} + +function readParentFlexBasisPixels( + element: HTMLElement, + size: { width: number; height: number }, +): number | null { + const parent = element.parentElement; + if (!parent) return null; + + const display = readStyleOrComputed(parent, "display").trim(); + if (display !== "flex" && display !== "inline-flex") return null; + + const direction = readStyleOrComputed(parent, "flex-direction").trim(); + return Math.round(Math.max(1, direction.startsWith("column") ? size.height : size.width)); +} + +function restoreStaleStudioScaleResize(element: HTMLElement): void { + if (!element.hasAttribute(STUDIO_ORIGINAL_SCALE_ATTR)) return; + restoreOriginalBoxSizeProperty(element, "scale", STUDIO_ORIGINAL_SCALE_ATTR); + restoreOriginalBoxSizeProperty( + element, + "transform-origin", + STUDIO_ORIGINAL_TRANSFORM_ORIGIN_ATTR, + ); +} + +function applyStudioBoxSizeDimensions( + element: HTMLElement, + size: { width: number; height: number }, +): void { + writeStudioBoxSizeVars(element, size); + restoreStaleStudioScaleResize(element); + + const width = Math.round(Math.max(1, size.width)); + const height = Math.round(Math.max(1, size.height)); + element.style.setProperty("box-sizing", "border-box"); + element.style.setProperty("width", `${width}px`); + element.style.setProperty("height", `${height}px`); + element.style.setProperty("min-width", "0px"); + element.style.setProperty("min-height", "0px"); + element.style.setProperty("max-width", "none"); + element.style.setProperty("max-height", "none"); + const flexBasis = readParentFlexBasisPixels(element, size); + if (flexBasis != null) { + element.style.setProperty("flex-basis", `${flexBasis}px`); + element.style.setProperty("flex-grow", "0"); + element.style.setProperty("flex-shrink", "0"); + } + const computedDisplay = safeComputedStyleProperty(element, "display"); + if (computedDisplay === "inline") { + element.style.setProperty("display", "inline-block"); + } +} + +function styleUsesStudioOffset(value: string): boolean { + return value.includes(STUDIO_OFFSET_X_PROP) || value.includes(STUDIO_OFFSET_Y_PROP); +} + +function styleUsesStudioSize(value: string): boolean { + return value.includes(STUDIO_WIDTH_PROP) || value.includes(STUDIO_HEIGHT_PROP); +} + +function styleUsesStudioRotation(value: string): boolean { + return value.includes(STUDIO_ROTATION_PROP); +} + +export function applyStudioPathOffset( + element: HTMLElement, + offset: { x: number; y: number }, +): void { + writeStudioPathOffsetVars(element, offset); + element.style.setProperty( + "translate", + `var(${STUDIO_OFFSET_X_PROP}, 0px) var(${STUDIO_OFFSET_Y_PROP}, 0px)`, + ); +} + +export function applyStudioPathOffsetDraft( + element: HTMLElement, + offset: { x: number; y: number }, +): void { + writeStudioPathOffsetVars(element, offset); + element.style.setProperty("translate", `${Math.round(offset.x)}px ${Math.round(offset.y)}px`); +} + +export function applyStudioBoxSize( + element: HTMLElement, + size: { width: number; height: number }, +): void { + applyStudioBoxSizeDimensions(element, size); +} + +export function applyStudioBoxSizeDraft( + element: HTMLElement, + size: { width: number; height: number }, +): void { + applyStudioBoxSizeDimensions(element, size); +} + +export function applyStudioRotation(element: HTMLElement, rotation: { angle: number }): void { + writeStudioRotationVars(element, rotation); + element.style.setProperty( + "rotate", + composeStudioRotationValue(element, `var(${STUDIO_ROTATION_PROP}, 0deg)`), + ); +} + +export function applyStudioRotationDraft(element: HTMLElement, rotation: { angle: number }): void { + writeStudioRotationVars(element, rotation); + element.style.setProperty( + "rotate", + composeStudioRotationValue(element, `${roundRotationAngle(rotation.angle)}deg`), + ); +} + +function clearStudioPathOffset(element: HTMLElement): void { + if ( + element.hasAttribute(STUDIO_PATH_OFFSET_ATTR) || + styleUsesStudioOffset(element.style.getPropertyValue("translate")) + ) { + element.style.removeProperty("translate"); + } + element.style.removeProperty(STUDIO_OFFSET_X_PROP); + element.style.removeProperty(STUDIO_OFFSET_Y_PROP); + element.removeAttribute(STUDIO_PATH_OFFSET_ATTR); +} + +function clearStudioRotation(element: HTMLElement): void { + if ( + element.hasAttribute(STUDIO_ROTATION_ATTR) || + styleUsesStudioRotation(element.style.getPropertyValue("rotate")) + ) { + restoreOriginalRotationProperty(element); + } + + element.style.removeProperty(STUDIO_ROTATION_PROP); + element.removeAttribute(STUDIO_ROTATION_ATTR); + element.removeAttribute(STUDIO_ORIGINAL_ROTATE_ATTR); +} + +function restoreOriginalBoxSizeProperty( + element: HTMLElement, + property: + | "width" + | "height" + | "min-width" + | "min-height" + | "max-width" + | "max-height" + | "flex-basis" + | "flex-grow" + | "flex-shrink" + | "box-sizing" + | "scale" + | "transform-origin" + | "display", + attribute: string, +): void { + const original = element.getAttribute(attribute); + if (original == null || original === "") element.style.removeProperty(property); + else element.style.setProperty(property, original); + element.removeAttribute(attribute); +} + +function restoreOriginalRotationProperty(element: HTMLElement): void { + const original = element.getAttribute(STUDIO_ORIGINAL_ROTATE_ATTR); + if (original == null || original === "") element.style.removeProperty("rotate"); + else element.style.setProperty("rotate", original); + element.removeAttribute(STUDIO_ORIGINAL_ROTATE_ATTR); +} + +function clearStudioBoxSize(element: HTMLElement): void { + if ( + element.hasAttribute(STUDIO_BOX_SIZE_ATTR) || + styleUsesStudioSize(element.style.getPropertyValue("width")) || + styleUsesStudioSize(element.style.getPropertyValue("height")) || + element.hasAttribute(STUDIO_ORIGINAL_SCALE_ATTR) + ) { + restoreOriginalBoxSizeProperty(element, "width", STUDIO_ORIGINAL_WIDTH_ATTR); + restoreOriginalBoxSizeProperty(element, "height", STUDIO_ORIGINAL_HEIGHT_ATTR); + restoreOriginalBoxSizeProperty(element, "min-width", STUDIO_ORIGINAL_MIN_WIDTH_ATTR); + restoreOriginalBoxSizeProperty(element, "min-height", STUDIO_ORIGINAL_MIN_HEIGHT_ATTR); + restoreOriginalBoxSizeProperty(element, "max-width", STUDIO_ORIGINAL_MAX_WIDTH_ATTR); + restoreOriginalBoxSizeProperty(element, "max-height", STUDIO_ORIGINAL_MAX_HEIGHT_ATTR); + restoreOriginalBoxSizeProperty(element, "flex-basis", STUDIO_ORIGINAL_FLEX_BASIS_ATTR); + restoreOriginalBoxSizeProperty(element, "flex-grow", STUDIO_ORIGINAL_FLEX_GROW_ATTR); + restoreOriginalBoxSizeProperty(element, "flex-shrink", STUDIO_ORIGINAL_FLEX_SHRINK_ATTR); + restoreOriginalBoxSizeProperty(element, "box-sizing", STUDIO_ORIGINAL_BOX_SIZING_ATTR); + restoreOriginalBoxSizeProperty(element, "scale", STUDIO_ORIGINAL_SCALE_ATTR); + restoreOriginalBoxSizeProperty( + element, + "transform-origin", + STUDIO_ORIGINAL_TRANSFORM_ORIGIN_ATTR, + ); + restoreOriginalBoxSizeProperty(element, "display", STUDIO_ORIGINAL_DISPLAY_ATTR); + } + + element.style.removeProperty(STUDIO_WIDTH_PROP); + element.style.removeProperty(STUDIO_HEIGHT_PROP); + element.removeAttribute(STUDIO_BOX_SIZE_ATTR); +} + +function parseStoredOffset(value: string): number { + const parsed = Number.parseFloat(value); + return Number.isFinite(parsed) ? parsed : 0; +} + +export interface StudioBoxSizeSnapshot { + width: string; + height: string; + minWidth: string; + minHeight: string; + maxWidth: string; + maxHeight: string; + flexBasis: string; + flexGrow: string; + flexShrink: string; + boxSizing: string; + scale: string; + transformOrigin: string; + display: string; + studioWidth: string; + studioHeight: string; + marker: string | null; + originalWidth: string | null; + originalHeight: string | null; + originalMinWidth: string | null; + originalMinHeight: string | null; + originalMaxWidth: string | null; + originalMaxHeight: string | null; + originalFlexBasis: string | null; + originalFlexGrow: string | null; + originalFlexShrink: string | null; + originalBoxSizing: string | null; + originalScale: string | null; + originalTransformOrigin: string | null; + originalDisplay: string | null; +} + +export interface StudioRotationSnapshot { + rotate: string; + studioRotation: string; + marker: string | null; + originalRotate: string | null; +} + +export function captureStudioBoxSize(element: HTMLElement): StudioBoxSizeSnapshot { + return { + width: element.style.getPropertyValue("width"), + height: element.style.getPropertyValue("height"), + minWidth: element.style.getPropertyValue("min-width"), + minHeight: element.style.getPropertyValue("min-height"), + maxWidth: element.style.getPropertyValue("max-width"), + maxHeight: element.style.getPropertyValue("max-height"), + flexBasis: element.style.getPropertyValue("flex-basis"), + flexGrow: element.style.getPropertyValue("flex-grow"), + flexShrink: element.style.getPropertyValue("flex-shrink"), + boxSizing: element.style.getPropertyValue("box-sizing"), + scale: element.style.getPropertyValue("scale"), + transformOrigin: element.style.getPropertyValue("transform-origin"), + display: element.style.getPropertyValue("display"), + studioWidth: element.style.getPropertyValue(STUDIO_WIDTH_PROP), + studioHeight: element.style.getPropertyValue(STUDIO_HEIGHT_PROP), + marker: element.getAttribute(STUDIO_BOX_SIZE_ATTR), + originalWidth: element.getAttribute(STUDIO_ORIGINAL_WIDTH_ATTR), + originalHeight: element.getAttribute(STUDIO_ORIGINAL_HEIGHT_ATTR), + originalMinWidth: element.getAttribute(STUDIO_ORIGINAL_MIN_WIDTH_ATTR), + originalMinHeight: element.getAttribute(STUDIO_ORIGINAL_MIN_HEIGHT_ATTR), + originalMaxWidth: element.getAttribute(STUDIO_ORIGINAL_MAX_WIDTH_ATTR), + originalMaxHeight: element.getAttribute(STUDIO_ORIGINAL_MAX_HEIGHT_ATTR), + originalFlexBasis: element.getAttribute(STUDIO_ORIGINAL_FLEX_BASIS_ATTR), + originalFlexGrow: element.getAttribute(STUDIO_ORIGINAL_FLEX_GROW_ATTR), + originalFlexShrink: element.getAttribute(STUDIO_ORIGINAL_FLEX_SHRINK_ATTR), + originalBoxSizing: element.getAttribute(STUDIO_ORIGINAL_BOX_SIZING_ATTR), + originalScale: element.getAttribute(STUDIO_ORIGINAL_SCALE_ATTR), + originalTransformOrigin: element.getAttribute(STUDIO_ORIGINAL_TRANSFORM_ORIGIN_ATTR), + originalDisplay: element.getAttribute(STUDIO_ORIGINAL_DISPLAY_ATTR), + }; +} + +export function captureStudioRotation(element: HTMLElement): StudioRotationSnapshot { + return { + rotate: element.style.getPropertyValue("rotate"), + studioRotation: element.style.getPropertyValue(STUDIO_ROTATION_PROP), + marker: element.getAttribute(STUDIO_ROTATION_ATTR), + originalRotate: element.getAttribute(STUDIO_ORIGINAL_ROTATE_ATTR), + }; +} + +function restoreAttribute(element: HTMLElement, attribute: string, value: string | null): void { + if (value == null) element.removeAttribute(attribute); + else element.setAttribute(attribute, value); +} + +function restoreStyleProperty(element: HTMLElement, property: string, value: string): void { + if (value) element.style.setProperty(property, value); + else element.style.removeProperty(property); +} + +export function restoreStudioBoxSize(element: HTMLElement, previous: StudioBoxSizeSnapshot): void { + restoreStyleProperty(element, "width", previous.width); + restoreStyleProperty(element, "height", previous.height); + restoreStyleProperty(element, "min-width", previous.minWidth); + restoreStyleProperty(element, "min-height", previous.minHeight); + restoreStyleProperty(element, "max-width", previous.maxWidth); + restoreStyleProperty(element, "max-height", previous.maxHeight); + restoreStyleProperty(element, "flex-basis", previous.flexBasis); + restoreStyleProperty(element, "flex-grow", previous.flexGrow); + restoreStyleProperty(element, "flex-shrink", previous.flexShrink); + restoreStyleProperty(element, "box-sizing", previous.boxSizing); + restoreStyleProperty(element, "scale", previous.scale); + restoreStyleProperty(element, "transform-origin", previous.transformOrigin); + restoreStyleProperty(element, "display", previous.display); + restoreStyleProperty(element, STUDIO_WIDTH_PROP, previous.studioWidth); + restoreStyleProperty(element, STUDIO_HEIGHT_PROP, previous.studioHeight); + restoreAttribute(element, STUDIO_BOX_SIZE_ATTR, previous.marker); + restoreAttribute(element, STUDIO_ORIGINAL_WIDTH_ATTR, previous.originalWidth); + restoreAttribute(element, STUDIO_ORIGINAL_HEIGHT_ATTR, previous.originalHeight); + restoreAttribute(element, STUDIO_ORIGINAL_MIN_WIDTH_ATTR, previous.originalMinWidth); + restoreAttribute(element, STUDIO_ORIGINAL_MIN_HEIGHT_ATTR, previous.originalMinHeight); + restoreAttribute(element, STUDIO_ORIGINAL_MAX_WIDTH_ATTR, previous.originalMaxWidth); + restoreAttribute(element, STUDIO_ORIGINAL_MAX_HEIGHT_ATTR, previous.originalMaxHeight); + restoreAttribute(element, STUDIO_ORIGINAL_FLEX_BASIS_ATTR, previous.originalFlexBasis); + restoreAttribute(element, STUDIO_ORIGINAL_FLEX_GROW_ATTR, previous.originalFlexGrow); + restoreAttribute(element, STUDIO_ORIGINAL_FLEX_SHRINK_ATTR, previous.originalFlexShrink); + restoreAttribute(element, STUDIO_ORIGINAL_BOX_SIZING_ATTR, previous.originalBoxSizing); + restoreAttribute(element, STUDIO_ORIGINAL_SCALE_ATTR, previous.originalScale); + restoreAttribute( + element, + STUDIO_ORIGINAL_TRANSFORM_ORIGIN_ATTR, + previous.originalTransformOrigin, + ); + restoreAttribute(element, STUDIO_ORIGINAL_DISPLAY_ATTR, previous.originalDisplay); +} + +export function restoreStudioRotation( + element: HTMLElement, + previous: StudioRotationSnapshot, +): void { + restoreStyleProperty(element, "rotate", previous.rotate); + restoreStyleProperty(element, STUDIO_ROTATION_PROP, previous.studioRotation); + restoreAttribute(element, STUDIO_ROTATION_ATTR, previous.marker); + restoreAttribute(element, STUDIO_ORIGINAL_ROTATE_ATTR, previous.originalRotate); +} + +export function restoreStudioPathOffset( + element: HTMLElement, + previous: { translate: string; x: string; y: string }, +): void { + const hadStudioOffset = + Boolean(previous.x) || Boolean(previous.y) || styleUsesStudioOffset(previous.translate); + + applyStudioPathOffset(element, { + x: parseStoredOffset(previous.x), + y: parseStoredOffset(previous.y), + }); + + if (previous.translate) element.style.setProperty("translate", previous.translate); + else element.style.removeProperty("translate"); + + if (previous.x) element.style.setProperty(STUDIO_OFFSET_X_PROP, previous.x); + else element.style.removeProperty(STUDIO_OFFSET_X_PROP); + + if (previous.y) element.style.setProperty(STUDIO_OFFSET_Y_PROP, previous.y); + else element.style.removeProperty(STUDIO_OFFSET_Y_PROP); + + if (!hadStudioOffset) element.removeAttribute(STUDIO_PATH_OFFSET_ATTR); +} + +function markSeekWrapped(fn: (time: number) => unknown): void { + try { + Object.defineProperty(fn, STUDIO_MANUAL_EDITS_WRAPPED_SEEK_PROP, { + configurable: false, + enumerable: false, + value: true, + }); + } catch { + try { + (fn as unknown as Record)[STUDIO_MANUAL_EDITS_WRAPPED_SEEK_PROP] = true; + } catch { + // Ignore non-extensible functions. + } + } +} + +function isSeekWrapped(fn: (time: number) => unknown): boolean { + return Boolean((fn as unknown as Record)[STUDIO_MANUAL_EDITS_WRAPPED_SEEK_PROP]); +} + +function wrapSeekReapplyFunction( + win: StudioManualEditSeekWindow, + owner: Record | undefined, + key: string, +): boolean { + const fn = owner?.[key]; + if (!owner || typeof fn !== "function") return false; + const seek = fn as (time: number) => unknown; + if (isSeekWrapped(seek)) return true; + + const wrappedSeek = function (this: unknown, time: number): unknown { + const result = seek.call(this, time); + win.__hfStudioManualEditsApply?.(); + return result; + }; + markSeekWrapped(wrappedSeek); + owner[key] = wrappedSeek; + return true; +} + +export function installStudioManualEditSeekReapply(win: Window, apply: () => void): boolean { + const studioWin = win as StudioManualEditSeekWindow; + studioWin[STUDIO_MANUAL_EDITS_APPLY_PROP] = apply; + + const wrappedHfSeek = wrapSeekReapplyFunction(studioWin, studioWin.__hf, "seek"); + const wrappedPlayerSeek = wrapSeekReapplyFunction(studioWin, studioWin.__player, "seek"); + const wrappedPlayerRenderSeek = wrapSeekReapplyFunction( + studioWin, + studioWin.__player, + "renderSeek", + ); + const wrappedTimelineSeek = wrapSeekReapplyFunction(studioWin, studioWin.__timeline, "seek"); + let wrappedNamedTimelineSeek = false; + for (const timeline of Object.values(studioWin.__timelines ?? {})) { + wrappedNamedTimelineSeek = + wrapSeekReapplyFunction(studioWin, timeline, "seek") || wrappedNamedTimelineSeek; + } + + return ( + wrappedHfSeek || + wrappedPlayerSeek || + wrappedPlayerRenderSeek || + wrappedTimelineSeek || + wrappedNamedTimelineSeek + ); +} + +function sourceFileMatches(activeCompositionPath: string | null, sourceFile: string): boolean { + if (!activeCompositionPath || activeCompositionPath === "index.html") return true; + return activeCompositionPath === sourceFile; +} + +function findClosestByAttribute(el: HTMLElement, attributeNames: string[]): HTMLElement | null { + let current: HTMLElement | null = el; + while (current) { + const candidate = current; + if (attributeNames.some((attribute) => candidate.hasAttribute(attribute))) { + return candidate; + } + current = current.parentElement; + } + return null; +} + +function getManualEditSourceFileForElement( + el: HTMLElement, + activeCompositionPath: string | null, +): string { + const ownerRoot = findClosestByAttribute(el, ["data-composition-id"]); + return ( + ownerRoot?.getAttribute("data-composition-file") ?? + ownerRoot?.getAttribute("data-composition-src") ?? + activeCompositionPath ?? + "index.html" + ); +} + +function elementMatchesManualEditSourceFile( + element: HTMLElement, + sourceFile: string, + activeCompositionPath: string | null, +): boolean { + return getManualEditSourceFileForElement(element, activeCompositionPath) === sourceFile; +} + +function queryManualEditSelectorCandidates( + doc: Document, + selector: string, + htmlElement: typeof HTMLElement, +): HTMLElement[] { + const className = selector.match(/^\.([A-Za-z0-9_-]+)$/)?.[1]; + if (className) { + return Array.from(doc.getElementsByTagName("*")).filter( + (element): element is HTMLElement => + element instanceof htmlElement && element.classList.contains(className), + ); + } + + if (/^[A-Za-z][A-Za-z0-9-]*$/.test(selector)) { + return Array.from(doc.getElementsByTagName(selector)).filter( + (element): element is HTMLElement => element instanceof htmlElement, + ); + } + + return Array.from(doc.querySelectorAll(selector)).filter( + (element): element is HTMLElement => element instanceof htmlElement, + ); +} + +function resolveManualEditTarget( + doc: Document, + edit: StudioManualEdit, + activeCompositionPath: string | null, +): HTMLElement | null { + if (!sourceFileMatches(activeCompositionPath, edit.target.sourceFile)) return null; + const htmlElement = doc.defaultView?.HTMLElement; + if (!htmlElement) return null; + + if (edit.target.id) { + const byId = doc.getElementById(edit.target.id); + if ( + byId instanceof htmlElement && + elementMatchesManualEditSourceFile(byId, edit.target.sourceFile, activeCompositionPath) + ) { + return byId; + } + + const matchesById = [doc.documentElement, ...Array.from(doc.getElementsByTagName("*"))].filter( + (element): element is HTMLElement => + element instanceof htmlElement && + element.id === edit.target.id && + elementMatchesManualEditSourceFile(element, edit.target.sourceFile, activeCompositionPath), + ); + if (matchesById[0]) return matchesById[0]; + } + + if (!edit.target.selector) return null; + try { + const matches = queryManualEditSelectorCandidates( + doc, + edit.target.selector, + htmlElement, + ).filter((element) => + elementMatchesManualEditSourceFile(element, edit.target.sourceFile, activeCompositionPath), + ); + return matches[edit.target.selectorIndex ?? 0] ?? null; + } catch { + return null; + } +} + +function collectStudioManualEditElements(doc: Document): HTMLElement[] { + const htmlElement = doc.defaultView?.HTMLElement; + if (!htmlElement) return []; + + const elements = [doc.documentElement, ...Array.from(doc.getElementsByTagName("*"))].filter( + (element): element is HTMLElement => element instanceof htmlElement, + ); + + return elements.filter( + (element) => + element.hasAttribute(STUDIO_PATH_OFFSET_ATTR) || + element.hasAttribute(STUDIO_MANUAL_EDIT_GESTURE_ATTR) || + element.hasAttribute(STUDIO_BOX_SIZE_ATTR) || + element.hasAttribute(STUDIO_ROTATION_ATTR) || + element.hasAttribute(STUDIO_ORIGINAL_MIN_WIDTH_ATTR) || + element.hasAttribute(STUDIO_ORIGINAL_FLEX_BASIS_ATTR) || + element.hasAttribute(STUDIO_ORIGINAL_SCALE_ATTR) || + element.hasAttribute(STUDIO_ORIGINAL_ROTATE_ATTR) || + Boolean(element.style.getPropertyValue(STUDIO_OFFSET_X_PROP)) || + Boolean(element.style.getPropertyValue(STUDIO_OFFSET_Y_PROP)) || + Boolean(element.style.getPropertyValue(STUDIO_WIDTH_PROP)) || + Boolean(element.style.getPropertyValue(STUDIO_HEIGHT_PROP)) || + Boolean(element.style.getPropertyValue(STUDIO_ROTATION_PROP)), + ); +} + +export function applyStudioManualEditManifest( + doc: Document, + manifest: StudioManualEditManifest, + activeCompositionPath: string | null, +): number { + const resolvedEdits: Array<{ edit: StudioManualEdit; element: HTMLElement }> = []; + const pathOffsetTargets = new Set(); + const boxSizeTargets = new Set(); + const rotationTargets = new Set(); + + for (const edit of manifest.edits) { + const element = resolveManualEditTarget(doc, edit, activeCompositionPath); + if (!element) continue; + if (isStudioManualEditGestureActive(element)) continue; + resolvedEdits.push({ edit, element }); + if (edit.kind === "path-offset") pathOffsetTargets.add(element); + if (edit.kind === "box-size") boxSizeTargets.add(element); + if (edit.kind === "rotation") rotationTargets.add(element); + } + + for (const element of collectStudioManualEditElements(doc)) { + if (isStudioManualEditGestureActive(element)) continue; + if (!pathOffsetTargets.has(element)) { + clearStudioPathOffset(element); + } + if (!boxSizeTargets.has(element)) { + clearStudioBoxSize(element); + } + if (!rotationTargets.has(element)) { + clearStudioRotation(element); + } + } + + let applied = 0; + for (const { edit, element } of resolvedEdits) { + if (edit.kind === "path-offset") { + applyStudioPathOffset(element, { x: edit.x, y: edit.y }); + } else if (edit.kind === "box-size") { + applyStudioBoxSize(element, { width: edit.width, height: edit.height }); + } else { + applyStudioRotation(element, { angle: edit.angle }); + } + applied += 1; + } + return applied; +} diff --git a/packages/studio/src/components/editor/manualEditsRenderScript.test.ts b/packages/studio/src/components/editor/manualEditsRenderScript.test.ts new file mode 100644 index 0000000000..6ec886d9a3 --- /dev/null +++ b/packages/studio/src/components/editor/manualEditsRenderScript.test.ts @@ -0,0 +1,188 @@ +import { describe, expect, it } from "vitest"; +import { Window } from "happy-dom"; +import { createStudioManualEditsRenderBodyScript } from "./manualEditsRenderScript"; + +function runScript( + window: Window, + script: string, + getComputedStyle: typeof window.getComputedStyle = window.getComputedStyle.bind(window), + timers: { + setInterval?: typeof globalThis.setInterval; + clearInterval?: typeof globalThis.clearInterval; + } = {}, +): void { + const execute = new Function( + "window", + "document", + "HTMLElement", + "getComputedStyle", + "setInterval", + "clearInterval", + script, + ); + execute( + window, + window.document, + window.HTMLElement, + getComputedStyle, + timers.setInterval ?? + (((callback: TimerHandler) => { + void callback; + return 0 as never; + }) as typeof globalThis.setInterval), + timers.clearInterval ?? globalThis.clearInterval, + ); +} + +describe("createStudioManualEditsRenderBodyScript", () => { + it("returns null for an empty manifest", () => { + expect(createStudioManualEditsRenderBodyScript("")).toBeNull(); + }); + + it("applies manual edits and reapplies them after render seeks", () => { + const window = new Window(); + window.document.body.innerHTML = '
'; + const card = window.document.getElementById("card"); + if (!(card instanceof window.HTMLElement)) { + throw new Error("card fixture missing"); + } + + let seekCalls = 0; + ( + window as unknown as { + __hf: { seek: (time: number) => void }; + } + ).__hf = { + seek: () => { + seekCalls += 1; + card.style.removeProperty("translate"); + }, + }; + + const script = createStudioManualEditsRenderBodyScript( + JSON.stringify({ + version: 1, + edits: [ + { + kind: "path-offset", + target: { sourceFile: "index.html", id: "card" }, + x: 12, + y: 24, + }, + { + kind: "box-size", + target: { sourceFile: "index.html", id: "card" }, + width: 120, + height: 64, + }, + { + kind: "rotation", + target: { sourceFile: "index.html", id: "card" }, + angle: 15, + }, + ], + }), + ); + if (!script) throw new Error("script fixture missing"); + + const computedStyle = (element: Element) => + ({ + display: element === card ? "block" : "block", + flexDirection: "row", + }) as CSSStyleDeclaration; + + const intervalCallbacks: Array<() => void> = []; + runScript(window, script, computedStyle, { + setInterval: ((callback: TimerHandler) => { + if (typeof callback === "function") intervalCallbacks.push(callback as () => void); + return 0 as never; + }) as typeof globalThis.setInterval, + }); + + expect(card.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x"); + expect(card.style.getPropertyValue("width")).toBe("120px"); + expect(card.style.getPropertyValue("height")).toBe("64px"); + expect(card.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation"); + + ( + window as unknown as { + __hf: { seek: (time: number) => void }; + } + ).__hf.seek(1); + + expect(seekCalls).toBe(1); + expect(card.style.getPropertyValue("translate")).toContain("--hf-studio-offset-x"); + + ( + window as unknown as { + __hf: { seek: (time: number) => void }; + } + ).__hf.seek = () => { + card.style.removeProperty("rotate"); + }; + intervalCallbacks.forEach((callback) => callback()); + ( + window as unknown as { + __hf: { seek: (time: number) => void }; + } + ).__hf.seek(2); + expect(card.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation"); + + ( + window as unknown as { + __player: { renderSeek: (time: number) => void }; + } + ).__player = { + renderSeek: () => { + card.style.removeProperty("rotate"); + }, + }; + intervalCallbacks.forEach((callback) => callback()); + ( + window as unknown as { + __player: { renderSeek: (time: number) => void }; + } + ).__player.renderSeek(3); + expect(card.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation"); + }); + + it("applies render edits to the matching source file target", () => { + const window = new Window(); + window.document.body.innerHTML = ` +
+
+
+
+
+
+ `; + const cards = Array.from(window.document.getElementsByTagName("*")).filter( + (element): element is HTMLElement => + element instanceof window.HTMLElement && element.id === "card", + ); + const rootCard = cards[0]; + const nestedCard = cards[1]; + if (!rootCard || !nestedCard) { + throw new Error("source-scoped render fixture missing"); + } + + const script = createStudioManualEditsRenderBodyScript( + JSON.stringify({ + version: 1, + edits: [ + { + kind: "rotation", + target: { sourceFile: "scenes/nested.html", id: "card" }, + angle: 21, + }, + ], + }), + ); + if (!script) throw new Error("script fixture missing"); + + runScript(window, script); + + expect(rootCard.style.getPropertyValue("rotate")).toBe(""); + expect(nestedCard.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation"); + }); +}); diff --git a/packages/studio/src/components/editor/manualEditsRenderScript.ts b/packages/studio/src/components/editor/manualEditsRenderScript.ts new file mode 100644 index 0000000000..0bf7b3fd76 --- /dev/null +++ b/packages/studio/src/components/editor/manualEditsRenderScript.ts @@ -0,0 +1,273 @@ +export function createStudioManualEditsRenderBodyScript(manifestContent: string): string | null { + if (!manifestContent.trim()) return null; + return `(${studioManualEditsRenderRuntime.toString()})(${JSON.stringify(manifestContent)});`; +} + +function studioManualEditsRenderRuntime(manifestContent: string): void { + const OFFSET_X_PROP = "--hf-studio-offset-x"; + const OFFSET_Y_PROP = "--hf-studio-offset-y"; + const WIDTH_PROP = "--hf-studio-width"; + const HEIGHT_PROP = "--hf-studio-height"; + const ROTATION_PROP = "--hf-studio-rotation"; + const WRAPPED_SEEK_PROP = "__hfStudioManualEditsWrapped"; + + const finiteNumber = (value: unknown): number | null => + typeof value === "number" && Number.isFinite(value) ? value : null; + + const objectRecord = (value: unknown): Record | null => + value && typeof value === "object" ? (value as Record) : null; + + const parsedManifest = (() => { + try { + return objectRecord(JSON.parse(manifestContent)); + } catch { + return null; + } + })(); + const manifestEdits = Array.isArray(parsedManifest?.edits) ? parsedManifest.edits : []; + if (manifestEdits.length === 0) return; + + const findClosestByAttribute = ( + element: HTMLElement, + attributeNames: string[], + ): HTMLElement | null => { + let current: HTMLElement | null = element; + while (current) { + const candidate = current; + if (attributeNames.some((attribute) => candidate.hasAttribute(attribute))) { + return candidate; + } + current = current.parentElement; + } + return null; + }; + + const sourceFileForElement = (element: HTMLElement): string => { + const ownerRoot = findClosestByAttribute(element, ["data-composition-id"]); + return ( + ownerRoot?.getAttribute("data-composition-file") ?? + ownerRoot?.getAttribute("data-composition-src") ?? + "index.html" + ); + }; + + const elementMatchesSourceFile = (element: HTMLElement, sourceFile: string): boolean => + sourceFileForElement(element) === sourceFile; + + const querySelectorCandidates = (selector: string): HTMLElement[] => { + const className = selector.match(/^\.([A-Za-z0-9_-]+)$/)?.[1]; + if (className) { + return Array.from(document.getElementsByTagName("*")).filter( + (element): element is HTMLElement => + element instanceof HTMLElement && element.classList.contains(className), + ); + } + + if (/^[A-Za-z][A-Za-z0-9-]*$/.test(selector)) { + return Array.from(document.getElementsByTagName(selector)).filter( + (element): element is HTMLElement => element instanceof HTMLElement, + ); + } + + return Array.from(document.querySelectorAll(selector)).filter( + (element): element is HTMLElement => element instanceof HTMLElement, + ); + }; + + const resolveTarget = (edit: Record): HTMLElement | null => { + const targetRecord = objectRecord(edit.target); + if (!targetRecord) return null; + + const sourceFile = typeof targetRecord.sourceFile === "string" ? targetRecord.sourceFile : ""; + if (!sourceFile) return null; + + const id = typeof targetRecord.id === "string" ? targetRecord.id : ""; + if (id) { + const byId = document.getElementById(id); + if (byId instanceof HTMLElement && elementMatchesSourceFile(byId, sourceFile)) return byId; + + const matchesById = [ + document.documentElement, + ...Array.from(document.getElementsByTagName("*")), + ].filter( + (element): element is HTMLElement => + element instanceof HTMLElement && + element.id === id && + elementMatchesSourceFile(element, sourceFile), + ); + if (matchesById[0]) return matchesById[0]; + } + + const selector = typeof targetRecord.selector === "string" ? targetRecord.selector : ""; + if (!selector) return null; + + try { + const matches = querySelectorCandidates(selector).filter((element) => + elementMatchesSourceFile(element, sourceFile), + ); + const selectorIndex = finiteNumber(targetRecord.selectorIndex) ?? 0; + return matches[Math.max(0, Math.floor(selectorIndex))] ?? null; + } catch { + return null; + } + }; + + const roundRotationAngle = (angle: number): number => Math.round(angle * 10) / 10; + + const isSimpleRotateAngle = (value: string): boolean => + /^-?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|rad|turn|grad)$/.test(value.trim()); + + const composeRotation = (element: HTMLElement, rotationValue: string): string => { + const original = element.getAttribute("data-hf-studio-original-rotate")?.trim(); + if (!original || original === "none" || !isSimpleRotateAngle(original)) { + return rotationValue; + } + return `calc(${original} + ${rotationValue})`; + }; + + const applyPathOffset = (element: HTMLElement, edit: Record): void => { + const x = finiteNumber(edit.x); + const y = finiteNumber(edit.y); + if (x == null || y == null) return; + element.setAttribute("data-hf-studio-path-offset", "true"); + element.style.setProperty(OFFSET_X_PROP, `${Math.round(x)}px`); + element.style.setProperty(OFFSET_Y_PROP, `${Math.round(y)}px`); + element.style.setProperty("translate", `var(${OFFSET_X_PROP}, 0px) var(${OFFSET_Y_PROP}, 0px)`); + }; + + const readParentFlexBasisPixels = ( + element: HTMLElement, + size: { width: number; height: number }, + ): number | null => { + const parent = element.parentElement; + if (!parent) return null; + const styles = getComputedStyle(parent); + if (styles.display !== "flex" && styles.display !== "inline-flex") return null; + return Math.round( + Math.max(1, styles.flexDirection.startsWith("column") ? size.height : size.width), + ); + }; + + const applyBoxSize = (element: HTMLElement, edit: Record): void => { + const width = finiteNumber(edit.width); + const height = finiteNumber(edit.height); + if (width == null || height == null || width <= 0 || height <= 0) return; + + const rounded = { + width: Math.round(Math.max(1, width)), + height: Math.round(Math.max(1, height)), + }; + element.setAttribute("data-hf-studio-box-size", "true"); + element.style.setProperty(WIDTH_PROP, `${rounded.width}px`); + element.style.setProperty(HEIGHT_PROP, `${rounded.height}px`); + element.style.setProperty("box-sizing", "border-box"); + element.style.setProperty("width", `${rounded.width}px`); + element.style.setProperty("height", `${rounded.height}px`); + element.style.setProperty("min-width", "0px"); + element.style.setProperty("min-height", "0px"); + element.style.setProperty("max-width", "none"); + element.style.setProperty("max-height", "none"); + + const flexBasis = readParentFlexBasisPixels(element, rounded); + if (flexBasis != null) { + element.style.setProperty("flex-basis", `${flexBasis}px`); + element.style.setProperty("flex-grow", "0"); + element.style.setProperty("flex-shrink", "0"); + } + if (getComputedStyle(element).display === "inline") { + element.style.setProperty("display", "inline-block"); + } + }; + + const applyRotation = (element: HTMLElement, edit: Record): void => { + const angle = finiteNumber(edit.angle); + if (angle == null) return; + if (!element.hasAttribute("data-hf-studio-rotation")) { + element.setAttribute( + "data-hf-studio-original-rotate", + element.style.getPropertyValue("rotate"), + ); + } + element.setAttribute("data-hf-studio-rotation", "true"); + element.style.setProperty(ROTATION_PROP, `${roundRotationAngle(angle)}deg`); + element.style.setProperty("rotate", composeRotation(element, `var(${ROTATION_PROP}, 0deg)`)); + }; + + const applyManifest = (): number => { + let applied = 0; + for (const edit of manifestEdits) { + const editRecord = objectRecord(edit); + if (!editRecord) continue; + const element = resolveTarget(editRecord); + if (!element) continue; + if (editRecord.kind === "path-offset") applyPathOffset(element, editRecord); + if (editRecord.kind === "box-size") applyBoxSize(element, editRecord); + if (editRecord.kind === "rotation") applyRotation(element, editRecord); + applied += 1; + } + return applied; + }; + + const markWrapped = (fn: (time: number) => unknown): void => { + try { + Object.defineProperty(fn, WRAPPED_SEEK_PROP, { + configurable: false, + enumerable: false, + value: true, + }); + } catch { + try { + (fn as unknown as Record)[WRAPPED_SEEK_PROP] = true; + } catch { + // Ignore non-extensible functions. + } + } + }; + + const isWrapped = (fn: (time: number) => unknown): boolean => + Boolean((fn as unknown as Record)[WRAPPED_SEEK_PROP]); + + const wrapFunction = (owner: Record | undefined, key: string): boolean => { + const fn = owner?.[key]; + if (!owner || typeof fn !== "function") return false; + const seek = fn as (time: number) => unknown; + if (isWrapped(seek)) { + applyManifest(); + return true; + } + + const wrappedSeek = function (this: unknown, time: number): unknown { + const result = seek.call(this, time); + applyManifest(); + return result; + }; + markWrapped(wrappedSeek); + owner[key] = wrappedSeek; + applyManifest(); + return true; + }; + + const wrapSeekFunctions = (): boolean => { + const runtimeWindow = window as Window & { + __hf?: { seek?: (time: number) => unknown }; + __player?: { renderSeek?: (time: number) => unknown }; + }; + const wrappedHfSeek = wrapFunction(runtimeWindow.__hf, "seek"); + const wrappedPlayerRenderSeek = wrapFunction(runtimeWindow.__player, "renderSeek"); + return wrappedHfSeek || wrappedPlayerRenderSeek; + }; + + if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", () => applyManifest(), { once: true }); + } else { + applyManifest(); + } + + wrapSeekFunctions(); + let remainingSeekWrapAttempts = 120; + const seekWrapInterval = setInterval(() => { + wrapSeekFunctions(); + remainingSeekWrapAttempts -= 1; + if (remainingSeekWrapAttempts <= 0) clearInterval(seekWrapInterval); + }, 50); +} diff --git a/packages/studio/src/components/renders/RenderQueue.tsx b/packages/studio/src/components/renders/RenderQueue.tsx index 8cca8edefb..297a9bbfe1 100644 --- a/packages/studio/src/components/renders/RenderQueue.tsx +++ b/packages/studio/src/components/renders/RenderQueue.tsx @@ -2,16 +2,18 @@ import { memo, useState, useRef, useEffect } from "react"; import { RenderQueueItem } from "./RenderQueueItem"; import type { RenderJob, ResolutionPreset } from "./useRenderQueue"; +type StartRenderHandler = ( + format: "mp4" | "webm" | "mov", + quality: "draft" | "standard" | "high", + resolution: ResolutionPreset | "auto", +) => void | Promise; + interface RenderQueueProps { jobs: RenderJob[]; projectId: string; onDelete: (jobId: string) => void; onClearCompleted: () => void; - onStartRender: ( - format: "mp4" | "webm" | "mov", - quality: "draft" | "standard" | "high", - resolution: ResolutionPreset | "auto", - ) => void; + onStartRender: StartRenderHandler; isRendering: boolean; } @@ -125,11 +127,7 @@ function FormatExportButton({ onStartRender, isRendering, }: { - onStartRender: ( - format: "mp4" | "webm" | "mov", - quality: "draft" | "standard" | "high", - resolution: ResolutionPreset | "auto", - ) => void; + onStartRender: StartRenderHandler; isRendering: boolean; }) { const [format, setFormat] = useState<"mp4" | "webm" | "mov">("mp4"); @@ -185,7 +183,9 @@ function FormatExportButton({