Zum Inhalt springen

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

Aus Home Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Markierung: 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 textarea = document.createElement("textarea");
    const originalCode = pre.textContent;
        textarea.value = text;
    const regex = /{{(\w+)}}/g;
        document.body.appendChild(textarea);
    const vars = [];
        textarea.select();
    let match;
        document.execCommand("copy");
    while ((match = regex.exec(originalCode)) !== null) {
        document.body.removeChild(textarea);
         if (!vars.includes(match[1])) vars.push(match[1]);
 
        success();
         function success() {
            btn.textContent = "✅ Kopiert!";
            setTimeout(() => btn.textContent = "📋 Kopieren", 1500);
        }
     }
     }
    if (vars.length === 0) return;


     function highlightPlaceholders(code) {
     // Wrap code in <code>
         return code.replace(/{{(\w+)}}/g, '<span class="placeholder">{{$1}}</span>');
    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');
     }
     }


     // Dynamische Codeblöcke
     // Container für Inputs
     function initDynamicCodeBlock(pre) {
     const container = document.createElement('div');
        if (pre.dataset.dynamicInit) return;
    container.style.display = 'flex';
        pre.dataset.dynamicInit = true;
    container.style.flexWrap = 'wrap';
 
    container.style.alignItems = 'center';
        const originalCode = pre.textContent;
    container.style.justifyContent = 'space-between';
        const regex = /{{(\w+)}}/g;
    container.style.gap = '6px';
        const vars = [];
    container.style.marginBottom = '4px';
        let match;
    container.style.padding = '4px 6px';
        while ((match = regex.exec(originalCode)) !== null) {
    container.style.border = '1px solid #ddd';
            if (!vars.includes(match[1])) vars.push(match[1]);
    container.style.borderRadius = '6px';
        }
    container.style.background = '#f5f5f5';
        if (vars.length === 0) return;
 
        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}}}`; // Beispielwert in Klammern
            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';
 
            lbl.appendChild(inp);
            inputWrapper.appendChild(lbl);
        });


        // Kopier-Button rechts
    const inputWrapper = document.createElement('div');
        const btn = document.createElement('button');
    inputWrapper.style.display = 'flex';
        btn.textContent = '📋 Kopieren';
    inputWrapper.style.flexWrap = 'wrap';
        btn.style.padding = '2px 6px';
    inputWrapper.style.gap = '6px';
        btn.style.fontSize = '12px';
    container.appendChild(inputWrapper);
        btn.style.cursor = 'pointer';
        btn.style.border = '1px solid #888';
        btn.style.borderRadius = '4px';
        btn.style.background = '#eee';
        btn.onclick = () => copyText(pre.textContent, btn);


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


         // Highlight initial
         const inp = document.createElement('input');
         pre.innerHTML = highlightPlaceholders(originalCode);
        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';


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


    // Statische Codeblöcke
        lbl.appendChild(inp);
    function initStaticCodeBlock(pre) {
         inputWrapper.appendChild(lbl);
         if (pre.dataset.staticInit) return;
    });
        pre.dataset.staticInit = true;


        const container = document.createElement('div');
    // Kopierbutton
        container.style.position = 'relative';
    const btn = document.createElement('button');
        container.style.marginBottom = '6px';
    btn.textContent = '📋 Kopieren';
        container.appendChild(pre);
    btn.style.padding = '2px 6px';
 
    btn.style.fontSize = '12px';
        const btn = document.createElement('button');
    btn.style.cursor = 'pointer';
        btn.textContent = '📋 Kopieren';
    btn.style.border = '1px solid #888';
        btn.style.position = 'absolute';
    btn.style.borderRadius = '4px';
        btn.style.top = '4px';
    btn.style.background = '#eee';
        btn.style.right = '4px';
    btn.onclick = () => copyText(codeEl.textContent, btn);
        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, btn);
 
        container.appendChild(btn);
        pre.parentNode.insertBefore(container, pre);
    }
 
    function initPage() {
        document.querySelectorAll('pre.dynamic-code').forEach(initDynamicCodeBlock);
        document.querySelectorAll('pre.static-code').forEach(initStaticCodeBlock);
    }
 
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initPage);
    } else {
        initPage();
    }
    mw.hook('wikipage.content').add(initPage);


     // --- CSS für Highlight ---
     container.appendChild(btn);
    const style = document.createElement('style');
     pre.parentNode.insertBefore(container, pre);
     style.textContent = `
}
        .placeholder { color: #d14; font-weight: bold; }
    `;
    document.head.appendChild(style);


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

Version vom 14. Februar 2026, 13:00 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);