11import { useEffect , useReducer , useRef , useState } from 'react' ;
22import PropTypes from 'prop-types' ;
3- import { connect , useDispatch } from 'react-redux' ;
3+ import { connect } from 'react-redux' ;
44import cn from 'bem-cn-lite' ;
55import _ from 'lodash' ;
66import 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' ;
2729import { getExplainQuery , getExplainQueryAst } from '../../../store/reducers/explainQuery' ;
2830import { showTooltip } from '../../../store/reducers/tooltip' ;
@@ -45,7 +47,7 @@ import {
4547 paneVisibilityToggleReducerCreator ,
4648} from '../utils/paneVisibilityToggleHelpers' ;
4749import Preview from '../Preview/Preview' ;
48- import { setShowPreview } from '../../../store/reducers/schema' ;
50+ import { setShowPreview } from '../../../store/reducers/schema' ;
4951
5052export 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 = {
9598function 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
674695QueryEditor . propTypes = propTypes ;
0 commit comments