assistant-note/src/redux/dirMessage_reducer.js

205 lines
7.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";
/*
"fileName": filePath,
"filePath": filePath,
"dirFlag": true,
"childList": fileChildList
*/
2024-01-24 04:59:31 -05:00
export const dirMessageSlice = createSlice({
name: 'dirMessage',
initialState: {
2024-02-04 21:59:35 -05:00
data: [],
// selectDirKey:""
2024-01-24 04:59:31 -05:00
},
reducers: {
dirAdd: (state, action) => {
console.log("dirMessage:dirAdd", state, action)
2024-02-04 21:59:35 -05:00
if (action.payload) {
2024-02-06 02:19:12 -05:00
// 新添加进来的目录,要判断是否包含或者被包含,如果包含则统一树结构
let fileFilter = state.data.filter((fileMessage) =>
fileMessage.filePath.startsWith(action.payload[0].filePath)
2024-01-24 21:07:09 -05:00
);
2024-02-06 02:19:12 -05:00
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 {
2024-01-24 21:07:09 -05:00
// 添加进当前目录
2024-02-04 21:59:35 -05:00
state.data = [...new Set([...state.data, ...action.payload])];
console.log('state.data:', state.data)
2024-01-24 21:07:09 -05:00
}
2024-01-24 04:59:31 -05:00
}
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;
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;
}
})
},
2024-02-04 21:59:35 -05:00
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)
}
})
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
// 查找旧文件并且修改文件信息
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;
}
})
2024-01-24 04:59:31 -05:00
}
}
})
2024-02-04 21:59:35 -05:00
2024-02-19 03:11:55 -05:00
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
}
})
}
2024-02-04 21:59:35 -05:00
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)
}
})
}
2024-02-19 03:11:55 -05:00
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)
}
})
}
2024-02-04 21:59:35 -05:00
export const {
dirAdd,
2024-02-19 03:11:55 -05:00
nextDirAdd,
dirRemove,
updateFileName,
dirFileAdd,
dirFileRemove,
2024-02-04 21:59:35 -05:00
} = dirMessageSlice.actions
2024-01-24 04:59:31 -05:00
export default dirMessageSlice.reducer