Skip to content

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

feat(sql-editor): drag-to-sort tab list (#2941)

Liu Ji6b7db689804a
diff --git a/frontend/package.json b/frontend/package.json
index f9e89f2719..51ca49440d 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -40,6 +40,7 @@
"pinia": "2.0.22",
"qs": "6.11.0",
"rollup": "2.79.0",
+ "scroll-into-view-if-needed": "^2.2.29",
"semver": "7.3.7",
"slug": "8.0.0",
"splitpanes": "3.1.1",
@@ -53,6 +54,7 @@
"vue": "3.2.40",
"vue-i18n": "9.2.2",
"vue-router": "4.1.5",
+ "vuedraggable": "^4.1.0",
"vueuc": "0.4.48"
},
"devDependencies": {
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index d6656f53eb..c90aafa49a 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -63,6 +63,7 @@ specifiers:
prettier: 2.7.1
qs: 6.11.0
rollup: 2.79.0
+ scroll-into-view-if-needed: ^2.2.29
semver: 7.3.7
slug: 8.0.0
splitpanes: 3.1.1
@@ -85,6 +86,7 @@ specifiers:
vue-i18n: 9.2.2
vue-router: 4.1.5
vue-tsc: 0.40.13
+ vuedraggable: ^4.1.0
vueuc: 0.4.48
dependencies:
@@ -115,6 +117,7 @@ dependencies:
pinia: 2.0.22_lkgxmynzgulk7qrgf4psevjapq
qs: 6.11.0
rollup: 2.79.0
+ scroll-into-view-if-needed: 2.2.29
semver: 7.3.7
slug: 8.0.0
splitpanes: 3.1.1
@@ -128,6 +131,7 @@ dependencies:
vue: 3.2.40
vue-router: [email protected]
+ vuedraggable: [email protected]
devDependencies:
@@ -1523,6 +1527,10 @@ packages:
engines: {node: '>= 10'}
dev: false
+ /compute-scroll-into-view/1.0.17:
+ resolution: {integrity: sha512-j4dx+Fb0URmzbwwMUrhqWM2BEWHdFGx+qZ9qqASHRPqvTYdqvWnHg0H1hIbcyLnvgnoNAVMlwkepyqM3DaIFUg==}
+ dev: false
+
/concat-map/0.0.1:
resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==}
@@ -3355,6 +3363,12 @@ packages:
source-map-js: 1.0.2
dev: false
+ /scroll-into-view-if-needed/2.2.29:
+ resolution: {integrity: sha512-hxpAR6AN+Gh53AdAimHM6C8oTN1ppwVZITihix+WqalywBeFcQ6LdQP5ABNl26nX8GTEL7VT+b8lKpdqq65wXg==}
+ dependencies:
+ compute-scroll-into-view: 1.0.17
+ dev: false
+
/seemly/0.3.6:
resolution: {integrity: sha512-lEV5VB8BUKTo/AfktXJcy+JeXns26ylbMkIUco8CYREsQijuz4mrXres2Q+vMLdwkuLxJdIPQ8IlCIxLYm71Yw==}
dev: false
@@ -3408,6 +3422,10 @@ packages:
resolution: {integrity: sha512-aIDda8JjwGHPPVHRAusym8OmXhw1PnM6P28/ShUUv/4RsUVYwxwL0eljtcBDg0yI4UbW7YADrZTWp1bLqa7+DQ==}
dev: false
+ /sortablejs/1.14.0:
+ resolution: {integrity: sha512-pBXvQCs5/33fdN1/39pPL0NZF20LeRbLQ5jtnheIPN9JQAaufGjKdWduZn4U7wCtVuzKhmRkI0DFYHYRbB2H1w==}
+ dev: false
+
/source-map-js/1.0.2:
resolution: {integrity: sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==}
engines: {node: '>=0.10.0'}
@@ -3919,6 +3937,15 @@ packages:
'@vue/server-renderer': [email protected]
'@vue/shared': 3.2.40
+ /vuedraggable/[email protected]:
+ resolution: {integrity: sha512-FU5HCWBmsf20GpP3eudURW3WdWTKIbEIQxh9/8GE806hydR9qZqRRxRE3RjqX7PkuLuMQG/A7n3cfj9rCEchww==}
+ peerDependencies:
+ vue: ^3.0.1
+ dependencies:
+ sortablejs: 1.14.0
+ vue: 3.2.40
+ dev: false
+
resolution: {integrity: sha512-dQTBLxCzfaPuzD3c4/dIxAULtnyY+xwdotCRFUDgf0DJiwuR3tI+txJ9K8uJKmaHwc1JDUVqhRAj9Jd/pvInWg==}
peerDependencies:
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
index d353e65a15..832ec6e53b 100644
--- a/frontend/src/store/modules/tab.ts
+++ b/frontend/src/store/modules/tab.ts
@@ -46,9 +46,10 @@ export const useTabStore = defineStore("tab", () => {
const currentTabId = ref<string>();
// getters
- const tabList = computed((): TabInfo[] =>
- tabIdList.value.map((id) => tabs.value.get(id) ?? getDefaultTab())
- );
+ const getTabById = (id: string) => {
+ return tabs.value.get(id) ?? getDefaultTab();
+ };
+ const tabList = computed((): TabInfo[] => tabIdList.value.map(getTabById));
const currentTab = computed((): TabInfo => {
const tab = tabList.value.find((tab) => tab.id === currentTabId.value);
return tab ?? getDefaultTab();
@@ -202,6 +203,7 @@ export const useTabStore = defineStore("tab", () => {
currentTabId,
currentTab,
isDisconnected,
+ getTabById,
addTab,
removeTab,
updateCurrentTab,
diff --git a/frontend/src/views/sql-editor/SQLEditorPage.vue b/frontend/src/views/sql-editor/SQLEditorPage.vue
index 91d750c0f3..4d97206b6e 100644
--- a/frontend/src/views/sql-editor/SQLEditorPage.vue
+++ b/frontend/src/views/sql-editor/SQLEditorPage.vue
@@ -1,6 +1,6 @@
<template>
<div class="sqleditor--wrapper">
- <TabListContainer />
+ <TabList />
<Splitpanes class="default-theme flex flex-col flex-1 overflow-hidden">
<Pane size="20">
<AsidePanel />
@@ -30,7 +30,7 @@ import { computed } from "vue";
import { useSQLEditorStore, useTabStore } from "@/store";
import AsidePanel from "./AsidePanel/AsidePanel.vue";
import EditorPanel from "./EditorPanel/EditorPanel.vue";
-import TabListContainer from "./TabListContainer.vue";
+import TabList from "./TabList";
import TablePanel from "./TablePanel/TablePanel.vue";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/Label.vue b/frontend/src/views/sql-editor/TabList/TabItem/Label.vue
new file mode 100644
index 0000000000..56c14f3589
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/TabItem/Label.vue
@@ -0,0 +1,118 @@
+<template>
+ <div class="label">
+ <span
+ class="name"
+ :class="state.editing && 'invisible'"
+ @dblclick="beginEdit"
+ >
+ {{ state.name }}
+ </span>
+
+ <input
+ v-if="state.editing"
+ ref="inputRef"
+ v-model="state.name"
+ type="text"
+ class="edit"
+ @blur="confirmEdit"
+ @keyup.enter="confirmEdit"
+ @keyup.esc="cancelEdit"
+ />
+ </div>
+</template>
+<script lang="ts" setup>
+import { computed, nextTick, PropType, reactive, ref, watch } from "vue";
+
+import type { TabInfo } from "@/types";
+import { UNKNOWN_ID } from "@/types";
+import { useSheetStore, useTabStore } from "@/store";
+
+type LocalState = {
+ editing: boolean;
+ name: string;
+};
+
+const props = defineProps({
+ tab: {
+ type: Object as PropType<TabInfo>,
+ required: true,
+ },
+ index: {
+ type: Number,
+ required: true,
+ },
+});
+
+const state = reactive<LocalState>({
+ editing: false,
+ name: props.tab.name,
+});
+
+const tabStore = useTabStore();
+const sheetStore = useSheetStore();
+const inputRef = ref<HTMLInputElement>();
+
+const isCurrentTab = computed(() => props.tab.id === tabStore.currentTabId);
+
+const beginEdit = () => {
+ state.editing = true;
+ state.name = props.tab.name;
+ nextTick(() => {
+ inputRef.value?.focus();
+ });
+};
+
+const confirmEdit = () => {
+ const { tab } = props;
+
+ const name = state.name.trim();
+ if (name === "") {
+ return cancelEdit();
+ }
+
+ tab.name = name;
+
+ if (tab.sheetId && tab.sheetId !== UNKNOWN_ID) {
+ sheetStore.patchSheetById({
+ id: tab.sheetId,
+ name,
+ });
+ }
+
+ state.editing = false;
+};
+
+const cancelEdit = () => {
+ state.editing = false;
+ state.name = props.tab.name;
+};
+
+watch(
+ () => props.tab.name,
+ (name) => {
+ state.name = name;
+ }
+);
+watch(isCurrentTab, (value) => {
+ if (!value) {
+ cancelEdit();
+ }
+});
+</script>
+
+<style scoped lang="postcss">
+.label {
+ @apply relative flex items-center whitespace-nowrap min-w-[6rem] max-w-[12rem] truncate;
+}
+
+.name {
+ @apply h-6 w-full flex items-center text-sm;
+}
+.edit {
+ @apply border-0 border-b absolute inset-0 p-0 text-sm;
+}
+
+.temp .name {
+ @apply italic;
+}
+</style>
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/Prefix.vue b/frontend/src/views/sql-editor/TabList/TabItem/Prefix.vue
new file mode 100644
index 0000000000..6207e9c883
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/TabItem/Prefix.vue
@@ -0,0 +1,44 @@
+<template>
+ <template v-if="sheet.id !== UNKNOWN_ID">
+ <heroicons-outline:user-group
+ v-if="sheet.visibility === 'PROJECT'"
+ class="w-4 h-4"
+ />
+ <heroicons-outline:globe
+ v-if="sheet.visibility === 'PUBLIC'"
+ class="w-4 h-4"
+ />
+ </template>
+</template>
+
+<script lang="ts" setup>
+import { computed, PropType } from "vue";
+
+import type { TabInfo } from "@/types";
+import { unknown, UNKNOWN_ID } from "@/types";
+import { useSheetStore } from "@/store";
+
+const props = defineProps({
+ tab: {
+ type: Object as PropType<TabInfo>,
+ required: true,
+ },
+ index: {
+ type: Number,
+ required: true,
+ },
+});
+
+const sheetStore = useSheetStore();
+
+const sheet = computed(() => {
+ const { sheetId } = props.tab;
+ if (sheetId) {
+ const sheet = sheetStore.sheetById.get(sheetId);
+ if (sheet) {
+ return sheet;
+ }
+ }
+ return unknown("SHEET");
+});
+</script>
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue b/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue
new file mode 100644
index 0000000000..8b152a72e3
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/TabItem/Suffix.vue
@@ -0,0 +1,76 @@
+<template>
+ <div
+ class="suffix"
+ @mouseenter="state.hovering = true"
+ @mouseleave="state.hovering = false"
+ >
+ <carbon:dot-mark v-if="icon === 'unsaved'" class="icon unsaved" />
+ <heroicons-solid:x
+ v-else-if="icon === 'close'"
+ class="icon close"
+ @click.stop.prevent="$emit('close', tab, index)"
+ />
+ <span v-else class="icon dummy"></span>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed, PropType, reactive } from "vue";
+
+import type { TabInfo } from "@/types";
+import { useTabStore } from "@/store";
+
+type LocalState = {
+ hovering: boolean;
+};
+
+type IconType = "unsaved" | "close";
+
+const props = defineProps({
+ tab: {
+ type: Object as PropType<TabInfo>,
+ required: true,
+ },
+ index: {
+ type: Number,
+ required: true,
+ },
+});
+
+const state = reactive<LocalState>({
+ hovering: false,
+});
+
+defineEmits<{
+ (e: "close", tab: TabInfo, index: number): void;
+}>();
+
+const tabStore = useTabStore();
+
+const icon = computed((): IconType | undefined => {
+ if (state.hovering) {
+ if (tabStore.tabList.length > 1) {
+ // Show 'close' if
+ // - hovering
+ // - and closeable (not the only one tab)
+ return "close";
+ }
+ }
+ if (!props.tab.isSaved) {
+ return "unsaved";
+ }
+ return undefined;
+});
+</script>
+
+<style scoped lang="postcss">
+.suffix {
+ @apply flex items-center min-w-[1.25rem];
+}
+.icon {
+ @apply block w-5 h-5 p-0.5 text-gray-300 hover:text-gray-500 hover:bg-gray-200 rounded;
+}
+.dummy {
+ @apply invisible;
+}
+</style>
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue b/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue
new file mode 100644
index 0000000000..803c56c11c
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/TabItem/TabItem.vue
@@ -0,0 +1,76 @@
+<template>
+ <div
+ class="tab-item"
+ :class="{
+ current: isCurrentTab,
+ temp: isTempTab(tab),
+ hovering: state.hovering,
+ }"
+ @mousedown="$emit('select', tab, index)"
+ @mouseenter="state.hovering = true"
+ @mouseleave="state.hovering = false"
+ >
+ <div class="body">
+ <Prefix :tab="tab" :index="index" />
+ <Label :tab="tab" :index="index" />
+ <Suffix :tab="tab" :index="index" @close="$emit('close', tab, index)" />
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed, PropType, reactive } from "vue";
+import { storeToRefs } from "pinia";
+
+import type { TabInfo } from "@/types";
+import { isTempTab } from "@/utils";
+import { useTabStore } from "@/store";
+import Prefix from "./Prefix.vue";
+import Label from "./Label.vue";
+import Suffix from "./Suffix.vue";
+
+type LocalState = {
+ hovering: boolean;
+};
+
+const props = defineProps({
+ tab: {
+ type: Object as PropType<TabInfo>,
+ required: true,
+ },
+ index: {
+ type: Number,
+ required: true,
+ },
+});
+
+defineEmits<{
+ (e: "select", tab: TabInfo, index: number): void;
+ (e: "close", tab: TabInfo, index: number): void;
+}>();
+
+const state = reactive<LocalState>({
+ hovering: false,
+});
+
+const tabStore = useTabStore();
+const { currentTabId } = storeToRefs(tabStore);
+
+const isCurrentTab = computed(() => props.tab.id === currentTabId.value);
+</script>
+
+<style scoped lang="postcss">
+.tab-item {
+ @apply cursor-pointer border-r bg-white gap-x-2;
+}
+.hovering {
+ @apply bg-gray-50;
+}
+
+.body {
+ @apply flex items-center justify-between gap-x-1 pl-2 pr-1 py-1 border-t-2 border-t-transparent;
+}
+.current .body {
+ @apply relative bg-white text-accent border-t-accent;
+}
+</style>
diff --git a/frontend/src/views/sql-editor/TabList/TabItem/index.ts b/frontend/src/views/sql-editor/TabList/TabItem/index.ts
new file mode 100644
index 0000000000..aabe8dc019
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/TabItem/index.ts
@@ -0,0 +1,3 @@
+import TabItem from "./TabItem.vue";
+
+export default TabItem;
diff --git a/frontend/src/views/sql-editor/TabList/TabList.vue b/frontend/src/views/sql-editor/TabList/TabList.vue
new file mode 100644
index 0000000000..6679abce92
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/TabList.vue
@@ -0,0 +1,202 @@
+<template>
+ <div class="flex box-border text-gray-500 text-sm border-b">
+ <div class="relative flex flex-nowrap overflow-hidden">
+ <Draggable
+ id="tab-list"
+ ref="tabListRef"
+ v-model="tabStore.tabIdList"
+ item-key="id"
+ animation="300"
+ class="tab-list"
+ :class="{
+ 'more-left': scrollState.moreLeft,
+ 'more-right': scrollState.moreRight,
+ }"
+ ghost-class="ghost"
+ @start="state.dragging = true"
+ @end="state.dragging = false"
+ @scroll="recalculateScrollState"
+ >
+ <template
+ #item="{ element: id, index }: { element: string, index: number }"
+ >
+ <TabItem
+ :tab="tabStore.getTabById(id)"
+ :index="index"
+ :data-tab-id="id"
+ @select="(tab) => handleSelectTab(tab)"
+ @close="(tab, index) => handleRemoveTab(tab, index)"
+ />
+ </template>
+ </Draggable>
+ </div>
+
+ <button class="px-1" @click="handleAddTab">
+ <heroicons-solid:plus class="h-6 w-6 p-1 hover:bg-gray-200 rounded-md" />
+ </button>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { ref, reactive, nextTick, computed, onMounted, watch } from "vue";
+import { useI18n } from "vue-i18n";
+import { useDialog } from "naive-ui";
+import Draggable from "vuedraggable";
+import scrollIntoView from "scroll-into-view-if-needed";
+
+import type { TabInfo } from "@/types";
+import { useTabStore } from "@/store";
+import TabItem from "./TabItem";
+import { useResizeObserver } from "@vueuse/core";
+
+type LocalState = {
+ dragging: boolean;
+ hoverTabId: string;
+};
+
+const tabStore = useTabStore();
+
+const { t } = useI18n();
+const dialog = useDialog();
+
+const state = reactive<LocalState>({
+ dragging: false,
+ hoverTabId: "",
+});
+
+const tabListRef = ref<InstanceType<typeof Draggable>>();
+
+const scrollState = reactive({
+ moreLeft: false,
+ moreRight: false,
+});
+
+const handleSelectTab = async (tab: TabInfo) => {
+ tabStore.setCurrentTabId(tab.id);
+};
+
+const handleAddTab = () => {
+ tabStore.addTab();
+ nextTick(recalculateScrollState);
+};
+
+const handleRemoveTab = async (tab: TabInfo, index: number) => {
+ if (!tab.isSaved) {
+ const $dialog = dialog.create({
+ title: t("sql-editor.hint-tips.confirm-to-close-unsaved-tab"),
+ type: "info",
+ onPositiveClick() {
+ $dialog.destroy();
+ },
+ async onNegativeClick() {
+ remove(index);
+ $dialog.destroy();
+ },
+ negativeText: t("common.confirm"),
+ positiveText: t("common.cancel"),
+ showIcon: false,
+ });
+ } else {
+ remove(index);
+ }
+
+ function remove(index: number) {
+ if (tabStore.tabList.length <= 1) return;
+
+ tabStore.removeTab(tab);
+
+ // select a tab near the removed tab.
+ const nextIndex = Math.min(index, tabStore.tabList.length - 1);
+ const nextTab = tabStore.tabList[nextIndex];
+ handleSelectTab(nextTab);
+
+ nextTick(recalculateScrollState);
+ }
+};
+
+const tabListElement = computed((): HTMLElement | undefined => {
+ const list = tabListRef.value;
+ if (!list) return undefined;
+ const element = list.$el as HTMLElement;
+ return element;
+});
+
+useResizeObserver(tabListRef, () => {
+ recalculateScrollState();
+});
+
+const recalculateScrollState = () => {
+ const element = tabListElement.value;
+ if (!element) {
+ return;
+ }
+
+ const { scrollWidth, offsetWidth, scrollLeft } = element;
+
+ if (scrollLeft === 0) {
+ scrollState.moreLeft = false;
+ } else {
+ scrollState.moreLeft = true;
+ }
+
+ if (scrollWidth > offsetWidth) {
+ // The actual width is wider than the visible width
+ const rightBound = scrollLeft + offsetWidth;
+ if (rightBound < scrollWidth) {
+ scrollState.moreRight = true;
+ } else {
+ scrollState.moreRight = false;
+ }
+ } else {
+ scrollState.moreRight = false;
+ }
+};
+
+watch(
+ () => tabStore.currentTabId,
+ (id) => {
+ requestAnimationFrame(() => {
+ // Scroll the selected tab into view if needed.
+ const tabElements = Array.from(document.querySelectorAll(".tab-item"));
+ const tabElement = tabElements.find(
+ (elem) => elem.getAttribute("data-tab-id") === id
+ );
+ if (tabElement) {
+ scrollIntoView(tabElement, {
+ scrollMode: "if-needed",
+ });
+ }
+ });
+ },
+ { immediate: true }
+);
+
+onMounted(() => recalculateScrollState());
+</script>
+
+<style scoped lang="postcss">
+.ghost {
+ @apply opacity-50 bg-white;
+}
+
+.tab-list {
+ @apply flex flex-nowrap overflow-x-auto w-full hide-scrollbar;
+}
+
+.tab-list::before {
+ @apply absolute top-0 left-0 w-4 h-full z-50 pointer-events-none transition-shadow;
+ content: "";
+ box-shadow: none;
+}
+.tab-list::after {
+ @apply absolute top-0 right-0 w-4 h-full z-50 pointer-events-none transition-shadow;
+ content: "";
+ box-shadow: none;
+}
+.tab-list.more-left::before {
+ box-shadow: inset 1rem 0 0.5rem -0.5rem rgb(0 0 0 / 25%);
+}
+.tab-list.more-right::after {
+ box-shadow: inset -1rem 0 0.5rem -0.5rem rgb(0 0 0 / 25%);
+}
+</style>
diff --git a/frontend/src/views/sql-editor/TabList/index.ts b/frontend/src/views/sql-editor/TabList/index.ts
new file mode 100644
index 0000000000..ac629c99eb
--- /dev/null
+++ b/frontend/src/views/sql-editor/TabList/index.ts
@@ -0,0 +1,3 @@
+import TabList from "./TabList.vue";
+
+export default TabList;
diff --git a/frontend/src/views/sql-editor/TabListContainer.vue b/frontend/src/views/sql-editor/TabListContainer.vue
deleted file mode 100644
index d1e4e19407..0000000000
--- a/frontend/src/views/sql-editor/TabListContainer.vue
+++ /dev/null
@@ -1,351 +0,0 @@
-<template>
- <div class="tab-list-container">
- <!-- tab list-->
- <div
- class="tab-list-wrapper relative overflow-hidden"
- :class="{ 'is-scrolling': scrollState.isScrolling }"
- >
- <div
- ref="tablistRef"
- class="tab-list-tablist"
- @wheel="handleScollTabList"
- >
- <div
- v-for="tab in tabStore.tabList"
- :key="tab.id"
- class="tab-list-tab"
- :class="{ active: tab.id === tabStore.currentTabId }"
- :style="scrollState.style"
- @click="handleSelectTab(tab)"
- @mouseover="hoverTabId = tab.id"
- @mouseleave="hoverTabId = ''"
- >
- <div
- class="label max-w-5xl w-48 truncate"
- @dblclick="handleEditLabel(tab)"
- >
- <div
- v-if="labelState.editingTabId === tab.id"
- class="label-input relative"
- >
- <input
- ref="labelInputRef"
- v-model="labelState.currentLabelName"
- type="text"
- class="rounded px-2 py-0 text-sm w-full absolute left-0 bottom-0"
- @blur="(e: Event) => handleTryChangeLabel()"
- @keyup.enter="(e: Event) => handleTryChangeLabel()"
- @keyup.esc="handleCancelChangeLabel"
- />
- <!-- this is a trick -->
- <span class="w-full h-full invisible">
- Edit {{ labelState.currentLabelName }}
- </span>
- </div>
- <span v-else class="flex items-center space-x-2">
- <heroicons-outline:user-group
- v-if="sheetOfTab(tab).visibility === 'PROJECT'"
- class="w-4 h-4"
- />
- <heroicons-outline:globe
- v-if="sheetOfTab(tab).visibility === 'PUBLIC'"
- class="w-4 h-4"
- />
- <span :class="isTempTab(tab) && 'italic'">{{ tab.name }}</span>
- </span>
- </div>
- <template v-if="hoverTabId === tab.id && tabStore.tabList.length > 1">
- <span
- class="suffix close hover:bg-gray-200 rounded-sm"
- @click.prevent.stop="handleRemoveTab(tab)"
- >
- <heroicons-solid:x class="icon" />
- </span>
- </template>
- <template v-else>
- <template v-if="!tab.isSaved">
- <span class="suffix editing text-gray-400">
- <carbon:dot-mark class="h-4 w-4" />
- </span>
- </template>
- <template
- v-else-if="
- tab.id === tabStore.currentTabId && tabStore.tabList.length > 1
- "
- >
- <span
- class="suffix close hover:bg-gray-200 rounded-sm"
- @click.prevent="handleRemoveTab(tab)"
- >
- <heroicons-solid:x class="icon" />
- </span>
- </template>
- <template v-else>
- <span class="suffix" />
- </template>
- </template>
- </div>
- </div>
- </div>
-
- <div class="tab-list-add">
- <button class="p-1 hover:bg-gray-200 rounded-md" @click="handleAddTab">
- <heroicons-solid:plus class="h-4 w-4" />
- </button>
- </div>
- <div class="tab-list-more">
- <NPopselect
- v-model:value="tabStore.currentTabId"
- :options="localTabList"
- trigger="click"
- size="medium"
- scrollable
- @update:value="handleSelectTabFromPopselect"
- >
- <NTooltip trigger="hover" placement="left-center">
- <template #trigger>
- <button class="ml-8 p-1 hover:bg-gray-200 rounded-md">
- <heroicons-solid:chevron-down class="h-4 w-4" />
- </button>
- </template>
- {{ $t("sql-editor.view-all-tabs") }}
- </NTooltip>
- </NPopselect>
- </div>
- </div>
-</template>
-
-<script lang="ts" setup>
-import { ref, reactive, nextTick, computed } from "vue";
-import { debounce } from "lodash-es";
-import { useI18n } from "vue-i18n";
-import { useDialog } from "naive-ui";
-import { pushNotification, useTabStore, useSheetStore } from "@/store";
-import { TabInfo, unknown } from "@/types";
-import { isTempTab } from "@/utils";
-
-const tabStore = useTabStore();
-const sheetStore = useSheetStore();
-
-const { t } = useI18n();
-const dialog = useDialog();
-
-const hoverTabId = ref("");
-// edit label state
-const labelState = reactive({
- currentLabelName: "",
- editingTabId: "",
-});
-const labelInputRef = ref<HTMLInputElement | HTMLInputElement[]>();
-
-const sheetOfTab = (tab: TabInfo) => {
- const { sheetId } = tab;
- if (sheetId) {
- const sheet = sheetStore.sheetById.get(sheetId);
- if (sheet) {
- return sheet;
- }
- }
- return unknown("SHEET");
-};
-
-const localTabList = computed(() => {
- return tabStore.tabList.map((tab: TabInfo) => {
- return {
- label: tab.name,
- value: tab.id,
- };
- });
-});
-// scroll label state
-const tablistRef = ref<HTMLInputElement>();
-const scrollState = reactive({
- style: {},
- isScrolling: false,
- scrollWidth: 0,
- offsetWidth: 0,
-});
-
-const scrollingDistance = computed(() => {
- return scrollState.scrollWidth - scrollState.offsetWidth;
-});
-
-const recalculateScrollWidth = () => {
- scrollState.scrollWidth = tablistRef.value?.scrollWidth as number;
- scrollState.offsetWidth = tablistRef.value?.offsetWidth as number;
- scrollState.isScrolling = scrollingDistance.value > 0;
-};
-
-const updateSheetName = () => {
- if (tabStore.currentTab.sheetId) {
- sheetStore.patchSheetById({
- id: tabStore.currentTab.sheetId,
- name: labelState.currentLabelName,
- });
- }
-};
-
-const handleEditLabel = (tab: TabInfo) => {
- labelState.editingTabId = tab.id;
- labelState.currentLabelName = tab.name;
- nextTick(() => {
- const inputElem = Array.isArray(labelInputRef.value)
- ? labelInputRef.value[0]
- : labelInputRef.value;
- inputElem?.focus();
- });
-};
-
-const handleTryChangeLabel = () => {
- if (labelState.editingTabId) {
- if (labelState.currentLabelName !== "") {
- tabStore.updateCurrentTab({
- name: labelState.currentLabelName,
- });
-
- updateSheetName();
- nextTick(() => {
- recalculateScrollWidth();
- scrollState.style = {
- transform: `translateX(0px)`,
- };
- });
- } else {
- pushNotification({
- module: "bytebase",
- style: "CRITICAL",
- title: t("sql-editor.please-input-the-tab-label"),
- });
- }
- handleCancelChangeLabel();
- }
-};
-
-const handleCancelChangeLabel = () => {
- labelState.editingTabId = "";
- labelState.currentLabelName = "";
-};
-
-const handleSelectTab = async (tab: TabInfo) => {
- tabStore.setCurrentTabId(tab.id);
-};
-const handleAddTab = () => {
- tabStore.addTab();
- nextTick(recalculateScrollWidth);
-};
-
-const handleRemoveTab = async (tab: TabInfo) => {
- if (!tab.isSaved) {
- const $dialog = dialog.create({
- title: t("sql-editor.hint-tips.confirm-to-close-unsaved-tab"),
- type: "info",
- onPositiveClick() {
- $dialog.destroy();
- },
- async onNegativeClick() {
- await remove();
- $dialog.destroy();
- },
- negativeText: t("common.confirm"),
- positiveText: t("common.cancel"),
- showIcon: false,
- });
- } else {
- await remove();
- }
-
- async function remove() {
- await tabStore.removeTab(tab);
- const tabsLength = tabStore.tabList.length;
-
- if (tabsLength > 0) {
- handleSelectTab(tabStore.tabList[tabsLength - 1]);
- }
- nextTick(() => {
- recalculateScrollWidth();
- });
- }
-};
-const handleSelectTabFromPopselect = (tabId: string) => {
- tabStore.setCurrentTabId(tabId);
-};
-
-const handleScollTabList = debounce((e: WheelEvent) => {
- console.log(e);
- console.log(e.deltaX > 0 ? "Move to right" : "Move to left");
- console.log(e.offsetX);
-}, 333);
-</script>
-
-<style scoped>
-.tab-list-container {
- @apply flex box-border;
- @apply text-gray-500 text-sm;
- @apply border-b;
-}
-
-.tab-list-tablist {
- @apply flex overflow-auto;
- max-width: calc(100vw - 112px);
- scrollbar-width: none; /* firefox */
- -ms-overflow-style: none; /* IE 10+ */
-}
-
-.tab-list-wrapper.is-scrolling::before {
- @apply absolute top-0 left-0 w-4 h-full z-10;
- content: "";
- transition: box-shadow 0.3s cubic-bezier(0.4, 0, 0.2, 1);
- box-shadow: inset 10px 0 8px -8px rgb(0 0 0 / 16%);
-}
-.tab-list-wrapper.is-scrolling::after {
- @apply absolute top-0 right-0 w-4 h-full z-10;
- content: "";
- transition: box-shadow 0.3s cubic-bezier(0.4, 0, 0.2, 1);
- box-shadow: inset -10px 0 8px -8px rgb(0 0 0 / 16%);
-}
-.tab-list-tablist::-webkit-scrollbar {
- display: none; /* Chrome Safari */
-}
-
-.tab-list-tab {
- @apply inline-flex place-items-center;
- @apply cursor-pointer box-border;
- @apply px-2 border-r;
- @apply bg-gray-50;
- @apply whitespace-nowrap;
- transform: translateX(0);
- transition: transform 0.3s, -webkit-transform 0.3s;
-}
-
-.tab-list-tab.active {
- @apply cursor-text relative;
- @apply bg-white;
- @apply text-accent;
-}
-
-.tab-list-tab .label {
- @apply p-2;
-}
-.tab-list-tab .suffix {
- @apply flex justify-center items-center h-4 w-4;
-}
-
-.tab-list-tab .suffix.close {
- @apply cursor-pointer;
- @apply text-gray-500;
-}
-
-.tab-list-move-prev,
-.tab-list-move-next,
-.tab-list-add {
- @apply flex items-center;
- @apply cursor-pointer;
- @apply p-2;
-}
-
-.tab-list-more {
- @apply flex items-center justify-end flex-1;
- @apply cursor-pointer;
- @apply p-2;
-}
-</style>
diff --git a/frontend/src/views/sql-editor/TablePanel/useTableResize.ts b/frontend/src/views/sql-editor/TablePanel/useTableResize.ts
index 8fdccd573c..1ff321d2ea 100644
--- a/frontend/src/views/sql-editor/TablePanel/useTableResize.ts
+++ b/frontend/src/views/sql-editor/TablePanel/useTableResize.ts
@@ -202,6 +202,7 @@ const useTableResize = (options: TableResizeOptions) => {
export default useTableResize;
const getElementWidth = (elem: HTMLElement) => {
+ if (!elem) return 0;
const rect = elem.getBoundingClientRect();
return rect.width;
};