Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,38 @@ it.each<{ focus: string; control: ReactNode; toggles: boolean }>([
expect(onToggleOption.mock.calls).toEqual(toggles ? [["question", "Incremental"]] : []);
},
);

it.each<{ keys: string; toggles: boolean }>([
{ keys: "{Alt>}1{/Alt}", toggles: true },
{ keys: "1", toggles: false },
{ keys: "{Meta>}1{/Meta}", toggles: false },
{ keys: "{Alt>}{Shift>}1{/Shift}{/Alt}", toggles: false },
{ keys: "{Alt>}2{/Alt}", toggles: false },
])("$keys in the answer box toggles an option: $toggles", async ({ keys, toggles }) => {
const onToggleOption = vi.fn();
await using screen = await render(
<>
<div contentEditable data-testid="composer-editor" role="textbox" aria-label="Answer" />
<ComposerPendingUserInputPanel
pendingUserInputs={[prompt]}
cwd={undefined}
threadRef={threadRef}
respondingRequestIds={[]}
answers={{}}
questionIndex={0}
onToggleOption={onToggleOption}
onAdvance={vi.fn()}
onDismiss={vi.fn()}
/>
</>,
);
await expect.element(screen.getByText("Incremental")).toBeVisible();
screen.getByLabelText("Answer").element().focus();

await userEvent.keyboard(keys);

expect(onToggleOption.mock.calls).toEqual(toggles ? [["question", "Incremental"]] : []);
if (toggles) {
await expect.element(screen.getByLabelText("Answer")).toHaveTextContent("");
}
});
45 changes: 32 additions & 13 deletions apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { derivePendingUserInputProgress } from "../../pendingUserInput";
import { CheckboxMark } from "../ui/checkbox";
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
import { Kbd } from "../ui/kbd";
import { cn } from "~/lib/utils";
import { cn, isMacPlatform } from "~/lib/utils";
import { isInSideChatPane } from "~/lib/sideChatPane";
import { ComposerBanner } from "./ComposerBanner";
import ChatMarkdown from "../ChatMarkdown";
Expand Down Expand Up @@ -151,21 +151,29 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
useEffect(() => {
if (!activeQuestion || responseDisabled || isCollapsed || backgroundThread) return;
const handler = (event: globalThis.KeyboardEvent) => {
if (event.metaKey || event.ctrlKey || event.altKey) return;
const target = event.target;
if (isInSideChatPane(target)) return;
if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) {
return;
}
if (
target instanceof HTMLElement &&
target.closest(
'select, [contenteditable]:not([contenteditable="false"]), [role="dialog"], [aria-modal="true"], [role="menu"], [role="combobox"], [data-slot$="popup"]',
)
) {
return;
// Focus starts in the answer box, where a bare digit types, so a modifier selects there.
const inAnswerBox =
target instanceof Element && target.closest('[data-testid="composer-editor"]') !== null;
let digit: number;
if (inAnswerBox) {
digit = answerBoxShortcutDigit(event);
} else {
if (event.metaKey || event.ctrlKey || event.altKey) return;
if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) {
return;
}
if (
target instanceof HTMLElement &&
target.closest(
'select, [contenteditable]:not([contenteditable="false"]), [role="dialog"], [aria-modal="true"], [role="menu"], [role="combobox"], [data-slot$="popup"]',
)
) {
return;
}
digit = Number.parseInt(event.key, 10);
}
const digit = Number.parseInt(event.key, 10);
if (Number.isNaN(digit) || digit < 1 || digit > 9) return;
const optionIndex = digit - 1;
if (optionIndex >= activeQuestion.options.length) return;
Expand Down Expand Up @@ -364,3 +372,14 @@ function OptionRadio({ checked }: { checked: boolean }) {
</span>
);
}

// Option+digit everywhere, plus Control+digit on macOS. Mod+digit (Cmd on macOS, Ctrl elsewhere)
// jumps between threads. Option rewrites event.key on macOS, so the digit comes from event.code.
function answerBoxShortcutDigit(event: globalThis.KeyboardEvent): number {
if (event.metaKey || event.shiftKey) return Number.NaN;
const optionDigit = event.altKey && !event.ctrlKey;
const controlDigit = event.ctrlKey && !event.altKey && isMacPlatform(navigator.platform);
if (!optionDigit && !controlDigit) return Number.NaN;
const match = /^(?:Digit|Numpad)([1-9])$/.exec(event.code);
return match ? Number(match[1]) : Number.NaN;
}
Loading