assistant-note/src/redux/dirMessage_reducer.js

263 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {createSlice} from '@reduxjs/toolkit'
import {isEmpty} from "../utils/ObjectUtils";
import {fullFileNameFormat, getFileFullNameByPath} from "../utils/PathOperate";
/*
fileTitle:文件名
fileName:文件名.文件扩展名
fileDir:文件路径
filePath:文件路径+fileName
"fileName": filePath,
"filePath": filePath,
"dirFlag": true,
"children": fileChildList
*/
export const dirMessageSlice = createSlice({
name: 'dirMessage',
initialState: {
data: [],
// selectDirKey:""
},
reducers: {
dirAdd: (state, action) => {
console.log("dirMessage:dirAdd", state, action)
if (action.payload) {
// 新添加进来的目录,要判断是否包含或者被包含,如果包含则统一树结构
let fileFilter = state.data.filter((fileMessage) =>
fileMessage.filePath.startsWith(action.payload[0].filePath)
);
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 {
// 添加进当前目录
state.data = [...new Set([...state.data, ...action.payload])];
console.log('state.data:', state.data)
}
}
},
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.children.length > 0 && selectDirKey.startsWith(file.filePath)) {
file.children = filterChild(file.children, selectDirKey)
return true;
}else {
return true;
}
})
},
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.children.length === 0) {
file.children.push(action.payload.fileStateList[0].children)
} else if (file.children.length > 0) {
findChild(file.children, action, selectDirKey)
}
})
},
refreshDir: (state, action) => {
console.log("dirMessage:refreshDir", state, action)
// 获取当前选中的key
let selectDirKey = action.payload.selectDirKey;
// 遍历文件树找到对应的key并加入其中
state.data.forEach(file => {
if (file.filePath === selectDirKey && file.dirFlag) {
file.children=action.payload.fileStateList[0].children
} else if (file.children.length > 0) {
refreshChild(file.children, action, selectDirKey)
}
})
},
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 = getFileFullNameByPath(newFilePath)
} else if (file.children.length > 0 && oldFilePath.startsWith(file.filePath)) {
updateFileNameChild(file.children, oldFilePath, newFilePath)
}
})
},
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": []
}
// 查找旧文件并且修改文件信息
state.data.forEach(file => {
if (file.filePath === fileDir) {
if (Array.isArray(file.children)){
file.children.push(fileMessage)
}else {
file.children=[fileMessage]
}
} else if (file.children.length > 0 && filePath.startsWith(file.filePath)) {
dirFileAddChild(file.children, fileDir, 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": fileName,
"dirFlag": true,
"children": []
}
// 查找旧文件并且修改文件信息
state.data.forEach(file => {
if (file.filePath === fileDir) {
if (Array.isArray(file.children)){
file.children.push(fileMessage)
}else {
file.children=[fileMessage]
}
} else if (file.children.length > 0 && fileDir.startsWith(file.filePath)) {
dirFileAddChild(file.children, fileDir, 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.children.length > 0 && filePath.startsWith(file.filePath)) {
file.children = dirFileRemoveChild(file.children, filePath)
return true;
}else {
return true;
}
})
}
}
})
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.children) && file.children.length > 0) {
dirFileRemoveChild(file.children, selectDirKey)
return true
}else {
return true
}
})
}
function dirFileAddChild(fileList, fileDir, fileMessage){
fileList.forEach(file => {
if (file.filePath === fileDir) {
if (Array.isArray(file.children)){
file.children.push(fileMessage)
}else {
file.children=[fileMessage]
}
} else if (file.children.length > 0 && fileDir.startsWith(file.filePath)) {
dirFileAddChild(file.children, fileDir, 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.children) && file.children.length > 0) {
filterChild(file.children, selectDirKey)
return true
}else {
return true
}
})
}
function refreshChild(fileList, action, selectDirKey) {
fileList.forEach(file => {
if (file.filePath === selectDirKey && file.dirFlag) {
file.children = action.payload.fileStateList[0].children
return
}
if (file.dirFlag && Array.isArray(file.children) && file.children.length > 0) {
refreshChild(file.children, action, selectDirKey)
}
})
}
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,
nextDirAdd,
dirRemove,
updateFileName,
dirFileAdd,
dirDirAdd,
dirFileRemove,
refreshDir
} = dirMessageSlice.actions
export default dirMessageSlice.reducer