MediaWiki:Gadget-charinsert-core.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),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/**
 * Copied from [[mw:User:Alex Smotrov/edittools.js]], modified for use on the Cantonese Wikipedia.
 *
 * Configuration (to be set from [[Special:MyPage/common.js]]):
 *   window.charinsertCustom – Object. Merged into the default charinsert list. For example, setting
 *       this to { Symbols: '‽' } will add the interrobang to the end of the Symbols section.
 *   window.editToolsRecall – Boolean. Set true to create a recall switch.
 *   window.charinsertDontMove – Boolean. Set true to leave the box in its default position, rather
 *       than moving it above the edit summary.
 *   window.updateEditTools() – Function. Call after updating window.charinsertCustom to regenerate the
 *       EditTools window.
 */
/* global jQuery, mw, charinsertCustom */

window.updateEditTools = function () {
};

jQuery( document ).ready( function ( $ ) {
    var EditTools = {
        // Entries prefixed with ␥ (U+2425 SYMBOL FOR DELETE FORM TWO) will not appear in the article namespace (namespace 0).
        // Please make any changes to [[MediaWiki:Edittools]] as well, however, instead of using the ␥ symbol, use {{#ifeq:{{NAMESPACE}}|{{ns:0}}| | }}.
        charinsert: {
            '常用符号': '· – — ° ′ ″ ≈ ≠ ≤ ≥ ± − × ÷ ← → §␥在讨论页添加你的签名: ␥~~\~~  添加引用: <ref>+</ref>',
            'Wiki语法': '· – — ° ′ ″ ≈ ≠ ≤ ≥ ± − × ÷ ← → § ~~\~~ <ref>+</ref>  Wiki语法:  {\{+}}  {\{\{+}}}  |  [+]  [\[+]]  [\[分类:+]]  #REDIRECT.[\[+]]  &nbsp;  <s>+</s> {{color|red|+}} <sup>+</sup>  <sub>+</sub>  <code>+</code>  <pre>+</pre>  <blockquote>+</blockquote>  <ref.name="+"_/>  {\{#tag:ref|+|group="nb"|name=""}}  {\{Reflist}}  <references./>  <includeonly>+</includeonly>  <noinclude>+</noinclude>  {\{DEFAULTSORT:+}}  <nowiki>+</nowiki>  <!--.+_-->  <span.class="plainlinks">+</span>',
            '常用分类': '{{医籍}} {{医论}} {{医案}} {{医家}} {{方剂}} {{中药}} {{病名}} {{病证名}} {{病症名}} {{学术思想}} {{诊法}} {{治法}}  上述模板包含相应分类信息,请置于内容末尾,如无请自建:  [\[分类:+]]',
            '常用模板': '{{待校对条目}} {{内容待完善}} {{缺少概述图}}  请置于内容开头,如无请自建: {\{+}}  添加词条概述图: {{概述图|此处填写上传的图片文件名|此处填写文件描述}}  添加消歧义单独页: {{消歧义}}  消歧义指示: {{Dablink|相关词条或消歧义页}}',
            '特殊汉字': '一 叁 両 两 乙 氹 𢆡 十 廿 卅 卌 厂 厴 人 佢 俾 冖 冚 冧 刀 劏 𠝹 力 攰 又 双 厶 厾 口 唔 叾 咁 噉 咗 啲 哋 啱 㗎 嗮 𠺢 喺 嗰 嘅 𡁵 𡃁 𡁻 𠱁 嘢 噏 嘥 唞 唥 嘟 呔 叶 啓 喼 啫 嗱 嚟 喐 啩 噃 唓 嘞 㖭 嚿 喆 土 埗 坂 堺 堃 女 嫲 小 尐 弓 强 子 孭 孲 𤘅 尸 尻 𡰪 屄 屌 屘 㞗 𡳞 山 峯 弋 弌 弍 弎 手 担 捽 揼 搞 捵 𢫏 掹 搵 揸 揈‎ 撚 攞 㩒 撳 擳 𢳂 ‎撻 撠 攴 攷 敎 文 斌 木 枱 牀 朶 栢 櫈 樖 毋 乸 水 氹 滙 濶 瀄 潷 火 炆 煲 焗 燂 燶 煊 田 曱 甴 畀 畧 畲 目 睇 着 睺 𥄫 疒 癐 穴 窰 米 糭 糸 綉 綫 缶 罉 罇 羊 羣 肉 脷 膶 艸 蔴 行 衞 衣 裇 言 諗 𧨾 𧦠 貝 贏 車 軚 邑 邨 酉 酶 酞 足 踭 金 鎅 鉮 鈈 鈎 鐧 門 𨳍 𨳊 𨳒 閪 𨶙 音 韵 食 餸 骨 ⻣ 麥 麪 魚 鯭 鰂 鱲 鳥 鷄 鵞',
            '中文標點': ', 、 。 ? ! ; : — —— …… ﹋ · “+” ‘+’ (+) 《+》 〈+〉 『+』 【+】 「+」',
            '其它符號': '~ | ¡¿†‡↔↑↓•¶#∞  ‘+’ “+” ‹+› «+» {\{angle.bracket|+}}  ¤₳฿₵¢₡₢$₫₯€₠₣ƒ₴₭₤ℳ₥₦№₧₰£៛₨₪৳₮₩¥  ♠♣♥♦  ♭♯♮  ©®™ ◌ ℃ ℉ ‰ ¢ ¥ ¥ € £ § {\{Unicode|+}}',
        },

        charinsertDivider: "\240",

        cookieName: 'edittoolscharsubset',

        createEditTools: function ( placeholder ) {
            var sel, id;
            var box = document.createElement( "div" );
            var prevSubset = 0, curSubset = 0;
            box.id = "editpage-specialchars";
            box.title = '点击字符或者标签插入到编辑处';

            // append user-defined sets
            if ( window.charinsertCustom ) {
                for ( id in charinsertCustom ) {
                    if ( !EditTools.charinsert[id] ) {
                        EditTools.charinsert[id] = '';
                    }
                }
            }

            // create drop-down select
            sel = document.createElement( 'select' );
            for ( id in EditTools.charinsert ) {
                sel.options[sel.options.length] = new Option( id, id );
            }
            sel.selectedIndex = 0;
            sel.style.marginRight = '.3em';
            sel.title = '揀字符子集';
            sel.onchange = sel.onkeyup = selectSubset;
            box.appendChild( sel );

            // create "recall" switch
            if ( window.editToolsRecall ) {
                var recall = document.createElement( 'span' );
                recall.appendChild( document.createTextNode( '↕' ) ); // ↔
                recall.onclick = function() {
                    sel.selectedIndex = prevSubset;
                    selectSubset();
                };
                with ( recall.style ) {
                    cssFloat = styleFloat = 'left';
                    marginRight = '5px';
                    cursor = 'pointer';
                }
                box.appendChild( recall );
            }

            // load latest selection from cookies
            try {
                var cookieRe = new RegExp( "(?:^|;)\\s*" + EditTools.cookieName + "=(\\d+)\\s*(?:;|$)" );
                var m = cookieRe.exec( document.cookie );
                if ( m && m.length > 1 && parseInt( m[1] ) < sel.options.length ) {
                    sel.selectedIndex = parseInt( m[1] );
                }
            } catch ( err ) {
                // ignore
            }

            placeholder.parentNode.replaceChild( box, placeholder );
            selectSubset();
            return;

            function selectSubset() {
                // remember previous (for "recall" button)
                prevSubset = curSubset;
                curSubset = sel.selectedIndex;
                //save into cookies for persistence
                try {
                    var expires = new Date();
                    expires.setTime( expires.getTime() + 30 * 24 * 60 * 60 * 1000 );  // + 30 days
                    document.cookie = EditTools.cookieName + "=" + curSubset + ";path=/;expires=" + expires.toUTCString();
                } catch ( err ) {
                    // ignore
                }
                //hide other subsets
                var pp = box.getElementsByTagName( 'p' ) ;
                for ( var i = 0; i < pp.length; i++ ) {
                    pp[i].style.display = 'none';
                }
                //show/create current subset
                var id = sel.options[curSubset].value;
                var p = document.getElementById( id );
                if ( !p ) {
                    p = document.createElement( 'p' );
                    p.className = "nowraplinks";
                    p.id = id;
                    if ( id == '阿刺伯字母' || id == '希伯來字母' ) {
                        p.style.fontSize = '120%';
                        p.dir = 'rtl';
                    }
                    var tokens = EditTools.charinsert[id];
                    if ( window.charinsertCustom && charinsertCustom[id] ) {
                        if ( tokens.length > 0 ) {
                            tokens += ' ';
                        }
                        tokens += charinsertCustom[id];
                    }
                    EditTools.createTokens( p, tokens );
                    box.appendChild( p );
                }
                p.style.display = 'inline';
            }
        },

        createTokens: function ( paragraph, str ) {
            var tokens = str.split( ' ' ), token, i, n;
            var radical = ['一', '乙', '十', '厂', '人', '刀', '力', '又', '厶', '口', '土', '女', '小', '弓', '子', '尸', '山', '弋', '手', '攴', '文', '木', '毋', '水', '火', '田', '目', '疒', '穴', '米', '糸', '缶', '羊', '肉', '艸', '行', '衣', '言', '貝', '車', '邑', '酉', '足', '金', '門', '音', '食', '骨', '麥', '魚', '鳥'];
            for ( i = 0; i < tokens.length; i++ ) {
                token = tokens[i];
                n = token.indexOf( '+' );
                if ( token.charAt( 0 ) === '␥' ) {
                    if ( token.length > 1 && mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
                        continue;
                    } else {
                        token = token.substring( 1 );
                    }
                }
                if ( token == '' || token == '_' ) {
                    addText( EditTools.charinsertDivider + ' ' );
                } else if ( token == '\n' ) {
                    paragraph.appendChild( document.createElement( 'br' ) );
                } else if ( token == '___' ) {
                    paragraph.appendChild( document.createElement( 'hr' ) );
                } else if ( token.charAt( token.length-1 ) == ':' ) { // : at the end means just text
                    addBold( token );
                } else if ( radical.indexOf( token ) != -1 ) { // part of the radical character
                    addBold( token );
                } else if ( n == 0 ) { // +<tag>  ->   <tag>+</tag>
                    addLink( token.substring( 1 ), '</' + token.substring( 2 ), token.substring( 1 ) );
                } else if ( n > 0 ) { // <tag>+</tag>
                    addLink( token.substring( 0, n ), token.substring( n+1 ) );
                } else if ( token.length > 2 && token.charCodeAt( 0 ) > 127 ) { // a string of insertable characters
                    for ( var j = 0; j < token.length; j++ ) {
                        addLink( token.charAt( j ), '' );
                    }
                } else {
                    addLink( token, '' );
                }
            }
            return;

            function addLink( tagOpen, tagClose, name ) {
                var handler;
                var dle = tagOpen.indexOf( '\x10' );
                if ( dle > 0 ) {
                    var path = tagOpen.substring( dle + 1 ).split( '.' );
                    tagOpen = tagOpen.substring( 0, dle );
                    handler = window;
                    for ( var i = 0; i < path.length; i++ ) {
                        handler = handler[path[i]];
                    }
                } else {
                    tagOpen = tagOpen.replace( /\./g,' ' );
                    tagClose = tagClose ? tagClose.replace( /_/g,' ' ) : '';
                    handler = new Function(
                        "evt",
                        "mw.toolbar.insertTags('" + tagOpen + "', '" + tagClose + "', ''); evt.preventDefault(); return false;"
                    );
                }
                var a = document.createElement( 'a' );
                name = name || tagOpen + tagClose;
                name = name.replace( /\\n/g,'' );
                a.appendChild( document.createTextNode( name ) );
                a.href = "#";
                $( a ).click( handler );
                paragraph.appendChild( a );
                addText( ' ' );
            }

            function addBold( text ) {
                var b = document.createElement( 'b' );
                b.appendChild( document.createTextNode( text.replace( /_/g,' ' ) ) );
                paragraph.appendChild( b );
                addText( ' ' );
            }
            function addText( txt ) {
                paragraph.appendChild( document.createTextNode( txt ) );
            }
        },

        last_active_textfield: null,

        registerTextField: function ( evt ) {
            var e = evt || window.event;
            var node = e.target || e.srcElement;
            if ( !node ) {
                return;
            }
            EditTools.last_active_textfield = node.id;
            return true;
        },

        getTextArea: function () {
            var txtarea = null;
            if ( EditTools.last_active_textfield && EditTools.last_active_textfield != "" )
                txtarea = document.getElementById( EditTools.last_active_textfield );
            if ( !txtarea ) {
                // Fallback option: old behaviour
                if ( document.editform ) {
                    txtarea = document.editform.wpTextbox1;
                } else {
                    // Some alternate form? Take the first one we can find
                    txtarea = document.getElementsByTagName( 'textarea' );
                    if ( txtarea.length > 0 ) {
                        txtarea = txtarea[0];
                    } else {
                        txtarea = null;
                    }
                }
            }
            return txtarea;
        },

        setup: function () {
            var placeholder;
            if ( $( '#editpage-specialchars' ).length ) {
                placeholder = $( '#editpage-specialchars' )[0];
            } else {
                placeholder = $( '<div id="editpage-specialchars"> </div>' ).prependTo( '.mw-editTools' )[0];
            }
            if ( !placeholder ) {
                return;
            }
            if ( !window.charinsertDontMove ) {
                $( '.editOptions' ).before( placeholder );
            }
            EditTools.createEditTools( placeholder );
            window.updateEditTools = function () {
                EditTools.createEditTools( $( '#editpage-specialchars' )[0] );
            };
        }

    }; // end EditTools

    EditTools.setup();
} );