class ChatComponent extends HTMLElement {
constructor() {
super();
this.messages = [];
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
this.setupEventListeners();
}
render() {
this.shadowRoot.innerHTML = `
`;
// Initialize feather icons
const featherScript = document.createElement('script');
featherScript.src = 'https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js';
this.shadowRoot.appendChild(featherScript);
featherScript.onload = () => {
feather.replace();
};
}
setupEventListeners() {
const chatInput = this.shadowRoot.getElementById('chat-input');
const sendButton = this.shadowRoot.getElementById('send-button');
const messagesContainer = this.shadowRoot.getElementById('messages-container');
// Auto-resize textarea
chatInput.addEventListener('input', function() {
this.style.height = 'auto';
this.style.height = (this.scrollHeight) + 'px';
});
// Send message on Enter (but allow Shift+Enter for new lines)
chatInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
this.sendMessage();
}
});
// Send message on button click
sendButton.addEventListener('click', () => this.sendMessage());
// Initial greeting from Rosalinda
this.addMessage({
user: 'Rosalinda',
type: 'assistant',
text: 'Bonjour ! Je suis Rosalinda, votre assistante IA pour le développement web. Comment puis-je vous aider aujourd\'hui ? Je peux générer du code HTML, CSS, JavaScript ou React pour vous.',
time: new Date().toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' })
});
}
addMessage(message) {
this.messages.push(message);
this.renderMessages();
// Scroll to bottom
const messagesContainer = this.shadowRoot.getElementById('messages-container');
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
renderMessages() {
const messagesContainer = this.shadowRoot.getElementById('messages-container');
messagesContainer.innerHTML = '';
this.messages.forEach(msg => {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${msg.type}`;
// Check if message contains code
let messageText = msg.text;
const codeMatch = msg.text.match(/