Skip to content

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

feat(sql-editor): add the query selector logic (#415)

Yunwei Xiaodb950aa28587
diff --git a/frontend/package.json b/frontend/package.json
index ab8856bbe4..b35b2ee192 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -39,7 +39,8 @@
"vue-i18n": "^9.2.0-beta.20",
"vue-router": "^4.0.8",
"vuex": "^4.0.0",
- "vuex-composition-helpers": "^2.0.1"
+ "vuex-composition-helpers": "^2.0.1",
+ "vuex-persistedstate": "^4.1.0"
},
"devDependencies": {
"@iconify/json": "^1.1.432",
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 21f41e4296..2f71f42ee2 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -1096,6 +1096,7 @@ sql-editor:
search-history: Search History
hint-tips:
confirm-to-delete-this-history: Confirm to delete this history?
+ please-input-the-tab-label: Please input the tab label!
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 6a3335ff04..a9f0fd98cd 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -963,6 +963,7 @@ sql-editor:
search-history: 搜索历史记录
hint-tips:
confirm-to-delete-this-history: 确定删除这条记录?
+ please-input-the-tab-label: 请输入标签页名称!
label:
empty-label-value: <空值>
no-label: 没有标签
diff --git a/frontend/src/plugins/naive-ui.ts b/frontend/src/plugins/naive-ui.ts
index 233ce0ebab..3e2c0acd1d 100644
--- a/frontend/src/plugins/naive-ui.ts
+++ b/frontend/src/plugins/naive-ui.ts
@@ -11,6 +11,7 @@ import {
NMessageProvider,
NModal,
NPopover,
+ NPopselect,
NPopconfirm,
NSpace,
NTabs,
@@ -34,6 +35,7 @@ const naive = create({
NMessageProvider,
NModal,
NPopover,
+ NPopselect,
NPopconfirm,
NSpace,
NTabs,
diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.ts
index 9c1fa05407..61215aa067 100644
--- a/frontend/src/store/index.ts
+++ b/frontend/src/store/index.ts
@@ -1,4 +1,5 @@
import { createStore, Store } from "vuex";
+import createPersistedState from "vuex-persistedstate";
/**
* Following states are persisted in database
@@ -19,5 +20,10 @@ const isProd = import.meta.env.NODE_ENV == "production";
export const store: Store<any> = createStore({
modules,
+ plugins: [
+ createPersistedState({
+ paths: ["editorSelector"],
+ }),
+ ],
strict: !isProd,
});
diff --git a/frontend/src/views/SqlEditor/QuerySelector.vue b/frontend/src/views/SqlEditor/QuerySelector.vue
index 947982f302..d21caa56a4 100644
--- a/frontend/src/views/SqlEditor/QuerySelector.vue
+++ b/frontend/src/views/SqlEditor/QuerySelector.vue
@@ -1,130 +1,344 @@
<template>
- <div class="query-selector border-b">
+ <div class="query-selector">
+ <!-- query tab list-->
<div
- v-for="tab in queryTabList"
- :key="tab.id"
- class="query-selector--tab"
- :class="{ active: tab.id === activeTabId }"
- @click="handleSelectTab(tab)"
- @mouseover="enterTabId = tab.id"
- @mouseleave="enterTabId = ''"
+ class="query-selector-wrapper relative overflow-hidden"
+ :class="{ 'is-scrolling': scrollState.isScrolling }"
>
- <span class="prefix">
- <carbon:code class="h-4 w-4" />
- </span>
- <span class="label">
- {{ tab.label }}
- </span>
- <template v-if="enterTabId === tab.id && queryTabList.length > 1">
- <span
- class="suffix close hover:bg-gray-200 rounded-sm"
- @click.prevent="handleRemoveTab(tab)"
+ <div
+ ref="tablistRef"
+ class="query-selector-tablist"
+ @wheel="handleScollTabList"
+ >
+ <div
+ v-for="tab in queryTabList"
+ :key="tab.id"
+ class="query-selector-tab"
+ :class="{ active: tab.id === activeTabId }"
+ :style="scrollState.style"
+ @click="handleSelectTab(tab)"
+ @mouseover="enterTabId = tab.id"
+ @mouseleave="enterTabId = ''"
>
- <heroicons-solid:x class="icon" />
- </span>
- </template>
- <template v-else>
- <!-- <template v-if="!tab.isSaved">
- <span class="editing text-gray-400">
- <carbon:dot-mark class="h-4 w-4" />
+ <span class="prefix">
+ <carbon:code class="h-4 w-4" />
</span>
- </template> -->
- <template v-if="tab.id === activeTabId && queryTabList.length > 1">
- <span
- class="suffix close hover:bg-gray-200 rounded-sm"
- @click.prevent="handleRemoveTab(tab)"
+ <div
+ class="label max-w-5xl w-48 overflow-hidden whitespace-nowrap overflow-ellipsis"
+ @dblclick="handleEditLabel(tab)"
>
- <heroicons-solid:x class="icon" />
- </span>
- </template>
- <template v-else>
- <span class="suffix" />
- </template>
- </template>
+ <div
+ v-if="
+ labelState.isEditingLabel && 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="(e: Event) => handleCancelInput()"
+ />
+ <!-- this is a trick -->
+ <span class="w-full h-full invisible line-camp-1">
+ Edit {{ labelState.currentLabelName }}
+ </span>
+ </div>
+ <span v-else>
+ {{ tab.label }}
+ </span>
+ </div>
+ <template v-if="enterTabId === tab.id && queryTabList.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="editing text-gray-400">
+ <carbon:dot-mark class="h-4 w-4" />
+ </span>
+ </template>
+ <template v-if="tab.id === activeTabId && queryTabList.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="query-selector-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="query-selector-more">
+ <NPopselect
+ v-model:value="selectedTab"
+ :options="tabList"
+ 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>
+ View all Tabs
+ </NTooltip>
+ </NPopselect>
</div>
- <button
- v-if="isDev()"
- class="query-selector--added"
- @click="handleAddTab({})"
- >
- <heroicons-solid:plus class="h-4 w-4 hover:bg-gray-200 rounded-sm" />
- </button>
</div>
</template>
<script lang="ts" setup>
-import { ref } from "vue";
+import { ref, watch, reactive, nextTick, computed } from "vue";
+import { useStore } from "vuex";
+import { useI18n } from "vue-i18n";
import {
+ useNamespacedGetters,
useNamespacedState,
useNamespacedActions,
} from "vuex-composition-helpers";
import {
- EditorSelectorState,
TabInfo,
AnyTabInfo,
+ EditorSelectorGetters,
+ EditorSelectorState,
EditorSelectorActions,
} from "../../types";
-import { isDev } from "../../utils";
+import { debounce } from "lodash-es";
+
+const { currentTab } = useNamespacedGetters<EditorSelectorGetters>(
+ "editorSelector",
+ ["currentTab"]
+);
const { activeTabId, queryTabList } = useNamespacedState<EditorSelectorState>(
"editorSelector",
["activeTabId", "queryTabList"]
);
-const { addTab, removeTab, setActiveTabId } =
+const { addTab, removeTab, setActiveTabId, updateActiveTab } =
useNamespacedActions<EditorSelectorActions>("editorSelector", [
"addTab",
"removeTab",
"setActiveTabId",
+ "updateActiveTab",
]);
+const store = useStore();
+const { t } = useI18n();
+
const enterTabId = ref("");
+const selectedTab = computed(() => activeTabId.value);
+// edit label state
+const labelState = reactive({
+ isEditingLabel: false,
+ currentLabelName: "",
+ oldLabelName: "",
+ editingTabId: "",
+});
+const labelInputRef = ref<HTMLInputElement>();
+const tabList = computed(() => {
+ return queryTabList.value.map((tab: TabInfo) => {
+ return {
+ label: tab.label,
+ 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 reComputedScrollWidth = () => {
+ scrollState.scrollWidth = tablistRef.value?.scrollWidth as number;
+ scrollState.offsetWidth = tablistRef.value?.offsetWidth as number;
+ scrollState.isScrolling = scrollingDistance.value > 0;
+};
+
+// Edit label logic
+const handleEditLabel = (tab: TabInfo) => {
+ labelState.isEditingLabel = true;
+ labelState.editingTabId = tab.id;
+};
+const handleTryChangeLabel = () => {
+ if (labelState.currentLabelName !== "") {
+ labelState.isEditingLabel = false;
+ updateActiveTab({
+ label: labelState.currentLabelName,
+ });
+ nextTick(() => {
+ reComputedScrollWidth();
+ scrollState.style = {
+ transform: `translateX(0px)`,
+ };
+ });
+ } else {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("sql-editor.please-input-the-tab-label"),
+ });
+ }
+};
+const handleCancelInput = () => {
+ labelState.currentLabelName = labelState.oldLabelName;
+ updateActiveTab({
+ label: labelState.currentLabelName,
+ });
+ nextTick(() => {
+ labelState.isEditingLabel = false;
+ reComputedScrollWidth();
+ });
+};
const handleSelectTab = (tab: TabInfo) => {
setActiveTabId(tab.id);
};
const handleAddTab = (tab: AnyTabInfo) => {
addTab(tab);
+ nextTick(() => {
+ const tab = currentTab.value;
+ handleEditLabel(tab);
+ reComputedScrollWidth();
+ });
};
const handleRemoveTab = (tab: TabInfo) => {
removeTab(tab);
+ nextTick(() => {
+ reComputedScrollWidth();
+ });
+};
+const handleSelectTabFromPopselect = (tabId: string) => {
+ setActiveTabId(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);
+
+watch(
+ () => labelState.isEditingLabel,
+ (newVal) => {
+ if (newVal) {
+ labelState.currentLabelName = currentTab.value.label;
+ labelState.oldLabelName = currentTab.value.label;
+ nextTick(() => {
+ labelInputRef.value?.focus();
+ });
+ } else {
+ nextTick(() => {
+ labelState.currentLabelName = "";
+ labelState.editingTabId = "";
+ labelState.oldLabelName = "";
+ });
+ }
+ }
+);
</script>
<style scoped>
.query-selector {
height: var(--tab-height);
@apply flex box-border;
- @apply text-gray-500;
- @apply text-sm;
+ @apply text-gray-500 text-sm;
+ @apply border-b;
+}
+
+.query-selector-tablist {
+ @apply flex overflow-auto;
+ max-width: calc(100vw - 112px);
+ scrollbar-width: none; /* firefox */
+ -ms-overflow-style: none; /* IE 10+ */
}
-.query-selector--tab {
+.query-selector-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%);
+}
+.query-selector-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%);
+}
+.query-selector-tablist::-webkit-scrollbar {
+ display: none; /* Chrome Safari */
+}
+
+.query-selector-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;
}
-.query-selector--tab.active {
+.query-selector-tab.active {
@apply cursor-text relative;
@apply bg-white;
@apply text-accent;
}
-.query-selector--tab .label {
+.query-selector-tab .label {
@apply p-2;
}
-.query-selector--tab .suffix {
+.query-selector-tab .suffix {
@apply flex justify-center items-center h-4 w-4;
}
-.query-selector--tab .suffix.close {
+.query-selector-tab .suffix.close {
@apply cursor-pointer;
@apply text-gray-500;
}
-.query-selector--added {
+.query-selector-move-prev,
+.query-selector-move-next,
+.query-selector-add {
+ @apply flex items-center;
+ @apply cursor-pointer;
+ @apply p-2;
+}
+
+.query-selector-more {
+ @apply flex items-center justify-end flex-1;
@apply cursor-pointer;
@apply p-2;
}
diff --git a/frontend/src/views/SqlEditor/SqlEditor.vue b/frontend/src/views/SqlEditor/SqlEditor.vue
index c640b6b332..0ed3c3fa7b 100644
--- a/frontend/src/views/SqlEditor/SqlEditor.vue
+++ b/frontend/src/views/SqlEditor/SqlEditor.vue
@@ -7,12 +7,12 @@
</Pane>
<Pane size="80">
<template v-if="hasTabs">
- <Splitpanes :key="paneKey" horizontal class="default-theme">
+ <Splitpanes horizontal class="default-theme">
<Pane size="60">
- <EditorPanel />
+ <EditorPanel :key="paneKey" />
</Pane>
<Pane size="40">
- <TablePanel />
+ <TablePanel :key="paneKey" />
</Pane>
</Splitpanes>
</template>
diff --git a/frontend/yarn.lock b/frontend/yarn.lock
index 040483d902..1649cf997d 100644
--- a/frontend/yarn.lock
+++ b/frontend/yarn.lock
@@ -1421,6 +1421,11 @@ deep-is@^0.1.3:
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.4.tgz#a6f2dce612fadd2ef1f519b73551f17e85199831"
integrity sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==
+deepmerge@^4.2.2:
+ version "4.2.2"
+ resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955"
+ integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==
+
defer-to-connect@^1.0.1:
version "1.1.3"
resolved "https://registry.yarnpkg.com/defer-to-connect/-/defer-to-connect-1.1.3.tgz#331ae050c08dcf789f8c83a7b81f0ed94f4ac591"
@@ -4174,6 +4179,11 @@ shebang-regex@^3.0.0:
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
+shvl@^2.0.3:
+ version "2.0.3"
+ resolved "https://registry.yarnpkg.com/shvl/-/shvl-2.0.3.tgz#eb4bd37644f5684bba1fc52c3010c96fb5e6afd1"
+ integrity sha512-V7C6S9Hlol6SzOJPnQ7qzOVEWUQImt3BNmmzh40wObhla3XOYMe4gGiYzLrJd5TFa+cI2f9LKIRJTTKZSTbWgw==
+
signal-exit@^3.0.2, signal-exit@^3.0.3:
version "3.0.6"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af"
@@ -4968,6 +4978,14 @@ vuex-composition-helpers@^2.0.1:
resolved "https://registry.yarnpkg.com/vuex-composition-helpers/-/vuex-composition-helpers-2.0.1.tgz#8a1a2e5ac18e5e8e2c52f7ec717797f6261bcf6c"
integrity sha512-i2Bg24AH9/AL5nDdh2lmP19Mcl86EJLV7EeVjrCtqY29wkzZa6PWaYYKcbwbYffYoAiTtz1n54VhIaZ1MxNAkw==
+vuex-persistedstate@^4.1.0:
+ version "4.1.0"
+ resolved "https://registry.yarnpkg.com/vuex-persistedstate/-/vuex-persistedstate-4.1.0.tgz#127165f85f5b4534fb3170a5d3a8be9811bd2a53"
+ integrity sha512-3SkEj4NqwM69ikJdFVw6gObeB0NHyspRYMYkR/EbhR0hbvAKyR5gksVhtAfY1UYuWUOCCA0QNGwv9pOwdj+XUQ==
+ dependencies:
+ deepmerge "^4.2.2"
+ shvl "^2.0.3"
+
vuex@^4.0.0:
version "4.0.2"
resolved "https://registry.yarnpkg.com/vuex/-/vuex-4.0.2.tgz#f896dbd5bf2a0e963f00c67e9b610de749ccacc9"