mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-17 15:24:19 +00:00
feat: persist studio manual edits via manifest
This commit is contained in:
committed by
Miguel Ángel
parent
c7e10c13af
commit
62f212aacf
@@ -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, `<div data-composition-id="main" data-width="640" data-height="360">`);
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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)), {
|
||||
|
||||
@@ -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"),
|
||||
"<!doctype html><html><head></head><body></body></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");
|
||||
|
||||
@@ -420,6 +420,8 @@ export interface FileServerOptions {
|
||||
headScripts?: string[];
|
||||
/** Scripts injected before </body> of index.html. Default: render mode extension. */
|
||||
bodyScripts?: string[];
|
||||
/** Scripts injected after bodyScripts before </body> 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<FileServer
|
||||
const preHeadScripts = [HF_EARLY_STUB, ...(options.preHeadScripts ?? [])];
|
||||
// Default scripts: Hyperframe runtime in <head>, render mode in </body>
|
||||
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();
|
||||
|
||||
|
||||
@@ -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 </body> 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();
|
||||
}
|
||||
|
||||
+283
-150
@@ -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<DomEditSelection, "id" | "selector" | "sourceFile">,
|
||||
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<DomEditSelection, "id" | "selector" | "selectorIndex">,
|
||||
): string {
|
||||
@@ -785,6 +754,7 @@ export function StudioApp() {
|
||||
const lastBlockedDomMoveToastAtRef = useRef(0);
|
||||
const importedFontAssetsRef = useRef<ImportedFontAsset[]>([]);
|
||||
const previewHotkeyWindowRef = useRef<Window | null>(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<void>) => {
|
||||
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<string> => {
|
||||
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<string> => {
|
||||
return path === STUDIO_MANUAL_EDITS_PATH
|
||||
? readOptionalProjectFile(path)
|
||||
: readProjectFile(path);
|
||||
},
|
||||
[readOptionalProjectFile, readProjectFile],
|
||||
);
|
||||
|
||||
const writeHistoryProjectFile = useCallback(
|
||||
async (path: string, content: string): Promise<void> => {
|
||||
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
|
||||
@@ -3123,9 +3255,10 @@ export function StudioApp() {
|
||||
projectId={projectId}
|
||||
onDelete={renderQueue.deleteRender}
|
||||
onClearCompleted={renderQueue.clearCompleted}
|
||||
onStartRender={(format, quality, resolution) =>
|
||||
renderQueue.startRender({ format, quality, resolution })
|
||||
}
|
||||
onStartRender={async (format, quality, resolution) => {
|
||||
await waitForPendingDomEditSaves();
|
||||
await renderQueue.startRender({ format, quality, resolution });
|
||||
}}
|
||||
isRendering={renderQueue.isRendering}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -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<FocusOptions | undefined> = [];
|
||||
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 });
|
||||
});
|
||||
});
|
||||
@@ -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<HTMLDivElement>) => void;
|
||||
onSelectedDoubleClick: () => void;
|
||||
onBlockedMove: (selection: DomEditSelection) => void;
|
||||
onMoveCommit: (
|
||||
onPathOffsetCommit: (
|
||||
selection: DomEditSelection,
|
||||
next: { left: number; top: number },
|
||||
next: { x: number; y: number },
|
||||
) => Promise<void> | void;
|
||||
onResizeCommit: (
|
||||
onBoxSizeCommit: (
|
||||
selection: DomEditSelection,
|
||||
next: { width: number; height: number },
|
||||
) => Promise<void> | void;
|
||||
onRotationCommit: (selection: DomEditSelection, next: { angle: number }) => Promise<void> | 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<HTMLDivElement | null>(null);
|
||||
const boxRef = useRef<HTMLDivElement | null>(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({
|
||||
<div
|
||||
key={selectionKey}
|
||||
ref={overlayRef}
|
||||
className="absolute inset-0 z-10 pointer-events-auto"
|
||||
className="absolute inset-0 z-10 pointer-events-auto outline-none"
|
||||
tabIndex={-1}
|
||||
aria-label="Composition canvas"
|
||||
onPointerDownCapture={(event) => 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 && (
|
||||
<div
|
||||
className="pointer-events-none absolute"
|
||||
style={{
|
||||
left: overlayRect.left + overlayRect.width / 2,
|
||||
top: overlayRect.top - 34,
|
||||
width: 28,
|
||||
height: 34,
|
||||
transform: "translateX(-50%)",
|
||||
}}
|
||||
>
|
||||
<div className="absolute left-1/2 top-3 h-5 w-px -translate-x-1/2 bg-studio-accent/60" />
|
||||
<button
|
||||
type="button"
|
||||
className="pointer-events-auto absolute left-1/2 top-0 h-4 w-4 -translate-x-1/2 rounded-full border border-studio-accent/80 bg-neutral-950 p-0 shadow-[0_0_0_2px_rgba(60,230,172,0.18)]"
|
||||
style={{ cursor: "grab", touchAction: "none" }}
|
||||
title="Rotate"
|
||||
aria-label="Rotate selection"
|
||||
onPointerDown={(e) => {
|
||||
e.stopPropagation();
|
||||
startGesture("rotate", e);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div
|
||||
key={selectionKey}
|
||||
ref={boxRef}
|
||||
@@ -405,11 +657,14 @@ export const DomEditOverlay = memo(function DomEditOverlay({
|
||||
top: overlayRect.top,
|
||||
width: overlayRect.width,
|
||||
height: overlayRect.height,
|
||||
cursor: allowCanvasMovement && selection.capabilities.canMove ? "move" : "default",
|
||||
cursor:
|
||||
allowCanvasMovement && selection.capabilities.canApplyManualOffset
|
||||
? "move"
|
||||
: "default",
|
||||
}}
|
||||
onPointerDown={(e) => {
|
||||
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 && (
|
||||
<div
|
||||
className="absolute -right-1.5 -bottom-1.5 w-3 h-3 rounded-sm bg-studio-accent border border-studio-accent/60"
|
||||
style={{ cursor: "se-resize", touchAction: "none" }}
|
||||
|
||||
@@ -58,13 +58,10 @@ interface PropertyPanelProps {
|
||||
onSetTextFieldStyle: (fieldKey: string, property: string, value: string) => void;
|
||||
onAddTextField: (afterFieldKey?: string) => string | Promise<string | null> | null;
|
||||
onRemoveTextField: (fieldKey: string) => void;
|
||||
onDetachFromLayout: () => void;
|
||||
onAskAgent: () => void;
|
||||
onCopyAgentInstruction: (instruction: string) => void | Promise<void>;
|
||||
onImportAssets?: (files: FileList) => Promise<string[]>;
|
||||
fontAssets?: ImportedFontAsset[];
|
||||
onImportFonts?: (files: FileList | File[]) => Promise<ImportedFontAsset[]>;
|
||||
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)}
|
||||
/>
|
||||
</div>
|
||||
{disabledMoveReason && (
|
||||
<div className="mt-4 flex min-w-0 flex-wrap items-center justify-between gap-3 border-l border-amber-500/40 pl-3">
|
||||
<div className="min-w-0 text-[11px] leading-5 text-neutral-400">
|
||||
<div className="font-medium text-amber-100">{disabledMoveReason}</div>
|
||||
<div>Copy a targeted prompt so an agent can refactor this layer safely.</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
void Promise.resolve(
|
||||
onCopyAgentInstruction(buildMakeMovableAgentInstruction(disabledMoveReason)),
|
||||
);
|
||||
}}
|
||||
className="inline-flex h-8 flex-shrink-0 items-center rounded-lg border border-neutral-700 bg-neutral-950 px-3 text-[11px] font-medium text-neutral-100 transition-colors hover:border-amber-400/70 hover:text-amber-100"
|
||||
>
|
||||
{copiedAgentPrompt ? "Prompt copied" : "Ask agent to make movable"}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{allowLayoutDetach && element.capabilities.canDetachFromLayout && (
|
||||
<div className="mt-4 flex min-w-0 flex-wrap items-center justify-between gap-3 border-l border-amber-500/40 pl-3">
|
||||
<div className="min-w-0 text-[11px] leading-5 text-neutral-400">
|
||||
<div className="font-medium text-neutral-200">
|
||||
This layer is controlled by layout.
|
||||
</div>
|
||||
<div>Detaches from flex/grid flow and preserves current visual position.</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onDetachFromLayout}
|
||||
className="inline-flex h-8 flex-shrink-0 items-center rounded-lg border border-neutral-700 bg-neutral-950 px-3 text-[11px] font-medium text-neutral-100 transition-colors hover:border-amber-400/70 hover:text-amber-100"
|
||||
>
|
||||
Make movable
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</Section>
|
||||
|
||||
{showEditableSections && isFlex && (
|
||||
|
||||
@@ -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(`
|
||||
<div data-composition-id="main">
|
||||
<div id="card">
|
||||
<strong>Headline</strong>
|
||||
</div>
|
||||
</div>
|
||||
`);
|
||||
|
||||
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(`
|
||||
<div data-composition-id="main">
|
||||
<div id="card">Editable</div>
|
||||
</div>
|
||||
`);
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -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<DomEditSelection, "id" | "selector" | "selectorIndex" | "sourceFile">,
|
||||
): 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<DomEditSelection, "id" | "selector" | "selectorIndex" | "sourceFile">,
|
||||
@@ -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",
|
||||
|
||||
@@ -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(`<div id="card"></div>`);
|
||||
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(`
|
||||
<div style="display: flex; flex-direction: row">
|
||||
<div id="card" style="width: 160px; height: 90px"></div>
|
||||
</div>
|
||||
`);
|
||||
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(`<div id="card" style="rotate: 8deg"></div>`);
|
||||
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(`
|
||||
<div style="display: flex; flex-direction: column">
|
||||
<div id="card" style="width: 160px; height: 90px"></div>
|
||||
</div>
|
||||
`);
|
||||
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(`<div id="card"></div>`);
|
||||
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<typeof tween>;
|
||||
getProperty: (_target: Element, property: string) => unknown;
|
||||
set: (_target: Element, vars: Record<string, unknown>) => 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(`<div id="card"></div>`);
|
||||
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(`
|
||||
<div data-composition-id="root">
|
||||
<div id="card" class="tile"></div>
|
||||
<div data-composition-id="nested" data-composition-file="scenes/nested.html">
|
||||
<div id="card" class="tile"></div>
|
||||
<div class="tile"></div>
|
||||
</div>
|
||||
</div>
|
||||
`);
|
||||
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(`
|
||||
<div style="display: flex; flex-direction: row">
|
||||
<div id="card" style="width: 160px; height: 90px"></div>
|
||||
</div>
|
||||
`);
|
||||
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(`<div id="card" style="rotate: 8deg"></div>`);
|
||||
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(`<div id="card"></div>`);
|
||||
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(`<div id="card"></div>`);
|
||||
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<string, unknown>;
|
||||
};
|
||||
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);
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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 = '<div id="card" style="width: 20px; height: 20px"></div>';
|
||||
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 = `
|
||||
<div data-composition-id="root">
|
||||
<div id="card"></div>
|
||||
<div data-composition-id="nested" data-composition-file="scenes/nested.html">
|
||||
<div id="card"></div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
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");
|
||||
});
|
||||
});
|
||||
@@ -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<string, unknown> | null =>
|
||||
value && typeof value === "object" ? (value as Record<string, unknown>) : 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<string, unknown>): 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<string, unknown>): 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<string, unknown>): 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<string, unknown>): 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<string, unknown>)[WRAPPED_SEEK_PROP] = true;
|
||||
} catch {
|
||||
// Ignore non-extensible functions.
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const isWrapped = (fn: (time: number) => unknown): boolean =>
|
||||
Boolean((fn as unknown as Record<string, unknown>)[WRAPPED_SEEK_PROP]);
|
||||
|
||||
const wrapFunction = (owner: Record<string, unknown> | 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);
|
||||
}
|
||||
@@ -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<void>;
|
||||
|
||||
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({
|
||||
<option value="webm">WebM</option>
|
||||
</select>
|
||||
<button
|
||||
onClick={() => onStartRender(format, quality, resolution)}
|
||||
onClick={() => {
|
||||
void onStartRender(format, quality, resolution);
|
||||
}}
|
||||
disabled={isRendering}
|
||||
className="flex items-center gap-1 px-2 py-0.5 text-[10px] font-semibold rounded-r bg-studio-accent text-[#09090B] hover:brightness-110 transition-colors disabled:opacity-50"
|
||||
>
|
||||
|
||||
@@ -60,6 +60,7 @@ describe("createPersistentEditHistoryController", () => {
|
||||
},
|
||||
});
|
||||
expect(result.ok).toBe(true);
|
||||
expect(result.paths).toEqual(["index.html"]);
|
||||
|
||||
expect(controller.snapshot().canUndo).toBe(false);
|
||||
expect(controller.snapshot().canRedo).toBe(true);
|
||||
|
||||
@@ -39,6 +39,7 @@ interface ApplyResult {
|
||||
ok: boolean;
|
||||
reason?: "empty" | "content-mismatch";
|
||||
label?: string;
|
||||
paths?: string[];
|
||||
}
|
||||
|
||||
interface PersistentEditHistoryStoreOptions {
|
||||
@@ -196,7 +197,7 @@ export function createPersistentEditHistoryStore({
|
||||
});
|
||||
return {
|
||||
state: result.state,
|
||||
result: { ok: true, label: result.entry.label },
|
||||
result: { ok: true, label: result.entry.label, paths: Object.keys(result.entry.files) },
|
||||
};
|
||||
});
|
||||
},
|
||||
@@ -227,7 +228,7 @@ export function createPersistentEditHistoryStore({
|
||||
});
|
||||
return {
|
||||
state: result.state,
|
||||
result: { ok: true, label: result.entry.label },
|
||||
result: { ok: true, label: result.entry.label, paths: Object.keys(result.entry.files) },
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
|
||||
function createDocument(markup: string): Document {
|
||||
const window = new Window();
|
||||
Object.assign(window, { SyntaxError });
|
||||
window.document.body.innerHTML = markup;
|
||||
return window.document;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { copyTextToClipboard } from "./clipboard";
|
||||
|
||||
function installDocument(execCommand: (command: string) => boolean): void {
|
||||
const window = new Window();
|
||||
Object.assign(window, { SyntaxError });
|
||||
Object.defineProperty(window.document, "execCommand", {
|
||||
configurable: true,
|
||||
value: execCommand,
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
realpathSync,
|
||||
} from "node:fs";
|
||||
import { isAbsolute, join, relative, resolve } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import type {
|
||||
StudioApiAdapter,
|
||||
ResolvedProject,
|
||||
@@ -18,6 +19,7 @@ import { createProjectSignature } from "../core/src/studio-api/helpers/projectSi
|
||||
import { createRetryingModuleLoader, ensureProducerDist } from "./vite.producer";
|
||||
import { readNodeRequestBody } from "./vite.request-body.js";
|
||||
import { seekThumbnailPreview } from "./vite.thumbnail";
|
||||
import { createStudioManualEditsRenderBodyScript } from "./src/components/editor/manualEditsRenderScript";
|
||||
|
||||
// ── Shared Puppeteer browser ─────────────────────────────────────────────────
|
||||
|
||||
@@ -49,6 +51,241 @@ async function getSharedBrowser(): Promise<import("puppeteer-core").Browser | nu
|
||||
// In-flight thumbnail dedup
|
||||
const _thumbnailInflight = new Map<string, Promise<Buffer>>();
|
||||
const THUMBNAIL_CACHE_VERSION = "v3";
|
||||
const STUDIO_MANUAL_EDITS_PATH = ".hyperframes/studio-manual-edits.json";
|
||||
|
||||
interface ScreenshotClip {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
function readStudioManualEditManifestContent(projectDir: string): string {
|
||||
const manifestPath = join(projectDir, STUDIO_MANUAL_EDITS_PATH);
|
||||
if (!existsSync(manifestPath)) return "";
|
||||
try {
|
||||
return readFileSync(manifestPath, "utf-8");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
async function applyStudioManualEditsToThumbnailPage(
|
||||
page: import("puppeteer-core").Page,
|
||||
manifestContent: string,
|
||||
activeCompositionPath: string,
|
||||
): Promise<void> {
|
||||
if (!manifestContent.trim()) return;
|
||||
|
||||
await page.evaluate(
|
||||
(content: string, compositionPath: string) => {
|
||||
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 finiteNumber = (value: unknown) =>
|
||||
typeof value === "number" && Number.isFinite(value) ? value : null;
|
||||
|
||||
const sourceFileMatches = (sourceFile: string) =>
|
||||
!compositionPath || compositionPath === "index.html" || compositionPath === sourceFile;
|
||||
|
||||
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") ??
|
||||
compositionPath ??
|
||||
"index.html"
|
||||
);
|
||||
};
|
||||
|
||||
const elementMatchesSourceFile = (element: HTMLElement, sourceFile: string) =>
|
||||
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<string, unknown>): HTMLElement | null => {
|
||||
const target = edit.target;
|
||||
if (!target || typeof target !== "object") return null;
|
||||
const targetRecord = target as Record<string, unknown>;
|
||||
const sourceFile =
|
||||
typeof targetRecord.sourceFile === "string" ? targetRecord.sourceFile : "";
|
||||
if (!sourceFile || !sourceFileMatches(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) => Math.round(angle * 10) / 10;
|
||||
const isSimpleRotateAngle = (value: string) =>
|
||||
/^-?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|rad|turn|grad)$/.test(value.trim());
|
||||
const composeRotation = (element: HTMLElement, rotationValue: 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<string, unknown>) => {
|
||||
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 },
|
||||
) => {
|
||||
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<string, unknown>) => {
|
||||
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<string, unknown>) => {
|
||||
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)`),
|
||||
);
|
||||
};
|
||||
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(content);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const edits = (parsed as { edits?: unknown }).edits;
|
||||
if (!Array.isArray(edits)) return;
|
||||
|
||||
for (const edit of edits) {
|
||||
if (!edit || typeof edit !== "object") continue;
|
||||
const editRecord = edit as Record<string, unknown>;
|
||||
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);
|
||||
}
|
||||
},
|
||||
manifestContent,
|
||||
activeCompositionPath,
|
||||
);
|
||||
}
|
||||
|
||||
interface ScreenshotClip {
|
||||
x: number;
|
||||
@@ -78,6 +315,7 @@ function createViteAdapter(dataDir: string, server: ViteDevServer): StudioApiAda
|
||||
quality: "draft" | "standard" | "high";
|
||||
format: string;
|
||||
outputResolution?: "landscape" | "portrait" | "landscape-4k" | "portrait-4k";
|
||||
renderBodyScripts?: string[];
|
||||
}) => unknown;
|
||||
executeRenderJob: (
|
||||
job: unknown,
|
||||
@@ -106,8 +344,8 @@ function createViteAdapter(dataDir: string, server: ViteDevServer): StudioApiAda
|
||||
};
|
||||
|
||||
const getProducerModule = async () => {
|
||||
if (!_producerModulePromise) {
|
||||
_producerModulePromise = createRetryingModuleLoader(async () => {
|
||||
if (!_producerModuleLoader) {
|
||||
_producerModuleLoader = createRetryingModuleLoader(async () => {
|
||||
const { built } = ensureProducerDist({
|
||||
studioDir: __dirname,
|
||||
env: process.env,
|
||||
@@ -119,9 +357,9 @@ function createViteAdapter(dataDir: string, server: ViteDevServer): StudioApiAda
|
||||
}
|
||||
const producerPkg = "@hyperframes/producer";
|
||||
return await import(/* @vite-ignore */ producerPkg);
|
||||
})();
|
||||
});
|
||||
}
|
||||
return _producerModulePromise();
|
||||
return _producerModuleLoader();
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -241,11 +479,14 @@ function createViteAdapter(dataDir: string, server: ViteDevServer): StudioApiAda
|
||||
if (systemChrome) process.env.PRODUCER_HEADLESS_SHELL_PATH = systemChrome;
|
||||
}
|
||||
const { createRenderJob, executeRenderJob } = await getProducerModule();
|
||||
const manifestContent = readStudioManualEditManifestContent(opts.project.dir);
|
||||
const manualEditsRenderScript = createStudioManualEditsRenderBodyScript(manifestContent);
|
||||
const job = createRenderJob({
|
||||
fps: opts.fps as 24 | 30 | 60,
|
||||
quality: opts.quality as "draft" | "standard" | "high",
|
||||
format: opts.format,
|
||||
outputResolution: opts.outputResolution,
|
||||
...(manualEditsRenderScript ? { renderBodyScripts: [manualEditsRenderScript] } : {}),
|
||||
});
|
||||
const onProgress = (j: { progress: number; currentStage?: string }) => {
|
||||
state.progress = j.progress;
|
||||
@@ -278,7 +519,11 @@ function createViteAdapter(dataDir: string, server: ViteDevServer): StudioApiAda
|
||||
const selectorKey = opts.selector
|
||||
? `_${opts.selector.replace(/[^a-zA-Z0-9_-]+/g, "_").slice(0, 80)}_${opts.selectorIndex ?? 0}`
|
||||
: "";
|
||||
const cacheKey = `${THUMBNAIL_CACHE_VERSION}_${opts.compPath.replace(/\//g, "_")}_${opts.seekTime.toFixed(2)}${selectorKey}.jpg`;
|
||||
const manifestContent = readStudioManualEditManifestContent(opts.project.dir);
|
||||
const manifestKey = manifestContent.trim()
|
||||
? `_${createHash("sha1").update(manifestContent).digest("hex").slice(0, 16)}`
|
||||
: "";
|
||||
const cacheKey = `${THUMBNAIL_CACHE_VERSION}${manifestKey}_${opts.compPath.replace(/\//g, "_")}_${opts.seekTime.toFixed(2)}${selectorKey}.${opts.format === "png" ? "png" : "jpg"}`;
|
||||
|
||||
let bufferPromise = _thumbnailInflight.get(cacheKey);
|
||||
if (!bufferPromise) {
|
||||
@@ -305,6 +550,7 @@ function createViteAdapter(dataDir: string, server: ViteDevServer): StudioApiAda
|
||||
)
|
||||
.catch(() => {});
|
||||
await seekThumbnailPreview(page, opts.seekTime);
|
||||
await applyStudioManualEditsToThumbnailPage(page, manifestContent, opts.compPath);
|
||||
await page.evaluate("document.fonts?.ready");
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
let clip: ScreenshotClip | undefined;
|
||||
|
||||
Reference in New Issue
Block a user