fix(knowdge-base): 知识库上传提示优化

This commit is contained in:
sophie committed 2026-07-10 09:39:27 +08:00
1 parent 5e7e5a8717
commit 854793f3fe
10 files changed
+181 -23

No files matched your search

@@ -628,7 +628,10 @@ defineProps<{
<DeleteConfirmDialog
v-model="kb.showDeleteConfirmDialog"
:title="kb.deleteConfirmTitle"
:message="kb.deleteConfirmMessage"
:confirm-text="kb.deleteConfirmText"
:confirm-tone="kb.deleteConfirmTone"
@confirm="kb.handleDeleteConfirm"
@cancel="kb.closeDeleteConfirmDialog"
/>
@@ -1,6 +1,8 @@
/**
* features/kb/composables/kb-workspace/kb-workspace.state.ts
* 作业面共享响应式状态:各子 composable 通过同一 state 读写,避免重复 ref。
* 用途:作业面共享响应式状态;各子 composable 通过同一 state 读写,避免重复 ref。
* 依赖:vue-router、vue-i18n、auth、kb.api 类型、kb-workspace.types。
* 逻辑:createKbWorkspaceState 产出全部 ref;含公共确认弹窗(删除/上传路径等)辅助方法。
*/
import { computed, ref } from 'vue';
import { useRouter } from 'vue-router';
@@ -57,6 +59,9 @@ export function createKbWorkspaceState(options: CreateKbWorkspaceStateOptions =
const showPathUploadHistoryModal = ref(false);
const showDeleteConfirmDialog = ref(false);
const deleteConfirmMessage = ref('');
const deleteConfirmTitle = ref('');
const deleteConfirmText = ref('');
const deleteConfirmTone = ref<'danger' | 'primary'>('danger');
let pendingDeleteAction: (() => Promise<void> | void) | null = null;
const showPathUploadDetails = ref(false);
const fileInputRef = ref<HTMLInputElement | null>(null);
@@ -114,11 +119,27 @@ export function createKbWorkspaceState(options: CreateKbWorkspaceStateOptions =
items: [],
});
/**
* 打开公共确认弹窗(删除 / 上传路径等场景复用)。
* @param message - 正文
* @param onConfirm - 确认回调
* @param options.title - 标题;空则用删除默认文案
* @param options.confirmText - 确认按钮文案
* @param options.confirmTone - danger 删除 / primary 非破坏性确认
*/
const openDeleteConfirmDialog = (
message: string,
onConfirm: () => Promise<void> | void,
options?: {
title?: string;
confirmText?: string;
confirmTone?: 'danger' | 'primary';
},
) => {
deleteConfirmMessage.value = message;
deleteConfirmTitle.value = options?.title || '';
deleteConfirmText.value = options?.confirmText || '';
deleteConfirmTone.value = options?.confirmTone || 'danger';
pendingDeleteAction = onConfirm;
showDeleteConfirmDialog.value = true;
};
@@ -126,6 +147,9 @@ export function createKbWorkspaceState(options: CreateKbWorkspaceStateOptions =
const closeDeleteConfirmDialog = () => {
showDeleteConfirmDialog.value = false;
deleteConfirmMessage.value = '';
deleteConfirmTitle.value = '';
deleteConfirmText.value = '';
deleteConfirmTone.value = 'danger';
pendingDeleteAction = null;
};
@@ -170,6 +194,9 @@ export function createKbWorkspaceState(options: CreateKbWorkspaceStateOptions =
showPathUploadHistoryModal,
showDeleteConfirmDialog,
deleteConfirmMessage,
deleteConfirmTitle,
deleteConfirmText,
deleteConfirmTone,
showPathUploadDetails,
fileInputRef,
pathUploadInputRef,
@@ -1,6 +1,8 @@
/**
* features/kb/composables/kb-workspace/useKbWorkspacePathUpload.ts
* 路径上传:选文件、分批 job、轮询进度与历史批次操作。
* 用途:路径上传编排——选目录、分批 job、轮询进度与历史批次操作。
* 依赖:kb.api、kb-path-upload.utils、DeleteConfirmDialog(经 state.openDeleteConfirmDialog)。
* 逻辑:优先 showDirectoryPicker 避免浏览器原生目录确认;回退 webkitdirectory;确认后写入待上传清单。
*/
import type { KbJob } from '../../api/kb.api';
import {
@@ -169,21 +171,25 @@ export function useKbWorkspacePathUpload(
resetPathUploadDraft();
};
const triggerPathUploadSelection = () => {
pathUploadInputRef.value?.click();
};
const handlePathFilesSelected = (event: Event) => {
const input = event.target as HTMLInputElement;
const files = Array.from(input.files || []);
if (!files.length) {
input.value = '';
return;
}
/**
* 将 File 列表合并进待上传清单(按相对路径去重)。
* @param files - 目录遍历得到的文件
* @param getRelativePath - 取相对路径;缺省用 webkitRelativePath / name
*/
const appendPathUploadFiles = (
files: File[],
getRelativePath?: (file: File) => string,
) => {
if (!files.length) return;
const existing = new Set(pathUploadSelectedFiles.value.map((item) => item.displayPath));
const nextItems = files.flatMap((file, index) => {
const relativePath = (file as File & { webkitRelativePath?: string }).webkitRelativePath || file.name;
if (existing.has(relativePath)) return [];
const relativePath = (
getRelativePath?.(file)
|| (file as File & { webkitRelativePath?: string }).webkitRelativePath
|| file.name
).replace(/\\/g, '/');
if (!relativePath || existing.has(relativePath)) return [];
existing.add(relativePath);
const rootLabel = relativePath.includes('/') ? relativePath.split('/')[0] || file.name : file.name;
return [{
key: `${Date.now()}-${index}-${relativePath}`,
@@ -192,9 +198,111 @@ export function useKbWorkspacePathUpload(
rootLabel,
}];
});
if (!nextItems.length) return;
pathUploadSelectedFiles.value = [...pathUploadSelectedFiles.value, ...nextItems];
pathUploadDraftText.value = pathUploadSelectedFiles.value.map((item) => item.displayPath).join('\n');
};
/**
* 递归读取 File System Access API 目录句柄下的全部文件。
*/
const collectFilesFromDirectoryHandle = async (
handle: FileSystemDirectoryHandle,
parentPath = '',
): Promise<File[]> => {
const files: File[] = [];
// File System Access API:DirectoryHandle 可异步迭代子项
for await (const entry of (handle as any).values() as AsyncIterable<FileSystemHandle>) {
const entryPath = parentPath ? `${parentPath}/${entry.name}` : entry.name;
if (entry.kind === 'file') {
const file = await (entry as FileSystemFileHandle).getFile();
Object.defineProperty(file, 'webkitRelativePath', {
configurable: true,
value: entryPath,
});
files.push(file);
} else if (entry.kind === 'directory') {
files.push(...await collectFilesFromDirectoryHandle(entry as FileSystemDirectoryHandle, entryPath));
}
}
return files;
};
/**
* 打开目录选择器;优先 showDirectoryPicker(无 Windows 原生上传确认),
* 不支持时回退隐藏 input[webkitdirectory]。
* 选完后用公共 DeleteConfirmDialog 二次确认再写入清单。
*/
const triggerPathUploadSelection = async () => {
const picker = (window as Window & {
showDirectoryPicker?: (options?: { mode?: 'read' | 'readwrite' }) => Promise<FileSystemDirectoryHandle>;
}).showDirectoryPicker;
if (typeof picker === 'function') {
try {
const dirHandle = await picker({ mode: 'read' });
const files = await collectFilesFromDirectoryHandle(dirHandle, dirHandle.name);
if (!files.length) {
feedback.value = t('kbWorkspace.msg.pathUploadEmptyFolder');
feedbackType.value = 'error';
return;
}
openDeleteConfirmDialog(
t('kbWorkspace.pathUploadModal.folderConfirmMessage', {
count: files.length,
name: dirHandle.name,
}),
() => {
appendPathUploadFiles(files, (file) => (
(file as File & { webkitRelativePath?: string }).webkitRelativePath || file.name
));
},
{
title: t('kbWorkspace.pathUploadModal.folderConfirmTitle', { count: files.length }),
confirmText: t('kbWorkspace.pathUploadModal.folderConfirmAction'),
confirmTone: 'primary',
},
);
} catch (err) {
// 用户取消选目录不提示
if (err instanceof DOMException && err.name === 'AbortError') return;
feedback.value = t('kbWorkspace.msg.pathUploadPickFailed');
feedbackType.value = 'error';
}
return;
}
pathUploadInputRef.value?.click();
};
/**
* 旧版 input[webkitdirectory] 回退:浏览器会先弹原生确认,再进入本回调。
* 仍用公共确认弹窗二次确认后写入清单。
*/
const handlePathFilesSelected = (event: Event) => {
const input = event.target as HTMLInputElement;
const files = Array.from(input.files || []);
input.value = '';
const firstFile = files[0];
if (!firstFile) return;
const firstRelative = (firstFile as File & { webkitRelativePath?: string }).webkitRelativePath || firstFile.name;
const folderName = firstRelative.includes('/') ? (firstRelative.split('/')[0] || firstRelative) : firstRelative;
openDeleteConfirmDialog(
t('kbWorkspace.pathUploadModal.folderConfirmMessage', {
count: files.length,
name: folderName,
}),
() => {
appendPathUploadFiles(files);
},
{
title: t('kbWorkspace.pathUploadModal.folderConfirmTitle', { count: files.length }),
confirmText: t('kbWorkspace.pathUploadModal.folderConfirmAction'),
confirmTone: 'primary',
},
);
};
const removeSelectedPathFile = (key: string) => {