Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio): unmounting mid-edit saves the typed words, as blur does (#4651)
Miguel Ángel42f6a3a27e57
diff --git a/packages/studio/src/components/editor/previewReadOnly.overlay.test.tsx b/packages/studio/src/components/editor/previewReadOnly.overlay.test.tsxindex 7d187572b3..5c7a8c282d 100644--- a/packages/studio/src/components/editor/previewReadOnly.overlay.test.tsx+++ b/packages/studio/src/components/editor/previewReadOnly.overlay.test.tsx@@ -20,6 +20,11 @@ const layout = vi.hoisted(() => ({offCanvasElements: new Map<string, HTMLElement>(),}));+const actions = vi.hoisted(() => ({ current: null as unknown }));+vi.mock("../../contexts/DomEditContext", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../../contexts/DomEditContext")>()),+ useDomEditActionsContextOptional: () => actions.current,+}));vi.mock("./useOffCanvasIndicators", () => ({useOffCanvasIndicators: () => ({offCanvasRects: layout.offCanvas,@@ -141,6 +146,7 @@ beforeEach(() => {afterEach(() => {act(() => root.unmount());+ actions.current = null;document.body.innerHTML = "";layout.group = [];layout.hover = null;@@ -454,10 +460,18 @@ describe("DomEditOverlay onTextEditingChange", () => {expect(spies.onTextEditingChange.mock.calls).toEqual([[true], [false]]);});- it("reports false when the overlay unmounts mid-edit", () => {- const { spies, overlay } = fixture();+ it("saves the typed words and reports false when the overlay unmounts mid-edit", () => {+ const handleDomRichTextCommit = vi.fn();+ actions.current = { handleDomRichTextCommit };+ const { spies, selection, overlay } = fixture();pressEnter(overlay);+ selection.element.textContent = "Typed";act(() => root.unmount());+ expect(handleDomRichTextCommit).toHaveBeenCalledWith({+ element: selection.element,+ html: "Typed",+ previousHtml: "Title",+ });expect(spies.onTextEditingChange.mock.calls).toEqual([[true], [false]]);});});diff --git a/packages/studio/src/hooks/useDomEditCommits.ts b/packages/studio/src/hooks/useDomEditCommits.tsindex 4a13a3a76f..680d3d0222 100644--- a/packages/studio/src/hooks/useDomEditCommits.ts+++ b/packages/studio/src/hooks/useDomEditCommits.ts@@ -411,6 +411,7 @@ export function useDomEditCommits({handleDomAddTextField,handleDomRemoveTextField,} = useDomEditTextCommits({+ readOnlyPreview,activeCompPath,previewIframeRef,domEditSelection,diff --git a/packages/studio/src/hooks/useDomEditTextCommits.test.tsx b/packages/studio/src/hooks/useDomEditTextCommits.test.tsxindex a48bcf0775..4adaec4c68 100644--- a/packages/studio/src/hooks/useDomEditTextCommits.test.tsx+++ b/packages/studio/src/hooks/useDomEditTextCommits.test.tsx@@ -98,6 +98,7 @@ function commitParams(buildDomSelectionFromTarget: vi.fn(async () => null),persistDomEditOperations: vi.fn().mockResolvedValue(undefined),resolveImportedFontAsset: () => null,+ readOnlyPreview: false,...overrides,};}@@ -124,6 +125,41 @@ afterEach(() => {});describe("useDomEditTextCommits", () => {+ function richTextProbe() {+ const { iframe, element } = previewElement('<h1 id="t">Old</h1>', "t");+ const persist = vi.fn().mockResolvedValue(undefined);+ const base = commitParams({+ previewIframeRef: { current: iframe },+ domEditSelection: selectionFor(element),+ persistDomEditOperations: persist,+ });+ const captured: { hook: ReturnType<typeof useDomEditTextCommits> | null } = { hook: null };+ function Probe({ readOnlyPreview }: { readOnlyPreview: boolean }) {+ captured.hook = useDomEditTextCommits({ ...base, readOnlyPreview });+ return null;+ }+ const root = mountReactHarness(<Probe readOnlyPreview={false} />);+ cleanup = () => act(() => root.unmount());+ const save = captured.hook!.handleDomRichTextCommit;+ const commit = { element, html: "New", previousHtml: "Old" };+ element.innerHTML = "New";+ return { root, Probe, persist, element, save: () => act(async () => save(commit)) };+ }++ it("saves in-place text while the preview is editable", async () => {+ const { persist, save } = richTextProbe();+ await save();+ expect(persist).toHaveBeenCalledTimes(1);+ });++ it("refuses in-place text once the preview turns read-only, through an earlier handler, and puts the old text back", async () => {+ const { root, Probe, persist, element, save } = richTextProbe();+ act(() => root.render(<Probe readOnlyPreview />));+ await save();+ expect(persist).not.toHaveBeenCalled();+ expect(element.innerHTML).toBe("Old");+ });+it("keeps concurrent text commit ownership isolated by target", async () => {const { iframe, element: firstElement } = previewElement("<div id='first'>First</div><div id='second'>Second</div>",diff --git a/packages/studio/src/hooks/useDomEditTextCommits.ts b/packages/studio/src/hooks/useDomEditTextCommits.tsindex 16feac7056..0d771a82e1 100644--- a/packages/studio/src/hooks/useDomEditTextCommits.ts+++ b/packages/studio/src/hooks/useDomEditTextCommits.ts@@ -53,6 +53,7 @@ export interface UseDomEditTextCommitsParams {) => Promise<DomEditSelection | null>;persistDomEditOperations: PersistDomEditOperations;resolveImportedFontAsset: (fontFamilyValue: string) => ImportedFontAsset | null;+ readOnlyPreview: boolean;}function canCommitInlineTextSelection(selection: DomEditSelection, element: HTMLElement): boolean {@@ -114,7 +115,10 @@ export function useDomEditTextCommits({buildDomSelectionFromTarget,persistDomEditOperations,resolveImportedFontAsset,+ readOnlyPreview,}: UseDomEditTextCommitsParams) {+ const latestReadOnlyPreviewRef = useRef(readOnlyPreview);+ latestReadOnlyPreviewRef.current = readOnlyPreview;const domTextCommitVersionRef = useRef(new Map<string, symbol>());const domStyleCommitVersionRef = useRef(new Map<string, symbol>());@@ -319,6 +323,10 @@ export function useDomEditTextCommits({const handleDomRichTextCommit = useCallback(async ({ element, html, previousHtml }: InlineTextEditCommit) => {if (!domEditSelection) return;+ if (latestReadOnlyPreviewRef.current) {+ if (element.isConnected && element.innerHTML === html) element.innerHTML = previousHtml;+ return;+ }// The same gate that let the edit open, not the design panel's.//// The panel's rule is about its text fields, and it has none for andiff --git a/packages/studio/src/hooks/useInlineTextEdit.test.tsx b/packages/studio/src/hooks/useInlineTextEdit.test.tsxindex 31ce1e688a..25dfa7f020 100644--- a/packages/studio/src/hooks/useInlineTextEdit.test.tsx+++ b/packages/studio/src/hooks/useInlineTextEdit.test.tsx@@ -170,6 +170,45 @@ describe("useInlineTextEdit", () => {expect(element.hasAttribute("contenteditable")).toBe(false);});+ it("saves an open edit when the editor unmounts", () => {+ const element = heading();+ const { controls, root, onCommit } = mount();++ act(() => {+ controls().start(element);+ });+ element.textContent = "Motion Playground Live";+ act(() => root.unmount());++ expect(onCommit.mock.calls).toEqual([+ [{ element, html: "Motion Playground Live", previousHtml: "Motion Playground" }],+ ]);+ expect(element.hasAttribute("contenteditable")).toBe(false);+ });++ it("keeps the edit open when the commit function changes", () => {+ const element = heading();+ const controls: { current: InlineTextEditControls | null } = { current: null };+ function Probe({ onCommit }: { onCommit: () => void }) {+ controls.current = useInlineTextEdit({ onCommit });+ return null;+ }+ const root = createRoot(document.body.appendChild(document.createElement("div")));+ const first = vi.fn();+ const second = vi.fn();+ act(() => root.render(<Probe onCommit={first} />));+ act(() => {+ controls.current!.start(element);+ });+ act(() => root.render(<Probe onCommit={second} />));++ expect(controls.current!.session?.element).toBe(element);+ expect(first).not.toHaveBeenCalled();+ act(() => root.unmount());+ expect(first).not.toHaveBeenCalled();+ expect(second).toHaveBeenCalledTimes(1);+ });+it("hands the current text over exactly once when it commits", () => {const element = heading();const { controls, root, onCommit } = mount();diff --git a/packages/studio/src/hooks/useInlineTextEdit.ts b/packages/studio/src/hooks/useInlineTextEdit.tsindex f86cf04290..ffcd8f6f3d 100644--- a/packages/studio/src/hooks/useInlineTextEdit.ts+++ b/packages/studio/src/hooks/useInlineTextEdit.ts@@ -245,15 +245,11 @@ export function useInlineTextEdit({};}, [session, commit, cancel]);- // Navigation can remove the overlay while the opening frame is pending.- // Teardown is the single owner of cancelling that frame and restoring the- // composition node, so unmount closes through the same path as every exit.- useEffect(- () => () => {- teardown();- },- [teardown],- );+ // Unmount keeps the typed words, as blur does. Read through a ref so a new+ // onCommit never closes the session mid-edit.+ const commitRef = useRef(commit);+ commitRef.current = commit;+ useEffect(() => () => commitRef.current(), []);return { session, start, commit, cancel };}