|
| 1 | +<template> |
| 2 | + <div :style="$attrs.style" /> |
| 3 | +</template> |
| 4 | +<script> |
| 5 | +import loader from '@monaco-editor/loader'; |
| 6 | +import loadMonacoKusto from '@/helpers/loadMonacoKusto'; |
| 7 | +import { getKustoSchema } from '@/helpers/apiClient'; |
| 8 | +
|
| 9 | +export default { |
| 10 | + name: 'KustoMonacoEditor', |
| 11 | + model: { |
| 12 | + event: 'change', |
| 13 | + }, |
| 14 | + props: { |
| 15 | + options: { |
| 16 | + default: () => {}, |
| 17 | + type: Object, |
| 18 | + }, |
| 19 | + value: { |
| 20 | + type: String, |
| 21 | + required: true, |
| 22 | + }, |
| 23 | + cluster: { |
| 24 | + type: String, |
| 25 | + default: null, |
| 26 | + }, |
| 27 | + database: { |
| 28 | + type: String, |
| 29 | + default: null, |
| 30 | + }, |
| 31 | + }, |
| 32 | + data: () => ({ |
| 33 | + monaco: null, |
| 34 | + editor: null, |
| 35 | + }), |
| 36 | + watch: { |
| 37 | + value(newValue) { |
| 38 | + if (this.editor && this.value !== this.editor.getValue()) { |
| 39 | + this.editor.setValue(newValue); |
| 40 | + } |
| 41 | + }, |
| 42 | + cluster(newCluster, oldCluster) { |
| 43 | + if (newCluster && oldCluster !== newCluster) { |
| 44 | + this.loadSchema(); |
| 45 | + } |
| 46 | + }, |
| 47 | + database(newDatabase, oldDatabase) { |
| 48 | + if (newDatabase && oldDatabase !== newDatabase) { |
| 49 | + this.loadSchema(); |
| 50 | + } |
| 51 | + }, |
| 52 | + }, |
| 53 | + mounted() { |
| 54 | + this.initialiseMonaco(); |
| 55 | + }, |
| 56 | + methods: { |
| 57 | + async initialiseMonaco() { |
| 58 | + loader.config({ |
| 59 | + paths: { |
| 60 | + vs: `${import.meta.env.BASE_URL}monaco-editor/min/vs`, |
| 61 | + }, |
| 62 | + }); |
| 63 | +
|
| 64 | + const monaco = await loader.init(); |
| 65 | + this.monaco = monaco; |
| 66 | +
|
| 67 | + await loadMonacoKusto(); |
| 68 | + console.log('Loaded monaco kusto.'); |
| 69 | +
|
| 70 | + this.editor = monaco.editor.create( |
| 71 | + this.$el, |
| 72 | + { |
| 73 | + value: this.value, |
| 74 | + language: 'kusto', |
| 75 | + ...this.options, |
| 76 | + }, |
| 77 | + ); |
| 78 | + this.editor.onDidChangeModelContent(() => { |
| 79 | + const value = this.editor.getValue(); |
| 80 | + if (this.value !== value) { |
| 81 | + this.$emit('change', value); |
| 82 | + } |
| 83 | + }); |
| 84 | + }, |
| 85 | + async loadSchema() { |
| 86 | + if (this.cluster === '' || this.database === '') { |
| 87 | + return; |
| 88 | + } |
| 89 | +
|
| 90 | + const schema = await this.parseSchema(); |
| 91 | + const workerAccessor = await this.monaco.languages.kusto.getKustoWorker(); |
| 92 | + const model = this.editor.getModel(); |
| 93 | + const worker = await workerAccessor(model.uri); |
| 94 | + await worker.setSchemaFromShowSchema( |
| 95 | + schema, |
| 96 | + this.cluster, |
| 97 | + this.database, |
| 98 | + ); |
| 99 | + }, |
| 100 | + async parseSchema() { |
| 101 | + const schema = await getKustoSchema(this.cluster, this.database); |
| 102 | + return JSON.parse(schema?.data?.at(0)?.ClusterSchema); |
| 103 | + }, |
| 104 | + }, |
| 105 | +}; |
| 106 | +</script> |
0 commit comments