From 2be16d1a1f782e5c16ff8af3ac0c9acf7f42c066 Mon Sep 17 00:00:00 2001 From: rchiodo Date: Tue, 5 Jan 2021 11:05:18 -0800 Subject: [PATCH] Review feedback and fix undo/redo for interactive command --- .../interactive-common/interactiveBase.ts | 4 +- .../interactiveWindowTypes.ts | 4 ++ .../interactive-common/synchronization.ts | 2 + .../history-react/redux/reducers/index.ts | 4 +- .../native-editor/redux/reducers/creation.ts | 52 ++++++++++++++----- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/client/datascience/interactive-common/interactiveBase.ts b/src/client/datascience/interactive-common/interactiveBase.ts index 854b1a29d95..1a59c8402d6 100644 --- a/src/client/datascience/interactive-common/interactiveBase.ts +++ b/src/client/datascience/interactive-common/interactiveBase.ts @@ -406,12 +406,12 @@ export abstract class InteractiveBase extends WebviewPanelHost & Messa [InteractiveWindowMessages.SubmitNewCell]: MessageType.other, [InteractiveWindowMessages.Sync]: MessageType.other, [InteractiveWindowMessages.Undo]: MessageType.other, + [InteractiveWindowMessages.UndoCommand]: MessageType.other, + [InteractiveWindowMessages.RedoCommand]: MessageType.other, [InteractiveWindowMessages.UnfocusedCellEditor]: MessageType.syncWithLiveShare, [InteractiveWindowMessages.UpdateCellWithExecutionResults]: MessageType.syncAcrossSameNotebooks | MessageType.syncWithLiveShare, diff --git a/src/datascience-ui/history-react/redux/reducers/index.ts b/src/datascience-ui/history-react/redux/reducers/index.ts index 4efad81893f..6d517165f92 100644 --- a/src/datascience-ui/history-react/redux/reducers/index.ts +++ b/src/datascience-ui/history-react/redux/reducers/index.ts @@ -70,5 +70,7 @@ export const reducerMap: Partial = { [InteractiveWindowMessages.UpdateDisplayData]: CommonEffects.handleUpdateDisplayData, [InteractiveWindowMessages.HasCell]: Transfer.hasCell, [InteractiveWindowMessages.UpdateExternalCellButtons]: CommonEffects.handleWebviewButtons, - [InteractiveWindowMessages.ExecuteExternalCommand]: Transfer.executeExternalCommand + [InteractiveWindowMessages.ExecuteExternalCommand]: Transfer.executeExternalCommand, + [InteractiveWindowMessages.UndoCommand]: Execution.undo, + [InteractiveWindowMessages.RedoCommand]: Execution.redo }; diff --git a/src/datascience-ui/native-editor/redux/reducers/creation.ts b/src/datascience-ui/native-editor/redux/reducers/creation.ts index 754924fa79d..768a521f7fe 100644 --- a/src/datascience-ui/native-editor/redux/reducers/creation.ts +++ b/src/datascience-ui/native-editor/redux/reducers/creation.ts @@ -100,8 +100,8 @@ export namespace Creation { if (position >= 0) { newList.splice(position, 0, newVM); } else { - newList.splice(newList.length, 0, newVM); - position = newList.length; + newList.splice(0, 0, newVM); + position = 0; } const result = { @@ -145,7 +145,19 @@ export namespace Creation { } export function insertBelow(arg: NativeEditorReducerArg): IMainState { - const newVM = prepareCellVM(createEmptyCell(arg.payload.data.newCellId, null), false, arg.prevState.settings); + return insertExistingBelow({ + ...arg, + payload: { + ...arg.payload, + data: { ...arg.payload.data, cell: createEmptyCell(arg.payload.data.newCellId, null) } + } + }); + } + + export function insertExistingBelow( + arg: NativeEditorReducerArg + ): IMainState { + const newVM = prepareCellVM(arg.payload.data.cell, false, arg.prevState.settings); const newList = [...arg.prevState.cellVMs]; // Find the position where we want to insert @@ -370,17 +382,29 @@ export namespace Creation { payload: { ...arg.payload, data: { cellId: arg.payload.data.cell.id } } }); case 'remove': - const cellBelow = - arg.prevState.cellVMs.length > arg.payload.data.index - ? arg.prevState.cellVMs[arg.payload.data.index].cell - : undefined; - return insertExistingAbove({ - ...disabledQueueArg, - payload: { - ...arg.payload, - data: { cell: arg.payload.data.cell, cellId: cellBelow ? cellBelow.id : undefined } - } - }); + if (arg.prevState.cellVMs.length > arg.payload.data.index) { + const cellBelow = arg.prevState.cellVMs[arg.payload.data.index].cell; + return insertExistingAbove({ + ...disabledQueueArg, + payload: { + ...arg.payload, + data: { cell: arg.payload.data.cell, cellId: cellBelow ? cellBelow.id : undefined } + } + }); + } else { + // Delete is outside current range. Insert at the bottom + return insertExistingBelow({ + ...disabledQueueArg, + payload: { + ...arg.payload, + data: { + cell: arg.payload.data.cell, + cellId: undefined, + newCellId: arg.payload.data.cell.id + } + } + }); + } case 'remove_all': return loadAllCells({ ...disabledQueueArg,