Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turned react component from class to function #739

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/examples/react_python.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

<body>
<h2>React: Python Language Client & Language Server (Web Socket)</h2>
<button type="button" id="button-start">Start</button>
<label>Enable Strict mode:</label><input type="checkbox" id="checkbox-strictmode" />
<button type="button" id="button-dispose">Dispose</button>
<div id="monaco-editor-root"></div>
<script type="module">
import { configureMonacoWorkers, runPythonReact } from './src/python/client/reactPython.tsx';
Expand Down
3 changes: 3 additions & 0 deletions packages/examples/react_statemachine.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

<body>
<h2>React: Langium Statemachine Language Client & Language Server (Worker)</h2>
<button type="button" id="button-start">Start</button>
<label>Enable Strict mode:</label><input type="checkbox" id="checkbox-strictmode" />
<button type="button" id="button-dispose">Dispose</button>
<div id="monaco-editor-root"></div>
<script type="module">
import { configureMonacoWorkers, runStatemachineReact } from './src/langium/statemachine/main-react.tsx';
Expand Down
73 changes: 40 additions & 33 deletions packages/examples/src/langium/statemachine/main-react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See LICENSE in the package root for license information.
* ------------------------------------------------------------------------------------------ */

import React from 'react';
import React, { StrictMode, useEffect, useState } from 'react';
import ReactDOM from 'react-dom/client';
import { MonacoEditorReactComp } from '@typefox/monaco-editor-react';
import { useWorkerFactory } from 'monaco-editor-wrapper/workerFactory';
Expand All @@ -21,40 +21,47 @@ export const configureMonacoWorkers = () => {
};

export const runStatemachineReact = async () => {
const langiumGlobalConfig = await createLangiumGlobalConfig({
languageServerId: 'react',
useLanguageClient: true,
text,
worker: loadStatemachineWorkerRegular()
});

try {
const langiumGlobalConfig = await createLangiumGlobalConfig({
languageServerId: 'react',
useLanguageClient: true,
text,
worker: loadStatemachineWorkerRegular()
});
const styles = {
'paddingTop': '5px',
'height': '80vh'
};
const comp = <MonacoEditorReactComp
userConfig={langiumGlobalConfig}
style={styles}
/>;

const htmlElement = document.getElementById('monaco-editor-root');
ReactDOM.createRoot(htmlElement!).render(comp);

// container comp around MonacoEditorReactComp

// app comp
// => MonacoEditorReactComp

// use
setTimeout(() => {
console.log('Updating styles');
styles.height = '85vh';
if (langiumGlobalConfig.wrapperConfig.editorAppConfig.codeResources !== undefined) {
if (langiumGlobalConfig.wrapperConfig.editorAppConfig.codeResources.main !== undefined) {
langiumGlobalConfig.wrapperConfig.editorAppConfig.codeResources.main.text = 'tester';
}
document.querySelector('#button-start')?.addEventListener('click', async () => {
const htmlElement = document.getElementById('monaco-editor-root');
const App = () => {

const [ height, setHeight ] = useState('80vh');

useEffect(() => {
const timer = setTimeout(() => {
console.log('Updating styles');
setHeight('85vh');
}, 2000);

return () => clearTimeout(timer);
}, []);

return (
<div style={{ 'height': height }} >
<MonacoEditorReactComp
style={{ 'height': '100%' }}
userConfig={langiumGlobalConfig} />
</div>
);
};
const strictMode = (document.getElementById('checkbox-strictmode')! as HTMLInputElement).checked;
if (strictMode) {
ReactDOM.createRoot(htmlElement!).render(<StrictMode><App /></StrictMode>);
} else {
ReactDOM.createRoot(htmlElement!).render(<App />);
}
}, 2000);
});
document.querySelector('#button-dispose')?.addEventListener('click', () => {

});
} catch (e) {
console.error(e);
}
Expand Down
53 changes: 36 additions & 17 deletions packages/examples/src/python/client/reactPython.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,42 @@ export const runPythonReact = async () => {
registerFileSystemOverlay(1, fileSystemProvider);

const onTextChanged = (textChanges: TextChanges) => {
console.log(`Dirty? ${textChanges.isDirty}\ntext: ${textChanges.main}\ntextOriginal: ${textChanges.original}`);
console.log(`Dirty? ${textChanges.isDirty}\ntext: ${textChanges.text}\ntextOriginal: ${textChanges.textOriginal}`);
};

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>);
const root = ReactDOM.createRoot(htmlElement!);

try {
document.querySelector('#button-start')?.addEventListener('click', async () => {
const App = () => {
return (
<div style={{ 'height': '80vh', padding: '5px' }} >
<MonacoEditorReactComp
userConfig={createUserConfig('/workspace', badPyCode, '/workspace/bad.py')}
style={{ 'height': '100%' }}
onTextChanged={onTextChanged}
onLoad={(wrapper: MonacoEditorLanguageClientWrapper) => {
console.log(`Loaded ${wrapper.reportStatus().join('\n').toString()}`);
}}
onError={(e) => {
console.error(e);
}} />
</div>
);
};

const strictMode = (document.getElementById('checkbox-strictmode')! as HTMLInputElement).checked;

if (strictMode) {
root.render(<StrictMode><App /></StrictMode>);
} else {
root.render(<App />);
}
});
document.querySelector('#button-dispose')?.addEventListener('click', () => {
root.render([]);
});
} catch (e) {
console.error(e);
}
};
Loading