forked from TypeFox/monaco-languageclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapperLangium.ts
95 lines (85 loc) · 3.29 KB
/
wrapperLangium.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2024 TypeFox and others.
* Licensed under the MIT License. See LICENSE in the package root for license information.
* ------------------------------------------------------------------------------------------ */
import { MonacoEditorLanguageClientWrapper } from 'monaco-editor-wrapper';
import { setupLangiumClientExtended } from './config/extendedConfig.js';
import { setupLangiumClientClassic } from './config/classicConfig.js';
import { useWorkerFactory } from 'monaco-editor-wrapper/workerFactory';
import workerUrl from './worker/langium-server?worker&url';
let wrapper: MonacoEditorLanguageClientWrapper | undefined;
let extended = false;
const htmlElement = document.getElementById('monaco-editor-root');
export const configureMonacoWorkers = () => {
useWorkerFactory({
ignoreMapping: true,
workerLoaders: {
editorWorkerService: () => new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker.js', import.meta.url), { type: 'module' }),
}
});
};
export const runLangiumDslWrapper = async () => {
try {
document.querySelector('#button-start-classic')?.addEventListener('click', startLangiumClientClassic);
document.querySelector('#button-start-extended')?.addEventListener('click', startLangiumClientExtended);
document.querySelector('#button-dispose')?.addEventListener('click', disposeEditor);
} catch (e) {
console.error(e);
}
};
export const startLangiumClientExtended = async () => {
try {
if (checkStarted()) return;
extended = true;
disableButton('button-start-classic', true);
disableButton('button-start-extended', true);
const config = await setupLangiumClientExtended();
wrapper = new MonacoEditorLanguageClientWrapper();
wrapper.initAndStart(config, htmlElement);
} catch (e) {
console.log(e);
}
};
export const startLangiumClientClassic = async () => {
try {
if (checkStarted()) return;
disableButton('button-start-classic', true);
disableButton('button-start-extended', true);
const config = await setupLangiumClientClassic();
wrapper = new MonacoEditorLanguageClientWrapper();
await wrapper.initAndStart(config, htmlElement!);
} catch (e) {
console.log(e);
}
};
const checkStarted = () => {
if (wrapper?.isStarted() ?? false) {
alert('Editor was already started!\nPlease reload the page to test the alternative editor.');
return true;
}
return false;
};
const disableButton = (id: string, disabled: boolean) => {
const button = document.getElementById(id) as HTMLButtonElement | null;
if (button !== null) {
button.disabled = disabled;
}
};
export const disposeEditor = async () => {
if (!wrapper) return;
wrapper.reportStatus();
await wrapper.dispose();
wrapper = undefined;
if (extended) {
disableButton('button-start-extended', false);
} else {
disableButton('button-start-classic', false);
}
};
export const loadLangiumWorker = () => {
console.log(`Langium worker URL: ${workerUrl}`);
return new Worker(workerUrl, {
type: 'module',
name: 'Langium LS',
});
};