User:Fumple/vector.js

From Inkipedia, the Splatoon wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
if(["Scribunto", "javascript", "css"].includes(mw.config.get("wgPageContentModel")) && ["view", "edit", "submit"].includes(mw.config.get("wgAction"))){
    //Load editor (https://github.com/microsoft/monaco-editor/tree/main)
    $("head").append('<link rel="stylesheet" data-name="vs/editor/editor.main" href="https://microsoft.github.io/monaco-editor/node_modules/monaco-editor/min/vs/editor/editor.main.css" />');
    $("body").append([
        '<script>',
        '    var require = { paths: { vs: "https://microsoft.github.io/monaco-editor/node_modules/monaco-editor/min/vs" } };',
        '</script>',
        '<script src="https://microsoft.github.io/monaco-editor/node_modules/monaco-editor/min/vs/loader.js"></script>',
        '<script src="https://microsoft.github.io/monaco-editor/node_modules/monaco-editor/min/vs/editor/editor.main.nls.js"></script>',
        '<script src="https://microsoft.github.io/monaco-editor/node_modules/monaco-editor/min/vs/editor/editor.main.js"></script>'
    ].join("\n"));
    var fplEditorCheck = setInterval(function (){
        if(($("#wpTextbox1").length != 0 || $("pre.mw-code").length != 0) && typeof monaco != "undefined"){
            clearInterval(fplEditorCheck);
            var editable = $("#wpTextbox1").length != 0;
            if(!editable){
                $("pre.mw-code").after('<div id="vscodeeditor"></div>');
                $("pre.mw-code").css("display", "none");
                $("#vscodeeditor").css("height", "700px");
            } else {
                $("#wpTextbox1").after('<div id="vscodeeditor"></div>');
                $("#wpTextbox1").css("display", "none");
                $("#wpTextbox1").attr("readonly",true);
                $("#vscodeeditor").css("height", "700px");
            }
            new mw.Api().get({
                "action": "query",
                "format": "json",
                "list": "gadgets",
                "gaprop": "id",
                "gaenabledonly": 1
            }).then(function(data){
                window.fplEditor = monaco.editor.create(document.getElementById('vscodeeditor'), {
                    value: editable ? $("#wpTextbox1").val() : $("pre.mw-code").text(),
                    language: {"Scribunto":"lua", "javascript":"javascript", "css":"css"}[mw.config.get("wgPageContentModel")],
                    theme: data.query.gadgets.map(function(el){return el.id}).includes("dark-vector") ? "vs-dark" : "vs",
                    automaticLayout: true,
                    readOnly: !editable
                });
                window.fplEditor.onDidChangeModelContent(function (e) {
                    $("#wpTextbox1").val(window.fplEditor.getValue())
                });
            }).catch(function(ex){
                alert("An error occurred, check the console for more details");
                console.error(ex);
                $("#wpTextbox1").css("display", "");
                $("#wpTextbox1").attr("readonly",null);
                $("pre.mw-code").css("display", "");
                $("#vscodeeditor").remove();
            })
        }
    }, 100)
}