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) } } }, dirRemove:(state,action)=>{ console.log("dirMessage:dirRemove", state, action) // 获取当前选中的key let selectDirKey = action.payload.selectDirKey; state.data = state.data.filter(file=>{ if (file.filePath === selectDirKey && file.dirFlag) { return false; } else if (file.childList.length > 0 && selectDirKey.startsWith(file.filePath)) { file.childList = filterChild(file.childList, selectDirKey) return true; }else { return true; } }) }, 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) } }) }, updateFileName:(state,action)=>{ console.log("dirMessage:updateFileName", state, action) let newFilePath = action.payload.newFilePath let oldFilePath = action.payload.oldFilePath // 查找旧文件并且修改文件信息 state.data.forEach(file => { if (file.filePath === oldFilePath) { file.filePath = newFilePath file.fileName = newFilePath.substring(newFilePath.lastIndexOf("/")+1) } else if (file.childList.length > 0 && oldFilePath.startsWith(file.filePath)) { updateFileNameChild(file.childList, oldFilePath, newFilePath) } }) }, dirFileAdd:(state,action)=>{ console.log("dirMessage:dirFileAdd", state, action) let filePath = action.payload.filePath let fileName = action.payload.fileName let fileMessage = { "fileName": fileName.replace(filePath+"/",""), "filePath": fileName, "dirFlag": false, "childList": [] } // 查找旧文件并且修改文件信息 state.data.forEach(file => { if (file.filePath === filePath) { if (Array.isArray(file.children)){ file.children.push(fileMessage) }else { file.children=[fileMessage] } } else if (file.childList.length > 0 && file.filePath.startsWith(filePath)) { dirFileAddChild(file.childList, filePath, fileMessage) } }) }, dirFileRemove:(state,action)=>{ let filePath = action.payload.filePath; state.data = state.data.filter(file=>{ if (file.filePath === filePath && !file.dirFlag) { return false; } else if (file.childList.length > 0 && filePath.startsWith(file.filePath)) { file.childList = dirFileRemoveChild(file.childList, filePath) return true; }else { return true; } }) } } }) function dirFileRemoveChild(fileList, selectDirKey) { return fileList.filter(file => { if (file.filePath === selectDirKey && !file.dirFlag) { return false }else if (file.dirFlag && selectDirKey.startsWith(file.filePath) && Array.isArray(file.childList) && file.childList.length > 0) { dirFileRemoveChild(file.childList, selectDirKey) return true }else { return true } }) } function dirFileAddChild(fileList, filePath, fileMessage){ fileList.forEach(file => { if (file.filePath === filePath) { if (Array.isArray(file.children)){ file.children.push(fileMessage) }else { file.children=[fileMessage] } } else if (file.childList.length > 0 && file.filePath.startsWith(filePath)) { dirFileAddChild(file.childList, filePath, fileMessage) } }) } function filterChild(fileList, selectDirKey) { return fileList.filter(file => { if (file.filePath === selectDirKey && file.dirFlag) { return false }else if (file.dirFlag && selectDirKey.startsWith(file.filePath) && Array.isArray(file.childList) && file.childList.length > 0) { filterChild(file.childList, selectDirKey) return true }else { return true } }) } 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) } }) } function updateFileNameChild(fileList, oldFilePath, newFilePath) { fileList.forEach(file => { if (file.filePath === oldFilePath) { file.filePath = newFilePath file.fileName = newFilePath.substring(newFilePath.lastIndexOf("/")+1) return }else if (Array.isArray(file.childList) && file.childList.length > 0 && oldFilePath.startsWith(file.filePath)) { updateFileNameChild(file.childList, oldFilePath, newFilePath) } }) } export const { dirAdd, nextDirAdd, dirRemove, updateFileName, dirFileAdd, dirFileRemove, } = dirMessageSlice.actions export default dirMessageSlice.reducer