Skip to content

Commit 2e521ab

Browse files
authored
Style speedups for the monaco editor (#11014)
1 parent 1f29704 commit 2e521ab

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/datascience-ui/react-common/monacoEditor.tsx

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface IMonacoEditorState {
6666
// Need this to prevent wiping of the current value on a componentUpdate. react-monaco-editor has that problem.
6767

6868
export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEditorState> {
69+
private static lineHeight: number = 0;
6970
private containerRef: React.RefObject<HTMLDivElement>;
7071
private measureWidthRef: React.RefObject<HTMLDivElement>;
7172
private resizeTimer?: number;
@@ -90,6 +91,7 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
9091
* Reference to parameter widget (used by monaco to display parameter docs).
9192
*/
9293
private parameterWidget?: Element;
94+
private insideLayout = false;
9395

9496
constructor(props: IMonacoEditorProps) {
9597
super(props);
@@ -318,6 +320,7 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
318320
this.updateMargin(this.state.editor);
319321
}
320322
this.state.editor.updateOptions(this.props.options);
323+
MonacoEditor.lineHeight = 0; // Font size and family come from theoptions.
321324
}
322325
if (
323326
prevProps.value !== this.props.value &&
@@ -581,7 +584,10 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
581584
if (this.resizeTimer) {
582585
clearTimeout(this.resizeTimer);
583586
}
584-
this.resizeTimer = window.setTimeout(this.updateEditorSize.bind(this), 0);
587+
// If this was caused by a layout, don't bother updating again
588+
if (!this.insideLayout) {
589+
this.resizeTimer = window.setTimeout(this.updateEditorSize.bind(this), 0);
590+
}
585591
};
586592

587593
private startUpdateWidgetPosition = () => {
@@ -632,6 +638,24 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
632638
}
633639
}
634640

641+
private computeLineHeight(): number {
642+
if (MonacoEditor.lineHeight <= 0 && this.state.editor) {
643+
const editorDomNode = this.state.editor.getDomNode();
644+
if (editorDomNode) {
645+
const container = editorDomNode.getElementsByClassName('view-lines')[0] as HTMLElement;
646+
const lineHeightPx =
647+
container.firstChild && (container.firstChild as HTMLElement).style.height
648+
? (container.firstChild as HTMLElement).style.height
649+
: `${LINE_HEIGHT}px`;
650+
MonacoEditor.lineHeight =
651+
lineHeightPx && lineHeightPx.endsWith('px')
652+
? parseInt(lineHeightPx.substr(0, lineHeightPx.length - 2), 10)
653+
: LINE_HEIGHT;
654+
}
655+
}
656+
return MonacoEditor.lineHeight;
657+
}
658+
635659
private updateEditorSize() {
636660
if (
637661
this.measureWidthRef.current &&
@@ -648,14 +672,7 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
648672
const grandParent = this.containerRef.current.parentElement.parentElement;
649673
const container = editorDomNode.getElementsByClassName('view-lines')[0] as HTMLElement;
650674
const currLineCount = Math.max(container.childElementCount, this.state.model.getLineCount());
651-
const lineHeightPx =
652-
container.firstChild && (container.firstChild as HTMLElement).style.height
653-
? (container.firstChild as HTMLElement).style.height
654-
: `${LINE_HEIGHT}px`;
655-
const lineHeight =
656-
lineHeightPx && lineHeightPx.endsWith('px')
657-
? parseInt(lineHeightPx.substr(0, lineHeightPx.length - 2), 10)
658-
: LINE_HEIGHT;
675+
const lineHeight = this.computeLineHeight();
659676
const height = currLineCount * lineHeight + 3; // Fudge factor
660677
const width = this.measureWidthRef.current.clientWidth - grandParent.offsetLeft - 15; // Leave room for the scroll bar in regular cell table
661678

@@ -671,7 +688,12 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
671688
this.monacoContainer.addEventListener('mousemove', this.onContainerMove);
672689
}
673690
this.setState({ visibleLineCount: currLineCount, attached: true });
674-
this.state.editor.layout({ width, height });
691+
try {
692+
this.insideLayout = true;
693+
this.state.editor.layout({ width, height });
694+
} finally {
695+
this.insideLayout = false;
696+
}
675697
}
676698
}
677699
}

0 commit comments

Comments
 (0)