43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
require.config({ paths: { 'vs': 'https://unpkg.com/monaco-editor@0.8.3/min/vs' }});
|
|
window.MonacoEnvironment = { getWorkerUrl: () => proxy };
|
|
window.code = '';
|
|
|
|
let proxy = URL.createObjectURL(new Blob([`
|
|
self.MonacoEnvironment = {
|
|
baseUrl: 'https://unpkg.com/monaco-editor@0.8.3/min/'
|
|
};
|
|
importScripts('https://unpkg.com/monaco-editor@0.8.3/min/vs/base/worker/workerMain.js');
|
|
`], { type: 'text/javascript' }));
|
|
|
|
require(["vs/editor/editor.main"], function () {
|
|
|
|
let init_lang = 'python';
|
|
|
|
let editor = monaco.editor.create(document.getElementById('container'), {
|
|
value: '# put code here',
|
|
language: init_lang,
|
|
theme: 'vs-dark'
|
|
});
|
|
|
|
editor.addListener('didType', () => {
|
|
window.code = editor.getValue();
|
|
});
|
|
|
|
monaco.languages.getLanguages().forEach(x => {
|
|
let el = document.createElement('option');
|
|
el.id = x.id;
|
|
el.innerText = x.id;
|
|
|
|
if (x.id == init_lang) el.selected = true;
|
|
|
|
document.getElementById('lang').appendChild(el);
|
|
})
|
|
|
|
document.getElementById('lang').onchange = (e) => {
|
|
monaco.editor.setModelLanguage(editor.getModel(), e.target.value)
|
|
}
|
|
|
|
document.getElementById('run').onclick = (e) => {
|
|
executeCode(window.code, editor.getModel().getLanguageIdentifier().language)
|
|
}
|
|
}); |