Skip to content

Commit ebe59b3

Browse files
committed
fix(QueryEditor): key bindings should work properly
1 parent e7511be commit ebe59b3

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

src/containers/Tenant/QueryEditor/QueryEditor.js

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {useEffect, useReducer, useRef, useState} from 'react';
22
import PropTypes from 'prop-types';
3-
import {connect, useDispatch} from 'react-redux';
3+
import {connect} from 'react-redux';
44
import cn from 'bem-cn-lite';
55
import _ from 'lodash';
66
import MonacoEditor from 'react-monaco-editor';
@@ -23,6 +23,8 @@ import {
2323
goToNextQuery,
2424
selectRunAction,
2525
RUN_ACTIONS_VALUES,
26+
MONACO_HOT_KEY_ACTIONS,
27+
setMonacoHotKey,
2628
} from '../../../store/reducers/executeQuery';
2729
import {getExplainQuery, getExplainQueryAst} from '../../../store/reducers/explainQuery';
2830
import {showTooltip} from '../../../store/reducers/tooltip';
@@ -45,7 +47,7 @@ import {
4547
paneVisibilityToggleReducerCreator,
4648
} from '../utils/paneVisibilityToggleHelpers';
4749
import Preview from '../Preview/Preview';
48-
import { setShowPreview } from '../../../store/reducers/schema';
50+
import {setShowPreview} from '../../../store/reducers/schema';
4951

5052
export const RUN_ACTIONS = [
5153
{value: RUN_ACTIONS_VALUES.script, content: 'Run Script'},
@@ -83,6 +85,7 @@ const propTypes = {
8385
executeQuery: PropTypes.object,
8486
explainQuery: PropTypes.object,
8587
showTooltip: PropTypes.func,
88+
setMonacoHotKey: PropTypes.func,
8689
theme: PropTypes.string,
8790
type: PropTypes.string,
8891
};
@@ -95,8 +98,6 @@ const initialTenantCommonInfoState = {
9598
function QueryEditor(props) {
9699
const [resultType, setResultType] = useState(RESULT_TYPES.EXECUTE);
97100

98-
const dispatch = useDispatch()
99-
100101
const [resultVisibilityState, dispatchResultVisibilityState] = useReducer(
101102
paneVisibilityToggleReducerCreator(DEFAULT_IS_QUERY_RESULT_COLLAPSED),
102103
initialTenantCommonInfoState,
@@ -114,6 +115,7 @@ function QueryEditor(props) {
114115
window.removeEventListener('resize', onChangeWindow);
115116
window.removeEventListener('storage', storageEventHandler);
116117
window.onbeforeunload = undefined;
118+
props.setMonacoHotKey(null);
117119
};
118120
}, []);
119121

@@ -150,21 +152,37 @@ function QueryEditor(props) {
150152
}, [props.executeQuery, props.executeQuery]);
151153

152154
useEffect(() => {
153-
const {
154-
path,
155-
executeQuery: {input},
156-
} = props;
157-
if (resultType === RESULT_TYPES.EXPLAIN) {
158-
props.getExplainQuery({query: input, database: path});
155+
const {monacoHotKey, setMonacoHotKey} = props;
156+
setMonacoHotKey(null);
157+
switch (monacoHotKey) {
158+
case MONACO_HOT_KEY_ACTIONS.sendQuery: {
159+
return handleSendClick();
160+
}
161+
case MONACO_HOT_KEY_ACTIONS.goPrev: {
162+
return handlePreviousHistoryClick();
163+
}
164+
case MONACO_HOT_KEY_ACTIONS.goNext: {
165+
return handleNextHistoryClick();
166+
}
167+
case MONACO_HOT_KEY_ACTIONS.getExplain: {
168+
return handleGetExplainQueryClick();
169+
}
170+
default: {
171+
return;
172+
}
159173
}
160-
}, [resultType]);
174+
}, [props.monacoHotKey]);
161175

162176
const checkIfHasUnsavedInput = (e) => {
163177
e.preventDefault();
164178
// Chrome requires returnValue to be set
165179
e.returnValue = '';
166180
};
167181

182+
const handleKeyBinding = (value) => {
183+
return () => props.setMonacoHotKey(value);
184+
};
185+
168186
const editorDidMount = (editor, monaco) => {
169187
editorRef.current = editor;
170188
editor.focus();
@@ -183,9 +201,7 @@ function QueryEditor(props) {
183201
contextMenuOrder: 1,
184202
// Method that will be executed when the action is triggered.
185203
// @param editor The editor instance is passed in as a convinience
186-
run: () => {
187-
handleSendClick();
188-
},
204+
run: handleKeyBinding(MONACO_HOT_KEY_ACTIONS.sendQuery),
189205
});
190206

191207
editor.addAction({
@@ -197,9 +213,7 @@ function QueryEditor(props) {
197213
],
198214
contextMenuGroupId: CONTEXT_MENU_GROUP_ID,
199215
contextMenuOrder: 2,
200-
run: () => {
201-
handlePreviousHistoryClick();
202-
},
216+
run: handleKeyBinding(MONACO_HOT_KEY_ACTIONS.goPrev),
203217
});
204218
editor.addAction({
205219
id: 'next-query',
@@ -210,9 +224,7 @@ function QueryEditor(props) {
210224
],
211225
contextMenuGroupId: CONTEXT_MENU_GROUP_ID,
212226
contextMenuOrder: 3,
213-
run: () => {
214-
handleNextHistoryClick();
215-
},
227+
run: handleKeyBinding(MONACO_HOT_KEY_ACTIONS.goNext),
216228
});
217229

218230
editor.addAction({
@@ -228,9 +240,7 @@ function QueryEditor(props) {
228240
keybindingContext: null,
229241
contextMenuGroupId: CONTEXT_MENU_GROUP_ID,
230242
contextMenuOrder: 4,
231-
run: () => {
232-
handleGetExplainQueryClick();
233-
},
243+
run: handleKeyBinding(MONACO_HOT_KEY_ACTIONS.getExplain),
234244
});
235245
};
236246
const onChange = (newValue) => {
@@ -243,11 +253,12 @@ function QueryEditor(props) {
243253
executeQuery: {input, history, runAction},
244254
sendQuery,
245255
saveQueryToHistory,
256+
setShowPreview,
246257
} = props;
247258

248259
setResultType(RESULT_TYPES.EXECUTE);
249260
sendQuery({query: input, database: path, action: runAction});
250-
dispatch(setShowPreview(false))
261+
setShowPreview(false);
251262

252263
const {queries, currentIndex} = history;
253264
if (input !== queries[currentIndex]) {
@@ -257,8 +268,15 @@ function QueryEditor(props) {
257268
};
258269

259270
const handleGetExplainQueryClick = () => {
271+
const {
272+
path,
273+
executeQuery: {input},
274+
getExplainQuery,
275+
setShowPreview,
276+
} = props;
260277
setResultType(RESULT_TYPES.EXPLAIN);
261-
dispatch(setShowPreview(false))
278+
getExplainQuery({query: input, database: path});
279+
setShowPreview(false);
262280
dispatchResultVisibilityState(PaneVisibilityActionTypes.triggerExpand);
263281
};
264282

@@ -655,6 +673,7 @@ const mapStateToProps = (state) => {
655673
savedQueries: parseJson(getSettingValue(state, SAVED_QUERIES_KEY)),
656674
showPreview: state.schema.showPreview,
657675
currentSchema: state.schema.currentSchema,
676+
monacoHotKey: state.executeQuery?.monacoHotKey,
658677
};
659678
};
660679

@@ -669,6 +688,8 @@ const mapDispatchToProps = {
669688
getExplainQueryAst,
670689
setSettingValue,
671690
selectRunAction,
691+
setShowPreview,
692+
setMonacoHotKey,
672693
};
673694

674695
QueryEditor.propTypes = propTypes;

src/store/reducers/executeQuery.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ const SAVE_QUERY_TO_HISTORY = 'query/SAVE_QUERY_TO_HISTORY';
77
const GO_TO_PREVIOUS_QUERY = 'query/GO_TO_PREVIOUS_QUERY';
88
const GO_TO_NEXT_QUERY = 'query/GO_TO_NEXT_QUERY';
99
const SELECT_RUN_ACTION = 'query/SELECT_RUN_ACTION';
10+
const MONACO_HOT_KEY = 'query/MONACO_HOT_KEY';
1011

1112
export const RUN_ACTIONS_VALUES = {
1213
script: 'execute-script',
1314
scan: 'execute-scan',
1415
};
1516

17+
export const MONACO_HOT_KEY_ACTIONS = {
18+
sendQuery: 'sendQuery',
19+
goPrev: 'goPrev',
20+
goNext: 'goNext',
21+
getExplain: 'getExplain',
22+
};
23+
1624
const initialState = {
1725
loading: false,
1826
input: '',
@@ -21,6 +29,7 @@ const initialState = {
2129
currentIndex: -1,
2230
},
2331
runAction: RUN_ACTIONS_VALUES.script,
32+
monacoHotKey: null,
2433
};
2534

2635
const executeQuery = (state = initialState, action) => {
@@ -104,6 +113,13 @@ const executeQuery = (state = initialState, action) => {
104113
};
105114
}
106115

116+
case MONACO_HOT_KEY: {
117+
return {
118+
...state,
119+
monacoHotKey: action.data,
120+
};
121+
}
122+
107123
default:
108124
return state;
109125
}
@@ -155,4 +171,11 @@ export const changeUserInput = ({input}) => {
155171
};
156172
};
157173

174+
export const setMonacoHotKey = (value) => {
175+
return {
176+
type: MONACO_HOT_KEY,
177+
data: value,
178+
};
179+
};
180+
158181
export default executeQuery;

0 commit comments

Comments
 (0)