@@ -66,6 +66,7 @@ export interface IMonacoEditorState {
66
66
// Need this to prevent wiping of the current value on a componentUpdate. react-monaco-editor has that problem.
67
67
68
68
export class MonacoEditor extends React . Component < IMonacoEditorProps , IMonacoEditorState > {
69
+ private static lineHeight : number = 0 ;
69
70
private containerRef : React . RefObject < HTMLDivElement > ;
70
71
private measureWidthRef : React . RefObject < HTMLDivElement > ;
71
72
private resizeTimer ?: number ;
@@ -90,6 +91,7 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
90
91
* Reference to parameter widget (used by monaco to display parameter docs).
91
92
*/
92
93
private parameterWidget ?: Element ;
94
+ private insideLayout = false ;
93
95
94
96
constructor ( props : IMonacoEditorProps ) {
95
97
super ( props ) ;
@@ -318,6 +320,7 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
318
320
this . updateMargin ( this . state . editor ) ;
319
321
}
320
322
this . state . editor . updateOptions ( this . props . options ) ;
323
+ MonacoEditor . lineHeight = 0 ; // Font size and family come from theoptions.
321
324
}
322
325
if (
323
326
prevProps . value !== this . props . value &&
@@ -581,7 +584,10 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
581
584
if ( this . resizeTimer ) {
582
585
clearTimeout ( this . resizeTimer ) ;
583
586
}
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
+ }
585
591
} ;
586
592
587
593
private startUpdateWidgetPosition = ( ) => {
@@ -632,6 +638,24 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
632
638
}
633
639
}
634
640
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
+
635
659
private updateEditorSize ( ) {
636
660
if (
637
661
this . measureWidthRef . current &&
@@ -648,14 +672,7 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
648
672
const grandParent = this . containerRef . current . parentElement . parentElement ;
649
673
const container = editorDomNode . getElementsByClassName ( 'view-lines' ) [ 0 ] as HTMLElement ;
650
674
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 ( ) ;
659
676
const height = currLineCount * lineHeight + 3 ; // Fudge factor
660
677
const width = this . measureWidthRef . current . clientWidth - grandParent . offsetLeft - 15 ; // Leave room for the scroll bar in regular cell table
661
678
@@ -671,7 +688,12 @@ export class MonacoEditor extends React.Component<IMonacoEditorProps, IMonacoEdi
671
688
this . monacoContainer . addEventListener ( 'mousemove' , this . onContainerMove ) ;
672
689
}
673
690
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
+ }
675
697
}
676
698
}
677
699
}
0 commit comments