Jump to content

User:Vanished user 1929210/test.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * AceEditor 維基百科條目版
 * Author: [[User:Vjudge1]]
 *
 * 已知bug:
 * 1. 工具欄按鈕全部失效
 * 2. 壓根就沒寫完
 *
 * 未與其他編輯器(wikEd等等)進行測試因此不知道是否兼容。
 */

(function() {
    'use strict';

    var $editorElem, editor;
    var $textBox;
    var aceMode = true;

    function init() {
        $textBox = $('#wpTextbox1');

        // 建立一個與文本框同樣大小的div,並將文字放進去
        $editorElem = $('<div id="wpEditor"></div>');
        $editorElem.text($textBox.val());
        $editorElem.height($textBox.height());

        // 替換文本框
        $textBox.before($editorElem);

        editor = ace.edit('wpEditor');
        editor.getSession().setUseWrapMode(true);

        $editorElem = $('#wpEditor');
        $editorElem.css('font', $('#wpTextbox1').css('font'));

        function setAceVisible(value) {
            if (value) {
                $('#wikiEditor-ui-toolbar').hide();
                $('#editpage-specialchars').hide();
                $textBox.hide();
                $editorElem.show();

                editor.setValue($textBox.val());
                editor.focus();
            } else {
                $('#wikiEditor-ui-toolbar').show();
                $('#editpage-specialchars').show();
                $textBox.show();
                $editorElem.hide();

                $textBox.focus();
            }
        }

        setAceVisible(true);

        // 添加切換按鈕
        $('#mw-editpage-watch').after('<input name="wpAceEditor" type="checkbox" value="1" checked="checked" id="wpAceEditor">&nbsp;<label for="wpAceEditor" id="mw-ace-editor">AceEditor</label>');
        $('#wpAceEditor').click(function () {
            setAceVisible(document.editform.wpAceEditor.checked);
        });

        // *** DEBUG ***
        window.editor = editor;

        aceMode = true;

        // 保持同步
        editor.getSession().on('change', function(e) {
            $textBox.val(editor.getValue());
        });
    }

    var wgAction = mw.config.get("wgAction");
    var layoutEngine = $.client.profile().layout;

    if ((wgAction == "edit" || wgAction == "submit") &&
        mw.config.get("wgPageContentModel") == "wikitext" &&
        layoutEngine != "trident" && layoutEngine != "edge") {

        // 加載Ace
        mw.loader.using('ext.codeEditor.ace', function () {
            init();
            config();
        });
    }
})();