/** * 提示词管理组件 */ class PromptManager { constructor() { this.prompts = []; this.currentPromptId = null; this.initElements(); this.bindEvents(); } /** * 初始化DOM元素 */ initElements() { this.promptInput = document.getElementById('promptInput'); this.promptSelect = document.getElementById('promptSelect'); this.promptName = document.getElementById('promptName'); this.savePromptBtn = document.getElementById('savePrompt'); this.deletePromptBtn = document.getElementById('deletePrompt'); } /** * 绑定事件 */ bindEvents() { // 保存提示词 this.savePromptBtn.addEventListener('click', () => { this.savePrompt(); }); // 删除提示词 this.deletePromptBtn.addEventListener('click', () => { this.deletePrompt(); }); // 选择提示词 this.promptSelect.addEventListener('change', (e) => { this.selectPrompt(e.target.value); }); // 提示词输入变化 this.promptInput.addEventListener('input', () => { this.updateDeleteButtonState(); }); // 提示词名称输入 this.promptName.addEventListener('input', () => { this.updateSaveButtonState(); }); } /** * 加载所有提示词 */ async loadPrompts() { try { const response = await window.promptApi.getPrompts(); if (response.success) { this.prompts = response.prompts; this.updatePromptSelect(); } } catch (error) { console.error('加载提示词失败:', error); this.showToast('加载提示词失败: ' + error.message, 'error'); } } /** * 更新提示词下拉框 */ updatePromptSelect() { // 清空现有选项 this.promptSelect.innerHTML = ''; // 添加提示词选项 this.prompts.forEach(prompt => { const option = document.createElement('option'); option.value = prompt.id; option.textContent = prompt.name; this.promptSelect.appendChild(option); }); this.updateDeleteButtonState(); } /** * 选择提示词 */ selectPrompt(promptId) { if (!promptId) { this.currentPromptId = null; this.promptName.value = ''; this.updateDeleteButtonState(); return; } const prompt = this.prompts.find(p => p.id == promptId); if (prompt) { this.currentPromptId = prompt.id; this.promptInput.value = prompt.content; this.promptName.value = prompt.name; this.updateDeleteButtonState(); this.updateSaveButtonState(); } } /** * 保存提示词 */ async savePrompt() { const name = this.promptName.value.trim(); const content = this.promptInput.value.trim(); if (!name) { this.showToast('请输入提示词名称', 'warning'); return; } if (!content) { this.showToast('请输入提示词内容', 'warning'); return; } try { const response = await window.promptApi.savePrompt(name, content); if (response.success) { this.showToast('提示词保存成功', 'success'); // 重新加载提示词列表 await this.loadPrompts(); // 选中新保存的提示词 this.promptSelect.value = response.prompt_id; this.currentPromptId = response.prompt_id; this.updateDeleteButtonState(); } } catch (error) { console.error('保存提示词失败:', error); this.showToast('保存提示词失败: ' + error.message, 'error'); } } /** * 删除提示词 */ async deletePrompt() { if (!this.currentPromptId) { this.showToast('请先选择要删除的提示词', 'warning'); return; } const prompt = this.prompts.find(p => p.id == this.currentPromptId); if (!prompt) { this.showToast('提示词不存在', 'error'); return; } if (!confirm(`确定要删除提示词"${prompt.name}"吗?`)) { return; } try { const response = await window.promptApi.deletePrompt(this.currentPromptId); if (response.success) { this.showToast('提示词删除成功', 'success'); // 清空当前选择 this.currentPromptId = null; this.promptSelect.value = ''; this.promptName.value = ''; // 重新加载提示词列表 await this.loadPrompts(); } } catch (error) { console.error('删除提示词失败:', error); this.showToast('删除提示词失败: ' + error.message, 'error'); } } /** * 更新保存按钮状态 */ updateSaveButtonState() { const hasName = this.promptName.value.trim().length > 0; const hasContent = this.promptInput.value.trim().length > 0; this.savePromptBtn.disabled = !hasName || !hasContent; } /** * 更新删除按钮状态 */ updateDeleteButtonState() { this.deletePromptBtn.disabled = !this.currentPromptId; } /** * 获取当前提示词内容 */ getCurrentPrompt() { return this.promptInput.value.trim(); } /** * 设置提示词内容 */ setPrompt(content) { this.promptInput.value = content; this.updateSaveButtonState(); } /** * 清空提示词 */ clearPrompt() { this.promptInput.value = ''; this.promptName.value = ''; this.promptSelect.value = ''; this.currentPromptId = null; this.updateSaveButtonState(); this.updateDeleteButtonState(); } /** * 显示消息提示 */ showToast(message, type = 'success') { const toast = document.createElement('div'); toast.className = `toast ${type}`; toast.textContent = message; const container = document.getElementById('toastContainer'); container.appendChild(toast); // 3秒后自动移除 setTimeout(() => { if (toast.parentNode) { toast.parentNode.removeChild(toast); } }, 3000); } } // 导出到全局 window.PromptManager = PromptManager;