MediaWiki

MediaWiki:Card.js

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年10月23日 (四) 17:15的版本

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
(function() {
    'use strict';
    
    // 加载CSS
    mw.loader.load('/index.php?title=Mediawiki:Card.css&action=raw&ctype=text/css', 'text/css');
    
    // 状态管理
    const state = {
        currentFighter: '',
        fighters: [],
        cards: [],
        currentCard: null,
        defaultInfo: {
            order: '',
            ego: ''
        },
        editMode: 'base', // base, inspiration, god_inspiration
        currentGod: 'circen'
    };
    
    // 卡牌数据结构
    function createEmptyCard() {
        return {
            name: '',
            base: {
                displayname: '',
                art: '',
                group: '',
                rarity: '',
                god: '',
                ap: '',
                type: '',
                dict: '',
                desc_global: '',
                sub: '',
                isinspiration: 0,
                isgod_inspiration: 0
            },
            var: {
                inspiration: [],
                god_inspiration: {
                    circen: [],
                    diallos: [],
                    nihilum: [],
                    secred: [],
                    vitor: []
                }
            }
        };
    }
    
    // 创建元素辅助函数
    function createElement(tag, className, attributes = {}) {
        const el = document.createElement(tag);
        if (className) el.className = className;
        Object.entries(attributes).forEach(([key, value]) => {
            if (key === 'textContent') {
                el.textContent = value;
            } else if (key.startsWith('on')) {
                el.addEventListener(key.substring(2).toLowerCase(), value);
            } else {
                el.setAttribute(key, value);
            }
        });
        return el;
    }
    
    // 创建自定义下拉选择器
    function createSelect(options, selectedValue, onChange) {
        const select = createElement('div', 'form-select');
        select.textContent = selectedValue || options[0] || '请选择';
        select.setAttribute('tabindex', '0');
        
        const dropdown = createElement('div', 'dropdown-menu');
        dropdown.style.cssText = 'position:absolute;background:white;border:1px solid #ddd;border-radius:4px;max-height:200px;overflow-y:auto;display:none;z-index:1000;box-shadow:0 2px 8px rgba(0,0,0,0.15);';
        
        options.forEach(option => {
            const item = createElement('div', 'dropdown-item');
            item.textContent = option;
            item.style.cssText = 'padding:8px 12px;cursor:pointer;';
            item.addEventListener('mouseenter', () => item.style.background = '#f0f0f0');
            item.addEventListener('mouseleave', () => item.style.background = 'white');
            item.addEventListener('click', () => {
                select.textContent = option;
                dropdown.style.display = 'none';
                if (onChange) onChange(option);
            });
            dropdown.appendChild(item);
        });
        
        select.addEventListener('click', (e) => {
            e.stopPropagation();
            dropdown.style.display = dropdown.style.display === 'none' ? 'block' : 'none';
            const rect = select.getBoundingClientRect();
            dropdown.style.top = rect.bottom + 'px';
            dropdown.style.left = rect.left + 'px';
            dropdown.style.width = rect.width + 'px';
        });
        
        document.addEventListener('click', () => {
            dropdown.style.display = 'none';
        });
        
        const wrapper = createElement('div');
        wrapper.style.position = 'relative';
        wrapper.appendChild(select);
        document.body.appendChild(dropdown);
        
        return { wrapper, select, dropdown };
    }
    
    // 创建自定义输入框
    function createInput(type, value, onChange, placeholder = '') {
        const input = createElement('div', 'form-input');
        input.setAttribute('contenteditable', 'true');
        input.textContent = value || '';
        input.setAttribute('data-placeholder', placeholder);
        input.style.minHeight = type === 'textarea' ? '100px' : 'auto';
        
        if (!value) {
            input.style.color = '#999';
            input.textContent = placeholder;
        }
        
        input.addEventListener('focus', () => {
            if (input.textContent === placeholder) {
                input.textContent = '';
                input.style.color = '#333';
            }
        });
        
        input.addEventListener('blur', () => {
            if (!input.textContent.trim()) {
                input.textContent = placeholder;
                input.style.color = '#999';
            }
            if (onChange) onChange(input.textContent === placeholder ? '' : input.textContent);
        });
        
        input.addEventListener('input', () => {
            if (onChange && input.textContent !== placeholder) {
                onChange(input.textContent);
            }
        });
        
        return input;
    }
    
    // 创建复选框
    function createCheckbox(checked, onChange) {
        const wrapper = createElement('div', 'checkbox-wrapper');
        const checkbox = createElement('div', checked ? 'checkbox checked' : 'checkbox');
        const label = createElement('div', '');
        label.textContent = '是';
        
        wrapper.addEventListener('click', () => {
            const newChecked = !checkbox.classList.contains('checked');
            if (newChecked) {
                checkbox.classList.add('checked');
            } else {
                checkbox.classList.remove('checked');
            }
            if (onChange) onChange(newChecked ? 1 : 0);
        });
        
        wrapper.appendChild(checkbox);
        wrapper.appendChild(label);
        return wrapper;
    }
    
    // 创建表单组
    function createFormGroup(label, control) {
        const group = createElement('div', 'form-group');
        const labelEl = createElement('div', 'form-label');
        labelEl.textContent = label;
        group.appendChild(labelEl);
        group.appendChild(control);
        return group;
    }
    
    // 创建按钮
    function createButton(text, className, onClick) {
        const btn = createElement('div', 'btn ' + className);
        btn.textContent = text;
        btn.addEventListener('click', onClick);
        return btn;
    }
    
    // 文本插入辅助函数
    function insertTextAtCursor(element, text, wrap = false) {
        element.focus();
        const selection = window.getSelection();
        
        if (wrap && selection.rangeCount > 0) {
            const range = selection.getRangeAt(0);
            const selectedText = range.toString();
            
            if (selectedText) {
                range.deleteContents();
                const textNode = document.createTextNode(text.replace('选择文字', selectedText));
                range.insertNode(textNode);
                return;
            }
        }
        
        document.execCommand('insertText', false, text);
    }
    
    // 加载战斗员列表
    async function loadFighters() {
        try {
            const api = new mw.Api();
            const result = await api.get({
                action: 'query',
                list: 'categorymembers',
                cmtitle: 'Category:战斗员',
                cmlimit: 500
            });
            
            state.fighters = result.query.categorymembers.map(page => page.title);
            return state.fighters;
        } catch (error) {
            console.error('加载战斗员列表失败:', error);
            return [];
        }
    }
    
    // 加载战斗员卡牌数据
    async function loadFighterCards(fighter) {
        try {
            const api = new mw.Api();
            const result = await api.get({
                action: 'parse',
                page: '模块:卡牌/' + fighter,
                prop: 'wikitext'
            });
            
            // 这里需要解析Lua代码,实际实现中可能需要更复杂的解析
            // 简化处理:返回空数组,实际使用时需要完整的Lua解析器
            return [];
        } catch (error) {
            console.error('加载卡牌数据失败:', error);
            return [];
        }
    }
    
    // 生成Lua代码
    function generateLuaCode() {
        let code = 'local card = {}\n\n';
        
        // 生成 card.order
        if (state.defaultInfo.order) {
            code += `card.order = { "${state.defaultInfo.order}" }\n\n`;
        }
        
        // 生成 card.info
        if (state.defaultInfo.ego) {
            code += 'card.info = {\n';
            code += `    ego = "${state.defaultInfo.ego}",\n`;
            code += '}\n\n';
        }
        
        // 生成每张卡牌
        state.cards.forEach(card => {
            code += `card["${card.name}"] = {\n`;
            code += '    base = {\n';
            
            const base = card.base;
            if (base.displayname) code += `        displayname = "${base.displayname}",\n`;
            if (base.art) code += `        art = "${base.art}",\n`;
            if (base.group) code += `        group = "${base.group}",\n`;
            if (base.rarity) code += `        rarity = "${base.rarity}",\n`;
            if (base.god) code += `        god = "${base.god}",\n`;
            if (base.ap) code += `        ap = ${base.ap},\n`;
            if (base.type) code += `        type = "${base.type}",\n`;
            if (base.dict) code += `        dict = "${base.dict}",\n`;
            if (base.desc_global) code += `        desc_global = "${base.desc_global}",\n`;
            if (base.sub) code += `        sub = "${base.sub}",\n`;
            if (base.isinspiration) code += `        isinspiration = ${base.isinspiration},\n`;
            if (base.isgod_inspiration) code += `        isgod_inspiration = ${base.isgod_inspiration},\n`;
            
            code += '    },\n';
            
            // 生成 var 部分
            if (card.var && (card.var.inspiration.length > 0 || Object.values(card.var.god_inspiration).some(arr => arr.length > 0))) {
                code += '    var = {\n';
                
                // 灵光一闪
                if (card.var.inspiration.length > 0) {
                    code += '        inspiration = {\n';
                    card.var.inspiration.forEach(insp => {
                        code += '            {\n';
                        Object.entries(insp).forEach(([key, value]) => {
                            if (typeof value === 'number') {
                                code += `                ${key} = ${value},\n`;
                            } else {
                                code += `                ${key} = "${value}",\n`;
                            }
                        });
                        code += '            },\n';
                    });
                    code += '        },\n';
                }
                
                // 神光一闪
                const hasGodInspiration = Object.values(card.var.god_inspiration).some(arr => arr.length > 0);
                if (hasGodInspiration) {
                    code += '        god_inspiration = {\n';
                    Object.entries(card.var.god_inspiration).forEach(([god, inspList]) => {
                        if (inspList.length > 0) {
                            code += `            ${god} = {\n`;
                            inspList.forEach(insp => {
                                code += '                {\n';
                                Object.entries(insp).forEach(([key, value]) => {
                                    if (typeof value === 'number') {
                                        code += `                    ${key} = ${value},\n`;
                                    } else {
                                        code += `                    ${key} = "${value}",\n`;
                                    }
                                });
                                code += '                },\n';
                            });
                            code += '            },\n';
                        }
                    });
                    code += '        },\n';
                }
                
                code += '    },\n';
            }
            
            code += '}\n\n';
        });
        
        code += 'return card';
        
        return code;
    }
    
    // 渲染界面
    function render() {
        const container = document.getElementById('card-manager-container');
        if (!container) return;
        
        container.innerHTML = '';
        
        const manager = createElement('div', 'card-manager');
        
        // 左侧输入区
        const inputSection = createElement('div', 'card-input-section');
        
        // 战斗员选择
        const fighterGroup = createElement('div', 'form-group');
        const fighterLabel = createElement('div', 'form-label');
        fighterLabel.textContent = '当前战斗员:';
        fighterGroup.appendChild(fighterLabel);
        
        const fighterSelect = createSelect(state.fighters, state.currentFighter, (value) => {
            state.currentFighter = value;
            loadFighterCards(value).then(cards => {
                state.cards = cards;
                render();
            });
        });
        fighterGroup.appendChild(fighterSelect.wrapper);
        inputSection.appendChild(fighterGroup);
        
        // 默认信息区
        const defaultSection = createElement('div', 'default-info-section');
        const defaultTitle = createElement('div', 'section-title');
        defaultTitle.textContent = '默认信息';
        defaultSection.appendChild(defaultTitle);
        
        const orderInput = createInput('text', state.defaultInfo.order, (value) => {
            state.defaultInfo.order = value;
            render();
        }, '例:灭,灭,救,虚无残影');
        defaultSection.appendChild(createFormGroup('卡牌顺序(card.order):', orderInput));
        
        const egoInput = createInput('text', state.defaultInfo.ego, (value) => {
            state.defaultInfo.ego = value;
            render();
        }, '例:虚无');
        defaultSection.appendChild(createFormGroup('属性(ego):', egoInput));
        
        inputSection.appendChild(defaultSection);
        
        // 卡牌数据输入区
        if (state.currentCard) {
            const cardSection = createElement('div', 'card-data-section');
            const cardTitle = createElement('div', 'section-title');
            cardTitle.textContent = '卡牌数据';
            cardSection.appendChild(cardTitle);
            
            const card = state.currentCard;
            
            // 卡牌名称
            const nameInput = createInput('text', card.name, (value) => {
                card.name = value;
                render();
            }, '例:黑洞');
            cardSection.appendChild(createFormGroup('卡牌名称:', nameInput));
            
            // 显示名称
            const displaynameInput = createInput('text', card.base.displayname, (value) => {
                card.base.displayname = value;
                render();
            });
            cardSection.appendChild(createFormGroup('显示名称(displayname):', displaynameInput));
            
            // 图片
            const artInput = createInput('text', card.base.art, (value) => {
                card.base.art = value;
                render();
            }, '例:unique_1064_02.png');
            cardSection.appendChild(createFormGroup('图片(art):', artInput));
            
            // 卡组
            const groupSelect = createSelect(
                ['自我意识技能', '起始卡牌', '独特卡牌', '灵光一闪', '神光一闪'],
                card.base.group,
                (value) => {
                    card.base.group = value;
                    render();
                }
            );
            cardSection.appendChild(createFormGroup('卡组(group):', groupSelect.wrapper));
            
            // 稀有度
            const raritySelect = createSelect(
                ['白', '蓝', '橙', '彩'],
                card.base.rarity,
                (value) => {
                    card.base.rarity = value;
                    render();
                }
            );
            cardSection.appendChild(createFormGroup('稀有度(rarity):', raritySelect.wrapper));
            
            // 神明
            const godSelect = createSelect(
                ['circen', 'diallos', 'nihilum', 'secred', 'vitor'],
                card.base.god,
                (value) => {
                    card.base.god = value;
                    render();
                }
            );
            cardSection.appendChild(createFormGroup('神明:', godSelect.wrapper));
            
            // AP
            const apInput = createInput('text', card.base.ap, (value) => {
                card.base.ap = value;
                render();
            }, '整数/X/Ø');
            cardSection.appendChild(createFormGroup('AP:', apInput));
            
            // 卡牌类型
            const typeSelect = createSelect(
                ['攻击', '技能', '强化'],
                card.base.type,
                (value) => {
                    card.base.type = value;
                    render();
                }
            );
            cardSection.appendChild(createFormGroup('卡牌类型(type):', typeSelect.wrapper));
            
            // 机制
            const dictInput = createInput('text', card.base.dict, (value) => {
                card.base.dict = value;
                render();
            });
            cardSection.appendChild(createFormGroup('机制(dict):', dictInput));
            
            // 描述输入区按钮
            const descButtonGroup = createElement('div', 'button-group');
            descButtonGroup.appendChild(createButton('蓝色文本', 'btn-blue', () => {
                insertTextAtCursor(descInput, '{{文本|蓝|选择文字}}', true);
            }));
            descButtonGroup.appendChild(createButton('绿色文本', 'btn-green', () => {
                insertTextAtCursor(descInput, '{{文本|绿|选择文字}}', true);
            }));
            descButtonGroup.appendChild(createButton('绿色描边', 'btn-green', () => {
                insertTextAtCursor(descInput, '{{描边|绿|选择文字}}', true);
            }));
            descButtonGroup.appendChild(createButton('词典', 'btn', () => {
                insertTextAtCursor(descInput, '{{词典|选择文字}}', true);
            }));
            descButtonGroup.appendChild(createButton('换行', 'btn', () => {
                insertTextAtCursor(descInput, '<br>');
            }));
            
            cardSection.appendChild(createFormGroup('描述编辑工具:', descButtonGroup));
            
            // 描述
            const descInput = createInput('textarea', card.base.desc_global, (value) => {
                card.base.desc_global = value;
                render();
            }, '输入卡牌描述...');
            cardSection.appendChild(createFormGroup('描述(desc_global):', descInput));
            
            // 衍生卡牌
            const subInput = createInput('text', card.base.sub, (value) => {
                card.base.sub = value;
                render();
            });
            cardSection.appendChild(createFormGroup('衍生卡牌(sub):', subInput));
            
            // 是否存在灵光一闪
            const inspirationCheckbox = createCheckbox(card.base.isinspiration, (value) => {
                card.base.isinspiration = value;
                render();
            });
            cardSection.appendChild(createFormGroup('是否存在灵光一闪:', inspirationCheckbox));
            
            // 是否存在神光一闪
            const godInspirationCheckbox = createCheckbox(card.base.isgod_inspiration, (value) => {
                card.base.isgod_inspiration = value;
                render();
            });
            cardSection.appendChild(createFormGroup('是否存在神光一闪:', godInspirationCheckbox));
            
            inputSection.appendChild(cardSection);
        }
        
        // 添加新卡牌按钮
        const addCardBtn = createButton('+ 新增卡牌', 'btn-success', () => {
            const newCard = createEmptyCard();
            newCard.name = '新卡牌' + (state.cards.length + 1);
            state.cards.push(newCard);
            state.currentCard = newCard;
            render();
        });
        inputSection.appendChild(addCardBtn);
        
        manager.appendChild(inputSection);
        
        // 中间列表区
        const listSection = createElement('div', 'card-list-section');
        
        // 卡牌列表
        const cardListContainer = createElement('div', 'list-container');
        const cardListHeader = createElement('div', 'list-header');
        const cardListTitle = createElement('div', 'list-title');
        cardListTitle.textContent = '卡牌列表';
        cardListHeader.appendChild(cardListTitle);
        cardListContainer.appendChild(cardListHeader);
        
        if (state.cards.length === 0) {
            const emptyState = createElement('div', 'empty-state');
            const emptyIcon = createElement('div', 'empty-state-icon');
            emptyIcon.textContent = '📋';
            const emptyText = createElement('div', 'empty-state-text');
            emptyText.textContent = '暂无卡牌,点击"新增卡牌"开始创建';
            emptyState.appendChild(emptyIcon);
            emptyState.appendChild(emptyText);
            cardListContainer.appendChild(emptyState);
        } else {
            state.cards.forEach((card, index) => {
                const cardItem = createElement('div', 'card-item' + (state.currentCard === card ? ' active' : ''));
                
                const cardName = createElement('div', 'card-item-name');
                cardName.textContent = card.name || '未命名卡牌';
                
                const cardInfo = createElement('div', 'card-item-info');
                const infoText = [];
                if (card.base.type) infoText.push(card.base.type);
                if (card.base.ap) infoText.push('AP:' + card.base.ap);
                if (card.base.rarity) infoText.push(card.base.rarity);
                cardInfo.textContent = infoText.join(' | ') || '暂无信息';
                
                cardItem.appendChild(cardName);
                cardItem.appendChild(cardInfo);
                
                cardItem.addEventListener('click', () => {
                    state.currentCard = card;
                    state.editMode = 'base';
                    render();
                });
                
                // 删除按钮
                const deleteBtn = createButton('删除', 'btn-danger', (e) => {
                    e.stopPropagation();
                    if (confirm('确定要删除卡牌"' + card.name + '"吗?')) {
                        state.cards.splice(index, 1);
                        if (state.currentCard === card) {
                            state.currentCard = state.cards[0] || null;
                        }
                        render();
                    }
                });
                deleteBtn.style.cssText = 'margin-top:8px;width:100%;';
                cardItem.appendChild(deleteBtn);
                
                cardListContainer.appendChild(cardItem);
            });
        }
        
        listSection.appendChild(cardListContainer);
        
        // 灵光一闪列表
        if (state.currentCard && state.currentCard.base.isinspiration) {
            const inspirationSection = createElement('div', 'inspiration-section');
            const inspirationHeader = createElement('div', 'list-header');
            const inspirationTitle = createElement('div', 'list-title');
            inspirationTitle.textContent = '灵光一闪列表';
            inspirationHeader.appendChild(inspirationTitle);
            
            const addInspirationBtn = createButton('+ 添加', 'btn-primary', () => {
                state.currentCard.var.inspiration.push({
                    ap: '',
                    type: '',
                    desc_global: ''
                });
                render();
            });
            inspirationHeader.appendChild(addInspirationBtn);
            inspirationSection.appendChild(inspirationHeader);
            
            if (state.currentCard.var.inspiration.length === 0) {
                const emptyState = createElement('div', 'empty-state');
                emptyState.style.padding = '20px';
                const emptyText = createElement('div', 'empty-state-text');
                emptyText.textContent = '暂无灵光一闪,点击"添加"创建';
                emptyState.appendChild(emptyText);
                inspirationSection.appendChild(emptyState);
            } else {
                state.currentCard.var.inspiration.forEach((insp, idx) => {
                    const inspItem = createElement('div', 'inspiration-item');
                    
                    const inspHeader = createElement('div', 'inspiration-item-header');
                    const inspTitle = createElement('div', 'inspiration-item-title');
                    inspTitle.textContent = `灵光 #${idx + 1}`;
                    inspHeader.appendChild(inspTitle);
                    
                    const deleteInspBtn = createButton('删除', 'btn-danger', () => {
                        if (confirm('确定要删除这个灵光一闪吗?')) {
                            state.currentCard.var.inspiration.splice(idx, 1);
                            render();
                        }
                    });
                    deleteInspBtn.style.padding = '4px 8px';
                    deleteInspBtn.style.fontSize = '12px';
                    inspHeader.appendChild(deleteInspBtn);
                    inspItem.appendChild(inspHeader);
                    
                    // AP输入
                    const inspApInput = createInput('text', insp.ap, (value) => {
                        insp.ap = value;
                        render();
                    }, 'AP');
                    inspApInput.style.marginBottom = '8px';
                    inspApInput.style.fontSize = '13px';
                    inspItem.appendChild(inspApInput);
                    
                    // 类型选择
                    const inspTypeSelect = createSelect(
                        ['', '攻击', '技能', '强化'],
                        insp.type || '',
                        (value) => {
                            insp.type = value;
                            render();
                        }
                    );
                    inspTypeSelect.wrapper.style.marginBottom = '8px';
                    inspItem.appendChild(inspTypeSelect.wrapper);
                    
                    // 描述输入
                    const inspDescInput = createInput('textarea', insp.desc_global, (value) => {
                        insp.desc_global = value;
                        render();
                    }, '描述');
                    inspDescInput.style.minHeight = '60px';
                    inspDescInput.style.fontSize = '13px';
                    inspItem.appendChild(inspDescInput);
                    
                    inspirationSection.appendChild(inspItem);
                });
            }
            
            listSection.appendChild(inspirationSection);
        }
        
        // 神光一闪列表
        if (state.currentCard && state.currentCard.base.isgod_inspiration) {
            const godInspirationSection = createElement('div', 'inspiration-section');
            const godInspirationHeader = createElement('div', 'list-header');
            const godInspirationTitle = createElement('div', 'list-title');
            godInspirationTitle.textContent = '神光一闪列表';
            godInspirationHeader.appendChild(godInspirationTitle);
            godInspirationSection.appendChild(godInspirationHeader);
            
            // 神明选择标签
            const godSelectGroup = createElement('div', 'god-select-group');
            ['circen', 'diallos', 'nihilum', 'secred', 'vitor'].forEach(god => {
                const godTab = createElement('div', 'god-tab' + (state.currentGod === god ? ' active' : ''));
                godTab.textContent = god;
                godTab.addEventListener('click', () => {
                    state.currentGod = god;
                    render();
                });
                godSelectGroup.appendChild(godTab);
            });
            godInspirationSection.appendChild(godSelectGroup);
            
            // 当前神明的神光一闪列表
            const currentGodInspirations = state.currentCard.var.god_inspiration[state.currentGod];
            
            const addGodInspirationBtn = createButton('+ 添加 ' + state.currentGod + ' 神光', 'btn-primary', () => {
                currentGodInspirations.push({
                    ap: '',
                    type: '',
                    desc_global: ''
                });
                render();
            });
            addGodInspirationBtn.style.marginBottom = '10px';
            addGodInspirationBtn.style.width = '100%';
            godInspirationSection.appendChild(addGodInspirationBtn);
            
            if (currentGodInspirations.length === 0) {
                const emptyState = createElement('div', 'empty-state');
                emptyState.style.padding = '20px';
                const emptyText = createElement('div', 'empty-state-text');
                emptyText.textContent = `暂无 ${state.currentGod} 神光一闪`;
                emptyState.appendChild(emptyText);
                godInspirationSection.appendChild(emptyState);
            } else {
                currentGodInspirations.forEach((insp, idx) => {
                    const inspItem = createElement('div', 'inspiration-item');
                    
                    const inspHeader = createElement('div', 'inspiration-item-header');
                    const inspTitle = createElement('div', 'inspiration-item-title');
                    inspTitle.textContent = `${state.currentGod} 神光 #${idx + 1}`;
                    inspHeader.appendChild(inspTitle);
                    
                    const deleteInspBtn = createButton('删除', 'btn-danger', () => {
                        if (confirm('确定要删除这个神光一闪吗?')) {
                            currentGodInspirations.splice(idx, 1);
                            render();
                        }
                    });
                    deleteInspBtn.style.padding = '4px 8px';
                    deleteInspBtn.style.fontSize = '12px';
                    inspHeader.appendChild(deleteInspBtn);
                    inspItem.appendChild(inspHeader);
                    
                    // AP输入
                    const inspApInput = createInput('text', insp.ap, (value) => {
                        insp.ap = value;
                        render();
                    }, 'AP');
                    inspApInput.style.marginBottom = '8px';
                    inspApInput.style.fontSize = '13px';
                    inspItem.appendChild(inspApInput);
                    
                    // 类型选择
                    const inspTypeSelect = createSelect(
                        ['', '攻击', '技能', '强化'],
                        insp.type || '',
                        (value) => {
                            insp.type = value;
                            render();
                        }
                    );
                    inspTypeSelect.wrapper.style.marginBottom = '8px';
                    inspItem.appendChild(inspTypeSelect.wrapper);
                    
                    // 描述输入
                    const inspDescInput = createInput('textarea', insp.desc_global, (value) => {
                        insp.desc_global = value;
                        render();
                    }, '描述');
                    inspDescInput.style.minHeight = '60px';
                    inspDescInput.style.fontSize = '13px';
                    inspItem.appendChild(inspDescInput);
                    
                    godInspirationSection.appendChild(inspItem);
                });
            }
            
            listSection.appendChild(godInspirationSection);
        }
        
        manager.appendChild(listSection);
        
        // 右侧预览区
        const previewSection = createElement('div', 'card-preview-section');
        const previewTitle = createElement('div', 'section-title');
        previewTitle.textContent = 'Lua 代码预览';
        previewSection.appendChild(previewTitle);
        
        // 复制按钮
        const copyBtn = createButton('复制代码', 'btn-primary', () => {
            const code = generateLuaCode();
            navigator.clipboard.writeText(code).then(() => {
                alert('代码已复制到剪贴板!');
            }).catch(err => {
                console.error('复制失败:', err);
                // 降级方案
                const textarea = document.createElement('textarea');
                textarea.value = code;
                textarea.style.position = 'fixed';
                textarea.style.opacity = '0';
                document.body.appendChild(textarea);
                textarea.select();
                document.execCommand('copy');
                document.body.removeChild(textarea);
                alert('代码已复制到剪贴板!');
            });
        });
        copyBtn.style.marginBottom = '10px';
        copyBtn.style.width = '100%';
        previewSection.appendChild(copyBtn);
        
        // 保存按钮
        const saveBtn = createButton('保存到Wiki', 'btn-success', async () => {
            if (!state.currentFighter) {
                alert('请先选择战斗员!');
                return;
            }
            
            const code = generateLuaCode();
            const pageName = '模块:卡牌/' + state.currentFighter;
            
            try {
                const api = new mw.Api();
                await api.postWithToken('csrf', {
                    action: 'edit',
                    title: pageName,
                    text: code,
                    summary: '通过卡牌管理器更新卡牌数据',
                    contentmodel: 'Scribunto'
                });
                alert('保存成功!');
            } catch (error) {
                console.error('保存失败:', error);
                alert('保存失败:' + error);
            }
        });
        saveBtn.style.marginBottom = '10px';
        saveBtn.style.width = '100%';
        previewSection.appendChild(saveBtn);
        
        // 代码显示
        const codePreview = createElement('div', 'code-preview');
        codePreview.textContent = generateLuaCode();
        previewSection.appendChild(codePreview);
        
        manager.appendChild(previewSection);
        
        container.appendChild(manager);
    }
    
    // 初始化
    async function init() {
        // 确保在正确的页面
        const pageName = mw.config.get('wgPageName');
        
        // 创建容器
        let container = document.getElementById('card-manager-container');
        if (!container) {
            container = createElement('div', '');
            container.id = 'card-manager-container';
            
            // 查找插入位置
            const content = document.getElementById('mw-content-text');
            if (content) {
                content.insertBefore(container, content.firstChild);
            } else {
                document.body.appendChild(container);
            }
        }
        
        // 加载战斗员列表
        await loadFighters();
        
        // 如果有战斗员,默认选择第一个
        if (state.fighters.length > 0) {
            state.currentFighter = state.fighters[0];
            state.cards = await loadFighterCards(state.currentFighter);
        }
        
        // 渲染界面
        render();
    }
    
    // 等待DOM加载完成后初始化
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
    
    // 导出API供外部使用
    window.CardManager = {
        init: init,
        render: render,
        state: state,
        createCard: createEmptyCard,
        generateCode: generateLuaCode
    };
    
})();