2
2
// Licensed under the MIT License.
3
3
'use strict' ;
4
4
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api' ;
5
+ import { bindActionCreators , Dispatch } from 'redux' ;
5
6
import * as uuid from 'uuid/v4' ;
6
7
import { InteractiveWindowMessages , NativeCommandType } from '../../../client/datascience/interactive-common/interactiveWindowTypes' ;
7
8
import { IJupyterVariable , IJupyterVariablesRequest } from '../../../client/datascience/types' ;
@@ -11,40 +12,72 @@ import {
11
12
CommonActionType ,
12
13
createIncomingAction ,
13
14
createIncomingActionWithPayload ,
14
- IAddCellAction ,
15
15
ICellAction ,
16
16
ICellAndCursorAction ,
17
- IChangeCellTypeAction ,
18
17
ICodeAction ,
19
18
ICodeCreatedAction ,
20
19
IEditCellAction ,
21
- IExecuteAction ,
22
20
ILinkClickAction ,
23
21
IRefreshVariablesAction ,
24
22
ISendCommandAction ,
25
23
IShowDataViewerAction
26
24
} from '../../interactive-common/redux/reducers/types' ;
27
25
26
+ const actionCreatorsWithDispatch = ( dispatch : Dispatch ) => {
27
+ return {
28
+ addCell : ( ) => {
29
+ const newCellId = uuid ( ) ;
30
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . ADD_NEW_CELL , { newCellId } ) ) ;
31
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . FOCUS_CELL , { cellId : newCellId , cursorPos : CursorPos . Current } ) ) ;
32
+ } ,
33
+ insertAboveFirst : ( ) => {
34
+ const newCellId = uuid ( ) ;
35
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . INSERT_ABOVE_FIRST , { newCellId } ) ) ;
36
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . FOCUS_CELL , { cellId : newCellId , cursorPos : CursorPos . Current } ) ) ;
37
+ } ,
38
+ insertAbove : ( cellId : string | undefined ) => {
39
+ // Use settimeout to ensure the newly created cell does not end up with the key that was entered by user.
40
+ // E.g. if user types `a`, then we create a new cell, however old approach would result in the new editor having a character `a`.
41
+ setTimeout ( ( ) => {
42
+ const newCellId = uuid ( ) ;
43
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . INSERT_ABOVE , { cellId, newCellId } ) ) ;
44
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . FOCUS_CELL , { cellId : newCellId , cursorPos : CursorPos . Current } ) ) ;
45
+ } , 1 ) ;
46
+ } ,
47
+ insertBelow : ( cellId : string | undefined ) => {
48
+ if ( ! cellId ) {
49
+ return ;
50
+ }
51
+ // Use settimeout to ensure the newly created cell does not end up with the key that was entered by user.
52
+ // E.g. if user types `a`, then we create a new cell, however old approach would result in the new editor having a character `a`.
53
+ setTimeout ( ( ) => {
54
+ const newCellId = uuid ( ) ;
55
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . INSERT_BELOW , { cellId, newCellId } ) ) ;
56
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . FOCUS_CELL , { cellId : newCellId , cursorPos : CursorPos . Current } ) ) ;
57
+ } , 1 ) ;
58
+ } ,
59
+ executeCell : ( cellId : string , code : string , moveOp : 'add' | 'select' | 'none' ) => {
60
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . EXECUTE_CELL , { cellId, code, moveOp } ) ) ;
61
+ if ( moveOp === 'add' ) {
62
+ const newCellId = uuid ( ) ;
63
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . INSERT_BELOW , { cellId, newCellId } ) ) ;
64
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . FOCUS_CELL , { cellId : newCellId , cursorPos : CursorPos . Current } ) ) ;
65
+ }
66
+ } ,
67
+ changeCellType : ( cellId : string , currentCode : string ) => {
68
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . CHANGE_CELL_TYPE , { cellId, currentCode } ) ) ;
69
+ dispatch ( createIncomingActionWithPayload ( CommonActionType . FOCUS_CELL , { cellId, cursorPos : CursorPos . Current } ) ) ;
70
+ }
71
+ } ;
72
+ } ;
73
+
28
74
// See https://react-redux.js.org/using-react-redux/connect-mapdispatch#defining-mapdispatchtoprops-as-an-object
29
- export const actionCreators = {
30
- insertAbove : ( cellId : string | undefined ) : CommonAction < ICellAction & IAddCellAction > =>
31
- createIncomingActionWithPayload ( CommonActionType . INSERT_ABOVE , { cellId, newCellId : uuid ( ) } ) ,
32
- insertAboveFirst : ( ) : CommonAction < IAddCellAction > => createIncomingActionWithPayload ( CommonActionType . INSERT_ABOVE_FIRST , { newCellId : uuid ( ) } ) ,
33
- insertBelow : ( cellId : string | undefined ) : CommonAction < ICellAction & IAddCellAction > =>
34
- createIncomingActionWithPayload ( CommonActionType . INSERT_BELOW , { cellId, newCellId : uuid ( ) } ) ,
75
+ const actionCreators = {
35
76
focusCell : ( cellId : string , cursorPos : CursorPos = CursorPos . Current ) : CommonAction < ICellAndCursorAction > =>
36
77
createIncomingActionWithPayload ( CommonActionType . FOCUS_CELL , { cellId, cursorPos } ) ,
37
78
unfocusCell : ( cellId : string , code : string ) : CommonAction < ICodeAction > => createIncomingActionWithPayload ( CommonActionType . UNFOCUS_CELL , { cellId, code } ) ,
38
79
selectCell : ( cellId : string , cursorPos : CursorPos = CursorPos . Current ) : CommonAction < ICellAndCursorAction > =>
39
80
createIncomingActionWithPayload ( CommonActionType . SELECT_CELL , { cellId, cursorPos } ) ,
40
- addCell : ( ) : CommonAction < IAddCellAction > => createIncomingActionWithPayload ( CommonActionType . ADD_NEW_CELL , { newCellId : uuid ( ) } ) ,
41
- executeCell : ( cellId : string , code : string , moveOp : 'add' | 'select' | 'none' ) : CommonAction < IExecuteAction > => {
42
- if ( moveOp === 'add' ) {
43
- return createIncomingActionWithPayload ( CommonActionType . EXECUTE_CELL , { cellId, code, moveOp, newCellId : uuid ( ) } ) ;
44
- } else {
45
- return createIncomingActionWithPayload ( CommonActionType . EXECUTE_CELL , { cellId, code, moveOp } ) ;
46
- }
47
- } ,
48
81
executeAllCells : ( ) : CommonAction => createIncomingAction ( CommonActionType . EXECUTE_ALL_CELLS ) ,
49
82
executeAbove : ( cellId : string ) : CommonAction < ICellAction > => createIncomingActionWithPayload ( CommonActionType . EXECUTE_ABOVE , { cellId } ) ,
50
83
executeCellAndBelow : ( cellId : string , code : string ) : CommonAction < ICodeAction > => createIncomingActionWithPayload ( CommonActionType . EXECUTE_CELL_AND_BELOW , { cellId, code } ) ,
@@ -62,8 +95,7 @@ export const actionCreators = {
62
95
createIncomingActionWithPayload ( CommonActionType . SEND_COMMAND , { command, commandType } ) ,
63
96
moveCellUp : ( cellId : string ) : CommonAction < ICellAction > => createIncomingActionWithPayload ( CommonActionType . MOVE_CELL_UP , { cellId } ) ,
64
97
moveCellDown : ( cellId : string ) : CommonAction < ICellAction > => createIncomingActionWithPayload ( CommonActionType . MOVE_CELL_DOWN , { cellId } ) ,
65
- changeCellType : ( cellId : string , currentCode : string ) : CommonAction < IChangeCellTypeAction > =>
66
- createIncomingActionWithPayload ( CommonActionType . CHANGE_CELL_TYPE , { cellId, currentCode } ) ,
98
+ // changeCellType: (cellId: string, currentCode: string) => createIncomingActionWithPayload(CommonActionType.CHANGE_CELL_TYPE, { cellId, currentCode }),
67
99
toggleLineNumbers : ( cellId : string ) : CommonAction < ICellAction > => createIncomingActionWithPayload ( CommonActionType . TOGGLE_LINE_NUMBERS , { cellId } ) ,
68
100
toggleOutput : ( cellId : string ) : CommonAction < ICellAction > => createIncomingActionWithPayload ( CommonActionType . TOGGLE_OUTPUT , { cellId } ) ,
69
101
deleteCell : ( cellId : string ) : CommonAction < ICellAction > => createIncomingActionWithPayload ( InteractiveWindowMessages . DeleteCell , { cellId } ) ,
@@ -86,3 +118,12 @@ export const actionCreators = {
86
118
getVariableData : ( newExecutionCount : number , startIndex : number = 0 , pageSize : number = 100 ) : CommonAction < IJupyterVariablesRequest > =>
87
119
createIncomingActionWithPayload ( CommonActionType . GET_VARIABLE_DATA , { executionCount : newExecutionCount , sortColumn : 'name' , sortAscending : true , startIndex, pageSize } )
88
120
} ;
121
+
122
+ export type ActionCreators = typeof actionCreators & ReturnType < typeof actionCreatorsWithDispatch > ;
123
+
124
+ export function mapDispatchToProps ( dispatch : Dispatch ) : ActionCreators {
125
+ return {
126
+ ...actionCreatorsWithDispatch ( dispatch ) ,
127
+ ...bindActionCreators ( actionCreators , dispatch )
128
+ } ;
129
+ }
0 commit comments