random-web-tools/src/components/MonacoEditor.vue

64 lines
1.2 KiB
Vue
Raw Normal View History

2024-03-07 13:41:39 +02:00
<template>
2024-10-16 11:19:00 +03:00
<div :id="name"></div>
2024-03-07 13:41:39 +02:00
</template>
<script>
2024-10-16 11:19:00 +03:00
import loader from "@monaco-editor/loader";
2024-03-07 13:41:39 +02:00
let monacoResultInst = null;
export default {
name: "monacoEditor",
props: {
name: {
type: String,
required: true,
},
language: {
type: String,
2024-10-16 11:19:00 +03:00
default: "text",
2024-03-07 13:41:39 +02:00
},
value: {
type: String,
2024-10-16 11:19:00 +03:00
default: "",
2024-03-07 13:41:39 +02:00
},
readonly: {
type: Boolean,
default: true,
},
},
mounted() {
loader.init().then((monaco) => {
monacoResultInst = monaco;
monaco.editor.create(document.querySelector(`#${this.name}`), {
value: this.value,
2024-03-07 13:41:39 +02:00
language: this.language,
readOnly: this.readonly,
});
});
},
unmounted() {
if (monacoResultInst) {
2024-10-16 11:19:00 +03:00
monacoResultInst.editor.getModels().forEach((model) => model.dispose());
}
},
2024-03-07 13:41:39 +02:00
watch: {
value: {
// the callback will be called immediately after the start of the observation
immediate: true,
deep: true,
async handler(value, oldValue) {
while (!monacoResultInst) {
2024-10-16 11:19:00 +03:00
await new Promise((r) => setTimeout(r, 10));
2024-03-07 13:41:39 +02:00
}
monacoResultInst.editor.getModels()[0].setValue(value.toString());
2024-10-16 11:19:00 +03:00
},
},
},
2024-03-07 13:41:39 +02:00
};
</script>
2024-10-16 11:19:00 +03:00
<style scoped></style>