Spaces:
Running
Running
File size: 5,297 Bytes
744a895 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
class CustomChat extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
.chat-container {
background: white;
border-radius: 1rem;
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
height: 500px;
display: flex;
flex-direction: column;
}
.chat-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 1rem;
border-top-left-radius: 1rem;
border-top-right-radius: 1rem;
}
.chat-messages {
flex-grow: 1;
padding: 1rem;
overflow-y: auto;
}
.message {
margin-bottom: 1rem;
max-width: 80%;
}
.user-message {
background: #3b82f6;
color: white;
border-radius: 1rem 1rem 0 1rem;
padding: 0.5rem 1rem;
margin-left: auto;
}
.bot-message {
background: #e5e7eb;
color: #111827;
border-radius: 1rem 1rem 1rem 0;
padding: 0.5rem 1rem;
margin-right: auto;
}
.chat-input-container {
display: flex;
padding: 1rem;
border-top: 1px solid #e5e7eb;
}
.chat-input {
flex-grow: 1;
padding: 0.5rem 1rem;
border: 1px solid #e5e7eb;
border-radius: 0.5rem;
outline: none;
}
.chat-input:focus {
border-color: #3b82f6;
}
.send-button {
background: #3b82f6;
color: white;
border: none;
border-radius: 0.5rem;
padding: 0.5rem 1rem;
margin-left: 0.5rem;
cursor: pointer;
}
.send-button:hover {
background: #2563eb;
}
@media (max-width: 768px) {
.chat-container {
height: 400px;
}
}
</style>
<div class="chat-container">
<div class="chat-header">
<h2 class="font-bold">Chat with your Avatar</h2>
</div>
<div class="chat-messages" id="chat-messages">
<div class="message bot-message">
Hello! I'm your AI Avatar. What would you like me to do?
</div>
</div>
<div class="chat-input-container">
<input type="text" class="chat-input" id="user-input" placeholder="Type a command...">
<button class="send-button" id="send-button">
<i data-feather="send"></i>
</button>
</div>
</div>
`;
// Add event listeners after rendering
setTimeout(() => {
const sendButton = this.shadowRoot.getElementById('send-button');
const userInput = this.shadowRoot.getElementById('user-input');
const chatMessages = this.shadowRoot.getElementById('chat-messages');
const handleSend = () => {
const message = userInput.value.trim();
if (message) {
// Add user message
const userMsg = document.createElement('div');
userMsg.className = 'message user-message';
userMsg.textContent = message;
chatMessages.appendChild(userMsg);
// Get bot response
const response = window.avatarCommander.handleCommand(message);
// Add bot response after a short delay
setTimeout(() => {
const botMsg = document.createElement('div');
botMsg.className = 'message bot-message';
botMsg.textContent = response;
chatMessages.appendChild(botMsg);
chatMessages.scrollTop = chatMessages.scrollHeight;
}, 500);
userInput.value = '';
chatMessages.scrollTop = chatMessages.scrollHeight;
}
};
sendButton.addEventListener('click', handleSend);
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
handleSend();
}
});
}, 0);
}
}
customElements.define('custom-chat', CustomChat); |