diff --git a/src/pages/Note/Hlexical/plugins/SaveFilePlugin.js b/src/pages/Note/Hlexical/plugins/SaveFilePlugin.js index c8db020..dad4e79 100644 --- a/src/pages/Note/Hlexical/plugins/SaveFilePlugin.js +++ b/src/pages/Note/Hlexical/plugins/SaveFilePlugin.js @@ -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 diff --git a/src/redux/dirMessage_reducer.js b/src/redux/dirMessage_reducer.js index 72a3f70..c1d337c 100644 --- a/src/redux/dirMessage_reducer.js +++ b/src/redux/dirMessage_reducer.js @@ -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, diff --git a/src/utils/FileTree.js b/src/utils/FileTree.js index d7b9deb..7ffdae5 100644 --- a/src/utils/FileTree.js +++ b/src/utils/FileTree.js @@ -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 + } } }