File size: 15,610 Bytes
1de5225 | 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | // chatInterface.js - 贝拉的聊天界面组件
// 这个模块负责创建和管理优雅的聊天界面,体现贝拉的温暖个性
class ChatInterface {
constructor() {
this.isVisible = false;
this.messages = [];
this.maxMessages = 50; // 最多显示50条消息
this.chatContainer = null;
this.messageContainer = null;
this.inputContainer = null;
this.messageInput = null;
this.sendButton = null;
this.toggleButton = null;
this.settingsPanel = null;
this.isSettingsVisible = false;
this.init();
}
// 初始化聊天界面
init() {
this.createChatContainer();
this.createToggleButton();
this.createSettingsPanel();
this.bindEvents();
this.addWelcomeMessage();
}
// 创建聊天容器
createChatContainer() {
// 主聊天容器
this.chatContainer = document.createElement('div');
this.chatContainer.className = 'bella-chat-container';
this.chatContainer.innerHTML = `
<div class="bella-chat-header">
<div class="bella-chat-title">
<div class="bella-avatar">💝</div>
<div class="bella-title-text">
<h3>贝拉</h3>
<span class="bella-status">在线</span>
</div>
</div>
<div class="bella-chat-controls">
<button class="bella-settings-btn" title="设置">
<i class="fas fa-cog"></i>
</button>
<button class="bella-minimize-btn" title="最小化">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="bella-chat-messages"></div>
<div class="bella-chat-input-container">
<div class="bella-input-wrapper">
<input type="text" class="bella-message-input" placeholder="和贝拉聊聊天..." maxlength="500">
<button class="bella-send-btn" title="发送">
<i class="fas fa-paper-plane"></i>
</button>
</div>
<div class="bella-input-hint">
按 Enter 发送,Shift + Enter 换行
</div>
</div>
`;
// 获取关键元素引用
this.messageContainer = this.chatContainer.querySelector('.bella-chat-messages');
this.inputContainer = this.chatContainer.querySelector('.bella-chat-input-container');
this.messageInput = this.chatContainer.querySelector('.bella-message-input');
this.sendButton = this.chatContainer.querySelector('.bella-send-btn');
document.body.appendChild(this.chatContainer);
}
// 创建切换按钮
createToggleButton() {
this.toggleButton = document.createElement('button');
this.toggleButton.className = 'bella-chat-toggle';
this.toggleButton.innerHTML = `
<div class="bella-toggle-icon">
<i class="fas fa-comments"></i>
</div>
<div class="bella-toggle-text">与贝拉聊天</div>
`;
this.toggleButton.title = '打开聊天窗口';
document.body.appendChild(this.toggleButton);
}
// 创建设置面板
createSettingsPanel() {
this.settingsPanel = document.createElement('div');
this.settingsPanel.className = 'bella-settings-panel';
this.settingsPanel.innerHTML = `
<div class="bella-settings-header">
<h4>聊天设置</h4>
<button class="bella-settings-close">
<i class="fas fa-times"></i>
</button>
</div>
<div class="bella-settings-content">
<div class="bella-setting-group">
<label>AI服务提供商</label>
<select class="bella-provider-select">
<option value="local">本地模型</option>
<option value="openai">OpenAI GPT</option>
<option value="qwen">通义千问</option>
<option value="ernie">文心一言</option>
<option value="glm">智谱AI</option>
</select>
</div>
<div class="bella-setting-group bella-api-key-group" style="display: none;">
<label>API密钥</label>
<input type="password" class="bella-api-key-input" placeholder="请输入API密钥">
<button class="bella-api-key-save">保存</button>
</div>
<div class="bella-setting-group">
<label>聊天模式</label>
<select class="bella-mode-select">
<option value="casual">轻松聊天</option>
<option value="assistant">智能助手</option>
<option value="creative">创意伙伴</option>
</select>
</div>
<div class="bella-setting-group">
<button class="bella-clear-history">清除聊天记录</button>
</div>
</div>
`;
document.body.appendChild(this.settingsPanel);
}
// 绑定事件
bindEvents() {
// 切换聊天窗口
this.toggleButton.addEventListener('click', () => {
this.toggle();
});
// 最小化按钮
this.chatContainer.querySelector('.bella-minimize-btn').addEventListener('click', () => {
this.hide();
});
// 设置按钮
this.chatContainer.querySelector('.bella-settings-btn').addEventListener('click', () => {
this.toggleSettings();
});
// 发送消息
this.sendButton.addEventListener('click', () => {
this.sendMessage();
});
// 输入框事件
this.messageInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
this.sendMessage();
}
});
// 输入框自动调整高度
this.messageInput.addEventListener('input', () => {
this.adjustInputHeight();
});
// 设置面板事件
this.bindSettingsEvents();
}
// 绑定设置面板事件
bindSettingsEvents() {
// 关闭设置面板
this.settingsPanel.querySelector('.bella-settings-close').addEventListener('click', () => {
this.hideSettings();
});
// 提供商选择
const providerSelect = this.settingsPanel.querySelector('.bella-provider-select');
const apiKeyGroup = this.settingsPanel.querySelector('.bella-api-key-group');
providerSelect.addEventListener('change', (e) => {
const provider = e.target.value;
if (provider === 'local') {
apiKeyGroup.style.display = 'none';
} else {
apiKeyGroup.style.display = 'block';
}
// 触发提供商切换事件
this.onProviderChange?.(provider);
});
// API密钥保存
this.settingsPanel.querySelector('.bella-api-key-save').addEventListener('click', () => {
const provider = providerSelect.value;
const apiKey = this.settingsPanel.querySelector('.bella-api-key-input').value;
if (apiKey.trim()) {
this.onAPIKeySave?.(provider, apiKey.trim());
this.showNotification('API密钥已保存', 'success');
}
});
// 清除聊天记录
this.settingsPanel.querySelector('.bella-clear-history').addEventListener('click', () => {
this.clearMessages();
this.onClearHistory?.();
this.hideSettings();
});
}
// 添加欢迎消息
addWelcomeMessage() {
this.addMessage('assistant', '你好!我是贝拉,你的AI伙伴。很高兴见到你!有什么想聊的吗?', true);
}
// 切换聊天窗口显示/隐藏
toggle() {
console.log('ChatInterface.toggle() 被调用');
console.log('切换前 isVisible:', this.isVisible);
if (this.isVisible) {
this.hide();
} else {
this.show();
}
console.log('切换后 isVisible:', this.isVisible);
}
// 显示聊天窗口
show() {
console.log('ChatInterface.show() 被调用');
console.log('显示前 isVisible:', this.isVisible);
console.log('显示前 chatContainer.className:', this.chatContainer.className);
this.isVisible = true;
this.chatContainer.classList.add('visible');
console.log('显示后 isVisible:', this.isVisible);
console.log('显示后 chatContainer.className:', this.chatContainer.className);
console.log('chatContainer 计算样式 opacity:', window.getComputedStyle(this.chatContainer).opacity);
console.log('chatContainer 计算样式 transform:', window.getComputedStyle(this.chatContainer).transform);
this.toggleButton.classList.add('active');
this.messageInput.focus();
this.scrollToBottom();
}
// 隐藏聊天窗口
hide() {
this.isVisible = false;
this.chatContainer.classList.remove('visible');
this.toggleButton.classList.remove('active');
this.hideSettings();
}
// 切换设置面板
toggleSettings() {
if (this.isSettingsVisible) {
this.hideSettings();
} else {
this.showSettings();
}
}
// 显示设置面板
showSettings() {
this.isSettingsVisible = true;
this.settingsPanel.classList.add('visible');
}
// 隐藏设置面板
hideSettings() {
this.isSettingsVisible = false;
this.settingsPanel.classList.remove('visible');
}
// 发送消息
sendMessage() {
const text = this.messageInput.value.trim();
if (!text) return;
// 添加用户消息
this.addMessage('user', text);
// 清空输入框
this.messageInput.value = '';
this.adjustInputHeight();
// 触发消息发送事件
this.onMessageSend?.(text);
}
// 添加消息到聊天界面
addMessage(role, content, isWelcome = false) {
const messageElement = document.createElement('div');
messageElement.className = `bella-message bella-message-${role}`;
if (isWelcome) {
messageElement.classList.add('bella-welcome-message');
}
const timestamp = new Date().toLocaleTimeString('zh-CN', {
hour: '2-digit',
minute: '2-digit'
});
messageElement.innerHTML = `
<div class="bella-message-avatar">
${role === 'user' ? '👤' : '💝'}
</div>
<div class="bella-message-content">
<div class="bella-message-text">${this.formatMessage(content)}</div>
<div class="bella-message-time">${timestamp}</div>
</div>
`;
this.messageContainer.appendChild(messageElement);
this.messages.push({ role, content, timestamp: Date.now() });
// 限制消息数量
if (this.messages.length > this.maxMessages) {
const oldMessage = this.messageContainer.firstChild;
if (oldMessage) {
this.messageContainer.removeChild(oldMessage);
}
this.messages.shift();
}
// 滚动到底部
this.scrollToBottom();
// 添加动画效果
setTimeout(() => {
messageElement.classList.add('bella-message-appear');
}, 10);
}
// 格式化消息内容
formatMessage(content) {
// 简单的文本格式化,支持换行
return content
.replace(/\n/g, '<br>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.*?)\*/g, '<em>$1</em>');
}
// 显示打字指示器
showTypingIndicator() {
const existingIndicator = this.messageContainer.querySelector('.bella-typing-indicator');
if (existingIndicator) return;
const typingElement = document.createElement('div');
typingElement.className = 'bella-typing-indicator';
typingElement.innerHTML = `
<div class="bella-message-avatar">💝</div>
<div class="bella-message-content">
<div class="bella-typing-dots">
<span class="bella-typing-dot"></span>
<span class="bella-typing-dot"></span>
<span class="bella-typing-dot"></span>
</div>
</div>
`;
this.messageContainer.appendChild(typingElement);
this.scrollToBottom();
// 添加显示动画
setTimeout(() => {
typingElement.classList.add('bella-typing-show');
}, 10);
}
// 隐藏打字指示器
hideTypingIndicator() {
const indicator = this.messageContainer.querySelector('.bella-typing-indicator');
if (indicator) {
this.messageContainer.removeChild(indicator);
}
}
// 清除所有消息
clearMessages() {
this.messageContainer.innerHTML = '';
this.messages = [];
this.addWelcomeMessage();
}
// 滚动到底部
scrollToBottom() {
setTimeout(() => {
this.messageContainer.scrollTop = this.messageContainer.scrollHeight;
}, 10);
}
// 调整输入框高度
adjustInputHeight() {
this.messageInput.style.height = 'auto';
this.messageInput.style.height = Math.min(this.messageInput.scrollHeight, 120) + 'px';
}
// 显示通知
showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `bella-notification bella-notification-${type}`;
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.classList.add('bella-notification-show');
}, 10);
setTimeout(() => {
notification.classList.remove('bella-notification-show');
setTimeout(() => {
document.body.removeChild(notification);
}, 300);
}, 3000);
}
// 检查聊天窗口是否可见
getVisibility() {
return this.isVisible;
}
// 设置回调函数
onMessageSend = null;
onProviderChange = null;
onAPIKeySave = null;
onClearHistory = null;
}
// ES6模块导出
export { ChatInterface }; |