“MediaWiki:Gadget-prism-plugin-match-braces.js”的版本间的差异

来自萌娘共享
跳转至: 导航搜索
(代码变动:2d5d0ae - feat(Gadget/prism): 新的代码高亮工具 (#443) by U:AnnAngela, co-authored-by: GH:github-actions[bot],U:Bhsd
1个标签由机器人或全自动脚本执行的操作
(代码变动:2d5d0ae3 - feat(Gadget/prism): 新的代码高亮工具 (#443) by U:AnnAngela, co-authored-by: GH:github-actions[bot],U:Bhsd
1个标签由机器人或全自动脚本执行的操作
 
第4行: 第4行:
 
  * -------------------------------------------------------------------------
 
  * -------------------------------------------------------------------------
 
  */
 
  */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/prism-plugin-match-braces/MediaWiki:Gadget-prism-plugin-match-braces.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]、[[U:Bhsd]]|longId=2d5d0ae30421796cfa323a02a78713c0ed261417|shortId=2d5d0ae|summary=feat(Gadget/prism): 新的代码高亮工具 (#443)|body=<nowiki>Co-authored-by: github-actions[bot] <41898282+github-actions[bot]📧users.noreply.github.com>↩Co-authored-by: Bhsd <55071315+bhsd-harry📧users.noreply.github.com></nowiki>}}';  
+
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/prism-plugin-match-braces/MediaWiki:Gadget-prism-plugin-match-braces.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]、[[U:Bhsd]]|longId=2d5d0ae30421796cfa323a02a78713c0ed261417|shortId=2d5d0ae3|summary=feat(Gadget/prism): 新的代码高亮工具 (#443)|body=<nowiki>Co-authored-by: github-actions[bot] <41898282+github-actions[bot]📧users.noreply.github.com>↩Co-authored-by: Bhsd <55071315+bhsd-harry📧users.noreply.github.com></nowiki>}}';  
  
 
/* <pre> */
 
/* <pre> */

2024年4月16日 (二) 09:12的最新版本

/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * -------------------------------------------------------------------------
 */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/prism-plugin-match-braces/MediaWiki:Gadget-prism-plugin-match-braces.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]、[[U:Bhsd]]|longId=2d5d0ae30421796cfa323a02a78713c0ed261417|shortId=2d5d0ae3|summary=feat(Gadget/prism): 新的代码高亮工具 (#443)|body=<nowiki>Co-authored-by: github-actions[bot] <41898282+github-actions[bot]📧users.noreply.github.com>↩Co-authored-by: Bhsd <55071315+bhsd-harry📧users.noreply.github.com></nowiki>}}'; 

/* <pre> */

"use strict";
(function () {
    if (typeof Prism === 'undefined' || typeof document === 'undefined') {
        return;
    }
    function mapClassName(name) {
        var customClass = Prism.plugins.customClass;
        if (customClass) {
            return customClass.apply(name, 'none');
        }
        else {
            return name;
        }
    }
    var PARTNER = {
        '(': ')',
        '[': ']',
        '{': '}'
    };
    var NAMES = {
        '(': 'brace-round',
        '[': 'brace-square',
        '{': 'brace-curly'
    };
    var BRACE_ALIAS_MAP = {
        '${': '{'
    };
    var LEVEL_WARP = 12;
    var pairIdCounter = 0;
    var BRACE_ID_PATTERN = /^(pair-\d+-)(close|open)$/;
    function getPartnerBrace(brace) {
        var match = BRACE_ID_PATTERN.exec(brace.id);
        return document.querySelector('#' + match[1] + (match[2] == 'open' ? 'close' : 'open'));
    }
    function hoverBrace() {
        if (!Prism.util.isActive(this, 'brace-hover', true)) {
            return;
        }
        [this, getPartnerBrace(this)].forEach(function (e) {
            e.classList.add(mapClassName('brace-hover'));
        });
    }
    function leaveBrace() {
        [this, getPartnerBrace(this)].forEach(function (e) {
            e.classList.remove(mapClassName('brace-hover'));
        });
    }
    function clickBrace() {
        if (!Prism.util.isActive(this, 'brace-select', true)) {
            return;
        }
        [this, getPartnerBrace(this)].forEach(function (e) {
            e.classList.add(mapClassName('brace-selected'));
        });
    }
    Prism.hooks.add('complete', function (env) {
        var code = env.element;
        var pre = code.parentElement;
        if (!pre || pre.tagName != 'PRE') {
            return;
        }
        var toMatch = [];
        if (Prism.util.isActive(code, 'match-braces')) {
            toMatch.push('(', '[', '{');
        }
        if (toMatch.length == 0) {
            return;
        }
        if (!pre.__listenerAdded) {
            pre.addEventListener('mousedown', function removeBraceSelected() {
                var code = pre.querySelector('code');
                var className = mapClassName('brace-selected');
                Array.prototype.slice.call(code.querySelectorAll('.' + className)).forEach(function (e) {
                    e.classList.remove(className);
                });
            });
            Object.defineProperty(pre, '__listenerAdded', { value: true });
        }
        var punctuation = Array.prototype.slice.call(code.querySelectorAll('span.' + mapClassName('token') + '.' + mapClassName('punctuation')));
        var allBraces = [];
        toMatch.forEach(function (open) {
            var close = PARTNER[open];
            var name = mapClassName(NAMES[open]);
            var pairs = [];
            var openStack = [];
            for (var i = 0; i < punctuation.length; i++) {
                var element = punctuation[i];
                if (element.childElementCount == 0) {
                    var text = element.textContent;
                    text = BRACE_ALIAS_MAP[text] || text;
                    if (text === open) {
                        allBraces.push({ index: i, open: true, element: element });
                        element.classList.add(name);
                        element.classList.add(mapClassName('brace-open'));
                        openStack.push(i);
                    }
                    else if (text === close) {
                        allBraces.push({ index: i, open: false, element: element });
                        element.classList.add(name);
                        element.classList.add(mapClassName('brace-close'));
                        if (openStack.length) {
                            pairs.push([i, openStack.pop()]);
                        }
                    }
                }
            }
            pairs.forEach(function (pair) {
                var pairId = 'pair-' + (pairIdCounter++) + '-';
                var opening = punctuation[pair[0]];
                var closing = punctuation[pair[1]];
                opening.id = pairId + 'open';
                closing.id = pairId + 'close';
                [opening, closing].forEach(function (e) {
                    e.addEventListener('mouseenter', hoverBrace);
                    e.addEventListener('mouseleave', leaveBrace);
                    e.addEventListener('click', clickBrace);
                });
            });
        });
        var level = 0;
        allBraces.sort(function (a, b) { return a.index - b.index; });
        allBraces.forEach(function (brace) {
            if (brace.open) {
                brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
                level++;
            }
            else {
                level = Math.max(0, level - 1);
                brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
            }
        });
    });
}()); 

/* </pre> */