User:Vadim/common.js

From CruisersWiki

< User:Vadim(Difference between revisions)
Jump to: navigation, search
 
(91 intermediate revisions not shown)
Line 1: Line 1:
-
function addCustomEditButton (imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
+
addOnloadHook(function () {  
-
mwCustomEditButtons[mwEditButtons.length] =
+
-
{"imageId": imageId,
+
-
"imageFile": imageFile,
+
-
"speedTip": speedTip,
+
-
"tagOpen": tagOpen,
+
-
"tagClose": tagClose,
+
-
"sampleText": sampleText};
+
-
}
+
-
addCustomEditButton("/images/7/70/Button_POI.png","Add POI","{{poi | lat= | lon=\n| type=\n| name=","\n| text=\n}}\n","","mw-editbutton-poi");
+
    if (wgAction == 'edit' || wgAction == 'submit') {
-
// deploy jQuery
+
        //if (true) {
 +
        if (/\.js$/.test(wgPageName) || /\.css$/.test(wgPageName) || /^Template:/.test(wgPageName)) {
-
(function () {
+
            // load Ace -- code editor -- https://ace.c9.io
-
var onloadFunctsNew = onloadFuncts;
+
            //var acePath = 'https://cdn.jsdelivr.net/npm/[email protected]/src-min-noconflict'
-
onloadFuncts = [];
+
            var acePath = 'https://cdn.jsdelivr.net/npm/[email protected]/src-noconflict'
-
var doneOnloadHookNew = false;
+
            var initCodeEditor = function () { // https://ace.c9.io/#nav=embedding
 +
                // https://github.com/ajaxorg/ace/wiki/Default-Keyboard-Shortcuts<
 +
                var $wpTextbox1 = $( '#wpTextbox1' );
 +
                var $aceEditor = $( "<div></div>" ).insertAfter( "#wpTextbox1" );
 +
                $aceEditor.height($wpTextbox1.height());
 +
                $aceEditor.text($wpTextbox1.text());
 +
                $wpTextbox1.hide();
 +
                $('#toolbar').hide();
 +
           
 +
                var editor = ace.edit($aceEditor[0]);
 +
                var session = editor.getSession();
 +
           
 +
                session.on(
 +
                    'change',
 +
                    function(e) {
 +
                        // e.type, etc
 +
                        $wpTextbox1.text(editor.getValue());
 +
                });
 +
           
 +
                //    editor.setTheme("ace/theme/monokai");
 +
                session.setUseSoftTabs(true);
 +
                session.setUseWrapMode(true);
 +
           
 +
                if (/\.js$/.test(wgPageName)) {
 +
                    session.setMode("ace/mode/javascript");
 +
                } else if (/\.css$/.test(wgPageName)) {
 +
                    session.setMode("ace/mode/css");
 +
                }
 +
               
 +
                ace.config.set('basePath', acePath )
-
var addOnloadHookOld = addOnloadHook;
+
                // add command to lazy-load keybinding_menu extension
-
addOnloadHook = function (hookFunct) {
+
                editor.commands.addCommand({
-
    // Allows add-on scripts to add onload functions
+
                    name: "showKeyboardShortcuts",
-
    if(!doneOnloadHookNew) {
+
                    bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
-
    onloadFunctsNew[onloadFunctsNew.length] = hookFunct;
+
                    exec: function(editor) {
-
    } else {
+
                        ace.config.loadModule("ace/ext/keybinding_menu", function(module) {
-
    hookFunct(); // bug in MSIE script loading
+
                            module.init(editor);
-
    }
+
                            editor.showKeyboardShortcuts()
-
}
+
                        })
 +
                    }
 +
                })               
 +
            };
 +
       
 +
            loadScript( acePath + '/ace.js', initCodeEditor ); //function () {});
 +
        } else {
-
var elem = importScriptURI("https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js");
+
            //loadScript('https://rawcdn.githack.com/kynikos/wiki-monkey/v4.2.1/dist/WikiMonkey-ArchWiki.js')
-
console.log('jQuery ' + typeof jQuery);
+
            // install [[Wikipedia:WikEd]] in-browser text editor
-
addOnloadHook(function() {console.log('OnloadHook jQuery ' + typeof jQuery  + ' ' + typeof $)});
+
-
if (elem) {
+
            // loadWikiScript('CruisersWiki:WikiEd.js');
-
    elem.onreadystatechange = function() {console.log('onreadystatechange jQuery ' + typeof jQuery)};
+
            loadScript('https://en.wikipedia.org/w/index.php?title=User:Cacycle/wikEd.js&action=raw&ctype=text/javascript');
-
 
+
-
    elem.onload = function() {
+
-
        console.log('onload jQuery ' + typeof jQuery  + ' ' + typeof $);
+
-
 
+
-
        if (typeof jQuery !== 'undefined') {
+
-
        $.ajaxSetup({
+
-
            cache: true
+
-
        });
+
-
        $(document).ready(function(){
+
-
            console.log('document ready');
+
-
            if (!doneOnloadHookNew) {
+
-
        doneOnloadHookNew = true;
+
-
        doneOnloadHook = false;
+
-
        onloadFuncts = onloadFunctsNew;
+
-
        runOnloadHook();
+
-
            }
+
-
        });
+
-
        }
+
-
    };
+
-
}
+
-
})();
+
-
 
+
-
function loadScript(url, callback) {
+
-
    addOnloadHook( function () {
+
-
var elem = importScriptURI(url);
+
-
if (elem) {
+
-
    callback && (elem.onload = callback);
+
-
        } else {
+
-
          callback && callback();
+
         }
         }
-
    });
 
-
}
 
-
 
-
// load code editor if required
 
-
addOnloadHook(function () { // https://ace.c9.io/#nav=embedding
 
-
    if ((wgAction == 'edit' || wgAction == 'submit') && /\.js$/.test(wgPageName)) {
 
-
        loadScript('https://cdn.jsdelivr.net/ace/1.2.3/min/ace.js', initCodeEditor); //function () {});
 
     }
     }
-
})
+
      
-
 
+
-
function initCodeEditor() {
+
-
     $wpTextbox1 = $( '#wpTextbox1' );
+
-
    $aceEditor = $( "<div></div>" ).insertAfter( "#wpTextbox1" );
+
-
    $aceEditor.height($wpTextbox1.height());
+
-
    $aceEditor.text($wpTextbox1.text());
+
-
    $wpTextbox1.hide();
+
-
 
+
-
    editor = ace.edit($aceEditor[0]);
+
-
 
+
-
    editor.getSession().on('change', function(e) {
+
-
    // e.type, etc
+
-
    $wpTextbox1.text(editor.getValue());
+
});
});
-
 
-
//    editor.setTheme("ace/theme/monokai");
 
-
    editor.getSession().setMode("ace/mode/javascript");
 
-
}
 

Latest revision as of 13:56, 30 August 2018

addOnloadHook(function () { 

    if (wgAction == 'edit' || wgAction == 'submit') {

        //if (true) {
        if (/\.js$/.test(wgPageName) || /\.css$/.test(wgPageName) || /^Template:/.test(wgPageName)) {

            // load Ace -- code editor -- https://ace.c9.io
            //var acePath = 'https://cdn.jsdelivr.net/npm/[email protected]/src-min-noconflict'
            var acePath = 'https://cdn.jsdelivr.net/npm/[email protected]/src-noconflict'
            var initCodeEditor = function () { // https://ace.c9.io/#nav=embedding
                // https://github.com/ajaxorg/ace/wiki/Default-Keyboard-Shortcuts<
                var $wpTextbox1 = $( '#wpTextbox1' );
                var $aceEditor = $( "<div></div>" ).insertAfter( "#wpTextbox1" );
                $aceEditor.height($wpTextbox1.height());
                $aceEditor.text($wpTextbox1.text());
                $wpTextbox1.hide();
                $('#toolbar').hide();
            
                var editor = ace.edit($aceEditor[0]);
                var session = editor.getSession();
            
                session.on(
                    'change', 
                    function(e) {
                        // e.type, etc
                        $wpTextbox1.text(editor.getValue());
                });
            
                //    editor.setTheme("ace/theme/monokai");
                session.setUseSoftTabs(true);
                session.setUseWrapMode(true);
            
                if (/\.js$/.test(wgPageName)) {
                    session.setMode("ace/mode/javascript");
                } else if (/\.css$/.test(wgPageName)) {
                    session.setMode("ace/mode/css");
                }
                
                ace.config.set('basePath', acePath )

                // add command to lazy-load keybinding_menu extension
                editor.commands.addCommand({
                    name: "showKeyboardShortcuts",
                    bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
                    exec: function(editor) {
                        ace.config.loadModule("ace/ext/keybinding_menu", function(module) {
                            module.init(editor);
                            editor.showKeyboardShortcuts()
                        })
                    }
                })                
            };
        
            loadScript( acePath + '/ace.js', initCodeEditor ); //function () {});
        } else {

            //loadScript('https://rawcdn.githack.com/kynikos/wiki-monkey/v4.2.1/dist/WikiMonkey-ArchWiki.js')

            // install [[Wikipedia:WikEd]] in-browser text editor

            // loadWikiScript('CruisersWiki:WikiEd.js');
            loadScript('https://en.wikipedia.org/w/index.php?title=User:Cacycle/wikEd.js&action=raw&ctype=text/javascript');
        }
    }
    
});
Personal tools
advertisement
Friends of Cruisers Wiki