Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: chat undo key only from the chat and never on key repeat

ogsamrat8b5cd1ff57a7
diff --git a/apps/app/src/react-app/domains/session/commands/undo-key.ts b/apps/app/src/react-app/domains/session/commands/undo-key.ts
index 5a8a90f5..8837624e 100644
--- a/apps/app/src/react-app/domains/session/commands/undo-key.ts
+++ b/apps/app/src/react-app/domains/session/commands/undo-key.ts
@@ -1,18 +1,35 @@
-// Cmd/Ctrl+Z for the chat: undo the last turn (Shift: redo), only with focus outside any editable element.
+// Cmd/Ctrl+Z for the chat: undo the last turn (Shift: redo), only with focus in the chat itself.
-/** A key press Cmd/Ctrl+Z may take for the chat: never from an editable element (its own undo). */
-export function chatUndoKey(event: Pick<KeyboardEvent, "key" | "metaKey" | "ctrlKey" | "altKey" | "shiftKey" | "target" | "defaultPrevented">, mac: boolean): "undo" | "redo" | null {
- if (event.defaultPrevented || event.altKey) return null;
+type KeyTarget = {
+ tagName?: string;
+ isContentEditable?: boolean;
+ closest?: (selector: string) => unknown;
+ ownerDocument?: { querySelector?: (selector: string) => unknown } | null;
+};
+
+/**
+ * A key press Cmd/Ctrl+Z may take for the chat. Never from an editable element (its own undo), never a held
+ * key's auto-repeat (one press is one turn), and only with focus inside the chat pane, or on the page itself
+ * while no code editor is on screen: Cmd/Ctrl+Z in the IDE's file tree, tabs, search or problems lists means
+ * the editor's undo to the person, and taking a chat turn (and its file changes) back from there loses work.
+ */
+export function chatUndoKey(event: Pick<KeyboardEvent, "key" | "metaKey" | "ctrlKey" | "altKey" | "shiftKey" | "target" | "defaultPrevented"> & { repeat?: boolean }, mac: boolean): "undo" | "redo" | null {
+ if (event.defaultPrevented || event.altKey || event.repeat) return null;
if (!(mac ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey)) return null;
if (event.key?.toLowerCase() !== "z") return null;
- // Only with focus outside every editable element: a text field (the composer too, even empty),
- // a code editor or a terminal keeps its own undo.
- const target = event.target as HTMLElement | null;
+ const target = event.target as KeyTarget | null;
if (target && typeof target === "object" && "tagName" in target) {
if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.tagName === "SELECT") return null;
if (target.isContentEditable) return null;
- if (typeof target.closest === "function" && target.closest("[contenteditable], .monaco-editor, .xterm, .cm-editor")) return null;
+ if (target.tagName === "BODY" || target.tagName === "HTML") {
+ // Nothing focused: the chat's, unless a code editor shares the window (IDE layout).
+ const editorOnScreen = target.ownerDocument?.querySelector?.("[data-editor-panel]");
+ if (editorOnScreen) return null;
+ } else {
+ if (typeof target.closest !== "function") return null;
+ if (target.closest("[contenteditable], .monaco-editor, .xterm, .cm-editor")) return null;
+ if (!target.closest("[data-session-pane]")) return null;
+ }
}
return event.shiftKey ? "redo" : "undo";
}
-
diff --git a/apps/app/tests/session-commands.test.ts b/apps/app/tests/session-commands.test.ts
index 48904497..4a9ac526 100644
--- a/apps/app/tests/session-commands.test.ts
+++ b/apps/app/tests/session-commands.test.ts
@@ -195,8 +195,27 @@ describe("chatUndoKey", () => {
expect(chatUndoKey(key({ metaKey: true, target: { tagName: "TEXTAREA", isContentEditable: false, textContent: "" } }), true)).toBeNull();
expect(chatUndoKey(key({ metaKey: true, target: { tagName: "DIV", isContentEditable: true, textContent: "draft" } }), true)).toBeNull();
expect(chatUndoKey(key({ metaKey: true, target: { tagName: "DIV", isContentEditable: true, textContent: " " } }), true)).toBeNull();
- expect(chatUndoKey(key({ metaKey: true, target: { tagName: "BUTTON", isContentEditable: false, textContent: "Send", closest: () => null } }), true)).toBe("undo");
- const inEditor = { tagName: "DIV", isContentEditable: false, textContent: "", closest: (selector: string) => (selector.includes(".monaco-editor") ? {} : null) };
+ const inChat = (selector: string) => (selector === "[data-session-pane]" ? {} : null);
+ expect(chatUndoKey(key({ metaKey: true, target: { tagName: "BUTTON", isContentEditable: false, textContent: "Send", closest: inChat } }), true)).toBe("undo");
+ const inEditor = { tagName: "DIV", isContentEditable: false, textContent: "", closest: (selector: string) => (selector.includes(".monaco-editor") || selector === "[data-session-pane]" ? {} : null) };
expect(chatUndoKey(key({ ctrlKey: true, target: inEditor }), false)).toBeNull();
});
+
+ test("only with focus in the chat: the IDE's file tree, tabs and lists keep the key", () => {
+ const outsideChat = { tagName: "BUTTON", isContentEditable: false, closest: () => null };
+ expect(chatUndoKey(key({ metaKey: true, target: outsideChat }), true)).toBeNull();
+ const noClosest = { tagName: "DIV", isContentEditable: false };
+ expect(chatUndoKey(key({ metaKey: true, target: noClosest }), true)).toBeNull();
+ // Nothing focused: the chat's in the Chat layout, nobody's while the code editor is on screen.
+ const body = (editor: boolean) => ({ tagName: "BODY", isContentEditable: false, ownerDocument: { querySelector: () => (editor ? {} : null) } });
+ expect(chatUndoKey(key({ metaKey: true, target: body(false) }), true)).toBe("undo");
+ expect(chatUndoKey(key({ metaKey: true, target: body(true) }), true)).toBeNull();
+ });
+
+ test("a held key undoes one turn, not one per auto-repeat", () => {
+ const inChat = { tagName: "BUTTON", isContentEditable: false, closest: (selector: string) => (selector === "[data-session-pane]" ? {} : null) };
+ expect(chatUndoKey(key({ metaKey: true, target: inChat }), true)).toBe("undo");
+ expect(chatUndoKey({ ...key({ metaKey: true, target: inChat }), repeat: true }, true)).toBeNull();
+ expect(chatUndoKey({ ...key({ metaKey: true, shiftKey: true, target: inChat }), repeat: true }, true)).toBeNull();
+ });
});