import {createSlice} from '@reduxjs/toolkit' import {isEmpty} from "../utils/ObjectUtils"; import {fileDirFormat, filePathSplit, getFileDirByPath, getFileFullNameByPath} from "../utils/PathOperate"; import {FileTree, insertNode, removeNode, updateNode} from "../utils/FileTree" import {func} from "prop-types"; /* fileTitle:文件名 fileName:文件名.文件扩展名 fileDir:文件路径 filePath:文件路径+fileName "fileName": filePath, "filePath": filePath, "dirFlag": true, "children": fileChildList */ export const dirMessageSlice = createSlice({ name: 'dirMessage', initialState: { data: [], dirTree:new FileTree("root","/root",true,[]) }, reducers: { dirAdd: (state, action) => { console.log("dirMessage:dirAdd", state, action) // 添加目录 insertNode(state.dirTree,action.payload[0]) }, newFileAdd :(state,action)=>{ console.log("dirMessage:newFileAdd", state, action) insertNode(state.dirTree,action.payload[0]) }, dirRemove:(state,action)=>{ console.log("dirMessage:dirRemove", state, action) // 获取当前选中的key let selectDirKey = action.payload.selectDirKey; if (isEmpty(state.dirTree.filePath)){ return } removeNode(state.dirTree,{fileName:getFileFullNameByPath(selectDirKey),filePath:selectDirKey,dirFlag:true,children:[]}) }, nextDirAdd: (state, action) => { console.log("dirMessage:nextDirAdd", state, action) insertNode(state.dirTree,action.payload.fileStateList[0]) }, refreshDir: (state, action) => { console.log("dirMessage:refreshDir", state, action) insertNode(state.dirTree,action.payload.fileStateList[0]) }, updateFileName:(state,action)=>{ console.log("dirMessage:updateFileName", state, action) let newFilePath = action.payload.newFilePath let oldFilePath = action.payload.oldFilePath updateNode(state.dirTree, {fileName:getFileFullNameByPath(oldFilePath),filePath:oldFilePath,dirFlag:false,children:[]}, {fileName:getFileFullNameByPath(newFilePath),filePath:newFilePath,dirFlag:false,children:[]} ) }, dirFileAdd:(state,action)=>{ console.log("dirMessage:dirFileAdd", state, action) let fileDir = action.payload.fileDir let filePath = action.payload.filePath let fileName = action.payload.fileName let fileMessage = { "fileName": fileName, "filePath": filePath, "dirFlag": false, "children": [] } insertNode(state.dirTree,fileMessage) }, dirDirAdd:(state,action)=>{ console.log("dirMessage:dirDirAdd", state, action) let filePath = action.payload.filePath let fileDir = action.payload.fileDir let fileName = action.payload.fileName let fileMessage = { "fileName": fileName, "filePath": filePath, "dirFlag": true, "children": [] } insertNode(state.dirTree,fileMessage) }, dirFileRemove:(state,action)=>{ console.log("dirMessage:dirFileRemove", state, action) let filePath = action.payload.filePath; removeNode(state.dirTree,{fileName:getFileFullNameByPath(filePath),filePath:filePath,dirFlag:false,children:[]}) } } }) function findChild(fileList, action, selectDirKey) { fileList.forEach(file => { if (file.filePath === selectDirKey && file.dirFlag && (isEmpty(file.children) || (Array.isArray(file.children) && file.children.length === 0))) { file.children = action.payload.fileStateList[0].children return } if (file.dirFlag && Array.isArray(file.children) && file.children.length > 0) { findChild(file.children, action, selectDirKey) } }) } function updateFileNameChild(fileList, oldFilePath, newFilePath) { fileList.forEach(file => { if (file.filePath === oldFilePath) { file.filePath = newFilePath file.fileName = getFileFullNameByPath(newFilePath) return }else if (Array.isArray(file.children) && file.children.length > 0 && oldFilePath.startsWith(file.filePath)) { updateFileNameChild(file.children, oldFilePath, newFilePath) } }) } export const { dirAdd, newFileAdd, nextDirAdd, dirRemove, updateFileName, dirFileAdd, dirDirAdd, dirFileRemove, refreshDir } = dirMessageSlice.actions export default dirMessageSlice.reducer