diff --git a/news/2 Fixes/8491.md b/news/2 Fixes/8491.md new file mode 100644 index 000000000000..87fc913bf6ad --- /dev/null +++ b/news/2 Fixes/8491.md @@ -0,0 +1 @@ +Arrowing up and down through cells can lose code that was just typed. diff --git a/src/datascience-ui/interactive-common/mainStateController.ts b/src/datascience-ui/interactive-common/mainStateController.ts index f59699093fb8..3bc66df7e402 100644 --- a/src/datascience-ui/interactive-common/mainStateController.ts +++ b/src/datascience-ui/interactive-common/mainStateController.ts @@ -559,6 +559,18 @@ export class MainStateController implements IMessageHandler { } } + public updateCode = (cellId: string, code: string) => { + const index = this.pendingState.cellVMs.findIndex(c => c.cell.id === cellId); + if (index > 0) { + const cellVMs = [...this.pendingState.cellVMs]; + const current = this.pendingState.cellVMs[index]; + const newCell = { ...current, inputBlockText: code, cell: { ...current.cell, data: { ...current.cell.data, source: code } } }; + // tslint:disable-next-line: no-any + cellVMs[index] = (newCell as any); // This is because IMessageCell doesn't fit in here. But message cells can't change type + this.setState({ cellVMs }); + } + } + public changeCellType = (cellId: string, newType: 'code' | 'markdown') => { const index = this.pendingState.cellVMs.findIndex(c => c.cell.id === cellId); if (index >= 0 && this.pendingState.cellVMs[index].cell.data.cell_type !== newType) { diff --git a/src/datascience-ui/native-editor/nativeCell.tsx b/src/datascience-ui/native-editor/nativeCell.tsx index 08500ae84dd0..7b677e810ab0 100644 --- a/src/datascience-ui/native-editor/nativeCell.tsx +++ b/src/datascience-ui/native-editor/nativeCell.tsx @@ -385,6 +385,9 @@ export class NativeCell extends React.Component { const prevCellId = this.getPrevCellId(); if (prevCellId) { e.stopPropagation(); + if (e.editorInfo) { + this.props.stateController.updateCode(this.getCell().id, e.editorInfo.contents); + } this.moveSelection(prevCellId, this.isFocused(), CursorPos.Bottom); } @@ -396,6 +399,9 @@ export class NativeCell extends React.Component { if (nextCellId) { e.stopPropagation(); + if (e.editorInfo) { + this.props.stateController.updateCode(this.getCell().id, e.editorInfo.contents); + } this.moveSelection(nextCellId, this.isFocused(), CursorPos.Top); }