Skip to content

Commit

Permalink
Use ResizeObserver in MonacoEditor component (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
meyer committed Dec 15, 2022
1 parent 40992f3 commit 764ceff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 11 additions & 6 deletions websqrl/src/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,17 @@ export const MonacoEditor: React.FC<MonacoEditorProps> = ({

editorRef.current = editor;

// TODO(meyer) debounce this
const resizeHandler = () => {
editor.layout();
};
const resizeObserver = new ResizeObserver((entries) => {
const containerElement = entries.find(
(entry) => entry.target === containerRef.current
);
// container was resized
if (containerElement) {
editor.layout();
}
});

window.addEventListener("resize", resizeHandler);
resizeObserver.observe(containerRef.current);

const onChangeModelContentSubscription = editor.onDidChangeModelContent(
(event) => {
Expand All @@ -331,7 +336,7 @@ export const MonacoEditor: React.FC<MonacoEditorProps> = ({
editor.dispose();
model.dispose();
onChangeModelContentSubscription.dispose();
window.removeEventListener("resize", resizeHandler);
resizeObserver.disconnect();
};
}, [monacoEditorObj.state, sqrlFunctions]);

Expand Down
3 changes: 2 additions & 1 deletion websqrl/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"sqrl-redis-functions": ["../packages/sqrl-redis-functions"],
"sqrl-test-utils": ["../packages/sqrl-test-utils"],
"sqrl-text-functions": ["../packages/sqrl-text-functions"]
}
},
"noEmit": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"references": [
Expand Down

0 comments on commit 764ceff

Please sign in to comment.