assistant-note/src/redux/dirMessage_reducer.js

131 lines
4.8 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-04 03:14:12 -05:00
import {fileDirFormat, filePathSplit, getFileDirByPath, getFileFullNameByPath} from "../utils/PathOperate";
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-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-02-04 21:59:35 -05:00
data: [],
2024-03-04 03:14:12 -05:00
dirTree:new FileTree("root","/root",true,[])
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)
insertNode(state.dirTree,action.payload[0])
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
}
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 03:14:12 -05:00
updateNode(state.dirTree,
{fileName:getFileFullNameByPath(oldFilePath),filePath:oldFilePath,dirFlag:false,children:[]},
{fileName:getFileFullNameByPath(newFilePath),filePath:newFilePath,dirFlag:false,children:[]}
)
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-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
function findChild(fileList, action, selectDirKey) {
fileList.forEach(file => {
if (file.filePath === selectDirKey && file.dirFlag &&
2024-02-19 23:38:49 -05:00
(isEmpty(file.children) || (Array.isArray(file.children) && file.children.length === 0))) {
file.children = action.payload.fileStateList[0].children
2024-02-04 21:59:35 -05:00
return
}
2024-02-19 23:38:49 -05:00
if (file.dirFlag && Array.isArray(file.children) && file.children.length > 0) {
findChild(file.children, action, selectDirKey)
2024-02-04 21:59:35 -05:00
}
})
}
2024-02-19 03:11:55 -05:00
function updateFileNameChild(fileList, oldFilePath, newFilePath) {
fileList.forEach(file => {
if (file.filePath === oldFilePath) {
file.filePath = newFilePath
2024-02-19 23:38:49 -05:00
file.fileName = getFileFullNameByPath(newFilePath)
2024-02-19 03:11:55 -05:00
return
2024-02-19 23:38:49 -05:00
}else if (Array.isArray(file.children) && file.children.length > 0 && oldFilePath.startsWith(file.filePath)) {
updateFileNameChild(file.children, oldFilePath, newFilePath)
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,
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