@@ -339,26 +339,21 @@ export class MainStateController implements IMessageHandler {
339
339
this . clearAllSilent ( ) ;
340
340
}
341
341
342
- public updateCellSource = ( cellId : string ) => {
343
- const models = monacoEditor . editor . getModels ( ) ;
344
- const cvm = this . findCell ( cellId ) ;
345
- if ( cvm ) {
346
- const modelId = this . getMonacoId ( cvm . cell . id ) ;
347
- if ( modelId ) {
348
- const model = models . find ( m => m . id === modelId ) ;
349
- if ( model ) {
350
- cvm . cell . data . source = cvm . inputBlockText = model . getValue ( ) . replace ( / \r / g, '' ) ;
351
- }
352
- }
353
- }
354
- }
355
-
356
342
public save = ( ) => {
357
343
// We have to take the current value of each cell to make sure we have the correct text.
358
- this . pendingState . cellVMs . forEach ( c => this . updateCellSource ( c . cell . id ) ) ;
344
+ const newVMs = [ ...this . pendingState . cellVMs ] ;
345
+ for ( let i = 0 ; i < newVMs . length ; i += 1 ) {
346
+ const text = this . getMonacoEditorContents ( newVMs [ i ] . cell . id ) ;
347
+ if ( text !== undefined ) {
348
+ newVMs [ i ] = { ...newVMs [ i ] , inputBlockText : text , cell : { ...newVMs [ i ] . cell , data : { ...newVMs [ i ] . cell . data , source : text } } } ;
349
+ }
350
+ }
351
+ this . setState ( {
352
+ cellVMs : newVMs
353
+ } ) ;
359
354
360
355
// Then send the save with the new state.
361
- this . sendMessage ( InteractiveWindowMessages . SaveAll , { cells : this . getNonEditCellVMs ( ) . map ( cvm => cvm . cell ) } ) ;
356
+ this . sendMessage ( InteractiveWindowMessages . SaveAll , { cells : newVMs . map ( cvm => cvm . cell ) } ) ;
362
357
}
363
358
364
359
public showPlot = ( imageHtml : string ) => {
@@ -764,6 +759,20 @@ export class MainStateController implements IMessageHandler {
764
759
return this . pendingState ;
765
760
}
766
761
762
+ public getMonacoEditorContents ( cellId : string ) : string | undefined {
763
+ const index = this . findCellIndex ( cellId ) ;
764
+ if ( index >= 0 ) {
765
+ // Get the model for the monaco editor
766
+ const monacoId = this . getMonacoId ( cellId ) ;
767
+ if ( monacoId ) {
768
+ const model = monacoEditor . editor . getModels ( ) . find ( m => m . id === monacoId ) ;
769
+ if ( model ) {
770
+ return model . getValue ( ) . replace ( / \r / g, '' ) ;
771
+ }
772
+ }
773
+ }
774
+ }
775
+
767
776
// Adjust the visibility or collapsed state of a cell
768
777
protected alterCellVM ( cellVM : ICellViewModel , visible : boolean , expanded : boolean ) : ICellViewModel {
769
778
if ( cellVM . cell . data . cell_type === 'code' ) {
@@ -811,20 +820,6 @@ export class MainStateController implements IMessageHandler {
811
820
return cellVM ;
812
821
}
813
822
814
- protected getMonacoEditorContents ( cellId : string ) : string | undefined {
815
- const index = this . findCellIndex ( cellId ) ;
816
- if ( index >= 0 ) {
817
- // Get the model for the monaco editor
818
- const monacoId = this . getMonacoId ( cellId ) ;
819
- if ( monacoId ) {
820
- const model = monacoEditor . editor . getModels ( ) . find ( m => m . id === monacoId ) ;
821
- if ( model ) {
822
- return model . getValue ( ) . replace ( / \r / g, '' ) ;
823
- }
824
- }
825
- }
826
- }
827
-
828
823
protected onCodeLostFocus ( _cellId : string ) {
829
824
// Default is do nothing.
830
825
}
0 commit comments