|
46 | 46 | let conversationHistory = []; |
47 | 47 |
|
48 | 48 | // Load conversation history from localStorage on page load |
49 | | -document.addEventListener('DOMContentLoaded', () => { |
| 49 | +function initializeChat() { |
50 | 50 | // Restore conversation history |
51 | 51 | const savedHistory = localStorage.getItem('chat_conversation_history'); |
52 | 52 | if (savedHistory) { |
@@ -78,15 +78,35 @@ document.addEventListener('DOMContentLoaded', () => { |
78 | 78 | localStorage.setItem('chat_selected_model', e.target.value); |
79 | 79 | }); |
80 | 80 | } |
| 81 | +} |
| 82 | + |
| 83 | +document.addEventListener('DOMContentLoaded', initializeChat); |
| 84 | + |
| 85 | + function setupChatInput() { |
| 86 | + const chatInput = document.getElementById('chat-input'); |
| 87 | + if (!chatInput) return; |
| 88 | + |
| 89 | + // Idempotent attachment |
| 90 | + if (chatInput.dataset.listenerAttached === 'true') return; |
| 91 | + |
| 92 | + chatInput.addEventListener('keydown', handleChatInputKeydown); |
| 93 | + chatInput.dataset.listenerAttached = 'true'; |
| 94 | + } |
81 | 95 |
|
82 | | - // Send on Enter (Shift+Enter for new line) |
83 | | - document.getElementById('chat-input').addEventListener('keydown', (e) => { |
| 96 | + function handleChatInputKeydown(e) { |
84 | 97 | if (e.key === 'Enter' && !e.shiftKey) { |
| 98 | + if (e.isComposing) return; |
85 | 99 | e.preventDefault(); |
86 | 100 | sendMessage(); |
87 | 101 | } |
88 | | - }); |
89 | | -}); |
| 102 | + } |
| 103 | + |
| 104 | + // Initialize |
| 105 | + if (document.readyState === 'loading') { |
| 106 | + document.addEventListener('DOMContentLoaded', setupChatInput); |
| 107 | + } else { |
| 108 | + setupChatInput(); |
| 109 | + } |
90 | 110 |
|
91 | 111 | function sendMessage() { |
92 | 112 | const input = document.getElementById('chat-input'); |
|
0 commit comments