diff --git a/docs/getting-started/quickstart.md b/docs/getting-started/quickstart.md index 16b74a92..78b494de 100644 --- a/docs/getting-started/quickstart.md +++ b/docs/getting-started/quickstart.md @@ -80,7 +80,7 @@ If you already used standalone OpenCode in those repositories, existing chats ap 1. Select your repository from the sidebar 2. Click **New Session** or type `/new` 3. Type your message -4. Press **Enter** to send +4. Press **Cmd+Enter** (`Ctrl+Enter` on other platforms) to send, or **Enter** on mobile ### Useful Commands @@ -105,7 +105,7 @@ If you already used standalone OpenCode in those repositories, existing chats ap | `/skills` | Load a skill | | `/connect` | Connect a provider | -Built-in commands use only the command text. Attached files and images are kept in the composer for your next message. +Press **Enter** to run a command once its name is complete; use **Shift+Enter** to continue its text on a new line. Built-in commands use only the command text. Attached files and images are kept in the composer for your next message. ### File Mentions @@ -129,7 +129,8 @@ Now that you're set up, explore more features: | Shortcut | Action | |----------|--------| -| `Enter` | Send message | +| `Cmd+Enter` / `Ctrl+Enter` | Send message | +| `Enter` | Run a slash command, such as `/btw `; otherwise a new line on desktop, send on mobile | | `Shift+Enter` | New line | The app uses a configurable leader key system (`Cmd+O` on Mac, `Ctrl+O` on other platforms) for additional shortcuts. Customize in Settings > Keyboard Shortcuts. diff --git a/frontend/src/components/message/PromptInput.command.test.tsx b/frontend/src/components/message/PromptInput.command.test.tsx index 5c46b5c8..a3d63c33 100644 --- a/frontend/src/components/message/PromptInput.command.test.tsx +++ b/frontend/src/components/message/PromptInput.command.test.tsx @@ -1,11 +1,11 @@ import { createRef } from 'react' -import { describe, it, expect, vi, beforeEach } from 'vitest' +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { render, screen, fireEvent, act, waitFor } from '@testing-library/react' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { PromptInput, type PromptInputHandle } from './PromptInput' import { useUIState } from '@/stores/uiStateStore' import { BUILTIN_COMMANDS } from '@/lib/builtinCommands' -import { createCommandActionsMock } from '@/test/test-utils' +import { createCommandActionsMock, stubMatchMedia } from '@/test/test-utils' const mocks = vi.hoisted(() => ({ runCommand: vi.fn(), @@ -187,6 +187,39 @@ describe('PromptInput command submission', () => { useUIState.getState().clearPendingPromptFile() }) + afterEach(() => { + Reflect.deleteProperty(window, 'matchMedia') + }) + + it('focuses the prompt on open with a fine pointer', async () => { + stubMatchMedia(true) + renderComponent() + + const input = await screen.findByPlaceholderText('Send a message...') + expect(input).toHaveFocus() + }) + + it('does not focus the prompt on open on touch devices', async () => { + stubMatchMedia(false) + renderComponent() + + const input = await screen.findByPlaceholderText('Send a message...') + expect(input).not.toHaveFocus() + }) + + it('does not take focus from an element that already has it', async () => { + stubMatchMedia(true) + const other = document.createElement('button') + document.body.appendChild(other) + other.focus() + + renderComponent() + + await screen.findByPlaceholderText('Send a message...') + expect(other).toHaveFocus() + other.remove() + }) + it('sends parsed command attachments and offsets to runCommand without injecting the selected agent', async () => { const { container } = renderComponent() @@ -226,6 +259,53 @@ describe('PromptInput command submission', () => { expect(call.skills).toEqual([]) }) + it('runs a command whose arguments span multiple lines', async () => { + renderComponent() + + const input = await screen.findByPlaceholderText('Send a message...') + fireEvent.change(input, { target: { value: '/review first line\nsecond line' } }) + fireEvent.click(screen.getByTitle('Send')) + + await waitFor(() => expect(mocks.runCommand).toHaveBeenCalled()) + expect(mocks.runCommand.mock.calls[0][0]).toMatchObject({ + name: 'review', + text: 'first line\nsecond line', + }) + }) + + it('submits on plain Enter when the prompt is a known command', async () => { + renderComponent() + + const input = await screen.findByPlaceholderText('Send a message...') + fireEvent.change(input, { target: { value: '/btw first line\nsecond line' } }) + fireEvent.keyDown(input, { key: 'Enter' }) + + await waitFor(() => expect(defaultProps.commandActions.askSideQuestion).toHaveBeenCalledWith('first line\nsecond line')) + }) + + it('does not submit a known command on Shift+Enter', async () => { + renderComponent() + + const input = await screen.findByPlaceholderText('Send a message...') + fireEvent.change(input, { target: { value: '/btw hi' } }) + fireEvent.keyDown(input, { key: 'Enter', shiftKey: true }) + + expect(defaultProps.commandActions.askSideQuestion).not.toHaveBeenCalled() + expect(input).toHaveValue('/btw hi') + }) + + it.each(['hello there', '/bt hi', '/unknown hi'])('does not submit %j on plain Enter', async (value) => { + renderComponent() + + const input = await screen.findByPlaceholderText('Send a message...') + fireEvent.change(input, { target: { value } }) + fireEvent.keyDown(input, { key: 'Enter' }) + + expect(input).toHaveValue(value) + expect(mocks.runCommand).not.toHaveBeenCalled() + expect(defaultProps.commandActions.askSideQuestion).not.toHaveBeenCalled() + }) + it('preserves the submitted command text when the command submission fails', async () => { mocks.runCommand.mockRejectedValueOnce(new Error('command failed')) renderComponent() diff --git a/frontend/src/components/message/PromptInput.tsx b/frontend/src/components/message/PromptInput.tsx index 1e0d1d97..c43720b3 100644 --- a/frontend/src/components/message/PromptInput.tsx +++ b/frontend/src/components/message/PromptInput.tsx @@ -14,6 +14,7 @@ import { useSessionAgentStore } from '@/stores/sessionAgentStore' import { useUIState } from '@/stores/uiStateStore' import { useSendErrorStore } from '@/stores/sendErrorStore' import { useMobile } from '@/hooks/useMobile' +import { FINE_POINTER_MEDIA_QUERY, useMediaQuery } from '@/hooks/useMediaQuery' import { usePermissions } from '@/contexts/EventContext' import { ArrowDown, Upload, X, Mic, MicOff } from 'lucide-react' @@ -40,6 +41,11 @@ import { isBuiltinCommand, type CommandActions, type PageCommandActions } from ' const ACCEPTED_IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp", "image/heic", "image/heif"] +function parseCommandPrompt(value: string): { name: string; args?: string } | null { + const match = value.match(/^\/([a-zA-Z0-9_-]+)(?:\s+([\s\S]*))?$/) + return match ? { name: match[1], args: match[2] } : null +} + const revokeBlobUrls = (attachments: ImageAttachment[]) => { attachments.forEach((attachment) => { @@ -133,6 +139,14 @@ export const PromptInput = memo(forwardRef( const textareaRef = useRef(null) const fileInputRef = useRef(null) const voiceButtonContainerRef = useRef(null) + const hasFinePointer = useMediaQuery(FINE_POINTER_MEDIA_QUERY) + + useEffect(() => { + if (!hasFinePointer) return + const activeElement = document.activeElement + if (activeElement && activeElement !== document.body) return + textareaRef.current?.focus() + }, [hasFinePointer, sessionID]) const resetVoiceGestureState = useCallback(() => { voiceGestureStartYRef.current = null @@ -215,6 +229,12 @@ export const PromptInput = memo(forwardRef( const isPromptSubmitPending = sendPrompt.isPending || sendShell.isPending const interruptSession = useInterruptSession() const { filterCommands } = useCommands({ directory }) + const isExactCommandPrompt = (value: string) => { + const commandPrompt = parseCommandPrompt(value) + if (!commandPrompt) return false + const [command] = filterCommands(commandPrompt.name) + return command?.name.toLowerCase() === commandPrompt.name.toLowerCase() + } const { files: searchResults } = useFileSearch( mentionQuery, @@ -330,13 +350,12 @@ export const PromptInput = memo(forwardRef( - const commandMatch = prompt.match(/^\/([a-zA-Z0-9_-]+)(?:\s+(.*))?$/) - if (commandMatch) { - const [, commandName, commandArgs] = commandMatch - const command = filterCommands(commandName)[0] + const commandPrompt = parseCommandPrompt(prompt) + if (commandPrompt) { + const command = filterCommands(commandPrompt.name)[0] if (command) { - const parsed = parsePromptToInput(commandArgs?.trim() || '', attachedFiles, agentNames, imageAttachments) + const parsed = parsePromptToInput(commandPrompt.args?.trim() || '', attachedFiles, agentNames, imageAttachments) const submittedPrompt = prompt const submittedAttachedFiles = attachedFiles const submittedImageAttachments = imageAttachments @@ -961,7 +980,7 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read) } } - if (e.key === 'Enter' && (e.metaKey || e.ctrlKey || (isMobile && !e.shiftKey))) { + if (e.key === 'Enter' && !e.nativeEvent.isComposing && (e.metaKey || e.ctrlKey || (!e.shiftKey && (isMobile || isExactCommandPrompt(prompt))))) { e.preventDefault() if (isMobile) { textareaRef.current?.blur() diff --git a/frontend/src/components/session/SessionMessagePickerDialog.test.tsx b/frontend/src/components/session/SessionMessagePickerDialog.test.tsx index f70e93c9..aaba2f8e 100644 --- a/frontend/src/components/session/SessionMessagePickerDialog.test.tsx +++ b/frontend/src/components/session/SessionMessagePickerDialog.test.tsx @@ -1,5 +1,7 @@ -import { describe, it, expect, vi } from 'vitest' -import { render, screen, fireEvent } from '@testing-library/react' +import { describe, it, expect, vi, afterEach } from 'vitest' +import { stubMatchMedia } from '@/test/test-utils' +import { useState } from 'react' +import { render, screen, fireEvent, waitFor } from '@testing-library/react' import type { SessionMessageInfo } from '@opencode-manager/shared/opencode' import { SessionMessagePickerDialog } from './SessionMessagePickerDialog' @@ -25,7 +27,48 @@ const renderPicker = ( />, ) +function FocusHost() { + const [open, setOpen] = useState(false) + return ( + <> +