Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sql-editor): add a format SQL button (#935)
Yunwei Xiao357f02630bbc
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vueindex 5b8542cebb..86d67dcf8a 100644--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue@@ -28,6 +28,7 @@ import {SqlEditorActions,SqlEditorState,SheetGetters,+ SqlDialect,} from "../../types";import { useLineDecorations } from "./lineDecorations";@@ -62,17 +63,20 @@ const language = toRef(props, "language");const store = useStore();const { t } = useI18n();-const { shouldSetContent } = useNamespacedState<SqlEditorState>("sqlEditor", [- "shouldSetContent",-]);+const { shouldSetContent, shouldFormatContent } =+ useNamespacedState<SqlEditorState>("sqlEditor", [+ "shouldSetContent",+ "shouldFormatContent",+ ]);const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);const { isReadOnly } = useNamespacedGetters<SheetGetters>("sheet", ["isReadOnly",]);-const { setShouldSetContent } = useNamespacedActions<SqlEditorActions>(- "sqlEditor",- ["setShouldSetContent"]-);+const { setShouldSetContent, setShouldFormatContent } =+ useNamespacedActions<SqlEditorActions>("sqlEditor", [+ "setShouldSetContent",+ "setShouldFormatContent",+ ]);let editorInstance: Editor.IStandaloneCodeEditor;@@ -148,7 +152,7 @@ const init = async () => {id: "FormatSQL",label: "Format SQL",keybindings: [- monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyF,+ monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyF,],contextMenuGroupId: "operation",contextMenuOrder: 1,@@ -161,7 +165,7 @@ const init = async () => {});return;}- formatContent(editorInstance, language.value);+ formatContent(editorInstance, language.value as SqlDialect);nextTick(() => setPositionAtEndOfLine(editorInstance));},});@@ -235,6 +239,21 @@ watch(}});++// trigger format code from outside+watch(+ () => shouldFormatContent.value,+ () => {+ if (shouldFormatContent.value) {+ formatContent(editorInstance, language.value as SqlDialect);+ nextTick(() => {+ setPositionAtEndOfLine(editorInstance);+ editorInstance.focus();+ });+ setShouldFormatContent(false);+ }+ }+);</script><style>diff --git a/frontend/src/components/MonacoEditor/useMonaco.ts b/frontend/src/components/MonacoEditor/useMonaco.tsindex b1ad48afbd..3632d46692 100644--- a/frontend/src/components/MonacoEditor/useMonaco.ts+++ b/frontend/src/components/MonacoEditor/useMonaco.ts@@ -171,11 +171,39 @@ const useMonaco = async (lang: string) => {})(),]);+ /**+ * set new content in monaco editor+ * use executeEdits API can preserve undo stack, allow user to undo/redo+ * @param editorInstance Editor.IStandaloneCodeEditor+ * @param content string+ */const setContent = (editorInstance: Editor.IStandaloneCodeEditor,content: string) => {- if (editorInstance) editorInstance.setValue(content);+ if (editorInstance) {+ const range = editorInstance?.getModel()?.getFullModelRange();+ // get the current endLineNumber, or use 100000 as the default+ const endLineNumber =+ range && range?.endLineNumber > 0 ? range.endLineNumber + 1 : 100000;+ editorInstance.executeEdits("delete-content", [+ {+ range: new monaco.Range(1, 1, endLineNumber, 1),+ text: "",+ forceMoveMarkers: true,+ },+ ]);+ // set the new content+ editorInstance.executeEdits("insert-content", [+ {+ range: new monaco.Range(1, 1, 1, 1),+ text: content,+ forceMoveMarkers: true,+ },+ ]);+ // reset the selection+ editorInstance.setSelection(new monaco.Range(0, 0, 0, 0));+ }};const formatContent = (diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.ymlindex 35eec08f47..0842d3510c 100644--- a/frontend/src/locales/en.yml+++ b/frontend/src/locales/en.yml@@ -1178,6 +1178,7 @@ sql-editor:save-sheet: Save Sheetsave-sheet-input-placeholder: Please input a sheet nameuntitled-sheet: Untitled Sheet+ format: Formatlabel:empty-label-value: <Empty Value>no-label: No labelsdiff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.ymlindex c46584f5ea..e4377a6cd8 100644--- a/frontend/src/locales/zh-CN.yml+++ b/frontend/src/locales/zh-CN.yml@@ -1036,6 +1036,7 @@ sql-editor:save-sheet: 保存工作表save-sheet-input-placeholder: 请输入工作表名称untitled-sheet: 未命名工作表+ format: 格式化label:empty-label-value: <空值>no-label: 没有标签diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.tsindex 51ce68e03f..c542295df1 100644--- a/frontend/src/store/modules/sqlEditor.ts+++ b/frontend/src/store/modules/sqlEditor.ts@@ -38,6 +38,7 @@ const state: () => SqlEditorState = () => ({isExecuting: false,isShowExecutingHint: false,shouldSetContent: false,+ shouldFormatContent: false,// Related data and statusqueryHistoryList: [],isFetchingQueryHistory: false,@@ -148,6 +149,9 @@ const mutations = {[types.SET_SHOULD_SET_CONTENT](state: SqlEditorState, payload: boolean) {state.shouldSetContent = payload;},+ [types.SET_SHOULD_FORMAT_CONTENT](state: SqlEditorState, payload: boolean) {+ state.shouldFormatContent = payload;+ },[types.SET_IS_EXECUTING](state: SqlEditorState, payload: boolean) {state.isExecuting = payload;},@@ -170,6 +174,7 @@ type SqlEditorActionsMap = {setConnectionTree: typeof mutations.SET_CONNECTION_TREE;setConnectionContext: typeof mutations.SET_CONNECTION_CONTEXT;setShouldSetContent: typeof mutations.SET_SHOULD_SET_CONTENT;+ setShouldFormatContent: typeof mutations.SET_SHOULD_FORMAT_CONTENT;setIsExecuting: typeof mutations.SET_IS_EXECUTING;setQueryHistoryList: typeof mutations.SET_QUERY_HISTORY_LIST;setIsFetchingQueryHistory: typeof mutations.SET_IS_FETCHING_QUERY_HISTORY;@@ -181,6 +186,7 @@ const actions = {setConnectionTree: types.SET_CONNECTION_TREE,setConnectionContext: types.SET_CONNECTION_CONTEXT,setShouldSetContent: types.SET_SHOULD_SET_CONTENT,+ setShouldFormatContent: types.SET_SHOULD_FORMAT_CONTENT,setIsExecuting: types.SET_IS_EXECUTING,setQueryHistoryList: types.SET_QUERY_HISTORY_LIST,setIsFetchingQueryHistory: types.SET_IS_FETCHING_QUERY_HISTORY,diff --git a/frontend/src/store/mutation-types.ts b/frontend/src/store/mutation-types.tsindex 82cb1e5705..8eff9d8968 100644--- a/frontend/src/store/mutation-types.ts+++ b/frontend/src/store/mutation-types.ts@@ -3,6 +3,7 @@ export const SET_SQL_EDITOR_STATE = "SET_SQL_EDITOR_STATE";export const SET_CONNECTION_TREE = "SET_CONNECTION_TREE";export const SET_CONNECTION_CONTEXT = "SET_CONNECTION_CONTEXT";export const SET_SHOULD_SET_CONTENT = "SET_SHOULD_SET_CONTENT";+export const SET_SHOULD_FORMAT_CONTENT = "SET_SHOULD_FORMAT_CONTENT";export const SET_IS_EXECUTING = "SET_IS_EXECUTING";export const SET_QUERY_HISTORY_LIST = "SET_QUERY_HISTORY_LIST";export const SET_IS_FETCHING_QUERY_HISTORY = "SET_IS_FETCHING_QUERY_HISTORY";diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.tsindex 89154f4738..2a22911cd3 100644--- a/frontend/src/types/store.ts+++ b/frontend/src/types/store.ts@@ -196,6 +196,7 @@ export interface SqlEditorState {connectionTree: ConnectionAtom[];connectionContext: ConnectionContext;shouldSetContent: boolean;+ shouldFormatContent: boolean;queryHistoryList: QueryHistory[];isFetchingQueryHistory: boolean;isExecuting: boolean;diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vueindex 0f4faac531..e4231cd967 100644--- a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue@@ -1,6 +1,6 @@<template>- <div class="sqleditor-editor-actions">- <div class="actions-left w-1/3 space-x-2">+ <div class="editor-action">+ <div class="action-left space-x-2 flex items-center"><NButtontype="primary":disabled="isEmptyStatement || executeState.isLoadingData"@@ -14,8 +14,14 @@><mdi:play class="h-5 w-5" /> Explain (⌘+E)</NButton>+ <NButton+ :disabled="isEmptyStatement || executeState.isLoadingData"+ @click="handleFormatSQL"+ >+ {{ $t("sql-editor.format") }} (⇧+⌥+F)+ </NButton></div>- <div class="actions-right space-x-2 flex w-2/3 justify-end items-center">+ <div class="action-right space-x-2 flex justify-end items-center"><NPopoverv-if="connectionContext.instanceId !== UNKNOWN_ID && !hasReadonlyDataSource@@ -118,12 +124,14 @@ import { computed, ref, defineEmits } from "vue";import {useNamespacedState,useNamespacedGetters,+ useNamespacedActions,} from "vuex-composition-helpers";import { useStore } from "vuex";import {SqlEditorState,SqlEditorGetters,+ SqlEditorActions,TabGetters,UNKNOWN_ID,Instance,@@ -149,6 +157,11 @@ const { isDisconnected } = useNamespacedGetters<SqlEditorGetters>("sqlEditor", [const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);+const { setShouldFormatContent } = useNamespacedActions<SqlEditorActions>(+ "sqlEditor",+ ["setShouldFormatContent"]+);+const isShowSharePopover = ref(false);const isEmptyStatement = computed(@@ -199,10 +212,14 @@ const handleClickoutside = (e: any) => {const toggleSharePopover = () => {isShowSharePopover.value = !isShowSharePopover.value;};++const handleFormatSQL = () => {+ setShouldFormatContent(true);+};</script><style scoped>-.sqleditor-editor-actions {+.editor-action {@apply w-full flex justify-between items-center p-2;}</style>