fix(studio): Escape keeps the text you typed in an in-place edit (#4841)

This commit is contained in:
Miguel Ángel
2026-10-01 02:59:24 -07:00
committed by GitHub
parent 2eb1919103
commit bb0d076c14
2 changed files with 35 additions and 5 deletions
@@ -345,7 +345,7 @@ describe("useInlineTextEdit", () => {
act(() => root.unmount());
});
it("cancels on Escape", () => {
it("commits on Escape", () => {
const element = heading("Original");
const { controls, root, onCommit } = mount();
act(() => {
@@ -355,8 +355,38 @@ describe("useInlineTextEdit", () => {
element.textContent = "half-typed";
press(element, "Escape");
expect(element.textContent).toBe("Original");
expect(onCommit).not.toHaveBeenCalled();
expect(element.textContent).toBe("half-typed");
expect(onCommit).toHaveBeenCalledTimes(1);
expect(controls().session).toBeNull();
act(() => root.unmount());
});
// The save carries the styled markup it replaced, which is what undo puts back.
it("saves typing ended by Escape once, with the markup it replaced", async () => {
const element = heading();
element.innerHTML = 'Hello <span style="color: red">world</span>';
const { controls, root, onCommit } = mount();
act(() => {
controls().start(element);
});
await act(async () => {
await new Promise((resolve) => requestAnimationFrame(() => resolve(null)));
});
expect(document.activeElement).toBe(element);
element.innerHTML = 'Hello <span style="color: red">world</span>!';
press(element, "Escape");
expect(onCommit.mock.calls).toEqual([
[
{
element,
html: 'Hello <span style="color: red">world</span>!',
previousHtml: 'Hello <span style="color: red">world</span>',
},
],
]);
expect(element.innerHTML).toBe('Hello <span style="color: red">world</span>!');
act(() => root.unmount());
});
@@ -203,7 +203,7 @@ export function useInlineTextEdit({
}
if (event.key === "Escape") {
event.preventDefault();
cancel();
commit();
}
};
// Clicking away keeps the work, which is what every other field in Studio
@@ -243,7 +243,7 @@ export function useInlineTextEdit({
element.removeEventListener("dragover", onDragOver);
element.removeEventListener("drop", onDrop);
};
}, [session, commit, cancel]);
}, [session, commit]);
// Unmount keeps the typed words, as blur does. Read through a ref so a new
// onCommit never closes the session mid-edit.