Skip to content

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

feat(sql-editor): add batch close and rename context menu to tab list (#7843)

Liu Ji73859c370498
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 029b900e41..227c09af6f 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1908,7 +1908,17 @@
"connection": "Connection"
},
"tab": {
- "unsaved": "Unsaved"
+ "unsaved": "Unsaved",
+ "context-menu": {
+ "actions": {
+ "close": "Close",
+ "close-others": "Close others",
+ "close-to-the-right": "Close to the right",
+ "close-saved": "Close saved",
+ "close-all": "Close all",
+ "rename": "Rename"
+ }
+ }
}
},
"schema-editor": {
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index c357a66a67..e75d1e1fb1 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1904,7 +1904,17 @@
"connection": "Conexión"
},
"tab": {
- "unsaved": "No guardado"
+ "unsaved": "No guardado",
+ "context-menu": {
+ "actions": {
+ "close": "Cerca",
+ "close-others": "cerrar otros",
+ "close-to-the-right": "Cerca de la derecha",
+ "close-saved": "Cerrar guardado",
+ "close-all": "Cierra todo",
+ "rename": "Rebautizar"
+ }
+ }
}
},
"schema-editor": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index fef732a9ae..271288158c 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1903,7 +1903,17 @@
"connection": "连接"
},
"tab": {
- "unsaved": "未保存"
+ "unsaved": "未保存",
+ "context-menu": {
+ "actions": {
+ "close": "关闭",
+ "close-others": "关闭其他",
+ "close-to-the-right": "关闭右侧所有的",
+ "close-saved": "关闭已保存的",
+ "close-all": "关闭所有",
+ "rename": "重命名"
+ }
+ }
}
},
"schema-editor": {
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
index 5c2c4552e8..99322179fa 100644
--- a/frontend/src/store/modules/tab.ts
+++ b/frontend/src/store/modules/tab.ts
@@ -286,3 +286,17 @@ const maybeMigrateLegacyTab = (
}
return tab;
};
+
+export const isTabClosable = (tab: TabInfo) => {
+ const { tabList } = useTabStore();
+
+ if (tabList.length > 1) {
+ // Not the only one tab
+ return true;
+ }
+ if (tabList.length === 1) {
+ // It's the only one tab, and it's closable if it's a sheet tab
+ return !!tab.sheetName;
+ }
+ return false;
+};
diff --git a/frontend/src/views/sql-editor/TabList/ContextMenu.vue b/frontend/src/views/sql-editor/TabList/ContextMenu.vue
new file mode 100644
index 0000000000..1228844b9c
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/ContextMenu.vue
@@ -0,0 +1,116 @@
+<template>
+ <NDropdown
+ v-if="state"
+ trigger="manual"
+ placement="bottom-start"
+ :show="true"
+ :x="state?.x"
+ :y="state?.y"
+ :options="options"
+ @clickoutside="state = undefined"
+ @update:show="handleUpdateShow"
+ @select="handleSelect"
+ />
+</template>
+
+<script setup lang="ts">
+import { NDropdown, DropdownOption } from "naive-ui";
+import { computed, nextTick } from "vue";
+import { useI18n } from "vue-i18n";
+import { TabInfo, TabMode } from "@/types";
+import { CloseTabAction, useTabListContext } from "./context";
+
+const { t } = useI18n();
+const { contextMenu: state, events } = useTabListContext();
+
+const options = computed((): DropdownOption[] => {
+ if (!state.value) {
+ return [];
+ }
+
+ const CLOSE: DropdownOption = {
+ key: "CLOSE",
+ label: t("sql-editor.tab.context-menu.actions.close"),
+ };
+ const CLOSE_OTHERS: DropdownOption = {
+ key: "CLOSE_OTHERS",
+ label: t("sql-editor.tab.context-menu.actions.close-others"),
+ };
+ const CLOSE_TO_THE_RIGHT: DropdownOption = {
+ key: "CLOSE_TO_THE_RIGHT",
+ label: t("sql-editor.tab.context-menu.actions.close-to-the-right"),
+ };
+ const CLOSE_SAVED: DropdownOption = {
+ key: "CLOSE_SAVED",
+ label: t("sql-editor.tab.context-menu.actions.close-saved"),
+ };
+ const CLOSE_ALL: DropdownOption = {
+ key: "CLOSE_ALL",
+ label: t("sql-editor.tab.context-menu.actions.close-all"),
+ };
+
+ const options = [
+ CLOSE,
+ CLOSE_OTHERS,
+ CLOSE_TO_THE_RIGHT,
+ CLOSE_SAVED,
+ CLOSE_ALL,
+ ];
+
+ const { tab } = state.value;
+ if (tab.mode === TabMode.ReadOnly) {
+ const DIVIDER: DropdownOption = {
+ type: "divider",
+ key: "DIVIDER",
+ };
+ const RENAME: DropdownOption = {
+ key: "RENAME",
+ label: t("sql-editor.tab.context-menu.actions.rename"),
+ };
+
+ options.push(DIVIDER, RENAME);
+ }
+
+ return options;
+});
+
+const show = (tab: TabInfo, index: number, e: MouseEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ state.value = undefined;
+ nextTick(() => {
+ const { pageX, pageY } = e;
+ state.value = {
+ x: pageX,
+ y: pageY,
+ tab,
+ index,
+ };
+ });
+};
+
+const hide = () => {
+ state.value = undefined;
+};
+
+const handleUpdateShow = (show: boolean) => {
+ if (!show) {
+ hide();
+ }
+};
+
+const handleSelect = (action: CloseTabAction | "RENAME") => {
+ if (!state.value) return;
+ const { tab, index } = state.value;
+ if (action === "RENAME") {
+ events.emit("rename-tab", { tab, index });
+ } else {
+ events.emit("close-tab", { tab, index, action });
+ }
+};
+
+defineExpose({
+ show,
+ hide,
+});
+</script>
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/Label.vue b/frontend/src/views/sql-editor/TabList/TabItem/Label.vue
index 2ea39c5f55..e906713f2a 100644
--- a/frontend/src/views/sql-editor/TabList/TabItem/Label.vue
+++ b/frontend/src/views/sql-editor/TabList/TabItem/Label.vue
@@ -27,8 +27,10 @@
<script lang="ts" setup>
import { NEllipsis } from "naive-ui";
import { computed, nextTick, PropType, reactive, ref, watch } from "vue";
+import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
import { useSheetV1Store, useTabStore } from "@/store";
import type { TabInfo } from "@/types";
+import { useTabListContext } from "../context";
type LocalState = {
editing: boolean;
@@ -54,9 +56,17 @@ const state = reactive<LocalState>({
const tabStore = useTabStore();
const sheetV1Store = useSheetV1Store();
const inputRef = ref<HTMLInputElement>();
+const { events } = useTabListContext();
const isCurrentTab = computed(() => props.tab.id === tabStore.currentTabId);
+useEmitteryEventListener(events, "rename-tab", ({ tab }) => {
+ if (tab.id === props.tab.id) {
+ tabStore.setCurrentTabId(tab.id);
+ beginEdit();
+ }
+});
+
const beginEdit = () => {
state.editing = true;
state.name = props.tab.name;
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue b/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue
index 4d140d3639..a3ac45025a 100644
--- a/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue
+++ b/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue
@@ -24,7 +24,7 @@
<script lang="ts" setup>
import { computed, PropType, reactive } from "vue";
-import { useTabStore } from "@/store";
+import { isTabClosable } from "@/store";
import type { TabInfo } from "@/types";
import { TabMode } from "@/types";
import { sheetTypeForTab } from "@/utils";
@@ -54,19 +54,8 @@ defineEmits<{
(e: "close", tab: TabInfo, index: number): void;
}>();
-const tabStore = useTabStore();
-
const closable = computed(() => {
- const { tabList } = tabStore;
- if (tabList.length > 1) {
- // Not the only one tab
- return true;
- }
- if (tabList.length === 1) {
- // It's the only one tab, and it's closable if it's a sheet tab
- return !!props.tab.sheetName;
- }
- return false;
+ return isTabClosable(props.tab);
});
const icon = computed((): IconType | undefined => {
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue b/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue
index 8449a06601..ba30195cec 100644
--- a/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue
+++ b/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue
@@ -9,7 +9,7 @@
},
[sheetTypeForTab(tab).toLowerCase()],
]"
- @mousedown="$emit('select', tab, index)"
+ @mousedown.left="$emit('select', tab, index)"
@mouseenter="state.hovering = true"
@mouseleave="state.hovering = false"
>
@@ -66,7 +66,7 @@ const isCurrentTab = computed(() => props.tab.id === currentTabId.value);
<style scoped lang="postcss">
.tab-item {
- @apply cursor-pointer border-r bg-white gap-x-2;
+ @apply cursor-pointer border-r bg-white gap-x-2 relative;
}
.hovering {
@apply bg-gray-50;
diff --git a/frontend/src/views/sql-editor/TabList/TabList.vue b/frontend/src/views/sql-editor/TabList/TabList.vue
index f6cece113a..73696d1667 100644
--- a/frontend/src/views/sql-editor/TabList/TabList.vue
+++ b/frontend/src/views/sql-editor/TabList/TabList.vue
@@ -28,6 +28,9 @@
:data-tab-id="id"
@select="(tab) => handleSelectTab(tab)"
@close="(tab, index) => handleRemoveTab(tab, index)"
+ @contextmenu.stop.prevent="
+ contextMenuRef?.show(tabStore.getTabById(id), index, $event)
+ "
/>
</template>
</Draggable>
@@ -44,6 +47,8 @@
{{ $t("sql-editor.sheet.choose-sheet") }}
</NButton>
</div>
+
+ <ContextMenu ref="contextMenuRef" />
</div>
</template>
@@ -54,12 +59,15 @@ import scrollIntoView from "scroll-into-view-if-needed";
import { ref, reactive, nextTick, computed, onMounted, watch } from "vue";
import { useI18n } from "vue-i18n";
import Draggable from "vuedraggable";
+import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
import { useTabStore } from "@/store";
import type { TabInfo } from "@/types";
import { TabMode } from "@/types";
-import { getSuggestedTabNameFromConnection } from "@/utils";
+import { defer, getSuggestedTabNameFromConnection } from "@/utils";
import { useSheetContext } from "../Sheet";
+import ContextMenu from "./ContextMenu.vue";
import TabItem from "./TabItem";
+import { provideTabListContext } from "./context";
type LocalState = {
dragging: boolean;
@@ -75,9 +83,10 @@ const state = reactive<LocalState>({
dragging: false,
hoverTabId: "",
});
-
const { showPanel: showSheetPanel, events: sheetEvents } = useSheetContext();
const tabListRef = ref<InstanceType<typeof Draggable>>();
+const context = provideTabListContext();
+const contextMenuRef = ref<InstanceType<typeof ContextMenu>>();
const scrollState = reactive({
moreLeft: false,
@@ -101,8 +110,16 @@ const handleAddTab = () => {
sheetEvents.emit("add-sheet");
};
-const handleRemoveTab = async (tab: TabInfo, index: number) => {
+const handleRemoveTab = async (
+ tab: TabInfo,
+ index: number,
+ focusWhenConfirm = false
+) => {
+ const _defer = defer<boolean>();
if (tab.mode === TabMode.ReadOnly && !tab.isSaved) {
+ if (focusWhenConfirm) {
+ tabStore.setCurrentTabId(tab.id);
+ }
const $dialog = dialog.create({
title: t("sql-editor.hint-tips.confirm-to-close-unsaved-sheet.title"),
content: t("sql-editor.hint-tips.confirm-to-close-unsaved-sheet.content"),
@@ -114,9 +131,11 @@ const handleRemoveTab = async (tab: TabInfo, index: number) => {
onPositiveClick() {
remove(index);
$dialog.destroy();
+ _defer.resolve(true);
},
onNegativeClick() {
$dialog.destroy();
+ _defer.resolve(false);
},
positiveText: t("sql-editor.close-sheet"),
negativeText: t("common.cancel"),
@@ -124,6 +143,7 @@ const handleRemoveTab = async (tab: TabInfo, index: number) => {
});
} else {
remove(index);
+ _defer.resolve(true);
}
function remove(index: number) {
@@ -141,6 +161,8 @@ const handleRemoveTab = async (tab: TabInfo, index: number) => {
nextTick(recalculateScrollState);
}
+
+ return _defer.promise;
};
const tabListElement = computed((): HTMLElement | undefined => {
@@ -155,6 +177,8 @@ useResizeObserver(tabListRef, () => {
});
const recalculateScrollState = () => {
+ contextMenuRef.value?.hide();
+
const element = tabListElement.value;
if (!element) {
return;
@@ -201,6 +225,54 @@ watch(
);
onMounted(() => recalculateScrollState());
+
+useEmitteryEventListener(
+ context.events,
+ "close-tab",
+ async ({ tab, index, action }) => {
+ const tabList = tabStore.tabList;
+
+ const remove = async (tab: TabInfo, index: number) => {
+ await handleRemoveTab(tab, index, true);
+ await new Promise((r) => requestAnimationFrame(r));
+ };
+
+ if (action === "CLOSE") {
+ await remove(tab, index);
+ return;
+ }
+ const max = tabList.length - 1;
+ if (action === "CLOSE_OTHERS") {
+ for (let i = max; i > index; i--) {
+ await remove(tabList[i], i);
+ }
+ for (let i = index - 1; i >= 0; i--) {
+ await remove(tabList[i], i);
+ }
+ return;
+ }
+ if (action === "CLOSE_TO_THE_RIGHT") {
+ for (let i = max; i > index; i--) {
+ await remove(tabList[i], i);
+ }
+ return;
+ }
+ if (action === "CLOSE_SAVED") {
+ for (let i = max; i >= 0; i--) {
+ const tab = tabList[i];
+ if (tab.isSaved) {
+ await remove(tab, i);
+ }
+ }
+ return;
+ }
+ if (action === "CLOSE_ALL") {
+ for (let i = max; i >= 0; i--) {
+ await remove(tabList[i], i);
+ }
+ }
+ }
+);
</script>
<style scoped lang="postcss">
diff --git a/frontend/src/views/sql-editor/TabList/context.ts b/frontend/src/views/sql-editor/TabList/context.ts
new file mode 100644
index 0000000000..a43108ab51
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/context.ts
@@ -0,0 +1,46 @@
+import Emittery from "emittery";
+import { InjectionKey, Ref, inject, provide, ref } from "vue";
+import { TabInfo } from "@/types";
+
+export type CloseTabAction =
+ | "CLOSE"
+ | "CLOSE_OTHERS"
+ | "CLOSE_TO_THE_RIGHT"
+ | "CLOSE_SAVED"
+ | "CLOSE_ALL";
+
+type TabListEvents = Emittery<{
+ "close-tab": { tab: TabInfo; index: number; action: CloseTabAction };
+ "rename-tab": { tab: TabInfo; index: number };
+}>;
+
+export type TabListContextMenuState = {
+ x: number;
+ y: number;
+ tab: TabInfo;
+ index: number;
+};
+
+export type TabListContext = {
+ contextMenu: Ref<TabListContextMenuState | undefined>;
+ events: TabListEvents;
+};
+
+export const KEY = Symbol(
+ "bb.sql-editor.tab-list"
+) as InjectionKey<TabListContext>;
+
+export const useTabListContext = () => {
+ return inject(KEY)!;
+};
+
+export const provideTabListContext = () => {
+ const context: TabListContext = {
+ contextMenu: ref(),
+ events: new Emittery(),
+ };
+
+ provide(KEY, context);
+
+ return context;
+};