MediaWiki:Gadget-prism-plugin-match-braces.js

来自萌娘共享
跳转至: 导航搜索

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/**
 * -------------------------------------------------------------------------
 * !!! 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> */