code-editor.ts
Home
/
codeeditor-app /
elements /
code-editor /
code-editor.ts
import { BaseElement } from "../../../shared/_base";
import './code-editor.less';
import * as monaco from 'monaco-editor';
import { OutputFrame } from "../output-frame/output-frame";
import { debounceManager } from "../../../shared/ensure-debounce";
export class CodeEditor extends BaseElement {
public input: string = "";
public language: string = "";
onInit(): void {
this.initWorkers();
this.input = this.setInitInput();
this.language = this.tagName.substring(0, this.tagName.indexOf("-")).toLocaleLowerCase();
this.setInitSettings();
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.setContent(this.language, this.input);
});
});
this.waitFor(this.outputFrame, () => {
this.outputFrame.setContent(this.language, this.input);
});
}
setInitInput(): string {
return "";
}
setInitSettings(): void {
}
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");
}
}