-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonaco-kusto.js
50 lines (45 loc) · 1.44 KB
/
monaco-kusto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { monaco } from '@monaco-editor/react';
/**
* A promise to be returned by the init function.
*/
let promiseResolve = null;
const monacoKustoInitPromise = new Promise((resolve) => {
promiseResolve = resolve;
});
/**
* when monaco is loaded, it also loads a UMD module loader. the following script tag will load monaco kusto using that loader.
*/
const loadMonacoKusto = () => {
const script = document.createElement('script');
script.innerHTML = `require(['vs/language/kusto/monaco.contribution'], function() {
document.dispatchEvent(new Event('kusto_init'));
});
`;
return document.body.appendChild(script);
};
/**
* Configuring monaco's UMD loader to load all the rest of monaco (and monaco-kusto) from the following location (that's why we're copying everything to public folder).
*/
monaco.config({
paths: {
vs: `${process.env.PUBLIC_URL}/monaco-editor/min/vs`,
},
});
/**
* remove evnent listener and resolve init promise once script is loaded.
*/
const onMonacoKustoLoaded = () => {
document.removeEventListener('kusto_init', onMonacoKustoLoaded);
promiseResolve(window.monaco);
};
/**
* import monaco and monaco-kusto.
* @returns a promise that will be resolved once all dependencies are loaded.
*/
export const init = () => {
monaco.init().then(() => {
document.addEventListener('kusto_init', onMonacoKustoLoaded);
loadMonacoKusto();
});
return monacoKustoInitPromise;
};