code-editor.ts

130 lines | 4.76 kB Blame History Raw Download
import { BaseElement } from "../../../shared/_base";
import './code-editor.less';
import * as monaco from 'monaco-editor';
import { OutputFrame } from "../output-frame/output-frame";
import initJs from '!!raw-loader!./injects/editor-init.js';
import initHtml from '!!raw-loader!./injects/editor-init.html';
import initCss from '!!raw-loader!./injects/editor-init.css';
import spiralBoxesSolutionScript from '!!raw-loader!./injects/spiral-boxes-solution.js';
import { debounceManager } from "../../../shared/ensure-debounce";
import declarations from '!!raw-loader!./injects/declarations.d.ts';

export class CodeEditor extends BaseElement {

    public input: string = "";
    public language: string = "";

    onInit(): void {

        this.initWorkers();
        this.language = this.getAttribute("language")?.toLocaleLowerCase() ?? "javascript";
        this.input = this.setInitInput(this.language);

        const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)");

        var editor = monaco.editor.create(this, {
            value: this.input,
            language: this.language,
            automaticLayout: true,
            contextmenu: false,
            minimap: {
                enabled: false
            },
            autoIndent: "full",
            theme: prefersDarkScheme.matches ? 'vs-dark' : 'vs-light'
        });




        var debounce = debounceManager(1000);
        editor.onDidChangeModelContent((e) => {
            this.input = editor.getValue();
            debounce.ensureDebounce(() => {
                this.outputFrame.setScript(this.language, this.input);
            });
        });

        this.waitFor(this.outputFrame, () => {
            this.outputFrame.setScript(this.language, this.input);
        });

    }

    setInitInput(language: string | null) {
        if (language) {
            if (language.toLowerCase() === "javascript") {
                this.initJs();
                return initJs;
            }
            else if (language.toLowerCase() === 'html') {
                return initHtml;
            }
            else if (language.toLowerCase() === 'css') {
                return initCss;
            }
        }
        return "";
    }

    initJs() {
        monaco.languages.typescript.javascriptDefaults.addExtraLib(declarations, 'ts:filename/declarations.d.ts');

        monaco.languages.registerCompletionItemProvider("javascript", {
            provideCompletionItems: (model, position, context, token) => {
                return {
                    suggestions: [
                        {
                            label: 'for',
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: 'for (let i = 0; i < ${1:array}.length; i++) {\n\t$0\n}',
                            range: <any>null,
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'forr',
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: 'for (var i = ${1:array}.length - 1; i >= 0; i--) {\n\t$0\n}',
                            range: <any>null,
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'spiralBoxesSolutionSnippet',
                            detail: "inserts solution of spiral boxes",
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: spiralBoxesSolutionScript,
                            range: <any>null,
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        }
                    ]
                }
            }
        });
    }


    initWorkers() {
        // @ts-ignore
        self.MonacoEnvironment = {
            getWorkerUrl: function (moduleId: any, label: string) {
                if (label === 'json') {
                    return './json.worker.bundle.js';
                }
                if (label === 'css' || label === 'scss' || label === 'less') {
                    return './css.worker.bundle.js';
                }
                if (label === 'html' || label === 'handlebars' || label === 'razor') {
                    return './html.worker.bundle.js';
                }
                if (label === 'typescript' || label === 'javascript') {
                    return './ts.worker.bundle.js';
                }
                return './editor.worker.bundle.js';
            }
        };
    }

    get outputFrame(): OutputFrame {
        return <OutputFrame>this.find("output-frame");
    }
}