diff --git a/news/2 Fixes/8045.md b/news/2 Fixes/8045.md new file mode 100644 index 000000000000..aa1a10ecd523 --- /dev/null +++ b/news/2 Fixes/8045.md @@ -0,0 +1 @@ +Fix markdown disappearing after editing and hitting the escape key. diff --git a/src/datascience-ui/native-editor/nativeCell.tsx b/src/datascience-ui/native-editor/nativeCell.tsx index 066877ffa7ad..774615f886bb 100644 --- a/src/datascience-ui/native-editor/nativeCell.tsx +++ b/src/datascience-ui/native-editor/nativeCell.tsx @@ -374,6 +374,7 @@ export class NativeCell extends React.Component { // Unfocus the current cell by giving focus to the cell itself if (this.wrapperRef && this.wrapperRef.current && this.isFocused()) { e.stopPropagation(); + this.isCodeCell() ? this.onCodeUnfocused() : this.onMarkdownUnfocused(); this.props.focusCell(this.cellId, false); this.props.stateController.sendCommand(NativeCommandType.Unfocus, 'keyboard'); } diff --git a/src/datascience-ui/native-editor/nativeEditorStateController.ts b/src/datascience-ui/native-editor/nativeEditorStateController.ts index 2c5cafb4fcb3..8dbe1a0bee26 100644 --- a/src/datascience-ui/native-editor/nativeEditorStateController.ts +++ b/src/datascience-ui/native-editor/nativeEditorStateController.ts @@ -53,7 +53,7 @@ export class NativeEditorStateController extends MainStateController { case InteractiveWindowMessages.NotebookAddCellBelow: this.addNewCell(); break; - case InteractiveWindowMessages.DoSave: + case InteractiveWindowMessages.DoSave: this.save(); break; @@ -278,24 +278,31 @@ export class NativeEditorStateController extends MainStateController { protected onCodeLostFocus(cellId: string) { // Update the cell's source - const cell = this.findCell(cellId); - if (cell) { + const index = this.findCellIndex(cellId); + if (index >= 0) { // Get the model for the monaco editor const monacoId = this.getMonacoId(cellId); if (monacoId) { const model = monacoEditor.editor.getModels().find(m => m.id === monacoId); if (model) { const newValue = model.getValue().replace(/\r/g, ''); - cell.cell.data.source = cell.inputBlockText = newValue; + const newVMs = [...this.getState().cellVMs]; + + // Update our state + newVMs[index] = { + ...newVMs[index], + cell: { + ...newVMs[index].cell, + data: { + ...newVMs[index].cell.data, + source: newValue + } + } + }; + + this.setState({ cellVMs: newVMs }); } } } - - // Special case markdown in the edit cell. Submit it. - if (cell && cell.cell.id === Identifiers.EditCellId && cell.cell.data.cell_type === 'markdown') { - const code = cell.inputBlockText; - cell.cell.data.source = cell.inputBlockText = ''; - this.submitInput(code, cell); - } } }