Skip to content

Commit

Permalink
Feature: Add compatibility for loading Monaco Editor via ESM (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackw authored Jan 25, 2024
1 parent bcd76ad commit 098eefb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// generally used by snapshots, but can affect specific tests
process.env.TZ = 'UTC';

// Jest configuration provided by Grafana scaffolding
module.exports = {
// Jest configuration provided by Grafana scaffolding
...require('./.config/jest.config'),
};
41 changes: 32 additions & 9 deletions src/components/QueryEditor/OpenAIEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,38 @@ export const OpenAIEditor: React.FC<RawQueryEditorProps> = (props) => {
signatureHelpTriggerCharacters: ['(', ')'],
provideSignatureHelp: getSignatureHelp,
});
monaco.languages['kusto']
.getKustoWorker()
.then((kusto) => {
const model = editor.getModel();
return model && kusto(model.uri);
})
.then((worker) => {
setWorker(worker);
});

const model = editor.getModel();

// Handle cases where kusto is already loaded or will be loaded via AMD
if (monaco.languages['kusto'] && monaco.languages['kusto'].getKustoWorker) {
monaco.languages['kusto']
.getKustoWorker()
.then((kusto) => {
return model && kusto(model.uri);
})
.then((worker) => {
setWorker(worker);
});
} else {
// Handle cases where kusto should be loaded via ESM web worker (Grafana 10.3.x)
if ('System' in window) {
window.System.import('@kusto/monaco-kusto').then((kustoModule) => {
if (kustoModule && kustoModule.getKustoWorker) {
kustoModule
.getKustoWorker()
.then((workerAccessor) => {
return model && workerAccessor(model.uri);
})
.then((worker) => {
setWorker(worker);
});
} else {
console.log('kusto monaco language failed to load.');
}
});
}
}
};

useEffect(() => {
Expand Down
41 changes: 32 additions & 9 deletions src/components/QueryEditor/RawQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,38 @@ export const RawQueryEditor: React.FC<RawQueryEditorProps> = (props) => {
signatureHelpTriggerCharacters: ['(', ')'],
provideSignatureHelp: getSignatureHelp,
});
monaco.languages['kusto']
.getKustoWorker()
.then((kusto) => {
const model = editor.getModel();
return model && kusto(model.uri);
})
.then((worker) => {
setWorker(worker);
});

const model = editor.getModel();

// Handle cases where kusto is already loaded or will be loaded via AMD
if (monaco.languages['kusto'] && monaco.languages['kusto'].getKustoWorker) {
monaco.languages['kusto']
.getKustoWorker()
.then((kusto) => {
return model && kusto(model.uri);
})
.then((worker) => {
setWorker(worker);
});
} else {
// Handle cases where kusto should be loaded via ESM web worker (Grafana 10.3.x)
if ('System' in window) {
window.System.import('@kusto/monaco-kusto').then((kustoModule) => {
if (kustoModule && kustoModule.getKustoWorker) {
kustoModule
.getKustoWorker()
.then((workerAccessor) => {
return model && workerAccessor(model.uri);
})
.then((worker) => {
setWorker(worker);
});
} else {
console.log('kusto monaco language failed to load.');
}
});
}
}
};

useEffect(() => {
Expand Down
12 changes: 12 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export declare global {
interface Window {
System: {
import: (identifier: string) => Promise<Module>;
};
}

interface Module {
default?: any;
[exportName: string]: any;
}
}
2 changes: 1 addition & 1 deletion src/monaco/KustoMonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class KustoMonacoEditor extends React.Component<Props, MonacoState> {

if (!window.hasOwnProperty('monaco')) {
// using the standard import() here causes issues with webpack/babel/typescript because monaco is just so large
(window as any).System.import(`${props.pluginBaseUrl}/libs/monaco.min.js`).then(() => {
window.System.import(`${props.pluginBaseUrl}/libs/monaco.min.js`).then(() => {
setTimeout(() => {
this.initMonaco();
}, 1);
Expand Down
1 change: 0 additions & 1 deletion src/test/setupTests.ts

This file was deleted.

0 comments on commit 098eefb

Please sign in to comment.