Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(sql-editor): change the save sheet logic (#832)

Yunwei Xiao3479245625bf
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vue
index 2e5e049ea5..0f3ceba5b8 100644
--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue
@@ -191,6 +191,13 @@ const init = async () => {
emit("save", value);
});
+ // set the editor focus when the tab is selected
+ if (!isReadOnly.value) {
+ editorInstance.focus();
+
+ nextTick(() => setPositionAtEndOfLine(editorInstance));
+ }
+
watch(
() => isReadOnly.value,
(readOnly) => {
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 421e9312bb..899892336a 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -1172,6 +1172,9 @@ sql-editor:
public-desc: Sheet OWNER can read/write, and all others can read
create-read-only-data-source: Go to create
go-back: Go Back
+ save-sheet: Save Sheet
+ save-sheet-input-placeholder: Please input a sheet name
+ untitled-sheet: Untitled Sheet
label:
empty-label-value: <Empty Value>
no-label: No labels
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index 45446bfd49..3544e05653 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -1030,6 +1030,9 @@ sql-editor:
public-desc: 工作表所有者拥有读写权限,其他人只拥有只读权限.
create-read-only-data-source: 前去创建
go-back: 返回
+ save-sheet: 保存工作表
+ save-sheet-input-placeholder: 请输入工作表名称
+ untitled-sheet: 未命名工作表
label:
empty-label-value: <空值>
no-label: 没有标签
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
index 750cdad555..7c3f7a66d7 100644
--- a/frontend/src/store/modules/tab.ts
+++ b/frontend/src/store/modules/tab.ts
@@ -1,19 +1,7 @@
-import dayjs from "dayjs";
-import { v1 as uuidv1 } from "uuid";
import { TabInfo, AnyTabInfo, TabState } from "@/types";
import * as types from "../mutation-types";
import { makeActions } from "../actions";
-
-export const getDefaultTab = () => {
- return {
- id: uuidv1(),
- name: "Untitled Sheet",
- isSaved: false,
- savedAt: dayjs().format("YYYY-MM-DD HH:mm:ss"),
- statement: "",
- selectedStatement: "",
- };
-};
+import { getDefaultTab } from "../../utils/tab";
// tab store is the sheet store for frontend
const state: () => TabState = () => {
diff --git a/frontend/src/utils/tab.ts b/frontend/src/utils/tab.ts
new file mode 100644
index 0000000000..13d15bb520
--- /dev/null
+++ b/frontend/src/utils/tab.ts
@@ -0,0 +1,17 @@
+import dayjs from "dayjs";
+import { computed } from "vue";
+import { v1 as uuidv1 } from "uuid";
+import { t } from "../plugins/i18n";
+
+export const defaultTabName = computed(() => t("sql-editor.untitled-sheet"));
+
+export const getDefaultTab = () => {
+ return {
+ id: uuidv1(),
+ name: defaultTabName.value,
+ isSaved: true,
+ savedAt: dayjs().format("YYYY-MM-DD HH:mm:ss"),
+ statement: "",
+ selectedStatement: "",
+ };
+};
diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
index 3fab1a6105..57ee059e27 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue
@@ -87,7 +87,7 @@
strong
type="primary"
:disabled="isEmptyStatement || currentTab.isSaved"
- @click="handleSave"
+ @click="(e) => emit('save-sheet')"
>
<carbon:save class="h-5 w-5" /> &nbsp; {{ $t("common.save") }} (⌘+S)
</NButton>
@@ -115,10 +115,9 @@
</template>
<script lang="ts" setup>
-import { computed, ref } from "vue";
+import { computed, ref, defineEmits } from "vue";
import {
useNamespacedState,
- useNamespacedActions,
useNamespacedGetters,
} from "vuex-composition-helpers";
import { useStore } from "vuex";
@@ -127,8 +126,6 @@ import {
SqlEditorState,
SqlEditorGetters,
TabGetters,
- TabActions,
- SheetActions,
UNKNOWN_ID,
Instance,
} from "@/types";
@@ -137,6 +134,10 @@ import SharePopover from "./SharePopover.vue";
import { useRouter } from "vue-router";
import { instanceSlug } from "../../../utils/slug";
+const emit = defineEmits<{
+ (e: "save-sheet", content?: string): void;
+}>();
+
const store = useStore();
const router = useRouter();
@@ -149,15 +150,8 @@ const { isDisconnected } = useNamespacedGetters<SqlEditorGetters>("sqlEditor", [
const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
-// actions
-const { upsertSheet } = useNamespacedActions<SheetActions>("sheet", [
- "upsertSheet",
-]);
-const { updateCurrentTab } = useNamespacedActions<TabActions>("tab", [
- "updateCurrentTab",
-]);
-
const isShowSharePopover = ref(false);
+
const isEmptyStatement = computed(
() => !currentTab.value || currentTab.value.statement === ""
);
@@ -190,20 +184,6 @@ const handleExplainQuery = () => {
execute({ databaseType: selectedInstanceEngine.value }, { explain: true });
};
-const handleSave = async () => {
- const { name, statement, sheetId } = currentTab.value;
- const sheet = await upsertSheet({
- id: sheetId,
- name,
- statement,
- });
-
- updateCurrentTab({
- sheetId: sheet.id,
- isSaved: true,
- });
-};
-
const gotoInstanceDetailPage = () => {
router.push({
name: "workspace.instance.detail",
diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
index 5e766a4ce8..c92c22c235 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
@@ -1,9 +1,9 @@
<template>
<div class="editor-pane h-full">
- <EditorAction />
+ <EditorAction @save-sheet="handleSaveSheet" />
<template v-if="!isDisconnected">
- <QueryEditor />
+ <QueryEditor @save-sheet="handleSaveSheet" />
</template>
<template v-else>
<ConnectionHolder />
@@ -16,21 +16,38 @@
>
<ExecuteHint @close="handleClose" />
</BBModal>
+ <BBModal
+ v-if="isShowSaveSheetModal"
+ :title="$t('sql-editor.save-sheet')"
+ @close="handleCloseModal"
+ >
+ <SaveSheetModal @close="handleCloseModal" @save-sheet="handleSaveSheet" />
+ </BBModal>
</div>
</template>
<script lang="ts" setup>
+import { ref } from "vue";
import {
useNamespacedState,
useNamespacedGetters,
useNamespacedActions,
} from "vuex-composition-helpers";
-import { SqlEditorState, SqlEditorActions, SqlEditorGetters } from "@/types";
+import {
+ SqlEditorState,
+ SqlEditorActions,
+ SqlEditorGetters,
+ TabGetters,
+ TabActions,
+ SheetActions,
+} from "@/types";
import EditorAction from "./EditorAction.vue";
import QueryEditor from "./QueryEditor.vue";
import ExecuteHint from "./ExecuteHint.vue";
import ConnectionHolder from "./ConnectionHolder.vue";
+import SaveSheetModal from "./SaveSheetModal.vue";
+import { defaultTabName } from "../../../utils/tab";
const { isShowExecutingHint } = useNamespacedState<SqlEditorState>(
"sqlEditor",
@@ -45,10 +62,47 @@ const { setSqlEditorState } = useNamespacedActions<SqlEditorActions>(
"sqlEditor",
["setSqlEditorState"]
);
+const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
+
+// actions
+const { upsertSheet } = useNamespacedActions<SheetActions>("sheet", [
+ "upsertSheet",
+]);
+const { updateCurrentTab } = useNamespacedActions<TabActions>("tab", [
+ "updateCurrentTab",
+]);
+
+const isShowSaveSheetModal = ref(false);
const handleClose = () => {
setSqlEditorState({
isShowExecutingHint: false,
});
};
+
+const handleSaveSheet = async (sheetName?: string) => {
+ if (currentTab.value.name === defaultTabName.value && !sheetName) {
+ isShowSaveSheetModal.value = true;
+ return;
+ }
+ isShowSaveSheetModal.value = false;
+
+ const { name, statement, sheetId } = currentTab.value;
+
+ const sheet = await upsertSheet({
+ id: sheetId,
+ name: sheetName ? sheetName : name,
+ statement,
+ });
+
+ updateCurrentTab({
+ sheetId: sheet.id,
+ isSaved: true,
+ name: sheetName ? sheetName : name,
+ });
+};
+
+const handleCloseModal = () => {
+ isShowSaveSheetModal.value = false;
+};
</script>
diff --git a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
index a3ef845952..1286f4d79c 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue
@@ -4,13 +4,13 @@
@change="handleChange"
@change-selection="handleChangeSelection"
@run-query="handleRunQuery"
- @save="handleSave"
+ @save="(e) => emit('save-sheet')"
/>
</template>
<script lang="ts" setup>
import { debounce } from "lodash-es";
-import { computed } from "vue";
+import { computed, defineEmits } from "vue";
import { useStore } from "vuex";
import {
useNamespacedActions,
@@ -19,7 +19,11 @@ import {
} from "vuex-composition-helpers";
import { useExecuteSQL } from "@/composables/useExecuteSQL";
-import { TabActions, TabGetters, SheetActions, SqlEditorState } from "@/types";
+import { TabActions, TabGetters, SqlEditorState } from "@/types";
+
+const emit = defineEmits<{
+ (e: "save-sheet", content?: string): void;
+}>();
const store = useStore();
const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
@@ -29,9 +33,6 @@ const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
const { updateCurrentTab } = useNamespacedActions<TabActions>("tab", [
"updateCurrentTab",
]);
-const { upsertSheet } = useNamespacedActions<SheetActions>("sheet", [
- "upsertSheet",
-]);
const { execute } = useExecuteSQL();
@@ -59,21 +60,6 @@ const handleChangeSelection = debounce((value: string) => {
});
}, 300);
-const handleSave = async (statement: string) => {
- const { name, sheetId } = currentTab.value;
-
- const sheet = await upsertSheet({
- id: sheetId,
- name,
- statement,
- });
-
- updateCurrentTab({
- sheetId: sheet.id,
- isSaved: true,
- });
-};
-
const handleRunQuery = ({
explain,
query,
diff --git a/frontend/src/views/SqlEditor/EditorPanel/SaveSheetModal.vue b/frontend/src/views/SqlEditor/EditorPanel/SaveSheetModal.vue
new file mode 100644
index 0000000000..f326ce3946
--- /dev/null
+++ b/frontend/src/views/SqlEditor/EditorPanel/SaveSheetModal.vue
@@ -0,0 +1,38 @@
+<template>
+ <div class="save-sheet-modal w-80">
+ <NInput
+ ref="sheetNameInputRef"
+ v-model:value="sheetName"
+ :placeholder="$t('sql-editor.save-sheet-input-placeholder')"
+ @keyup.enter="(e: Event) => emit('save-sheet', sheetName)"
+ />
+ </div>
+ <div class="mt-4 flex justify-end space-x-2">
+ <NButton @click="(e: Event) => emit('close')">{{
+ $t("common.close")
+ }}</NButton>
+ <NButton type="primary" @click="emit('save-sheet', sheetName)">
+ {{ $t("common.save") }}
+ </NButton>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { ref, nextTick, defineEmits } from "vue";
+import { useNamespacedGetters } from "vuex-composition-helpers";
+import { TabGetters } from "../../../types";
+
+const emit = defineEmits<{
+ (e: "close"): void;
+ (e: "save-sheet", content: string): void;
+}>();
+
+const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
+
+const sheetName = ref(currentTab.value.name);
+const sheetNameInputRef = ref();
+
+nextTick(() => {
+ sheetNameInputRef.value?.focus();
+});
+</script>
diff --git a/frontend/src/views/SqlEditor/TabListContainer.vue b/frontend/src/views/SqlEditor/TabListContainer.vue
index edfab73bad..c52a51cf6e 100644
--- a/frontend/src/views/SqlEditor/TabListContainer.vue
+++ b/frontend/src/views/SqlEditor/TabListContainer.vue
@@ -86,7 +86,7 @@
<button
class="p-1 hover:bg-gray-200 rounded-md"
:class="{ 'cursor-not-allowed': isDisconnected }"
- @click="handleAddTab({})"
+ @click="handleAddTab"
>
<heroicons-solid:plus class="h-4 w-4" />
</button>
@@ -118,7 +118,7 @@
<script lang="ts" setup>
import { ref, reactive, nextTick, computed, onMounted, onUnmounted } from "vue";
-import { debounce, cloneDeep } from "lodash-es";
+import { debounce } from "lodash-es";
import { useI18n } from "vue-i18n";
import { useStore } from "vuex";
import {
@@ -130,14 +130,13 @@ import { useDialog } from "naive-ui";
import {
TabInfo,
- AnyTabInfo,
SqlEditorGetters,
TabGetters,
TabState,
TabActions,
SheetActions,
} from "@/types";
-import { getDefaultTab } from "@/store/modules/tab";
+import { getDefaultTab } from "@/utils/tab";
import { useSQLEditorConnection } from "@/composables/useSQLEditorConnection";
// getters map
@@ -163,10 +162,9 @@ const { addTab, removeTab, setCurrentTabId, updateCurrentTab } =
"setCurrentTabId",
"updateCurrentTab",
]);
-const { createSheet, patchSheetById } = useNamespacedActions<SheetActions>(
- "sheet",
- ["createSheet", "patchSheetById"]
-);
+const { patchSheetById } = useNamespacedActions<SheetActions>("sheet", [
+ "patchSheetById",
+]);
const store = useStore();
const { t } = useI18n();
@@ -260,24 +258,12 @@ const handleSelectTab = async (tab: TabInfo) => {
setCurrentTabId(tab.id);
setConnectionContextFromCurrentTab();
};
-const handleAddTab = (tab: AnyTabInfo) => {
+const handleAddTab = () => {
if (isDisconnected.value) return;
- addTab(tab);
-
- nextTick(async () => {
- const tab = cloneDeep(currentTab.value);
- handleEditLabel(tab);
+ addTab(getDefaultTab());
- // make a relation between the new sheet and the current tab
- const newSheet = await createSheet();
-
- updateCurrentTab({
- sheetId: newSheet.id,
- });
-
- recalculateScrollWidth();
- });
+ nextTick(recalculateScrollWidth);
};
const handleRemoveTab = async (tab: TabInfo) => {