Skip to content

Commit 16d0f08

Browse files
committed
Throttle resize events
1 parent db8af2f commit 16d0f08

File tree

2 files changed

+9
-11
lines changed
  • packages/app/src/app

2 files changed

+9
-11
lines changed

packages/app/src/app/components/CodeEditor/VSCode/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,11 +1049,9 @@ class MonacoEditor extends React.Component<Props> implements Editor {
10491049
};
10501050

10511051
resizeEditorInstantly = () => {
1052-
this.forceUpdate(() => {
1053-
if (this.editor) {
1054-
this.editor.editorPart.layout(this.props.width, this.props.height);
1055-
}
1056-
});
1052+
if (this.editor) {
1053+
this.editor.editorPart.layout(this.props.width, this.props.height);
1054+
}
10571055
};
10581056

10591057
/**

packages/app/src/app/pages/Sandbox/Editor/Content/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Prompt } from 'react-router-dom';
55
import { reaction } from 'mobx';
66
import { TextOperation } from 'ot';
77
import { inject, observer } from 'mobx-react';
8+
import { debounce } from 'lodash-es';
89

910
import getTemplateDefinition from 'common/lib/templates';
1011
import type { ModuleError } from 'common/lib/types';
@@ -63,6 +64,8 @@ class EditorPreview extends React.Component<Props, State> {
6364
() => this.forceUpdate()
6465
);
6566

67+
this.getBounds = debounce(this.getBoundsInstantly, 100);
68+
6669
window.addEventListener('resize', this.getBounds);
6770

6871
this.interval = setInterval(() => {
@@ -84,13 +87,12 @@ class EditorPreview extends React.Component<Props, State> {
8487
}
8588
}
8689

87-
getBounds = el => {
90+
getBoundsInstantly = el => {
8891
if (el) {
8992
this.el = this.el || el;
9093
}
9194
if (this.el) {
9295
const { width, height } = this.el.getBoundingClientRect();
93-
9496
if (width !== this.state.width || height !== this.state.height) {
9597
this.setState({ width, height });
9698
}
@@ -329,7 +331,7 @@ class EditorPreview extends React.Component<Props, State> {
329331
() => this.props.store.editor.previewWindowVisible,
330332
() => {
331333
requestAnimationFrame(() => {
332-
this.getBounds();
334+
this.getBoundsInstantly();
333335
});
334336
}
335337
);
@@ -553,7 +555,7 @@ class EditorPreview extends React.Component<Props, State> {
553555
}}
554556
>
555557
<div
556-
ref={this.getBounds}
558+
ref={this.getBoundsInstantly}
557559
style={{
558560
position: 'relative',
559561
display: 'flex',
@@ -575,8 +577,6 @@ class EditorPreview extends React.Component<Props, State> {
575577
isModuleSynced={store.editor.isModuleSynced}
576578
width={editorWidth}
577579
height={editorHeight}
578-
absoluteWidth={absoluteWidth}
579-
absoluteHeight={absoluteHeight}
580580
settings={settings(store)}
581581
sendTransforms={this.sendTransforms}
582582
readOnly={isReadOnly()}

0 commit comments

Comments
 (0)