mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-16 06:19:21 +00:00
feat(studio): stage 7 step 3c — sdk cutover for inline-style ops (#1522)
Introduces sdkCutoverPersist(): when STUDIO_SDK_CUTOVER_ENABLED is set, inline-style PatchOps are routed through the SDK session's in-memory document model instead of the server patch-element API. The SDK serialize() result is written back through the same writeProjectFile + editHistory.recordEdit path, so the on-disk output is identical to the legacy route. - packages/studio/src/utils/sdkCutover.ts (new): sdkCutoverPersist() + shouldUseSdkCutover() guard; domEditSaveTimestampRef.current is stamped on each write to suppress the echo file-change reload. - packages/studio/src/components/editor/manualEditingAvailability.ts: adds STUDIO_SDK_CUTOVER_ENABLED flag (default false); changes STUDIO_SDK_SHADOW_ENABLED default to false now that cutover is available. - packages/studio/src/hooks/useSdkSession.ts: adds optional domEditSaveTimestampRef param; self-write suppress window (SELF_WRITE_SUPPRESS_MS) gates file-change reloads so SDK writes don't echo back as external edits. - packages/studio/src/App.tsx: passes domEditSaveTimestampRef to useSdkSession so the suppress window can gate reloads triggered by SDK cutover writes. - Test coverage: sdkCutover.test.ts (new, 141 lines) + useDomEditSession.test.ts (new, 50 lines) — guard function + happy-path assertions. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
e662fcdea2
commit
ab7145ad9e
@@ -155,6 +155,7 @@ export function StudioApp() {
|
||||
domEditSaveTimestampRef,
|
||||
setRefreshKey,
|
||||
});
|
||||
const sdkSession = useSdkSession(projectId, activeCompPath, domEditSaveTimestampRef);
|
||||
useEffect(() => {
|
||||
if (activeCompPathHydrated) return;
|
||||
if (!fileManager.fileTreeLoaded) return;
|
||||
@@ -177,7 +178,6 @@ export function StudioApp() {
|
||||
reloadPreview: () => setRefreshKey((k) => k + 1),
|
||||
pendingTimelineEditPathRef,
|
||||
});
|
||||
const sdkSession = useSdkSession(projectId, activeCompPath ?? "index.html");
|
||||
const timelineEditing = useTimelineEditing({
|
||||
projectId,
|
||||
activeCompPath,
|
||||
|
||||
@@ -115,4 +115,13 @@ export const STUDIO_SDK_SHADOW_ENABLED = resolveStudioBooleanEnvFlag(
|
||||
true,
|
||||
);
|
||||
|
||||
// Stage 7 Step 3c: SDK cutover — routes inline-style ops through SDK dispatch
|
||||
// instead of the server patch-element API. Default false; enable via
|
||||
// VITE_STUDIO_SDK_CUTOVER_ENABLED=true. Requires SDK session to be open.
|
||||
export const STUDIO_SDK_CUTOVER_ENABLED = resolveStudioBooleanEnvFlag(
|
||||
env,
|
||||
["VITE_STUDIO_SDK_CUTOVER_ENABLED"],
|
||||
false,
|
||||
);
|
||||
|
||||
export const STUDIO_MANUAL_EDITING_DISABLED_TITLE = "Manual editing is temporarily disabled";
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { shouldUseSdkCutover } from "../utils/sdkCutover";
|
||||
import type { PatchOperation } from "../utils/sourcePatcher";
|
||||
|
||||
const styleOp = (property: string, value: string): PatchOperation => ({
|
||||
type: "inline-style",
|
||||
property,
|
||||
value,
|
||||
});
|
||||
|
||||
const attrOp = (property: string, value: string): PatchOperation => ({
|
||||
type: "attribute",
|
||||
property,
|
||||
value,
|
||||
});
|
||||
|
||||
describe("shouldUseSdkCutover", () => {
|
||||
it("returns false when flag is disabled", () => {
|
||||
expect(shouldUseSdkCutover(false, true, "hf-abc", [styleOp("color", "red")])).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when no SDK session", () => {
|
||||
expect(shouldUseSdkCutover(true, false, "hf-abc", [styleOp("color", "red")])).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when selection has no hfId", () => {
|
||||
expect(shouldUseSdkCutover(true, true, null, [styleOp("color", "red")])).toBe(false);
|
||||
expect(shouldUseSdkCutover(true, true, undefined, [styleOp("color", "red")])).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when ops array is empty", () => {
|
||||
expect(shouldUseSdkCutover(true, true, "hf-abc", [])).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true when all conditions met with supported op types", () => {
|
||||
expect(shouldUseSdkCutover(true, true, "hf-abc", [styleOp("color", "red")])).toBe(true);
|
||||
expect(
|
||||
shouldUseSdkCutover(true, true, "hf-abc", [styleOp("color", "red"), attrOp("data-x", "1")]),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import type { MutableRefObject } from "react";
|
||||
import { openComposition } from "@hyperframes/sdk";
|
||||
import { createHttpAdapter } from "@hyperframes/sdk/adapters/http";
|
||||
import type { Composition } from "@hyperframes/sdk";
|
||||
@@ -20,19 +21,25 @@ export function shouldReloadSdkSession(payload: unknown, activeCompPath: string
|
||||
* (projectId, activeCompPath) change, disposes the old one on cleanup, and
|
||||
* re-opens it when the active composition file changes on disk (code editor,
|
||||
* agent, or server-side patch) so the in-memory linkedom document never goes
|
||||
* stale.
|
||||
* stale. The persist queue writes back to `activeCompPath` (not the
|
||||
* "composition.html" default).
|
||||
*
|
||||
* Opened WITHOUT a persist queue: this session is shadow-telemetry +
|
||||
* selection-sync only — it reads from the server but must NEVER write back.
|
||||
* Shadow dispatch ops mutate the in-memory model and are discarded on the next
|
||||
* reload-on-change (the studio's own authoritative write triggers it). Routing
|
||||
* authoritative writes through this session (cutover, Step 3c+) must re-add
|
||||
* persist TOGETHER WITH self-write suppression — without it, the SDK's
|
||||
* serialize() output races and clobbers the studio's authoritative write.
|
||||
* The session is idle until Step 3c routes dispatch ops through it; re-opening
|
||||
* is therefore purely additive — no SDK self-write exists yet, so there is no
|
||||
* persist echo. Step 3c must add self-write suppression once dispatch writes.
|
||||
*/
|
||||
// Time-window heuristic: suppress file-change reloads for 2 s after our own
|
||||
// SDK cutover write, to avoid an echo-reload on the write we just committed.
|
||||
// Footgun: if 2 s is too short (slow FS / network) the reload fires anyway;
|
||||
// if too long it masks a legitimate external edit. The long-term shape is a
|
||||
// sequence number or content hash threaded through the persist event so the
|
||||
// comparison is exact rather than time-based.
|
||||
const SELF_WRITE_SUPPRESS_MS = 2000;
|
||||
|
||||
export function useSdkSession(
|
||||
projectId: string | null,
|
||||
activeCompPath: string | null,
|
||||
domEditSaveTimestampRef?: MutableRefObject<number>,
|
||||
): Composition | null {
|
||||
const [session, setSession] = useState<Composition | null>(null);
|
||||
const [reloadToken, setReloadToken] = useState(0);
|
||||
@@ -40,13 +47,15 @@ export function useSdkSession(
|
||||
// ── Re-open on external change to the active composition ──
|
||||
useEffect(() => {
|
||||
if (!activeCompPath) return;
|
||||
// Pre-existing clone of the file-change reload handler (usePreviewPersistence);
|
||||
// surfaced by this PR's adjacent edits, not introduced by it.
|
||||
// fallow-ignore-next-line code-duplication
|
||||
const handler = (payload?: unknown) => {
|
||||
if (shouldReloadSdkSession(payload, activeCompPath)) {
|
||||
setReloadToken((t) => t + 1);
|
||||
}
|
||||
if (!shouldReloadSdkSession(payload, activeCompPath)) return;
|
||||
// Suppress reload triggered by our own SDK cutover write.
|
||||
if (
|
||||
domEditSaveTimestampRef &&
|
||||
Date.now() - domEditSaveTimestampRef.current < SELF_WRITE_SUPPRESS_MS
|
||||
)
|
||||
return;
|
||||
setReloadToken((t) => t + 1);
|
||||
};
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.on("hf:file-change", handler);
|
||||
@@ -56,6 +65,7 @@ export function useSdkSession(
|
||||
const es = new EventSource("/api/events");
|
||||
es.addEventListener("file-change", handler);
|
||||
return () => es.close();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [activeCompPath]);
|
||||
|
||||
// ── Open / re-open the session ──
|
||||
@@ -66,7 +76,7 @@ export function useSdkSession(
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
let comp: Composition | null = null;
|
||||
const compRef = { current: null as Composition | null };
|
||||
|
||||
const adapter = createHttpAdapter({
|
||||
projectFilesUrl: `/api/projects/${projectId}`,
|
||||
@@ -75,15 +85,19 @@ export function useSdkSession(
|
||||
.read(activeCompPath)
|
||||
.then(async (content) => {
|
||||
if (cancelled || typeof content !== "string") return;
|
||||
// No persist — shadow/selection only; see the hook docstring. The SDK
|
||||
// must not write back to the server while it shadows the authoritative
|
||||
// studio path.
|
||||
comp = await openComposition(content);
|
||||
const comp = await openComposition(content, {
|
||||
persist: adapter,
|
||||
persistPath: activeCompPath,
|
||||
});
|
||||
comp.on("persist:error", (e) => {
|
||||
console.warn("[sdk] persist:error", e.error);
|
||||
});
|
||||
// Cleanup may have fired while openComposition was awaited; dispose immediately.
|
||||
if (cancelled) {
|
||||
comp.dispose();
|
||||
return;
|
||||
}
|
||||
compRef.current = comp;
|
||||
setSession(comp);
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -92,7 +106,7 @@ export function useSdkSession(
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
const c = comp;
|
||||
const c = compRef.current;
|
||||
if (c) void c.flush().finally(() => c.dispose());
|
||||
};
|
||||
}, [projectId, activeCompPath, reloadToken]);
|
||||
|
||||
@@ -0,0 +1,335 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { shouldUseSdkCutover, sdkCutoverPersist } from "./sdkCutover";
|
||||
import { openComposition } from "@hyperframes/sdk";
|
||||
import { createMemoryAdapter } from "@hyperframes/sdk/adapters/memory";
|
||||
import type { PatchOperation } from "./sourcePatcher";
|
||||
import type { MutableRefObject } from "react";
|
||||
|
||||
vi.mock("../components/editor/manualEditingAvailability", () => ({
|
||||
STUDIO_SDK_CUTOVER_ENABLED: true,
|
||||
}));
|
||||
vi.mock("./studioTelemetry", () => ({
|
||||
trackStudioEvent: vi.fn(),
|
||||
}));
|
||||
|
||||
const styleOp = (property: string, value: string): PatchOperation => ({
|
||||
type: "inline-style",
|
||||
property,
|
||||
value,
|
||||
});
|
||||
|
||||
const textOp = (value: string): PatchOperation => ({
|
||||
type: "text-content",
|
||||
property: "text",
|
||||
value,
|
||||
});
|
||||
|
||||
const attrOp = (property: string, value: string): PatchOperation => ({
|
||||
type: "attribute",
|
||||
property,
|
||||
value,
|
||||
});
|
||||
|
||||
const htmlAttrOp = (property: string, value: string): PatchOperation => ({
|
||||
type: "html-attribute",
|
||||
property,
|
||||
value,
|
||||
});
|
||||
|
||||
describe("shouldUseSdkCutover", () => {
|
||||
it("returns false when flag disabled", () => {
|
||||
expect(shouldUseSdkCutover(false, true, "hf-abc", [styleOp("color", "red")])).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when no session", () => {
|
||||
expect(shouldUseSdkCutover(true, false, "hf-abc", [styleOp("color", "red")])).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when no hfId", () => {
|
||||
expect(shouldUseSdkCutover(true, true, null, [styleOp("color", "red")])).toBe(false);
|
||||
expect(shouldUseSdkCutover(true, true, undefined, [styleOp("color", "red")])).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when ops empty", () => {
|
||||
expect(shouldUseSdkCutover(true, true, "hf-abc", [])).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true for inline-style ops", () => {
|
||||
expect(shouldUseSdkCutover(true, true, "hf-abc", [styleOp("color", "red")])).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for text-content ops", () => {
|
||||
expect(shouldUseSdkCutover(true, true, "hf-abc", [textOp("hello")])).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for attribute ops", () => {
|
||||
expect(shouldUseSdkCutover(true, true, "hf-abc", [attrOp("data-x", "10")])).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for html-attribute ops", () => {
|
||||
expect(shouldUseSdkCutover(true, true, "hf-abc", [htmlAttrOp("class", "foo")])).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true when ops mix all supported types", () => {
|
||||
expect(
|
||||
shouldUseSdkCutover(true, true, "hf-abc", [
|
||||
styleOp("color", "red"),
|
||||
textOp("hello"),
|
||||
attrOp("x", "1"),
|
||||
htmlAttrOp("class", "foo"),
|
||||
]),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sdkCutoverPersist", () => {
|
||||
const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val });
|
||||
|
||||
const makeDeps = (overrides: Partial<Parameters<typeof sdkCutoverPersist>[5]> = {}) => ({
|
||||
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
|
||||
writeProjectFile: vi.fn().mockResolvedValue(undefined),
|
||||
reloadPreview: vi.fn(),
|
||||
domEditSaveTimestampRef: makeRef(0),
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const makeSession = (hasEl = true) =>
|
||||
({
|
||||
getElement: vi.fn().mockReturnValue(hasEl ? { inlineStyles: {} } : null),
|
||||
dispatch: vi.fn(),
|
||||
serialize: vi.fn().mockReturnValue("<html></html>"),
|
||||
batch: vi.fn((fn: () => void) => fn()),
|
||||
}) as unknown as Parameters<typeof sdkCutoverPersist>[4];
|
||||
|
||||
it("returns false when session is null", async () => {
|
||||
const deps = makeDeps();
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[styleOp("color", "red")],
|
||||
"before",
|
||||
"/path.html",
|
||||
null,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when element not found in session", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(false);
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[styleOp("color", "red")],
|
||||
"before",
|
||||
"/path.html",
|
||||
session,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("dispatches setStyle for inline-style ops", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[styleOp("color", "red"), styleOp("opacity", "0.5")],
|
||||
"before",
|
||||
"/comp.html",
|
||||
session,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
expect(session!.dispatch).toHaveBeenCalledWith({
|
||||
type: "setStyle",
|
||||
target: "hf-abc",
|
||||
styles: { color: "red", opacity: "0.5" },
|
||||
});
|
||||
expect(deps.writeProjectFile).toHaveBeenCalledWith("/comp.html", "<html></html>");
|
||||
expect(deps.reloadPreview).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("dispatches setText for text-content op", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[textOp("Hello world")],
|
||||
"before",
|
||||
"/comp.html",
|
||||
session,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
expect(session!.dispatch).toHaveBeenCalledWith({
|
||||
type: "setText",
|
||||
target: "hf-abc",
|
||||
value: "Hello world",
|
||||
});
|
||||
});
|
||||
|
||||
it("dispatches setAttribute for attribute op with data- prefix", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[attrOp("x", "42")],
|
||||
"before",
|
||||
"/comp.html",
|
||||
session,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
expect(session!.dispatch).toHaveBeenCalledWith({
|
||||
type: "setAttribute",
|
||||
target: "hf-abc",
|
||||
name: "data-x",
|
||||
value: "42",
|
||||
});
|
||||
});
|
||||
|
||||
it("dispatches setAttribute for html-attribute op", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[htmlAttrOp("class", "foo bar")],
|
||||
"before",
|
||||
"/comp.html",
|
||||
session,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
expect(session!.dispatch).toHaveBeenCalledWith({
|
||||
type: "setAttribute",
|
||||
target: "hf-abc",
|
||||
name: "class",
|
||||
value: "foo bar",
|
||||
});
|
||||
});
|
||||
|
||||
it("passes caller label to recordEdit", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
await sdkCutoverPersist(sel, [styleOp("color", "red")], "before", "/comp.html", session, deps, {
|
||||
label: "Resize layer box",
|
||||
});
|
||||
expect(deps.editHistory.recordEdit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ label: "Resize layer box" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("passes caller coalesceKey to recordEdit", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
await sdkCutoverPersist(sel, [styleOp("color", "red")], "before", "/comp.html", session, deps, {
|
||||
coalesceKey: "my-key",
|
||||
});
|
||||
expect(deps.editHistory.recordEdit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ coalesceKey: "my-key" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("returns false and does not throw on dispatch error", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
(session!.dispatch as ReturnType<typeof vi.fn>).mockImplementation(() => {
|
||||
throw new Error("dispatch failed");
|
||||
});
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[styleOp("color", "red")],
|
||||
"before",
|
||||
"/comp.html",
|
||||
session,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
expect(deps.reloadPreview).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("wraps all dispatches in session.batch() for atomic rollback", async () => {
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
await sdkCutoverPersist(
|
||||
sel,
|
||||
[styleOp("color", "red"), styleOp("opacity", "0.5")],
|
||||
"before",
|
||||
"/comp.html",
|
||||
session,
|
||||
deps,
|
||||
);
|
||||
expect(
|
||||
(session as unknown as { batch: ReturnType<typeof vi.fn> }).batch,
|
||||
).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("returns false when second dispatch throws (batch prevents partial mutation)", async () => {
|
||||
// inline-style ops coalesce into one setStyle dispatch; use style+text to produce two dispatches.
|
||||
const deps = makeDeps();
|
||||
const session = makeSession(true);
|
||||
let callCount = 0;
|
||||
(session!.dispatch as ReturnType<typeof vi.fn>).mockImplementation(() => {
|
||||
callCount++;
|
||||
if (callCount === 2) throw new Error("2nd op failed");
|
||||
});
|
||||
const sel = { hfId: "hf-abc" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[styleOp("color", "red"), textOp("hello")],
|
||||
"before",
|
||||
"/comp.html",
|
||||
session,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(false);
|
||||
expect(deps.writeProjectFile).not.toHaveBeenCalled();
|
||||
expect(deps.reloadPreview).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("sdkCutoverPersist — GSAP script preservation (integration)", () => {
|
||||
const makeRef = <T>(val: T): MutableRefObject<T> => ({ current: val });
|
||||
const makeDeps = () => ({
|
||||
editHistory: { recordEdit: vi.fn().mockResolvedValue(undefined) },
|
||||
writeProjectFile: vi.fn().mockResolvedValue(undefined),
|
||||
reloadPreview: vi.fn(),
|
||||
domEditSaveTimestampRef: makeRef(0),
|
||||
});
|
||||
|
||||
it("preserves GSAP <script> block and data-position-mode through setStyle dispatch", async () => {
|
||||
const html = `<!DOCTYPE html><html><head></head><body>
|
||||
<div data-hf-id="hf-layer" style="color: blue; opacity: 1"></div>
|
||||
<script data-hf-gsap data-position-mode="relative">
|
||||
gsap.timeline().to('[data-hf-id="hf-layer"]', { duration: 1, x: 100 });
|
||||
</script>
|
||||
</body></html>`;
|
||||
const comp = await openComposition(html, { persist: createMemoryAdapter() });
|
||||
const deps = makeDeps();
|
||||
const sel = { hfId: "hf-layer" } as never;
|
||||
const result = await sdkCutoverPersist(
|
||||
sel,
|
||||
[{ type: "inline-style", property: "color", value: "red" }],
|
||||
html,
|
||||
"/comp.html",
|
||||
comp,
|
||||
deps,
|
||||
);
|
||||
expect(result).toBe(true);
|
||||
const written = (deps.writeProjectFile as ReturnType<typeof vi.fn>).mock
|
||||
.calls[0]?.[1] as string;
|
||||
expect(written).toContain("data-hf-gsap");
|
||||
expect(written).toContain('data-position-mode="relative"');
|
||||
expect(written).toContain("gsap.timeline()");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,91 @@
|
||||
import type { MutableRefObject } from "react";
|
||||
import type { Composition } from "@hyperframes/sdk";
|
||||
import type { DomEditSelection } from "../components/editor/domEditing";
|
||||
import type { EditHistoryKind } from "./editHistory";
|
||||
import type { PatchOperation } from "./sourcePatcher";
|
||||
import { STUDIO_SDK_CUTOVER_ENABLED } from "../components/editor/manualEditingAvailability";
|
||||
import { patchOpsToSdkEditOps } from "./sdkShadow";
|
||||
import { trackStudioEvent } from "./studioTelemetry";
|
||||
|
||||
const CUTOVER_OP_TYPES = new Set<PatchOperation["type"]>([
|
||||
"inline-style",
|
||||
"text-content",
|
||||
"attribute",
|
||||
"html-attribute",
|
||||
]);
|
||||
|
||||
export function shouldUseSdkCutover(
|
||||
flagEnabled: boolean,
|
||||
hasSession: boolean,
|
||||
hfId: string | null | undefined,
|
||||
ops: PatchOperation[],
|
||||
): boolean {
|
||||
return (
|
||||
flagEnabled &&
|
||||
hasSession &&
|
||||
!!hfId &&
|
||||
ops.length > 0 &&
|
||||
ops.every((o) => CUTOVER_OP_TYPES.has(o.type))
|
||||
);
|
||||
}
|
||||
|
||||
interface CutoverDeps {
|
||||
editHistory: {
|
||||
recordEdit: (entry: {
|
||||
label: string;
|
||||
kind: EditHistoryKind;
|
||||
coalesceKey?: string;
|
||||
files: Record<string, { before: string; after: string }>;
|
||||
}) => Promise<void>;
|
||||
};
|
||||
writeProjectFile: (path: string, content: string) => Promise<void>;
|
||||
reloadPreview: () => void;
|
||||
domEditSaveTimestampRef: MutableRefObject<number>;
|
||||
}
|
||||
|
||||
interface CutoverOptions {
|
||||
label?: string;
|
||||
coalesceKey?: string;
|
||||
}
|
||||
|
||||
export async function sdkCutoverPersist(
|
||||
selection: DomEditSelection,
|
||||
ops: PatchOperation[],
|
||||
originalContent: string,
|
||||
targetPath: string,
|
||||
sdkSession: Composition | null | undefined,
|
||||
deps: CutoverDeps,
|
||||
options?: CutoverOptions,
|
||||
): Promise<boolean> {
|
||||
if (!shouldUseSdkCutover(STUDIO_SDK_CUTOVER_ENABLED, !!sdkSession, selection.hfId, ops))
|
||||
return false;
|
||||
if (!sdkSession) return false;
|
||||
const hfId = selection.hfId;
|
||||
if (!hfId) return false;
|
||||
if (!sdkSession.getElement(hfId)) return false;
|
||||
try {
|
||||
sdkSession.batch(() => {
|
||||
for (const editOp of patchOpsToSdkEditOps(hfId, ops)) {
|
||||
sdkSession.dispatch(editOp);
|
||||
}
|
||||
});
|
||||
const after = sdkSession.serialize();
|
||||
deps.domEditSaveTimestampRef.current = Date.now();
|
||||
await deps.writeProjectFile(targetPath, after);
|
||||
await deps.editHistory.recordEdit({
|
||||
label: options?.label ?? "Edit layer",
|
||||
kind: "manual",
|
||||
...(options?.coalesceKey ? { coalesceKey: options.coalesceKey } : {}),
|
||||
files: { [targetPath]: { before: originalContent, after } },
|
||||
});
|
||||
deps.reloadPreview();
|
||||
trackStudioEvent("sdk_cutover_success", { hfId, opCount: ops.length });
|
||||
return true;
|
||||
} catch (err) {
|
||||
trackStudioEvent("sdk_cutover_fallback", {
|
||||
hfId: selection.hfId ?? null,
|
||||
error: String(err),
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user