feat:左侧树信息持久化

This commit is contained in:
shixiaohua 2024-02-04 12:30:40 +08:00
parent 172723e3b0
commit ed8ce9ae79
2 changed files with 26 additions and 31 deletions

View File

@ -5,6 +5,7 @@ import "./index.less"
const {Search} = Input; const {Search} = Input;
import {store} from "../../redux/store"; import {store} from "../../redux/store";
import {clickFile} from "../../redux/clickFile_reducer"; import {clickFile} from "../../redux/clickFile_reducer";
import {useSelector} from "react-redux";
const defaultData = []; const defaultData = [];
// //
@ -58,41 +59,34 @@ function generateChildList(fileList) {
const ItemTree = () => { const ItemTree = () => {
const [expandedKeys, setExpandedKeys] = useState([]); const [expandedKeys, setExpandedKeys] = useState([]);
const [searchValue, setSearchValue] = useState(''); const [searchValue, setSearchValue] = useState('');
const [defaultValueState, setDefaultValueState] = useState([]);
const [autoExpandParent, setAutoExpandParent] = useState(true); const [autoExpandParent, setAutoExpandParent] = useState(true);
useEffect(() => { const defaultValueStateSet = [];
let unsubscribe = store.subscribe(() => { let fileDirDate = useSelector(state => state.dirMessage.data);
let fileDirDate = store.getState().dirMessage.data; if (fileDirDate.length>0){
if (fileDirDate.length===0){ for (let i = 0; i < fileDirDate.length; i++) {
return const node = fileDirDate[i];
console.log("node:", node)
const {fileName, filePath, childList, dirFlag} = node;
const childListM = []
if (childList.length > 0) {
childListM.push(...generateChildList(childList));
} }
console.log("打开目录1fileDirDate:", fileDirDate) if (defaultData.filter(fileMessage => fileMessage.key === filePath).length === 0) {
for (let i = 0; i < fileDirDate.length; i++) { defaultData.push({
const node = fileDirDate[i]; "key": filePath,
console.log("node:", node) "title": fileName,
const {fileName, filePath, childList, dirFlag} = node; "icon": <FolderOutlined/>,
const childListM = [] "dirFlag": dirFlag,
if (childList.length > 0) { "children": childListM
childListM.push(...generateChildList(childList)); });
}
if (defaultData.filter(fileMessage =>
fileMessage.key === filePath
).length === 0) {
defaultData.push({
"key": filePath,
"title": fileName,
"icon": <FolderOutlined/>,
"dirFlag": dirFlag,
"children": childListM
});
}
} }
console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData))) }
setDefaultValueState(Array.from(new Set(defaultData))) console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData)))
}) defaultValueStateSet.push(...Array.from(new Set(defaultData)))
return ()=>unsubscribe() }
}, []) const [defaultValueState, setDefaultValueState] = useState(defaultValueStateSet);
const onExpand = (newExpandedKeys) => { const onExpand = (newExpandedKeys) => {
setExpandedKeys(newExpandedKeys); setExpandedKeys(newExpandedKeys);

View File

@ -7,6 +7,7 @@ export const clickFileSlice = createSlice({
data: {} data: {}
}, },
reducers: { reducers: {
// 点击文件添加item设置单前文件
clickFile: (state, action) => { clickFile: (state, action) => {
console.log("clickFileSlice:clickFile", state, action) console.log("clickFileSlice:clickFile", state, action)
state.data=action.payload state.data=action.payload