import {createSlice} from '@reduxjs/toolkit' import {isEmpty} from "../utils/ObjectUtils"; /* "fileName": filePath, "filePath": filePath, "dirFlag": true, "childList": fileChildList */ export const dirMessageSlice = createSlice({ name: 'dirMessage', initialState: { data: [], // selectDirKey:"" }, reducers: { dirAdd: (state, action) => { console.log("dirMessage:dirAdd", state, action) if (action.payload) { // 新添加进来的目录,要判断是否包含或者被包含,如果包含则统一树结构 let fileFilter = state.data.filter((fileMessage) => fileMessage.filePath.startsWith(action.payload[0].filePath) ); let actionFilter = state.data.filter((fileMessage) => action.payload[0].filePath.startsWith(fileMessage.filePath) ); let equalsFilter = state.data.filter((fileMessage) => action.payload[0].filePath===fileMessage.filePath ); // 打开相同目录不用处理 if (equalsFilter.length > 0) { console.log('equalsFilter', filter) } // 打开上级目录 else if (actionFilter.length>0){ } // 打开下级目录 else if (fileFilter.length>0){ } // 无关目录 else { // 添加进当前目录 state.data = [...new Set([...state.data, ...action.payload])]; console.log('state.data:', state.data) } } }, nextDirAdd: (state, action) => { console.log("dirMessage:nextDirAdd", state, action) // 获取当前选中的key let selectDirKey = action.payload.selectDirKey; // 便利文件树,找到对应的key并加入其中, // 如果包含下级目录则不更新,在刷新中更新。 state.data.forEach(file => { if (file.filePath === selectDirKey && file.dirFlag && file.childList.length === 0) { file.childList.push(action.payload.fileStateList[0].childList) } else if (file.childList.length > 0) { findChild(file.childList, action, selectDirKey) } }) } } }) function findChild(fileList, action, selectDirKey) { fileList.forEach(file => { if (file.filePath === selectDirKey && file.dirFlag && (isEmpty(file.childList) || (Array.isArray(file.childList) && file.childList.length === 0))) { file.childList = action.payload.fileStateList[0].childList return } if (file.dirFlag && Array.isArray(file.childList) && file.childList.length > 0) { findChild(file.childList, action, selectDirKey) } }) } export const { dirAdd, nextDirAdd } = dirMessageSlice.actions export default dirMessageSlice.reducer