@@ -29,16 +29,18 @@ import { functionTooltip } from './functionTooltips';
2929import { MySQLDialect , MySQLTooltips } from 'dialects/MySQLDialect' ;
3030import { QueryDialetType } from 'libs/QueryBuilder' ;
3131import { PgDialect , PgTooltips } from 'dialects/PgDialect copy' ;
32+ import { useKeybinding } from 'renderer/contexts/KeyBindingProvider' ;
3233
3334const SqlCodeEditor = forwardRef ( function SqlCodeEditor (
3435 props : ReactCodeMirrorProps & {
3536 schema ?: DatabaseSchemas ;
3637 currentDatabase ?: string ;
3738 dialect : QueryDialetType ;
3839 } ,
39- ref : Ref < ReactCodeMirrorRef >
40+ ref : Ref < ReactCodeMirrorRef > ,
4041) {
4142 const { schema, currentDatabase, ...codeMirrorProps } = props ;
43+ const { binding } = useKeybinding ( ) ;
4244 const theme = useCodeEditorTheme ( ) ;
4345
4446 const customAutoComplete = useCallback (
@@ -47,16 +49,16 @@ const SqlCodeEditor = forwardRef(function SqlCodeEditor(
4749 context ,
4850 tree ,
4951 schema ?. getSchema ( ) ,
50- currentDatabase
52+ currentDatabase ,
5153 ) ;
5254 } ,
53- [ schema , currentDatabase ]
55+ [ schema , currentDatabase ] ,
5456 ) ;
5557
5658 const tableNameHighlightPlugin = useMemo ( ( ) => {
5759 if ( schema && currentDatabase ) {
5860 return createSQLTableNameHighlightPlugin (
59- Object . keys ( schema . getTableList ( currentDatabase ) )
61+ Object . keys ( schema . getTableList ( currentDatabase ) ) ,
6062 ) ;
6163 }
6264 return createSQLTableNameHighlightPlugin ( [ ] ) ;
@@ -65,6 +67,44 @@ const SqlCodeEditor = forwardRef(function SqlCodeEditor(
6567 const dialect = props . dialect === 'mysql' ? MySQLDialect : PgDialect ;
6668 const tooltips = props . dialect === 'mysql' ? MySQLTooltips : PgTooltips ;
6769
70+ const keyExtension = useMemo ( ( ) => {
71+ return keymap . of ( [
72+ // Prevent the default behavior if it matches any of
73+ // these key binding. The reason is because the default
74+ // key binding for run is Ctrl + Enter. It is weird if
75+ // press Ctrl + Enter, will run and also insert newline
76+ // at the same time.
77+ ...[
78+ binding [ 'run-current-query' ] ,
79+ binding [ 'run-query' ] ,
80+ binding [ 'save-query' ] ,
81+ ] . map ( ( binding ) => ( {
82+ key : binding . toCodeMirrorKey ( ) ,
83+ preventDefault : true ,
84+ run : ( ) => true ,
85+ } ) ) ,
86+ {
87+ key : 'Tab' ,
88+ preventDefault : true ,
89+ run : ( target ) => {
90+ if ( completionStatus ( target . state ) === 'active' ) {
91+ acceptCompletion ( target ) ;
92+ } else {
93+ insertTab ( target ) ;
94+ }
95+ return true ;
96+ } ,
97+ } ,
98+ {
99+ key : 'Ctrl-Space' ,
100+ mac : 'Cmd-i' ,
101+ preventDefault : true ,
102+ run : startCompletion ,
103+ } ,
104+ ...defaultKeymap ,
105+ ] ) ;
106+ } , [ binding ] ) ;
107+
68108 return (
69109 < CodeMirror
70110 tabIndex = { 0 }
@@ -78,27 +118,7 @@ const SqlCodeEditor = forwardRef(function SqlCodeEditor(
78118 drawSelection : false ,
79119 } }
80120 extensions = { [
81- keymap . of ( [
82- {
83- key : 'Tab' ,
84- preventDefault : true ,
85- run : ( target ) => {
86- if ( completionStatus ( target . state ) === 'active' ) {
87- acceptCompletion ( target ) ;
88- } else {
89- insertTab ( target ) ;
90- }
91- return true ;
92- } ,
93- } ,
94- {
95- key : 'Ctrl-Space' ,
96- mac : 'Cmd-i' ,
97- preventDefault : true ,
98- run : startCompletion ,
99- } ,
100- ...defaultKeymap ,
101- ] ) ,
121+ keyExtension ,
102122 sql ( {
103123 dialect,
104124 } ) ,
0 commit comments