Zum Inhalt springen

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

Aus Home Wiki
Keine Bearbeitungszusammenfassung
Markierung: Zurückgesetzt
Keine Bearbeitungszusammenfassung
Markierungen: Manuelle Zurücksetzung Zurückgesetzt
Zeile 1: Zeile 1:
mw.loader.using(['mediawiki.util']).then(function () {
function highlightPlaceholders(code) {
    return code.replace(/{{(\w+)}}/g, '<span class="placeholder">{{$1}}</span>');
}


    function copyText(text, btn) {
function initDynamicCodeBlock(pre) {
        if (navigator.clipboard && window.isSecureContext) {
    if (pre.dataset.dynamicInit) return;
            navigator.clipboard.writeText(text).then(success);
    pre.dataset.dynamicInit = true;
            return;
 
        }
    const originalCode = pre.textContent;
        const textarea = document.createElement("textarea");
    const regex = /{{(\w+)}}/g;
        textarea.value = text;
    const vars = [];
        document.body.appendChild(textarea);
    let match;
        textarea.select();
    while ((match = regex.exec(originalCode)) !== null) {
        document.execCommand("copy");
         if (!vars.includes(match[1])) vars.push(match[1]);
        document.body.removeChild(textarea);
         function success() {
            btn.textContent = "✅ Kopiert!";
            setTimeout(() => btn.textContent = "📋 Kopieren", 1500);
        }
        success();
     }
     }
    if (vars.length === 0) return;


     // CSS für Platzhalter
     // Wrap code in <code>
     const style = document.createElement('style');
     let codeEl;
    style.textContent = `
    if (!pre.querySelector('code')) {
         .placeholder { color: #d14; font-weight: bold; }
        codeEl = document.createElement('code');
         pre.dynamic-code { position: relative; padding-right: 50px; } /* Platz für Button */
        codeEl.style.display = 'block';
    `;
         codeEl.style.whiteSpace = 'pre';
    document.head.appendChild(style);
        codeEl.innerHTML = highlightPlaceholders(originalCode);
 
         pre.textContent = '';
     function highlightPlaceholders(code) {
        pre.appendChild(codeEl);
         return code.replace(/({{\w+}})/g, '<span class="placeholder">$1</span>');
     } else {
         codeEl = pre.querySelector('code');
     }
     }


     function initDynamicCodeBlock(pre) {
     // Container für Inputs
        if (pre.dataset.dynamicInit) return;
    const container = document.createElement('div');
        pre.dataset.dynamicInit = true;
    container.style.display = 'flex';
 
    container.style.flexWrap = 'wrap';
        const originalCode = pre.textContent;
    container.style.alignItems = 'center';
        const regex = /{{(\w+)}}/g;
    container.style.justifyContent = 'space-between';
        const vars = [];
    container.style.gap = '6px';
        let match;
    container.style.marginBottom = '4px';
        while ((match = regex.exec(originalCode)) !== null) {
    container.style.padding = '4px 6px';
            if (!vars.includes(match[1])) vars.push(match[1]);
    container.style.border = '1px solid #ddd';
        }
    container.style.borderRadius = '6px';
        if (vars.length === 0) return;
    container.style.background = '#f5f5f5';
 
        // Container für Inputs
        const container = document.createElement('div');
        container.style.display = 'flex';
        container.style.flexWrap = 'wrap';
        container.style.alignItems = 'center';
        container.style.gap = '6px';
        container.style.marginBottom = '4px';
        container.style.padding = '4px 6px';
        container.style.border = '1px solid #ddd';
        container.style.borderRadius = '6px';
        container.style.background = '#f5f5f5';


        vars.forEach(v => {
    const inputWrapper = document.createElement('div');
            const lbl = document.createElement('label');
    inputWrapper.style.display = 'flex';
            lbl.style.display = 'flex';
    inputWrapper.style.flexWrap = 'wrap';
            lbl.style.flexDirection = 'column';
    inputWrapper.style.gap = '6px';
            lbl.style.fontSize = '12px';
    container.appendChild(inputWrapper);
            lbl.style.color = '#333';
            lbl.textContent = v;


            const inp = document.createElement('input');
    vars.forEach(v => {
            inp.type = 'text';
        const lbl = document.createElement('label');
            inp.value = `{{${v}}}`; // Beispielwert in Klammern
        lbl.style.display = 'flex';
            inp.dataset.varId = v;
        lbl.style.flexDirection = 'column';
            inp.style.width = '120px';
        lbl.style.fontSize = '12px';
            inp.style.padding = '2px 4px';
        lbl.style.color = '#333';
            inp.style.border = '1px solid #ccc';
        lbl.textContent = v;
            inp.style.borderRadius = '4px';
            inp.style.fontFamily = 'monospace';
            inp.style.fontSize = '12px';


            lbl.appendChild(inp);
        const inp = document.createElement('input');
            container.appendChild(lbl);
        inp.type = 'text';
        inp.value = `{{${v}}}`;
        inp.dataset.varId = v;
        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';


            inp.addEventListener('input', () => {
        inp.addEventListener('input', () => {
                let updated = originalCode;
            let updated = originalCode;
                vars.forEach(vv => {
            vars.forEach(varName => {
                    const val = container.querySelector(`input[data-var-id="${vv}"]`).value;
                const val = container.querySelector(`input[data-var-id="${varName}"]`).value;
                    updated = updated.replace(new RegExp(`{{${vv}}}`, 'g'), val);
                updated = updated.replace(new RegExp(`{{${varName}}}`, 'g'), val);
                });
                // Highlight nur auf Text, Button bleibt unberührt
                const btn = pre.querySelector('button.copy-btn');
                pre.innerHTML = highlightPlaceholders(updated);
                if (btn) pre.appendChild(btn);
             });
             });
            codeEl.innerHTML = highlightPlaceholders(updated);
         });
         });


         pre.parentNode.insertBefore(container, pre);
         lbl.appendChild(inp);
 
         inputWrapper.appendChild(lbl);
        // Copy-Button fix oben rechts im pre
     });
        const btn = document.createElement('button');
        btn.textContent = '📋 Kopieren';
        btn.classList.add('copy-btn');
         btn.style.position = 'absolute';
        btn.style.top = '4px';
        btn.style.right = '4px';
        btn.style.padding = '2px 6px';
        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.replace(/<[^>]*>/g, ''), btn);
        pre.appendChild(btn);
 
        // Highlight initial
        pre.innerHTML = highlightPlaceholders(originalCode);
     }
 
    function initStaticCodeBlock(pre) {
        if (pre.dataset.staticInit) return;
        pre.dataset.staticInit = true;
 
        pre.style.position = 'relative';
 
        const btn = document.createElement('button');
        btn.textContent = '📋 Kopieren';
        btn.style.position = 'absolute';
        btn.style.top = '4px';
        btn.style.right = '4px';
        btn.style.padding = '2px 6px';
        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.replace(/<[^>]*>/g, ''), btn);
 
        pre.appendChild(btn);


        // Highlight initial
    // Kopierbutton
        pre.innerHTML = highlightPlaceholders(pre.textContent);
    const btn = document.createElement('button');
    }
    btn.textContent = '📋 Kopieren';
    btn.style.padding = '2px 6px';
    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(codeEl.textContent, btn);


     function initPage() {
     container.appendChild(btn);
        document.querySelectorAll('pre.dynamic-code').forEach(initDynamicCodeBlock);
    pre.parentNode.insertBefore(container, pre);
        document.querySelectorAll('pre.static-code').forEach(initStaticCodeBlock);
}
    }


    if (document.readyState === 'loading') {
// CSS Highlight
        document.addEventListener('DOMContentLoaded', initPage);
const style = document.createElement('style');
    } else {
style.textContent = `
        initPage();
.placeholder { color: #d14; font-weight: bold; }
    }
`;
    mw.hook('wikipage.content').add(initPage);
document.head.appendChild(style);
});

Version vom 14. Februar 2026, 13:08 Uhr

function highlightPlaceholders(code) {
    return code.replace(/{{(\w+)}}/g, '<span class="placeholder">{{$1}}</span>');
}

function initDynamicCodeBlock(pre) {
    if (pre.dataset.dynamicInit) return;
    pre.dataset.dynamicInit = true;

    const originalCode = pre.textContent;
    const regex = /{{(\w+)}}/g;
    const vars = [];
    let match;
    while ((match = regex.exec(originalCode)) !== null) {
        if (!vars.includes(match[1])) vars.push(match[1]);
    }
    if (vars.length === 0) return;

    // Wrap code in <code>
    let codeEl;
    if (!pre.querySelector('code')) {
        codeEl = document.createElement('code');
        codeEl.style.display = 'block';
        codeEl.style.whiteSpace = 'pre';
        codeEl.innerHTML = highlightPlaceholders(originalCode);
        pre.textContent = '';
        pre.appendChild(codeEl);
    } else {
        codeEl = pre.querySelector('code');
    }

    // Container für Inputs
    const container = document.createElement('div');
    container.style.display = 'flex';
    container.style.flexWrap = 'wrap';
    container.style.alignItems = 'center';
    container.style.justifyContent = 'space-between';
    container.style.gap = '6px';
    container.style.marginBottom = '4px';
    container.style.padding = '4px 6px';
    container.style.border = '1px solid #ddd';
    container.style.borderRadius = '6px';
    container.style.background = '#f5f5f5';

    const inputWrapper = document.createElement('div');
    inputWrapper.style.display = 'flex';
    inputWrapper.style.flexWrap = 'wrap';
    inputWrapper.style.gap = '6px';
    container.appendChild(inputWrapper);

    vars.forEach(v => {
        const lbl = document.createElement('label');
        lbl.style.display = 'flex';
        lbl.style.flexDirection = 'column';
        lbl.style.fontSize = '12px';
        lbl.style.color = '#333';
        lbl.textContent = v;

        const inp = document.createElement('input');
        inp.type = 'text';
        inp.value = `{{${v}}}`;
        inp.dataset.varId = v;
        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';

        inp.addEventListener('input', () => {
            let updated = originalCode;
            vars.forEach(varName => {
                const val = container.querySelector(`input[data-var-id="${varName}"]`).value;
                updated = updated.replace(new RegExp(`{{${varName}}}`, 'g'), val);
            });
            codeEl.innerHTML = highlightPlaceholders(updated);
        });

        lbl.appendChild(inp);
        inputWrapper.appendChild(lbl);
    });

    // Kopierbutton
    const btn = document.createElement('button');
    btn.textContent = '📋 Kopieren';
    btn.style.padding = '2px 6px';
    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(codeEl.textContent, btn);

    container.appendChild(btn);
    pre.parentNode.insertBefore(container, pre);
}

// CSS Highlight
const style = document.createElement('style');
style.textContent = `
.placeholder { color: #d14; font-weight: bold; }
`;
document.head.appendChild(style);