Skip to content

Commit fe682d8

Browse files
committed
fix: Shift+Enter now inserts newline when Ctrl+Enter to send is enabled
Fixes #10386 When enterBehavior is set to "newline" (Ctrl+Enter to send mode), Shift+Enter was incorrectly triggering onSend() due to event.shiftKey being included in the send condition. This commit removes event.shiftKey from the condition so that: - Enter: inserts a new line - Shift+Enter: inserts a new line (consistent with standard text editing) - Ctrl/Cmd+Enter: sends the message Updated the test to reflect the corrected behavior.
1 parent ea62173 commit fe682d8

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

webview-ui/src/components/chat/ChatTextArea.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,14 @@ export const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
495495
// Handle Enter key based on enterBehavior setting
496496
if (event.key === "Enter" && !isComposing) {
497497
if (enterBehavior === "newline") {
498-
// New behavior: Enter = newline, Shift+Enter or Ctrl+Enter = send
499-
if (event.shiftKey || event.ctrlKey || event.metaKey) {
498+
// New behavior: Enter = newline, Ctrl/Cmd+Enter = send
499+
// Shift+Enter also inserts newline (consistent with standard text editing)
500+
if (event.ctrlKey || event.metaKey) {
500501
event.preventDefault()
501502
resetHistoryNavigation()
502503
onSend()
503504
}
504-
// Otherwise, let Enter create newline (don't preventDefault)
505+
// Otherwise, let Enter/Shift+Enter create newline (don't preventDefault)
505506
} else {
506507
// Default behavior: Enter = send, Shift+Enter = newline
507508
if (!event.shiftKey) {

webview-ui/src/components/chat/__tests__/ChatTextArea.spec.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ describe("ChatTextArea", () => {
10811081
expect(shiftEnterEvent.defaultPrevented).toBe(false)
10821082
})
10831083

1084-
it("should treat Ctrl/Cmd/Shift+Enter as send and plain Enter as newline in newline mode", () => {
1084+
it("should treat Ctrl/Cmd+Enter as send and plain Enter/Shift+Enter as newline in newline mode", () => {
10851085
const onSend = vi.fn()
10861086

10871087
;(useExtensionState as ReturnType<typeof vi.fn>).mockReturnValue({
@@ -1096,11 +1096,13 @@ describe("ChatTextArea", () => {
10961096

10971097
const textarea = container.querySelector("textarea")!
10981098

1099+
// Plain Enter should NOT send (allows newline)
10991100
const plainEnterEvent = new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true })
11001101
fireEvent(textarea, plainEnterEvent)
11011102
expect(onSend).not.toHaveBeenCalled()
11021103
expect(plainEnterEvent.defaultPrevented).toBe(false)
11031104

1105+
// Ctrl+Enter SHOULD send
11041106
const ctrlEnterEvent = new KeyboardEvent("keydown", {
11051107
key: "Enter",
11061108
ctrlKey: true,
@@ -1111,15 +1113,27 @@ describe("ChatTextArea", () => {
11111113
expect(onSend).toHaveBeenCalledTimes(1)
11121114
expect(ctrlEnterEvent.defaultPrevented).toBe(true)
11131115

1116+
// Shift+Enter should NOT send (allows newline, consistent with standard text editing)
11141117
const shiftEnterEvent = new KeyboardEvent("keydown", {
11151118
key: "Enter",
11161119
shiftKey: true,
11171120
bubbles: true,
11181121
cancelable: true,
11191122
})
11201123
fireEvent(textarea, shiftEnterEvent)
1124+
expect(onSend).toHaveBeenCalledTimes(1) // Still 1, not incremented
1125+
expect(shiftEnterEvent.defaultPrevented).toBe(false) // Should not prevent default
1126+
1127+
// Cmd+Enter (metaKey) SHOULD send
1128+
const metaEnterEvent = new KeyboardEvent("keydown", {
1129+
key: "Enter",
1130+
metaKey: true,
1131+
bubbles: true,
1132+
cancelable: true,
1133+
})
1134+
fireEvent(textarea, metaEnterEvent)
11211135
expect(onSend).toHaveBeenCalledTimes(2)
1122-
expect(shiftEnterEvent.defaultPrevented).toBe(true)
1136+
expect(metaEnterEvent.defaultPrevented).toBe(true)
11231137
})
11241138
})
11251139
})

0 commit comments

Comments
 (0)