Skip to content

Commit b0e8994

Browse files
authored
Fix cells being erased when saving and then changing focus to another… (#8482)
* Fix cells being erased when saving and then changing focus to another cell * Make sure to use the source even if it's empty
1 parent c027287 commit b0e8994

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

news/2 Fixes/8399.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix cells being erased when saving and then changing focus to another cell.

src/datascience-ui/interactive-common/mainStateController.ts

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -339,26 +339,21 @@ export class MainStateController implements IMessageHandler {
339339
this.clearAllSilent();
340340
}
341341

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-
356342
public save = () => {
357343
// 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+
});
359354

360355
// 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) });
362357
}
363358

364359
public showPlot = (imageHtml: string) => {
@@ -764,6 +759,20 @@ export class MainStateController implements IMessageHandler {
764759
return this.pendingState;
765760
}
766761

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+
767776
// Adjust the visibility or collapsed state of a cell
768777
protected alterCellVM(cellVM: ICellViewModel, visible: boolean, expanded: boolean): ICellViewModel {
769778
if (cellVM.cell.data.cell_type === 'code') {
@@ -811,20 +820,6 @@ export class MainStateController implements IMessageHandler {
811820
return cellVM;
812821
}
813822

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-
828823
protected onCodeLostFocus(_cellId: string) {
829824
// Default is do nothing.
830825
}

src/datascience-ui/native-editor/nativeCell.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
482482
let content: string | undefined ;
483483

484484
// If inside editor, submit this code
485-
if (possibleContents) {
485+
if (possibleContents !== undefined) {
486486
content = possibleContents;
487487
} else {
488488
// Outside editor, just use the cell
@@ -558,8 +558,8 @@ export class NativeCell extends React.Component<INativeCellProps> {
558558
private renderMiddleToolbar = () => {
559559
const cellId = this.props.cellVM.cell.id;
560560
const runCell = () => {
561-
this.props.stateController.updateCellSource(cellId);
562-
this.runAndMove(concatMultilineStringInput(this.props.cellVM.cell.data.source));
561+
const contents = this.props.stateController.getMonacoEditorContents(cellId);
562+
this.runAndMove(contents);
563563
this.props.stateController.sendCommand(NativeCommandType.Run, 'mouse');
564564
};
565565
const gatherCell = () => {

src/datascience-ui/native-editor/nativeEditorStateController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export class NativeEditorStateController extends MainStateController {
286286
if (index >= 0) {
287287
// Get the model source from the monaco editor
288288
const source = this.getMonacoEditorContents(cellId);
289-
if (source) {
289+
if (source !== undefined) {
290290
const newVMs = [...this.getState().cellVMs];
291291

292292
// Update our state

0 commit comments

Comments
 (0)