code-editor.ts

105 lines | 4.012 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 initScript from '!!raw-loader!./injects/editor-init.js';
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 = initScript;

    onInit(): void {

        this.initWorkers();

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

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

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

        monaco.languages.registerCompletionItemProvider("javascript", {
            triggerCharacters: ["."],
            provideCompletionItems: (model, position, context, token) => (
                {
                    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
                        }
                    ]
                }
            )
        });


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

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

    }

    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");
    }
}