Skip to content

Commit bcdefd5

Browse files
authored
feat: use monaco snippets for query templates (#1626)
1 parent 507aa71 commit bcdefd5

File tree

8 files changed

+122
-76
lines changed

8 files changed

+122
-76
lines changed

src/containers/Tenant/Query/NewSQL/NewSQL.tsx

+4-14
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@ import React from 'react';
33
import {ChevronDown} from '@gravity-ui/icons';
44
import {Button, DropdownMenu} from '@gravity-ui/uikit';
55

6-
import {changeUserInput} from '../../../../store/reducers/executeQuery';
7-
import {useTypedDispatch} from '../../../../utils/hooks';
86
import {useChangeInputWithConfirmation} from '../../../../utils/hooks/withConfirmation/useChangeInputWithConfirmation';
7+
import {insertSnippetToEditor} from '../../../../utils/monaco/insertSnippet';
98
import {bindActions} from '../../utils/newSQLQueryActions';
109

1110
import i18n from './i18n';
1211

1312
export function NewSQL() {
14-
const dispatch = useTypedDispatch();
15-
16-
const insertTemplate = React.useCallback(
17-
(input: string) => {
18-
dispatch(changeUserInput({input}));
19-
},
20-
[dispatch],
21-
);
13+
const insertTemplate = React.useCallback((input: string) => {
14+
insertSnippetToEditor(input);
15+
}, []);
2216

2317
const onTemplateClick = useChangeInputWithConfirmation(insertTemplate);
2418

@@ -56,10 +50,6 @@ export function NewSQL() {
5650
text: i18n('action.select-rows'),
5751
action: actions.selectQuery,
5852
},
59-
{
60-
text: i18n('action.select-from-external-table'),
61-
action: actions.selectQueryFromExternalTable,
62-
},
6353
{
6454
text: i18n('action.delete-rows'),
6555
action: actions.deleteRows,

src/containers/Tenant/Query/NewSQL/i18n/en.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
2-
"button.new-sql": "New SQL",
2+
"button.new-sql": "New query",
33
"action.create-row-table": "Create row table",
44
"action.create-column-table": "Create column table",
55
"action.create-external-table": "Create external table",
66
"action.upsert-to-table": "Upsert into table",
77
"action.update-table": "Update table",
88
"action.alter-table": "Alter table",
99
"action.select-rows": "Select from a table",
10-
"action.select-from-external-table": "Select from external table",
1110
"action.delete-rows": "Delete rows",
1211
"action.drop-table": "Drop table",
1312
"action.add-index": "Add index",

src/containers/Tenant/Query/QueryEditor/QueryEditor.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,22 @@ export default function QueryEditor(props: QueryEditorProps) {
200200
}
201201
});
202202

203+
const editorWillUnmount = () => {
204+
window.ydbEditor = undefined;
205+
};
206+
203207
const editorDidMount = (editor: Monaco.editor.IStandaloneCodeEditor, monaco: typeof Monaco) => {
208+
window.ydbEditor = editor;
204209
const keybindings = getKeyBindings(monaco);
210+
monaco.editor.registerCommand('insertSnippetToEditor', (_asessor, input: string) => {
211+
//suggestController is not properly typed yet in monaco-editor package
212+
const contribution = editor.getContribution<any>('snippetController2');
213+
if (contribution) {
214+
editor.focus();
215+
editor.setValue('');
216+
contribution.insert(input);
217+
}
218+
});
205219
initResizeHandler(editor);
206220
initUserPrompt(editor, getLastQueryText);
207221
editor.focus();
@@ -333,6 +347,7 @@ export default function QueryEditor(props: QueryEditorProps) {
333347
onChange={onChange}
334348
editorDidMount={editorDidMount}
335349
theme={`vs-${theme}`}
350+
editorWillUnmount={editorWillUnmount}
336351
/>
337352
</div>
338353
</div>

src/containers/Tenant/utils/newSQLQueryActions.ts

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export const bindActions = (changeUserInput: (input: string) => void) => {
4343
upsertQuery: inputQuery(upsertQueryTemplate),
4444
createExternalTable: inputQuery(createExternalTableTemplate),
4545
dropExternalTable: inputQuery(dropExternalTableTemplate),
46-
selectQueryFromExternalTable: inputQuery(selectQueryTemplate),
4746
createTopic: inputQuery(createTopicTemplate),
4847
alterTopic: inputQuery(alterTopicTemplate),
4948
dropTopic: inputQuery(dropTopicTemplate),

src/containers/Tenant/utils/schemaActions.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import copy from 'copy-to-clipboard';
22
import type {NavigationTreeNodeType, NavigationTreeProps} from 'ydb-ui-components';
33

44
import type {AppDispatch} from '../../../store';
5-
import {changeUserInput} from '../../../store/reducers/executeQuery';
65
import type {GetTableSchemaDataParams} from '../../../store/reducers/tableSchemaData';
76
import {TENANT_PAGES_IDS, TENANT_QUERY_TABS_ID} from '../../../store/reducers/tenant/constants';
87
import {setQueryTab, setTenantPage} from '../../../store/reducers/tenant/tenant';
98
import type {QuerySettings} from '../../../types/store/query';
109
import createToast from '../../../utils/createToast';
10+
import {insertSnippetToEditor} from '../../../utils/monaco/insertSnippet';
1111
import {transformPath} from '../ObjectSummary/transformPath';
1212
import type {SchemaData} from '../Schema/SchemaViewer/types';
1313
import i18n from '../i18n';
@@ -75,13 +75,13 @@ const bindActions = (
7575
})
7676
: Promise.resolve(undefined);
7777

78-
userInputDataPromise.then((tableData) => {
79-
dispatch(changeUserInput({input: tmpl({...params, tableData})}));
80-
});
81-
78+
//order is important here: firstly we should open query tab and initialize editor (it will be set to window.ydbEditor), after that it is possible to insert snippet
8279
dispatch(setTenantPage(TENANT_PAGES_IDS.query));
8380
dispatch(setQueryTab(TENANT_QUERY_TABS_ID.newQuery));
8481
setActivePath(params.path);
82+
userInputDataPromise.then((tableData) => {
83+
insertSnippetToEditor(tmpl({...params, tableData}));
84+
});
8585
};
8686
if (getConfirmation) {
8787
const confirmedPromise = getConfirmation();

0 commit comments

Comments
 (0)