Skip to content
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

Added shift+enter to run query in raw query editor #971

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 20 additions & 5 deletions src/components/QueryEditor/RawQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { QueryEditorProps, SelectableValue } from '@grafana/data';
import { getTemplateSrv, reportInteraction } from '@grafana/runtime';
import { CodeEditor, Monaco, MonacoEditor } from '@grafana/ui';
import { AdxDataSource } from 'datasource';
import React, { useEffect, useState } from 'react';
import { cloneDeep } from 'lodash';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { selectors } from 'test/selectors';
import { AdxDataSourceOptions, AdxSchema, KustoQuery } from 'types';
import { cloneDeep } from 'lodash';

import { getFunctions, getSignatureHelp } from './Suggestions';

Expand All @@ -27,25 +27,40 @@ export const RawQueryEditor: React.FC<RawQueryEditorProps> = (props) => {
const [worker, setWorker] = useState<Worker>();
const [variables] = useState(getTemplateSrv().getVariables());
const [stateSchema, setStateSchema] = useState(cloneDeep(schema));
const editorRef = useRef<MonacoEditor | null>(null);

const onRawQueryChange = (kql: string) => {
const onRawQueryChange = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const onRawQueryChange = () => {
const onRawQueryChange = useCallback(
(kql: string) => {
reportInteraction('grafana_ds_adx_raw_editor_query_blurred');
if (kql !== props.query.query) {
props.setDirty();
props.onChange({
...props.query,
query: kql,
});
props.onRunQuery();
}
},
[props]
);

reportInteraction('grafana_ds_adx_raw_editor_query_blurred');
const kql = editorRef.current?.getValue() || '';
if (kql !== props.query.query) {
props.setDirty();
props.onChange({
...props.query,
query: kql,
});
props.onRunQuery();
}
};

const onKeyDownCapture = useCallback(
(e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.key === 'Enter' && e.shiftKey) {
e.preventDefault();
e.stopPropagation();
onRawQueryChange();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onRawQueryChange();
onRawQueryChange(editorRef.current?.getValue() || '');

}
},
[onRawQueryChange]
);

useEffect(() => {
if (schema && !stateSchema) {
setStateSchema(cloneDeep(schema));
}
}, [schema, stateSchema]);

const handleEditorMount = (editor: MonacoEditor, monaco: Monaco) => {
editorRef.current = editor;
monaco.languages.registerSignatureHelpProvider('kusto', {
signatureHelpTriggerCharacters: ['(', ')'],
provideSignatureHelp: getSignatureHelp,
Expand Down Expand Up @@ -85,7 +100,7 @@ export const RawQueryEditor: React.FC<RawQueryEditorProps> = (props) => {
};

useEffect(() => {
if (worker && stateSchema) {
if (worker && stateSchema && stateSchema.Databases) {
// Populate Database schema with macros
Object.keys(stateSchema.Databases).forEach((db) =>
Object.assign(stateSchema.Databases[db].Functions, getFunctions(variables))
Expand All @@ -99,7 +114,7 @@ export const RawQueryEditor: React.FC<RawQueryEditorProps> = (props) => {
}

return (
<div>
<div onKeyDownCapture={onKeyDownCapture}>
<div data-testid={selectors.components.queryEditor.codeEditor.container}>
<CodeEditor
language="kusto"
Expand Down
Loading