forked from TypeFox/monaco-languageclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactPython.tsx
52 lines (47 loc) · 2.33 KB
/
reactPython.tsx
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
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2024 TypeFox and others.
* Licensed under the MIT License. See LICENSE in the package root for license information.
* ------------------------------------------------------------------------------------------ */
import * as vscode from 'vscode';
import { RegisteredFileSystemProvider, registerFileSystemOverlay, RegisteredMemoryFile } from '@codingame/monaco-vscode-files-service-override';
import React, { StrictMode } from 'react';
import ReactDOM from 'react-dom/client';
import type { TextChanges } from '@typefox/monaco-editor-react';
import { MonacoEditorReactComp } from '@typefox/monaco-editor-react';
import { useWorkerFactory } from 'monaco-editor-wrapper/workerFactory';
import { MonacoEditorLanguageClientWrapper } from 'monaco-editor-wrapper';
import { createUserConfig } from './config.js';
import badPyCode from './bad.py?raw';
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 runPythonReact = async () => {
const badPyUri = vscode.Uri.file('/workspace/bad.py');
const fileSystemProvider = new RegisteredFileSystemProvider(false);
fileSystemProvider.registerFile(new RegisteredMemoryFile(badPyUri, badPyCode));
registerFileSystemOverlay(1, fileSystemProvider);
const onTextChanged = (textChanges: TextChanges) => {
console.log(`Dirty? ${textChanges.isDirty}\ntext: ${textChanges.main}\ntextOriginal: ${textChanges.original}`);
};
const htmlElement = document.getElementById('monaco-editor-root');
const comp = <MonacoEditorReactComp
userConfig={createUserConfig('/workspace', badPyCode, '/workspace/bad.py')}
style={{
'paddingTop': '5px',
'height': '80vh'
}}
onTextChanged={onTextChanged}
onLoad={(wrapper: MonacoEditorLanguageClientWrapper) => {
console.log(`Loaded ${wrapper.reportStatus().join('\n').toString()}`);
}}
onError={(e) => {
console.error(e);
}}
/>;
ReactDOM.createRoot(htmlElement!).render(<StrictMode>{comp}</StrictMode>);
};