assistant-note/src/redux/dirMessage_reducer.js

156 lines
5.9 KiB
JavaScript
Raw Normal View History

2024-02-04 21:59:35 -05:00
import {createSlice} from '@reduxjs/toolkit'
import {isEmpty} from "../utils/ObjectUtils";
2024-03-13 04:21:03 -04:00
import {
fileDirFormat,
filePathSplit,
getFileDirByPath,
getFileFullNameByPath,
getFileNameByPath
} from "../utils/PathOperate";
2024-03-04 03:14:12 -05:00
import {FileTree, insertNode, removeNode, updateNode} from "../utils/FileTree"
import {func} from "prop-types";
2024-02-04 21:59:35 -05:00
/*
2024-02-19 23:38:49 -05:00
fileTitle:文件名
fileName:文件名.文件扩展名
fileDir:文件路径
filePath:文件路径+fileName
2024-03-11 06:28:15 -04:00
fileId:文件id
fileMd5:文件Md5
2024-02-19 23:38:49 -05:00
2024-02-04 21:59:35 -05:00
"fileName": filePath,
"filePath": filePath,
"dirFlag": true,
2024-02-19 23:38:49 -05:00
"children": fileChildList
2024-02-04 21:59:35 -05:00
*/
2024-01-24 04:59:31 -05:00
export const dirMessageSlice = createSlice({
name: 'dirMessage',
initialState: {
2024-03-13 04:21:03 -04:00
// 暂时只存储文件信息,不存贮文件夹信息。
data: new Map([['root','']]),
dirTree:new FileTree("root","/root",true,[],"root",undefined)
2024-01-24 04:59:31 -05:00
},
reducers: {
dirAdd: (state, action) => {
console.log("dirMessage:dirAdd", state, action)
2024-03-04 03:14:12 -05:00
// 添加目录
insertNode(state.dirTree,action.payload[0])
2024-02-22 03:11:01 -05:00
},
newFileAdd :(state,action)=>{
2024-03-04 03:14:12 -05:00
console.log("dirMessage:newFileAdd", state, action)
2024-03-04 04:38:31 -05:00
insertNode(state.dirTree,action.payload)
2024-03-13 04:21:03 -04:00
state.data.set(action.payload.fileId?action.payload.fileId:action.payload.filePath,action.payload)
2024-02-04 21:59:35 -05:00
},
2024-02-19 03:11:55 -05:00
dirRemove:(state,action)=>{
console.log("dirMessage:dirRemove", state, action)
// 获取当前选中的key
let selectDirKey = action.payload.selectDirKey;
2024-03-04 03:14:12 -05:00
if (isEmpty(state.dirTree.filePath)){
return
}
2024-03-04 04:38:31 -05:00
removeNode(state.dirTree,{"fileName":getFileFullNameByPath(selectDirKey),"filePath":selectDirKey,"dirFlag":true,"children":[]})
2024-02-19 03:11:55 -05:00
},
2024-02-04 21:59:35 -05:00
nextDirAdd: (state, action) => {
console.log("dirMessage:nextDirAdd", state, action)
2024-03-04 03:14:12 -05:00
insertNode(state.dirTree,action.payload.fileStateList[0])
2024-02-19 03:11:55 -05:00
},
2024-02-19 04:33:14 -05:00
refreshDir: (state, action) => {
console.log("dirMessage:refreshDir", state, action)
2024-03-04 03:14:12 -05:00
insertNode(state.dirTree,action.payload.fileStateList[0])
2024-02-19 04:33:14 -05:00
},
2024-02-19 03:11:55 -05:00
updateFileName:(state,action)=>{
console.log("dirMessage:updateFileName", state, action)
let newFilePath = action.payload.newFilePath
let oldFilePath = action.payload.oldFilePath
2024-03-04 04:38:31 -05:00
let oldFileName = getFileFullNameByPath(oldFilePath)
let newFileName = getFileFullNameByPath(newFilePath)
2024-03-04 03:14:12 -05:00
updateNode(state.dirTree,
2024-03-04 04:38:31 -05:00
{"fileName":oldFileName,"filePath":oldFilePath,"dirFlag":false,"children":[]},
{"fileName":newFileName,"filePath":newFilePath,"dirFlag":false,"children":[]}
2024-03-04 03:14:12 -05:00
)
2024-02-19 03:11:55 -05:00
},
2024-03-13 04:21:03 -04:00
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
})
},
2024-02-19 03:11:55 -05:00
dirFileAdd:(state,action)=>{
console.log("dirMessage:dirFileAdd", state, action)
2024-02-20 01:57:14 -05:00
let fileDir = action.payload.fileDir
2024-02-19 03:11:55 -05:00
let filePath = action.payload.filePath
let fileName = action.payload.fileName
let fileMessage = {
2024-02-20 01:57:14 -05:00
"fileName": fileName,
"filePath": filePath,
2024-02-19 03:11:55 -05:00
"dirFlag": false,
2024-02-19 23:38:49 -05:00
"children": []
2024-02-19 03:11:55 -05:00
}
2024-03-04 03:14:12 -05:00
insertNode(state.dirTree,fileMessage)
2024-03-13 04:21:03 -04:00
state.data.set(action.payload.fileId?action.payload.fileId:action.payload.filePath,fileMessage)
2024-02-20 01:57:14 -05:00
},
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,
2024-02-22 03:11:01 -05:00
"filePath": filePath,
2024-02-20 01:57:14 -05:00
"dirFlag": true,
"children": []
}
2024-03-04 03:14:12 -05:00
insertNode(state.dirTree,fileMessage)
2024-02-19 03:11:55 -05:00
},
dirFileRemove:(state,action)=>{
2024-02-21 00:28:03 -05:00
console.log("dirMessage:dirFileRemove", state, action)
2024-02-19 03:11:55 -05:00
let filePath = action.payload.filePath;
2024-03-04 03:14:12 -05:00
removeNode(state.dirTree,{fileName:getFileFullNameByPath(filePath),filePath:filePath,dirFlag:false,children:[]})
2024-01-24 04:59:31 -05:00
}
}
})
2024-02-04 21:59:35 -05:00
2024-02-19 03:11:55 -05:00
2024-02-04 21:59:35 -05:00
export const {
dirAdd,
2024-02-22 03:11:01 -05:00
newFileAdd,
2024-02-19 03:11:55 -05:00
nextDirAdd,
dirRemove,
updateFileName,
2024-03-13 04:21:03 -04:00
updateFileMd5,
2024-02-19 03:11:55 -05:00
dirFileAdd,
2024-02-20 01:57:14 -05:00
dirDirAdd,
2024-02-19 03:11:55 -05:00
dirFileRemove,
2024-02-19 04:33:14 -05:00
refreshDir
2024-02-04 21:59:35 -05:00
} = dirMessageSlice.actions
2024-01-24 04:59:31 -05:00
export default dirMessageSlice.reducer