Skip to content

Commit 3ab311f

Browse files
committed
Fix Enter to send in chat
1 parent b3b1dd7 commit 3ab311f

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

app/views/rails_mcp_engine/chat/show.html.erb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,33 @@ document.addEventListener('DOMContentLoaded', () => {
7979
});
8080
}
8181

82-
// Send on Enter (Shift+Enter for new line)
83-
document.getElementById('chat-input').addEventListener('keydown', (e) => {
82+
function setupChatInput() {
83+
const chatInput = document.getElementById('chat-input');
84+
if (!chatInput) return;
85+
86+
// Remove existing listener to prevent duplicates if any (though unlikely with this pattern)
87+
// We can't easily remove anonymous functions, but we can ensure we don't attach multiple times
88+
// by checking a data attribute or just relying on the fact that this runs on page load.
89+
// For simplicity in this context, we'll just attach.
90+
91+
chatInput.removeEventListener('keydown', handleChatInputKeydown);
92+
chatInput.addEventListener('keydown', handleChatInputKeydown);
93+
}
94+
95+
function handleChatInputKeydown(e) {
8496
if (e.key === 'Enter' && !e.shiftKey) {
97+
if (e.isComposing) return; // Ignore Enter during IME composition
8598
e.preventDefault();
8699
sendMessage();
87100
}
88-
});
89-
});
101+
}
102+
103+
// Initial setup
104+
setupChatInput();
105+
106+
// Re-setup on Turbo load if available, or just DOMContentLoaded
107+
document.addEventListener('turbo:load', setupChatInput);
108+
document.addEventListener('DOMContentLoaded', setupChatInput);
90109

91110
function sendMessage() {
92111
const input = document.getElementById('chat-input');

0 commit comments

Comments
 (0)