feat:修改dirTree

This commit is contained in:
shixiaohua 2024-03-04 17:38:31 +08:00
parent bf30771d66
commit 337b0bda48
3 changed files with 31 additions and 72 deletions

View File

@ -4,13 +4,14 @@ import {importFile, overWriteFile, saveFileWithName} from "../../../../utils/Fil
import {isEmpty} from "../../../../utils/ObjectUtils";
import {CLEAR_HISTORY_COMMAND} from "lexical";
import {TRANSFORMERS, $convertFromMarkdownString, $convertToMarkdownString,} from "@lexical/markdown";
import {getFileExtByPath, getFileNameByPath} from "../../../../utils/PathOperate";
import {getFileExtByPath, getFileFullNameByPath, getFileNameByPath} from "../../../../utils/PathOperate";
import {updatedSavedFile} from "../../../../redux/tableBarItem_reducer";
import {useDispatch, useSelector} from "react-redux";
import md5 from "md5"
import {message} from "antd";
const {ipcRenderer} = window.require('electron')
import "./ToobarPlugin.less"
import {newFileAdd} from "../../../../redux/dirMessage_reducer";
const SaveFilePlugin=(props)=> {
let activeKey = useSelector(state => state.tableBarItem.activeKey);
const dispatch = useDispatch();
@ -70,6 +71,7 @@ const SaveFilePlugin=(props)=> {
// 修改当前文件名
dispatch(updatedSavedFile({filePath: filePath}))
// 文件目录更新
dispatch(newFileAdd({fileName:getFileFullNameByPath(filePath),dirFlag:false,children:[],filePath: filePath}))
}
})
return

View File

@ -28,7 +28,7 @@ export const dirMessageSlice = createSlice({
},
newFileAdd :(state,action)=>{
console.log("dirMessage:newFileAdd", state, action)
insertNode(state.dirTree,action.payload[0])
insertNode(state.dirTree,action.payload)
},
dirRemove:(state,action)=>{
console.log("dirMessage:dirRemove", state, action)
@ -37,7 +37,7 @@ export const dirMessageSlice = createSlice({
if (isEmpty(state.dirTree.filePath)){
return
}
removeNode(state.dirTree,{fileName:getFileFullNameByPath(selectDirKey),filePath:selectDirKey,dirFlag:true,children:[]})
removeNode(state.dirTree,{"fileName":getFileFullNameByPath(selectDirKey),"filePath":selectDirKey,"dirFlag":true,"children":[]})
},
nextDirAdd: (state, action) => {
console.log("dirMessage:nextDirAdd", state, action)
@ -51,9 +51,11 @@ export const dirMessageSlice = createSlice({
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:getFileFullNameByPath(oldFilePath),filePath:oldFilePath,dirFlag:false,children:[]},
{fileName:getFileFullNameByPath(newFilePath),filePath:newFilePath,dirFlag:false,children:[]}
{"fileName":oldFileName,"filePath":oldFilePath,"dirFlag":false,"children":[]},
{"fileName":newFileName,"filePath":newFilePath,"dirFlag":false,"children":[]}
)
},
dirFileAdd:(state,action)=>{
@ -90,30 +92,6 @@ export const dirMessageSlice = createSlice({
}
})
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,

View File

@ -41,7 +41,14 @@ export function insertNode(root,fileMessage) {
if (!fileMessage.dirFlag){
currentNode.children.push(fileMessage)
} else {
currentNode.children = fileMessage.children
// 过滤调原来就右的文件,只增加新的
if (fileMessage.children.length>0){
let sourceFile = currentNode.children.map(file=>file.filePath);
let newFileList = fileMessage.children.filter(file=>!sourceFile||!sourceFile.includes(file.filePath));
if (newFileList&&newFileList.length>0){
currentNode.children.push(...newFileList)
}
}
}
}
}
@ -50,69 +57,41 @@ export function insertNode(root,fileMessage) {
export function removeNode(root,fileMessage) {
let currentNode = root;
let fileDir;
if (fileMessage.dirFlag){
fileDir=fileMessage.filePath
}else {
fileDir=getFileDirByPath(fileMessage.filePath)
}
let fileDir=getFileDirByPath(fileMessage.filePath);
let fileDirSplit = filePathSplit(fileDir).filter(fileD=>!isObject(fileD)&&fileD!=="");
for (let i = 0; i < fileDirSplit.length; i++) {
if (i===fileDirSplit.length-1) {
if (fileMessage.dirFlag) {
currentNode.children = currentNode.children.filter(file =>
file.fileName !== fileDirSplit[i] && file.dirFlag)
break
} else {
currentNode.children = currentNode.children.filter(file =>
file.fileName !== fileMessage.fileName && !file.dirFlag)
break
}
}
let find = currentNode.children.find(file=>file.dirFlag&&file.fileName===fileDirSplit[i]);
if (!isEmpty(find)){
currentNode=find;
}else {
break
}
if (i===fileDirSplit.length-1) {
currentNode.children = currentNode.children.filter(file =>
file.fileName !== fileMessage.fileName)
}
}
}
export function updateNode(root,fileMessage,newFileMessage) {
let currentNode = root;
let fileDir;
if (fileMessage.dirFlag){
fileDir=fileMessage.filePath
}else {
fileDir=getFileDirByPath(fileMessage.filePath)
}
let fileDir =getFileDirByPath(fileMessage.filePath)
let fileDirSplit = filePathSplit(fileDir).filter(fileD=>!isObject(fileD)&&fileD!=="");
for (let i = 0; i < fileDirSplit.length; i++) {
if (fileMessage.dirFlag){
if (i===fileDirSplit.length-1){
let node =currentNode.children.find(file=>
file.fileName===fileDirSplit[i]&&file.dirFlag);
node.fileName=newFileMessage.fileName
node.filePath=newFileMessage.filePath
node.dirFlag=newFileMessage.dirFlag
node.children=newFileMessage.children
}
}else {
if (i===fileDirSplit.length-1){
let node=currentNode.children.find(file=>
file.fileName===fileMessage.fileName&&!file.dirFlag)
node.fileName=newFileMessage.fileName
node.filePath=newFileMessage.filePath
node.dirFlag=newFileMessage.dirFlag
node.children=newFileMessage.children
}
}
let find = currentNode.children.find(file=>file.dirFlag&&file.fileName===fileDirSplit[i]);
if (!isEmpty(find)){
currentNode=find;
}else {
break
}
if (i===fileDirSplit.length-1) {
let node =currentNode.children.find(file=>
file.fileName===fileMessage.fileName);
node.fileName=newFileMessage.fileName
node.filePath=newFileMessage.filePath
node.dirFlag=newFileMessage.dirFlag
node.children=newFileMessage.children
}
}
}