Zum Inhalt springen

MediaWiki:Gadget-GlobalVariables.js: Unterschied zwischen den Versionen

Aus Home Wiki
Keine Bearbeitungszusammenfassung
Markierung: Zurückgesetzt
Keine Bearbeitungszusammenfassung
 
(8 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
mw.loader.using(['mediawiki.util']).then(function () {
mw.loader.using(['mediawiki.util']).then(function () {


     const globalVars = {}; // speichert die Variablenwerte
     const globalVars = {};
     const seenVars = new Set(); // speichert, welche Variablen schon in der Leiste sind
     const seenVars = new Set();
     const variableBlocks = {}; // speichert die Blöcke, die jede Variable nutzen
     const variableBlocks = {};


     // --- Globale Leiste erstellen ---
     // Leiste oben als flex-Container
     const bar = document.createElement('div');
     const bar = document.createElement('div');
     bar.id = 'global-var-bar';
     bar.id = 'global-var-bar';
     bar.style.display = 'flex';
     bar.style.display = 'flex';
     bar.style.flexWrap = 'wrap';
     bar.style.flexWrap = 'wrap';
    bar.style.alignItems = 'center';
     bar.style.gap = '8px';
     bar.style.gap = '8px';
     bar.style.padding = '8px';
     bar.style.padding = '8px';
Zeile 16: Zeile 17:
     bar.style.background = '#f5f5f5';
     bar.style.background = '#f5f5f5';
     bar.style.marginBottom = '12px';
     bar.style.marginBottom = '12px';
     document.querySelector('#content').prepend(bar);
     document.querySelector('#content').prepend(bar);


     function addVarOnce(varName) {
    // Button "Auf alle anwenden" rechts
    const applyAllBtn = document.createElement('button');
    applyAllBtn.textContent = '→ Auf alle anwenden';
    applyAllBtn.style.padding = '4px 8px';
    applyAllBtn.style.cursor = 'pointer';
    applyAllBtn.style.border = '1px solid #888';
    applyAllBtn.style.borderRadius = '4px';
    applyAllBtn.style.background = '#eee';
    applyAllBtn.style.marginLeft = 'auto'; // sitzt rechts
    bar.appendChild(applyAllBtn);
 
     function addVarOnce(varName, defaultValue = '') {
         if (seenVars.has(varName)) return;
         if (seenVars.has(varName)) return;
         seenVars.add(varName);
         seenVars.add(varName);


         globalVars[varName] = '';
         globalVars[varName] = defaultValue;


         const lbl = document.createElement('label');
         const lbl = document.createElement('label');
Zeile 29: Zeile 42:
         lbl.style.fontSize = '12px';
         lbl.style.fontSize = '12px';
         lbl.style.color = '#333';
         lbl.style.color = '#333';
         lbl.textContent = varName;
 
         const span = document.createElement('span');
        span.textContent = varName + ':';
        lbl.appendChild(span);


         const inp = document.createElement('input');
         const inp = document.createElement('input');
         inp.type = 'text';
         inp.type = 'text';
         inp.value = '';
 
        // Beim ersten Laden: Standardwert in Klammern, später alles wie eingegeben
        let initialValue = defaultValue;
        if (!initialValue.startsWith('{{')) {
            initialValue = `{{${initialValue}}}`;
        }
         inp.value = initialValue;
 
         inp.dataset.varId = varName;
         inp.dataset.varId = varName;
         inp.style.width = '120px';
         inp.style.width = '120px';
Zeile 41: Zeile 64:
         inp.style.fontFamily = 'monospace';
         inp.style.fontFamily = 'monospace';
         inp.style.fontSize = '12px';
         inp.style.fontSize = '12px';
        lbl.appendChild(inp);


         // Wenn Input geändert wird: alle Blöcke updaten
         // Eingaben sofort in globale Vars übertragen
         inp.addEventListener('input', () => {
         inp.addEventListener('input', () => {
             globalVars[varName] = inp.value;
             globalVars[varName] = inp.value; // nichts mehr automatisch ergänzen
            updateAllBlocks(varName);
         });
         });


         lbl.appendChild(inp);
         // Füge Label links ein, Button sitzt automatisch rechts
         bar.appendChild(lbl);
         bar.insertBefore(lbl, applyAllBtn);
     }
     }


     // --- Dynamische Codeblöcke initialisieren ---
     // Alle dynamischen Codeblöcke initialisieren
     function initDynamicCodeBlock(pre) {
     document.querySelectorAll('pre.dynamic-code').forEach(pre => {
        if (pre.dataset.dynamicInit) return;
        pre.dataset.dynamicInit = true;
 
        const originalCode = pre.textContent;
         const regex = /{{(\w+)}}/g;
         const regex = /{{(\w+)}}/g;
        const vars = [];
         let match;
         let match;
         while ((match = regex.exec(originalCode)) !== null) {
         while ((match = regex.exec(pre.textContent)) !== null) {
             if (!vars.includes(match[1])) vars.push(match[1]);
            const varName = match[1];
            const defaultValue = varName; // Standardwert = Name selbst
            addVarOnce(varName, defaultValue);
 
             if (!variableBlocks[varName]) variableBlocks[varName] = [];
            variableBlocks[varName].push(pre);
         }
         }
        if (vars.length === 0) return;
    });


        // Kopier-Button rechts oben in der Box
    // Button-Event für alle Variablen
        const btn = document.createElement('button');
    applyAllBtn.onclick = () => {
        btn.textContent = '📋 Kopieren';
         Object.keys(globalVars).forEach(name => {
        btn.style.position = 'absolute';
             const val = globalVars[name];
        btn.style.top = '4px';
        btn.style.right = '4px';
        btn.style.padding = '4px 8px';
        btn.style.fontSize = '12px';
        btn.style.cursor = 'pointer';
        btn.style.border = '1px solid #888';
        btn.style.borderRadius = '4px';
        btn.style.background = '#eee';
 
        btn.onclick = () => copyText(pre.textContent, btn);
 
         // Wrapper um Codeblock, um Button absolut zu positionieren
        const wrapper = document.createElement('div');
        wrapper.style.position = 'relative';
        wrapper.appendChild(btn);
        pre.parentNode.insertBefore(wrapper, pre);
        wrapper.appendChild(pre);
 
        // Variable für globale Updates registrieren
        vars.forEach(v => {
             addVarOnce(v);
            if (!variableBlocks[v]) variableBlocks[v] = [];
            variableBlocks[v].push(pre);
        });
 
        // initial update
        updateAllBlocks();
    }
 
    // --- Alle Blöcke updaten ---
    function updateAllBlocks(varName) {
        const varsToUpdate = varName ? [varName] : Object.keys(variableBlocks);
        varsToUpdate.forEach(name => {
             const blocks = variableBlocks[name] || [];
             const blocks = variableBlocks[name] || [];
             blocks.forEach(pre => {
             blocks.forEach(pre => {
                 let updated = pre.textContent;
                 pre.textContent = pre.textContent.replace(new RegExp(`{{${name}}}`, 'g'), val);
                 // alle Variablen im Block ersetzen
 
                 const regex = /{{(\w+)}}/g;
                 // lokale Inputs in den Codeblöcken synchronisieren
                updated = updated.replace(regex, (match, p1) => globalVars[p1] ?? p1);
                 const container = pre.previousSibling;
                pre.textContent = updated;
                if (container && container.querySelector) {
                    const localInp = container.querySelector(`input[data-var-id="${name}"]`);
                    if (localInp) localInp.value = val;
                }
             });
             });
         });
         });
    };


        // Sync Inputs in der Leiste
     console.log('Globale Variablenleiste initialisiert:', Array.from(seenVars));
        Object.keys(globalVars).forEach(name => {
            const inp = document.querySelector(`#global-var-bar input[data-var-id="${name}"]`);
            if (inp && inp.value !== globalVars[name]) inp.value = globalVars[name];
        });
    }
 
    // --- Copy-Funktion ---
     function copyText(text, btn) {
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(text).then(success);
        } else {
            const textarea = document.createElement('textarea');
            textarea.value = text;
            document.body.appendChild(textarea);
            textarea.select();
            document.execCommand('copy');
            document.body.removeChild(textarea);
            success();
        }
 
        function success() {
            btn.textContent = '✅ Kopiert!';
            setTimeout(() => btn.textContent = '📋 Kopieren', 1500);
        }
    }
 
    // --- Initialisierung ---
    function initPage() {
        document.querySelectorAll('pre.dynamic-code').forEach(initDynamicCodeBlock);
    }
 
    initPage();
    mw.hook('wikipage.content').add(initPage);


});
});

Aktuelle Version vom 14. Februar 2026, 14:19 Uhr

mw.loader.using(['mediawiki.util']).then(function () {

    const globalVars = {};
    const seenVars = new Set();
    const variableBlocks = {};

    // Leiste oben als flex-Container
    const bar = document.createElement('div');
    bar.id = 'global-var-bar';
    bar.style.display = 'flex';
    bar.style.flexWrap = 'wrap';
    bar.style.alignItems = 'center';
    bar.style.gap = '8px';
    bar.style.padding = '8px';
    bar.style.border = '1px solid #bbb';
    bar.style.borderRadius = '6px';
    bar.style.background = '#f5f5f5';
    bar.style.marginBottom = '12px';

    document.querySelector('#content').prepend(bar);

    // Button "Auf alle anwenden" rechts
    const applyAllBtn = document.createElement('button');
    applyAllBtn.textContent = '→ Auf alle anwenden';
    applyAllBtn.style.padding = '4px 8px';
    applyAllBtn.style.cursor = 'pointer';
    applyAllBtn.style.border = '1px solid #888';
    applyAllBtn.style.borderRadius = '4px';
    applyAllBtn.style.background = '#eee';
    applyAllBtn.style.marginLeft = 'auto'; // sitzt rechts
    bar.appendChild(applyAllBtn);

    function addVarOnce(varName, defaultValue = '') {
        if (seenVars.has(varName)) return;
        seenVars.add(varName);

        globalVars[varName] = defaultValue;

        const lbl = document.createElement('label');
        lbl.style.display = 'flex';
        lbl.style.flexDirection = 'column';
        lbl.style.fontSize = '12px';
        lbl.style.color = '#333';

        const span = document.createElement('span');
        span.textContent = varName + ':';
        lbl.appendChild(span);

        const inp = document.createElement('input');
        inp.type = 'text';

        // Beim ersten Laden: Standardwert in Klammern, später alles wie eingegeben
        let initialValue = defaultValue;
        if (!initialValue.startsWith('{{')) {
            initialValue = `{{${initialValue}}}`;
        }
        inp.value = initialValue;

        inp.dataset.varId = varName;
        inp.style.width = '120px';
        inp.style.padding = '2px 4px';
        inp.style.border = '1px solid #ccc';
        inp.style.borderRadius = '4px';
        inp.style.fontFamily = 'monospace';
        inp.style.fontSize = '12px';
        lbl.appendChild(inp);

        // Eingaben sofort in globale Vars übertragen
        inp.addEventListener('input', () => {
            globalVars[varName] = inp.value; // nichts mehr automatisch ergänzen
        });

        // Füge Label links ein, Button sitzt automatisch rechts
        bar.insertBefore(lbl, applyAllBtn);
    }

    // Alle dynamischen Codeblöcke initialisieren
    document.querySelectorAll('pre.dynamic-code').forEach(pre => {
        const regex = /{{(\w+)}}/g;
        let match;
        while ((match = regex.exec(pre.textContent)) !== null) {
            const varName = match[1];
            const defaultValue = varName; // Standardwert = Name selbst
            addVarOnce(varName, defaultValue);

            if (!variableBlocks[varName]) variableBlocks[varName] = [];
            variableBlocks[varName].push(pre);
        }
    });

    // Button-Event für alle Variablen
    applyAllBtn.onclick = () => {
        Object.keys(globalVars).forEach(name => {
            const val = globalVars[name];
            const blocks = variableBlocks[name] || [];
            blocks.forEach(pre => {
                pre.textContent = pre.textContent.replace(new RegExp(`{{${name}}}`, 'g'), val);

                // lokale Inputs in den Codeblöcken synchronisieren
                const container = pre.previousSibling;
                if (container && container.querySelector) {
                    const localInp = container.querySelector(`input[data-var-id="${name}"]`);
                    if (localInp) localInp.value = val;
                }
            });
        });
    };

    console.log('Globale Variablenleiste initialisiert:', Array.from(seenVars));

});