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