Skip to content

Commit b648e00

Browse files
authored
Merge pull request #187 from Open-STEM/kq-bugs
addressed issue #186
2 parents 26717dd + 2a9b494 commit b648e00

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

src/components/MonacoEditor.tsx

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import '@codingame/monaco-vscode-python-default-extension';
22
import '@codingame/monaco-vscode-theme-defaults-default-extension';
3-
import { useEffect, useMemo, useRef } from 'react';
3+
import { useEffect, useRef, useState } from 'react';
44
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
55
import { initialize } from 'vscode/services';
66
import getLanguagesServiceOverride from '@codingame/monaco-vscode-languages-service-override';
@@ -131,25 +131,37 @@ type MonacoEditorProps = {
131131

132132
const MonacoEditor = ({
133133
name,
134-
width = '100vw',
135-
height = '100vh',
136134
language = 'python',
137135
value,
138136
className,
139137
}: MonacoEditorProps) => {
140138
const { t } = useTranslation();
141139
const containerRef = useRef<HTMLDivElement | null>(null);
140+
const editorRef = useRef<HTMLDivElement | null>(null);
142141
const editor = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
142+
const [childWidth, setChildWidth] = useState(0);
143+
const [childHeight, setChildHeight] = useState(0);
143144

144-
const fixedWidth = width;
145-
const fixedHeight = height;
146-
const style = useMemo(
147-
() => ({
148-
width: fixedWidth,
149-
height: fixedHeight,
150-
}),
151-
[fixedWidth, fixedHeight],
152-
);
145+
useEffect(() => {
146+
if (containerRef.current) {
147+
const resizeObserver = new ResizeObserver((entries) => {
148+
// we only expect one element to be observed
149+
if (!entries || entries.length === 0) {
150+
return;
151+
}
152+
const {clientWidth, clientHeight} = entries[0].target as HTMLDivElement;
153+
setChildWidth(clientWidth);
154+
setChildHeight(clientHeight);
155+
if (editor.current) {
156+
editor.current.layout({ width: clientWidth, height: clientHeight });
157+
}
158+
});
159+
resizeObserver.observe(containerRef.current);
160+
return () => {
161+
resizeObserver.disconnect();
162+
};
163+
}
164+
}, []);
153165

154166
useEffect(() => {
155167
/**
@@ -181,7 +193,7 @@ const MonacoEditor = ({
181193
console.log('Editor Content', content);
182194
const loadContent = JSON.parse(content);
183195
if (loadContent.name !== name) return;
184-
if (containerRef.current && editor.current) {
196+
if (editorRef.current && editor.current) {
185197
const model = monaco.editor.createModel(loadContent.content, languageId);
186198
editor.current.setModel(model);
187199
}
@@ -219,14 +231,14 @@ const MonacoEditor = ({
219231
EditorMgr.getInstance().setSubscription(name);
220232
}
221233

222-
if (containerRef.current) {
234+
if (editorRef.current && childWidth > 0 && childHeight > 0) {
223235
if (editor.current === null) {
224236
updateUserConfiguration(`{
225237
"editor.fontSize": ${Constants.DEFAULT_FONTSIZE},
226238
"workbench.colorTheme": "${AppMgr.getInstance().getTheme() === Themes.DARK ? 'Default Dark Modern' : 'vs'}"
227239
}`);
228240

229-
editor.current = monaco.editor.create(containerRef.current, {
241+
editor.current = monaco.editor.create(editorRef.current, {
230242
value: value,
231243
language: language,
232244
});
@@ -277,9 +289,15 @@ const MonacoEditor = ({
277289
}
278290
}
279291
}
280-
}, [name, language, value, t]);
292+
}, [name, language, value, t, childWidth, childHeight]);
281293

282-
return <div ref={containerRef} style={style} className={className} />;
294+
return (
295+
<>
296+
<div ref={containerRef} className='w-full h-full'>
297+
<div ref={editorRef} style={{ width: childWidth, height: childHeight }} className={className} />
298+
</div>
299+
</>
300+
);
283301
};
284302

285303
export default MonacoEditor;

0 commit comments

Comments
 (0)