Skip to content

Commit 7acd91b

Browse files
committed
Improve Shift+Enter trigger in editor
1 parent 6bafb31 commit 7acd91b

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

package-lock.json

Lines changed: 26 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
"observable-hooks": "^4.2.3",
8585
"react": "17.0.2",
8686
"react-dom": "17.0.2",
87-
"tslib": "2.5.3"
87+
"tslib": "2.5.3",
88+
"usehooks-ts": "^3.1.0"
8889
},
8990
"packageManager": "[email protected]"
9091
}

src/components/QueryEditor/index.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { css } from '@emotion/css';
22

3-
import React, { createContext } from 'react';
3+
import React, { createContext, useRef } from 'react';
44
import { debounceTime, throttleTime } from 'rxjs';
55
import { useObservableCallback, useSubscription } from 'observable-hooks'
66

7+
import { useEventListener } from 'usehooks-ts'
8+
79
import { CoreApp, Field, getDefaultTimeRange, GrafanaTheme2, QueryEditorProps } from '@grafana/data';
810
import { InlineLabel, useStyles2 } from '@grafana/ui';
911

@@ -84,6 +86,17 @@ export const ElasticSearchQueryField = ({ value, onChange, onSubmit }: ElasticSe
8486
};
8587

8688
const QueryEditorForm = ({ value, onRunQuery }: Props) => {
89+
90+
const editorRef = useRef<HTMLDivElement>(null)
91+
const handleKeyBindings = (e: KeyboardEvent) => {
92+
// Shift+Enter triggers onRunQuery if the active element is inside the editor
93+
if (e.key === "Enter" && e.shiftKey && editorRef.current?.contains(document.activeElement)) {
94+
onRunQuery()
95+
}
96+
e.stopPropagation();
97+
}
98+
useEventListener("keypress", handleKeyBindings)
99+
87100
const dispatch = useDispatch();
88101
const nextId = useNextId();
89102
const styles = useStyles2(getStyles);
@@ -107,8 +120,8 @@ const QueryEditorForm = ({ value, onRunQuery }: Props) => {
107120
useSubscription(submitted$, onSubmit)
108121

109122
return (
110-
<>
111-
<div className={styles.root}>
123+
<div ref={editorRef}>
124+
<div className={styles.root} >
112125
<InlineLabel width={17}>Query type</InlineLabel>
113126
<div className={styles.queryItem}>
114127
<QueryTypeSelector />
@@ -124,6 +137,6 @@ const QueryEditorForm = ({ value, onRunQuery }: Props) => {
124137

125138
<MetricAggregationsEditor nextId={nextId} />
126139
{showBucketAggregationsEditor && <BucketAggregationsEditor nextId={nextId} />}
127-
</>
140+
</div>
128141
);
129142
};

0 commit comments

Comments
 (0)