156 lines
5.9 KiB
JavaScript
156 lines
5.9 KiB
JavaScript
import {createSlice} from '@reduxjs/toolkit'
|
|
import {isEmpty} from "../utils/ObjectUtils";
|
|
import {
|
|
fileDirFormat,
|
|
filePathSplit,
|
|
getFileDirByPath,
|
|
getFileFullNameByPath,
|
|
getFileNameByPath
|
|
} from "../utils/PathOperate";
|
|
import {FileTree, insertNode, removeNode, updateNode} from "../utils/FileTree"
|
|
import {func} from "prop-types";
|
|
/*
|
|
fileTitle:文件名
|
|
fileName:文件名.文件扩展名
|
|
fileDir:文件路径
|
|
filePath:文件路径+fileName
|
|
fileId:文件id
|
|
fileMd5:文件Md5
|
|
|
|
"fileName": filePath,
|
|
"filePath": filePath,
|
|
"dirFlag": true,
|
|
"children": fileChildList
|
|
*/
|
|
export const dirMessageSlice = createSlice({
|
|
name: 'dirMessage',
|
|
initialState: {
|
|
// 暂时只存储文件信息,不存贮文件夹信息。
|
|
data: new Map([['root','']]),
|
|
dirTree:new FileTree("root","/root",true,[],"root",undefined)
|
|
},
|
|
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)
|
|
state.data.set(action.payload.fileId?action.payload.fileId:action.payload.filePath,action.payload)
|
|
},
|
|
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
|
|
let oldFileName = getFileFullNameByPath(oldFilePath)
|
|
let newFileName = getFileFullNameByPath(newFilePath)
|
|
updateNode(state.dirTree,
|
|
{"fileName":oldFileName,"filePath":oldFilePath,"dirFlag":false,"children":[]},
|
|
{"fileName":newFileName,"filePath":newFilePath,"dirFlag":false,"children":[]}
|
|
)
|
|
},
|
|
updateFileMd5:(state,action)=>{
|
|
console.log("dirMessage:updateFileMd5", state, action)
|
|
updateNode(state.dirTree,
|
|
{"filePath":action.payload.filePath},
|
|
{"fileMd5":action.payload.fileMd5})
|
|
console.log("state.data",...state.data)
|
|
let fileId = action.payload.fileId?action.payload.fileId:action.payload.filePath;
|
|
if (state.data.fileIdKey){
|
|
state.data.fileIdKey.fileMd5=action.payload.fileMd5
|
|
}else {
|
|
let newMap = new Map();
|
|
state.data.forEach((value, key) => {
|
|
newMap.set(key, value);
|
|
});
|
|
newMap.set(action.payload.filePath,{
|
|
fileTitle:getFileNameByPath(action.payload.filePath),
|
|
fileName:getFileFullNameByPath(action.payload.filePath),
|
|
fileDir:false,
|
|
children:[],
|
|
filePath:action.payload.filePath,
|
|
fileId:fileId,
|
|
fileMd5:action.payload.fileMd5
|
|
})
|
|
state.data=newMap
|
|
}
|
|
state.data.set(action.payload.filePath,{
|
|
fileTitle:getFileNameByPath(action.payload.filePath),
|
|
fileName:getFileFullNameByPath(action.payload.filePath),
|
|
fileDir:false,
|
|
children:[],
|
|
filePath:action.payload.filePath,
|
|
fileId:fileId,
|
|
fileMd5:action.payload.fileMd5
|
|
})
|
|
},
|
|
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)
|
|
state.data.set(action.payload.fileId?action.payload.fileId:action.payload.filePath,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:[]})
|
|
}
|
|
}
|
|
})
|
|
|
|
|
|
export const {
|
|
dirAdd,
|
|
newFileAdd,
|
|
nextDirAdd,
|
|
dirRemove,
|
|
updateFileName,
|
|
updateFileMd5,
|
|
dirFileAdd,
|
|
dirDirAdd,
|
|
dirFileRemove,
|
|
refreshDir
|
|
} = dirMessageSlice.actions
|
|
|
|
export default dirMessageSlice.reducer
|