Skip to content

Improve Shift+Enter trigger in editor #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]"
}
19 changes: 15 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,15 @@ 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();
Expand All @@ -108,8 +119,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 @@ -125,6 +136,6 @@ const QueryEditorForm = ({ value, onRunQuery }: Props) => {

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