1、切换文件目录后,自动保存已选文件列表

This commit is contained in:
2026-05-13 12:50:36 +08:00
parent 083abb2c6b
commit f7fdc1b55a

View File

@@ -44,7 +44,7 @@
<div class="doc-actions">
<span class="doc-tips"
>已选择 {{ checkedDirKeys.length }} 个目录,
{{ selectedFileKeys.length }} 个文件</span
{{ selectedFileList.length }} 个文件</span
>
<a-space>
<a-button @click="clearSelection">清空选择</a-button>
@@ -113,6 +113,8 @@
(e: 'confirm', data: { dirKeys: (string | number)[], fileKeys: (string | number)[] }): void;
}>();
type DirFileSelectionMap = Record<string, (string | number)[]>;
// 树形结构相关
const expandedKeys = ref<(string | number)[]>([]);
const selectedKeys = ref<(string | number)[]>([]);
@@ -175,7 +177,7 @@
string,
{
dirKeys: (string | number)[];
fileKeys: (string | number)[];
dirFileSelections: DirFileSelectionMap;
}
>
>({});
@@ -185,23 +187,56 @@
const selectedFileKeys = ref<(string | number)[]>([]);
const checkedDirKeys = ref<(string | number)[]>([]); // 新增勾选的目录keys
const dirFileKeyCache = ref<Record<string, (string | number)[]>>({});
const dirFileSelections = ref<DirFileSelectionMap>({});
const getSelectionStorageKey = () => props.selectionKey || '__default__';
const getDirSelectionKey = (dirKey?: string | number) => {
if (dirKey === undefined || dirKey === null) return '';
return String(dirKey);
};
const getCurrentDirKey = () => getDirSelectionKey(selectedKeys.value[0]);
const getAllSelectedFileKeys = () => {
return Array.from(
new Set(
Object.values(dirFileSelections.value)
.flat()
.filter((key) => key !== undefined && key !== null)
)
);
};
const syncSelectedFileState = () => {
const allSelectedFileKeys = getAllSelectedFileKeys();
const currentDirKey = getCurrentDirKey();
selectedFileKeys.value = [...(dirFileSelections.value[currentDirKey] || [])];
selectedFileList.value = allSelectedFileKeys.map((key) => key.toString());
};
const saveSelectionState = () => {
selectionStateMap.value[getSelectionStorageKey()] = {
dirKeys: [...checkedDirKeys.value],
fileKeys: [...selectedFileKeys.value]
dirFileSelections: Object.fromEntries(
Object.entries(dirFileSelections.value).map(([key, value]) => [
key,
[...value]
])
)
};
};
const restoreSelectionState = () => {
const state = selectionStateMap.value[getSelectionStorageKey()];
checkedDirKeys.value = [...(state?.dirKeys || [])];
selectedFileKeys.value = [...(state?.fileKeys || [])];
selectedDocList.value = checkedDirKeys.value.map((key) => key.toString());
selectedFileList.value = selectedFileKeys.value.map((key) =>
key.toString()
dirFileSelections.value = Object.fromEntries(
Object.entries(state?.dirFileSelections || {}).map(([key, value]) => [
key,
[...value]
])
);
syncSelectedFileState();
};
const handleDocSelectCancel = () => {
@@ -215,11 +250,11 @@
// 传递选择的数据给父组件
emit('confirm', {
dirKeys: [...checkedDirKeys.value],
fileKeys: [...selectedFileKeys.value]
fileKeys: getAllSelectedFileKeys()
});
message.success(
`已选择 ${checkedDirKeys.value.length} 个目录和 ${selectedFileKeys.value.length} 个文件`
`已选择 ${checkedDirKeys.value.length} 个目录和 ${getAllSelectedFileKeys().length} 个文件`
);
showDocSelect.value = false;
};
@@ -243,6 +278,7 @@
selectedKeys.value = [rootDirs[0].id!];
}
syncSelectedFileState();
loadCloudFiles();
}
}
@@ -261,6 +297,7 @@
const onSelect = (keys: (string | number)[]) => {
selectedKeys.value = keys;
pagination.value.current = 1;
syncSelectedFileState();
loadCloudFiles();
};
@@ -278,10 +315,11 @@
return dirFileKeyCache.value[cacheKey];
}
const files = await listAiCloudFile({
const result: any = await listAiCloudFile({
docId: Number(dirKey)
});
const fileKeys = (files || [])
const files = Array.isArray(result) ? result : result?.records || [];
const fileKeys = files
.map((item) => item.id)
.filter((id) => id !== undefined && id !== null) as (string | number)[];
@@ -312,18 +350,13 @@
const addedFileGroups = await Promise.all(
addedKeys.map((key) => getDirFileKeys(key))
);
const removedFileGroups = await Promise.all(
removedKeys.map((key) => getDirFileKeys(key))
);
const nextFileKeySet = new Set<(string | number)>(selectedFileKeys.value);
addedFileGroups.flat().forEach((key) => nextFileKeySet.add(key));
removedFileGroups.flat().forEach((key) => nextFileKeySet.delete(key));
selectedFileKeys.value = Array.from(nextFileKeySet);
selectedFileList.value = selectedFileKeys.value.map((key) =>
key.toString()
);
addedKeys.forEach((key, index) => {
dirFileSelections.value[String(key)] = [...addedFileGroups[index]];
});
removedKeys.forEach((key) => {
delete dirFileSelections.value[String(key)];
});
syncSelectedFileState();
} catch (error) {
message.error('目录文件联动选择失败');
console.error('目录文件联动选择失败:', error);
@@ -361,8 +394,14 @@
selectedRowKeys: (string | number)[],
selectedRows: AiCloudFile[]
) => {
const currentDirKey = getCurrentDirKey();
if (!currentDirKey) return;
dirFileSelections.value[currentDirKey] = [...selectedRowKeys];
selectedFileKeys.value = selectedRowKeys;
selectedFileList.value = selectedRowKeys.map((key) => key.toString());
selectedFileList.value = getAllSelectedFileKeys().map((key) =>
key.toString()
);
saveSelectionState();
};
@@ -371,13 +410,8 @@
selectedDocList.value = [];
selectedFileList.value = [];
selectedFileKeys.value = [];
checkedDirKeys.value = []; // 新增:清空勾选的目录
// 重新选择当前目录
if (selectedKeys.value.length > 0) {
selectedDocList.value = [selectedKeys.value[0].toString()];
checkedDirKeys.value = [selectedKeys.value[0]]; // 新增:重新勾选当前目录
}
checkedDirKeys.value = [];
dirFileSelections.value = {};
saveSelectionState();
};