@@ -430,7 +430,7 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
430430 // that the changes of code are not sent to live users. We need to reset
431431 // this state when we're doing changing modules
432432 this . props . onCodeReceived ( ) ;
433- this . updatedCode = '' ;
433+ this . liveOperationCode = '' ;
434434 }
435435 } ) ;
436436 } ;
@@ -441,20 +441,22 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
441441 }
442442 } ;
443443
444- updatedCode = '' ;
444+ liveOperationCode = '' ;
445445 sendChangeOperations = changeEvent => {
446446 const { sendTransforms, isLive, onCodeReceived } = this . props ;
447447
448448 if ( sendTransforms && changeEvent . changes ) {
449449 const otOperation = new TextOperation ( ) ;
450450 // TODO: add a comment explaining what "delta" is
451451 let delta = 0 ;
452- this . updatedCode = this . updatedCode || this . currentModule . code || '' ;
452+
453+ this . liveOperationCode =
454+ this . liveOperationCode || this . currentModule . code || '' ;
453455 // eslint-disable-next-line no-restricted-syntax
454456 for ( const change of [ ...changeEvent . changes ] . reverse ( ) ) {
455457 const cursorStartOffset =
456458 lineAndColumnToIndex (
457- this . updatedCode . split ( '\n' ) ,
459+ this . liveOperationCode . split ( '\n' ) ,
458460 change . range . startLineNumber ,
459461 change . range . startColumn
460462 ) + delta ;
@@ -475,16 +477,16 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
475477 }
476478 }
477479
478- const remaining = this . updatedCode . length - otOperation . baseLength ;
480+ const remaining = this . liveOperationCode . length - otOperation . baseLength ;
479481 if ( remaining > 0 ) {
480482 otOperation . retain ( remaining ) ;
481483 }
482- this . updatedCode = otOperation . apply ( this . updatedCode ) ;
484+ this . liveOperationCode = otOperation . apply ( this . liveOperationCode ) ;
483485
484486 sendTransforms ( otOperation ) ;
485487
486488 requestAnimationFrame ( ( ) => {
487- this . updatedCode = '' ;
489+ this . liveOperationCode = '' ;
488490 } ) ;
489491 } else if ( ! isLive && onCodeReceived ) {
490492 onCodeReceived ( ) ;
@@ -715,7 +717,7 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
715717 } ;
716718
717719 applyOperation = ( operation : any ) = > {
718- this . updatedCode = '' ;
720+ this . liveOperationCode = '' ;
719721 let index = 0 ;
720722 for ( let i = 0 ; i < operation . ops . length ; i ++ ) {
721723 const op = operation . ops [ i ] ;
@@ -1110,6 +1112,11 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
11101112 forceMoveMarkers : false ,
11111113 } ;
11121114
1115+ // For the live operation we need to send the operation based on the old code,
1116+ // that's why we set the 'liveOperationCode' to the last code so the operation
1117+ // will be applied on that code instead of `currentModule.code`
1118+ this . liveOperationCode = this . getCode ( ) ;
1119+
11131120 this . editor . getModel ( ) . pushEditOperations ( [ ] , [ editOperation ] , null ) ;
11141121 this . editor . setPosition ( pos ) ;
11151122 }
0 commit comments