Skip to content

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

chore: support create new folder when saving the worksheet (#18101)

ecmadaoe19c4176a594
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 9c16cdd35f..e61d864878 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2047,7 +2047,8 @@
"project-write-desc": "Both sheet owner and project users with \"bb.worksheets.get\" permission can read/write",
"create-read-only-data-source": "Go to create",
"save-sheet": "Save Sheet",
- "choose-folder": "Choose folder",
+ "choose-folder": "Choose or create folder",
+ "choose-folder-tips": "You can create a new folder by entering a non-existent path. The folders should be joined with a \"/\".",
"save-sheet-input-placeholder": "Please input a sheet name",
"format": "Format",
"visualize-explain": "Visualize Explain",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index b98aa4e84a..93f3186648 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2048,6 +2048,7 @@
"create-read-only-data-source": "Ir a crear",
"save-sheet": "Guardar hoja",
"choose-folder": "Seleccionar carpeta",
+ "choose-folder-tips": "Puedes crear una nueva carpeta introduciendo una ruta que no exista. Las carpetas deben unirse con una barra diagonal (\"/\").",
"save-sheet-input-placeholder": "Por favor, ingresa un nombre para la hoja",
"format": "Formato",
"visualize-explain": "Visualizar explicación",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index edfb1e8de0..9e03c836ec 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -2048,6 +2048,7 @@
"create-read-only-data-source": "作成に行く",
"save-sheet": "ワークシートの保存",
"choose-folder": "フォルダを選択",
+ "choose-folder-tips": "存在しないパスを入力することで新しいフォルダを作成できます。フォルダは「/」で結合する必要があります。",
"save-sheet-input-placeholder": "ワークシート名を入力してください",
"format": "フォーマット",
"visualize-explain": "視覚化の説明",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index c5530febb3..8f816488af 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -2048,6 +2048,7 @@
"create-read-only-data-source": "Đi tới tạo",
"save-sheet": "Lưu trang tính",
"choose-folder": "Chọn thư mục",
+ "choose-folder-tips": "Bạn có thể tạo một thư mục mới bằng cách nhập một đường dẫn không tồn tại. Các thư mục phải được nối bằng dấu \"/\".",
"save-sheet-input-placeholder": "Vui lòng nhập tên trang tính",
"format": "Định dạng",
"visualize-explain": "Trực quan hóa Giải thích",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index bfb5ae50ec..849e4fea57 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2048,6 +2048,7 @@
"create-read-only-data-source": "前去创建",
"save-sheet": "保存工作表",
"choose-folder": "选择文件夹",
+ "choose-folder-tips": "您可以通过输入不存在的路径来创建新文件夹。文件夹之间应以“/”连接。",
"save-sheet-input-placeholder": "请输入工作表名称",
"format": "格式化",
"visualize-explain": "可视化 Explain",
diff --git a/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue b/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
index 3e6d8d2238..c1363f8a25 100644
--- a/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/SaveSheetModal.vue
@@ -15,29 +15,56 @@
:maxlength="200"
/>
</div>
- <div class="flex flex-col gap-y-1">
- <p>{{ $t("sql-editor.choose-folder") }}</p>
- <!-- TODO(ed): support creating new folders when saving the worksheet -->
- <NTreeSelect
- filterable
- clearable
- checkable
- show-path
- virtual-scroll
- :multiple="false"
- :options="[folderTree]"
- :check-strategy="'parent'"
- :render-prefix="renderPrefix"
- v-model:expanded-keys="expandedKeys"
- v-model:value="pendingEdit.folder"
- />
+ <div class="flex flex-col gap-y-2">
+ <div>
+ <p>{{ $t("sql-editor.choose-folder") }}</p>
+ <span class="textinfolabel">
+ {{ $t("sql-editor.choose-folder-tips") }}
+ </span>
+ </div>
+ <NPopover
+ placement="bottom"
+ :show="showPopover"
+ :show-arrow="false"
+ trigger="manual"
+ :width="folderInputRef?.wrapperElRef?.clientWidth"
+ >
+ <template #trigger>
+ <NInput
+ ref="folderInputRef"
+ :value="formattedFolderPath.split('/').join(' / ')"
+ :placeholder="$t('sql-editor.choose-folder')"
+ @focus="onFocus"
+ @update:value="onInput"
+ />
+ </template>
+ <NTree
+ ref="folderTreeRef"
+ block-line
+ block-node
+ virtual-scroll
+ :clearable="false"
+ :filterable="true"
+ :pattern="pendingEdit.folder"
+ :checkable="false"
+ :check-on-click="true"
+ :selectable="true"
+ :selected-keys="[pendingEdit.folder]"
+ :multiple="false"
+ :data="folderTree.children"
+ :render-prefix="renderPrefix"
+ :expanded-keys="expandedKeysArray"
+ @update:expanded-keys="(keys: string[]) => expandedKeys = new Set(keys)"
+ @update:selected-keys="onSelect"
+ />
+ </NPopover>
</div>
<div class="flex justify-end gap-x-2 mt-4">
<NButton @click="close">{{ $t("common.close") }}</NButton>
<NButton
:disabled="!pendingEdit.title"
type="primary"
- @click="() => doSaveSheet(pendingEdit)"
+ @click="doSaveSheet"
>
{{ $t("common.save") }}
</NButton>
@@ -48,13 +75,9 @@
<script lang="tsx" setup>
import { create } from "@bufbuild/protobuf";
-import {
- FolderCodeIcon,
- FolderMinusIcon,
- FolderOpenIcon,
-} from "lucide-vue-next";
-import { NButton, NInput, NTreeSelect, type TreeOption } from "naive-ui";
-import { ref } from "vue";
+import { onClickOutside } from "@vueuse/core";
+import { NButton, NInput, NPopover, NTree, type TreeOption } from "naive-ui";
+import { computed, nextTick, ref } from "vue";
import { BBModal } from "@/bbkit";
import RequiredStar from "@/components/RequiredStar.vue";
import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
@@ -67,6 +90,7 @@ import {
WorksheetSchema,
} from "@/types/proto-es/v1/worksheet_service_pb";
import { extractWorksheetUID } from "@/utils";
+import TreeNodePrefix from "@/views/sql-editor/AsidePanel/WorksheetPane/SheetList/TreeNodePrefix.vue";
import type { WorsheetFolderNode } from "@/views/sql-editor/Sheet";
import { useSheetContextByView } from "@/views/sql-editor/Sheet";
import { useSQLEditorContext } from "../context";
@@ -74,7 +98,7 @@ import { useSQLEditorContext } from "../context";
const tabStore = useSQLEditorTabStore();
const worksheetV1Store = useWorkSheetStore();
const { events: editorEvents } = useSQLEditorContext();
-const { folderTree, folderContext, getPwdForWorksheet } =
+const { folderTree, folderContext, getPwdForWorksheet, getPathesForWorksheet } =
useSheetContextByView("my");
const pendingEdit = ref<{
@@ -85,24 +109,32 @@ const pendingEdit = ref<{
title: "",
folder: "",
});
-const expandedKeys = ref<string[]>([folderContext.rootPath.value]);
+const expandedKeys = ref<Set<string>>(new Set([]));
+const expandedKeysArray = computed(() => Array.from(expandedKeys.value));
+const folderInputRef = ref<InstanceType<typeof NInput>>();
+const folderTreeRef = ref<InstanceType<typeof NTree>>();
+const showPopover = ref<boolean>(false);
const showModal = ref(false);
-const doSaveSheet = async ({
- title,
- folder,
- rawTab,
-}: {
- title: string;
- folder: string;
- rawTab?: SQLEditorTab;
-}) => {
- if (!rawTab) {
+onClickOutside(folderTreeRef, () => {
+ showPopover.value = false;
+});
+
+const formattedFolderPath = computed(() => {
+ let val = pendingEdit.value.folder.replace(folderContext.rootPath.value, "");
+ if (val[0] === "/") {
+ val = val.slice(1);
+ }
+ return val;
+});
+
+const doSaveSheet = async () => {
+ if (!pendingEdit.value.rawTab) {
return close();
}
- const { statement, worksheet } = rawTab;
+ const { statement, worksheet, connection } = pendingEdit.value.rawTab;
- if (title === "") {
+ if (pendingEdit.value.title === "") {
return;
}
@@ -111,13 +143,15 @@ const doSaveSheet = async ({
if (sheetId !== UNKNOWN_ID) {
const currentSheet = worksheetV1Store.getWorksheetByName(worksheet);
- if (!currentSheet) return;
+ if (!currentSheet) {
+ return;
+ }
worksheetEntity = await worksheetV1Store.patchWorksheet(
{
...currentSheet,
- title: title,
- database: rawTab.connection.database,
+ title: pendingEdit.value.title,
+ database: connection.database,
content: new TextEncoder().encode(statement),
},
["title", "content", "database"]
@@ -125,20 +159,20 @@ const doSaveSheet = async ({
} else {
worksheetEntity = await worksheetV1Store.createWorksheet(
create(WorksheetSchema, {
- title,
+ title: pendingEdit.value.title,
project: tabStore.project,
content: new TextEncoder().encode(statement),
- database: rawTab.connection.database,
+ database: connection.database,
visibility: Worksheet_Visibility.PRIVATE,
})
);
}
if (worksheetEntity) {
- if (folder && folder !== folderContext.rootPath.value) {
- const folders = folder
- .replace(folderContext.rootPath.value, "")
+ if (formattedFolderPath.value) {
+ const folders = formattedFolderPath.value
.split("/")
+ .map((p) => p.trim())
.filter((p) => p);
await worksheetV1Store.upsertWorksheetOrganizer(
{
@@ -148,10 +182,16 @@ const doSaveSheet = async ({
},
["folders"]
);
+
+ const folderPathes = new Set<string>([]);
+ for (const path of getPathesForWorksheet({ folders })) {
+ folderPathes.add(path);
+ }
+ folderContext.mergeFolders(folderPathes);
}
- tabStore.updateTab(rawTab.id, {
- title,
+ tabStore.updateTab(pendingEdit.value.rawTab.id, {
+ title: pendingEdit.value.title,
status: "CLEAN",
worksheet: worksheetEntity.name,
});
@@ -160,6 +200,27 @@ const doSaveSheet = async ({
showModal.value = false;
};
+const onFocus = () => {
+ showPopover.value = true;
+};
+
+const onSelect = (keys: string[]) => {
+ pendingEdit.value.folder = keys[0] ?? "";
+ nextTick(() => (showPopover.value = false));
+};
+
+const onInput = (val: string) => {
+ let changedVal = val;
+ if (val.endsWith(" /")) {
+ changedVal = val.slice(0, -2);
+ }
+ const rawPath = changedVal
+ .split("/")
+ .map((p) => p.trim())
+ .join("/");
+ pendingEdit.value.folder = rawPath;
+};
+
const needShowModal = (tab: SQLEditorTab) => {
// If the sheet is saved, we don't need to show the name popup.
return !tab.worksheet;
@@ -172,7 +233,7 @@ const close = () => {
useEmitteryEventListener(editorEvents, "save-sheet", ({ tab, editTitle }) => {
pendingEdit.value = {
title: tab.title,
- folder: folderContext.rootPath.value,
+ folder: "",
rawTab: tab,
};
@@ -184,27 +245,18 @@ useEmitteryEventListener(editorEvents, "save-sheet", ({ tab, editTitle }) => {
showModal.value = true;
return;
}
- doSaveSheet(pendingEdit.value);
+ doSaveSheet();
});
const renderPrefix = ({ option }: { option: TreeOption }) => {
const node = option as WorsheetFolderNode;
- if (node.worksheet) {
- return null;
- }
- if (expandedKeys.value.includes(node.key)) {
- // is opened folder
- return <FolderOpenIcon class="w-4 h-auto text-gray-600" />;
- }
- if (node.key === folderContext.rootPath.value) {
- // root folder icon
- return <FolderCodeIcon class="w-4 h-auto text-gray-600" />;
- }
- if (node.empty) {
- // empty folder icon
- return <FolderMinusIcon class="w-4 h-auto text-gray-600" />;
- }
- // fallback to normal folder icon
- return <FolderCodeIcon class="w-4 h-auto text-gray-600" />;
+ return (
+ <TreeNodePrefix
+ node={node}
+ expandedKeys={expandedKeys.value}
+ rootPath={folderContext.rootPath.value}
+ view={"my"}
+ />
+ );
};
</script>
diff --git a/frontend/src/views/sql-editor/Sheet/context.ts b/frontend/src/views/sql-editor/Sheet/context.ts
index b0bc9e89dc..c993466656 100644
--- a/frontend/src/views/sql-editor/Sheet/context.ts
+++ b/frontend/src/views/sql-editor/Sheet/context.ts
@@ -220,6 +220,7 @@ const useSheetTreeByView = (
parent.children.push(...sheets);
parent.empty = sheets.length === 0 && empty;
+ parent.isLeaf = parent.children.length === 0;
return parent;
};