File size: 2,369 Bytes
bbb6398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * API密钥管理器 - 模态框控制模块

 * 负责统一管理模态框的显示和交互

 */

// 打开添加密钥模态框
function openAddKeyModal() {
    if (!window.apiKeyManagerInstance) return;
    window.apiKeyManagerInstance.showAddModal = true;
    
    // 聚焦到平台选择下拉框
    setTimeout(() => {
        document.getElementById('platform')?.focus();
    }, 100);
}

// 打开编辑密钥模态框
function openEditKeyModal(id, name, key, platform) {
    if (!window.apiKeyManagerInstance) return;
    window.apiKeyManagerInstance.editApiKey(id, name, key, platform);
}

// 打开删除确认模态框
function openDeleteConfirmModal(id, name) {
    if (!window.apiKeyManagerInstance) return;
    window.apiKeyManagerInstance.deleteApiKey(id, name);
}

// 关闭所有模态框
function closeAllModals() {
    if (!window.apiKeyManagerInstance) return;
    
    window.apiKeyManagerInstance.showAddModal = false;
    window.apiKeyManagerInstance.showEditModal = false;
    window.apiKeyManagerInstance.showDeleteConfirm = false;
}

// 处理键盘快捷键
function setupModalKeyboardShortcuts() {
    document.addEventListener('keydown', (e) => {
        // ESC键关闭所有模态框
        if (e.key === 'Escape') {
            closeAllModals();
        }
        
        // Alt+N 打开添加模态框
        if (e.altKey && e.key === 'n') {
            openAddKeyModal();
        }
    });
}

// 初始化模态框设置
function initModals() {
    // 存储对API密钥管理器实例的引用,方便全局访问
    window.addEventListener('alpine:initialized', () => {
        const apiKeyManagerEl = document.querySelector('[x-data="apiKeyManager()"]');
        if (apiKeyManagerEl) {
            window.apiKeyManagerInstance = apiKeyManagerEl.__x.getUnobservedData();
        }
    });
    
    // 设置键盘快捷键
    setupModalKeyboardShortcuts();
    
    // 监听打开添加模态框事件
    window.addEventListener('open-add-modal', openAddKeyModal);
}

// 暴露给外部使用的方法
window.apiKeyModals = {
    openAddKeyModal,
    openEditKeyModal,
    openDeleteConfirmModal,
    closeAllModals,
    initModals
};

// 注册初始化函数
document.addEventListener('DOMContentLoaded', () => {
    initModals();
});