code-editor.ts

65 lines | 1.835 kB Blame History Raw Download
import { BaseElement } from "../_base";
import './code-editor.less';
import * as monaco from 'monaco-editor';
import { OutputFrame } from "../output-frame/output-frame";
import initScript from './injects/editor-init.txt';

export class CodeEditor extends BaseElement {

    public input: string = initScript;

    onInit(): void {

        
        this.initWorkers();

        var editor = monaco.editor.create(this, {
            value: this.input,
            language: 'javascript',
            automaticLayout: true,
            contextmenu: false,
            minimap: {
                enabled: false
            }
        });

        editor.onDidChangeModelContent((e) => {
            this.input = editor.getValue();
            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';
            }
        };
    }

    onUpdate(): void {
        console.log("APP ROOT UPDATE");
    }

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