Skip to content

Commit

Permalink
Improve Shift+Enter trigger in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelemeny committed Apr 9, 2024
1 parent 6bafb31 commit 7acd91b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
31 changes: 26 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"observable-hooks": "^4.2.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"tslib": "2.5.3"
"tslib": "2.5.3",
"usehooks-ts": "^3.1.0"
},
"packageManager": "[email protected]"
}
21 changes: 17 additions & 4 deletions src/components/QueryEditor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { css } from '@emotion/css';

import React, { createContext } from 'react';
import React, { createContext, useRef } from 'react';
import { debounceTime, throttleTime } from 'rxjs';
import { useObservableCallback, useSubscription } from 'observable-hooks'

import { useEventListener } from 'usehooks-ts'

import { CoreApp, Field, getDefaultTimeRange, GrafanaTheme2, QueryEditorProps } from '@grafana/data';
import { InlineLabel, useStyles2 } from '@grafana/ui';

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

const QueryEditorForm = ({ value, onRunQuery }: Props) => {

const editorRef = useRef<HTMLDivElement>(null)
const handleKeyBindings = (e: KeyboardEvent) => {
// Shift+Enter triggers onRunQuery if the active element is inside the editor
if (e.key === "Enter" && e.shiftKey && editorRef.current?.contains(document.activeElement)) {
onRunQuery()
}
e.stopPropagation();
}
useEventListener("keypress", handleKeyBindings)

const dispatch = useDispatch();
const nextId = useNextId();
const styles = useStyles2(getStyles);
Expand All @@ -107,8 +120,8 @@ const QueryEditorForm = ({ value, onRunQuery }: Props) => {
useSubscription(submitted$, onSubmit)

return (
<>
<div className={styles.root}>
<div ref={editorRef}>
<div className={styles.root} >
<InlineLabel width={17}>Query type</InlineLabel>
<div className={styles.queryItem}>
<QueryTypeSelector />
Expand All @@ -124,6 +137,6 @@ const QueryEditorForm = ({ value, onRunQuery }: Props) => {

<MetricAggregationsEditor nextId={nextId} />
{showBucketAggregationsEditor && <BucketAggregationsEditor nextId={nextId} />}
</>
</div>
);
};

0 comments on commit 7acd91b

Please sign in to comment.