feat:点击文件夹自动打开

This commit is contained in:
shixiaohua 2024-02-05 16:34:41 +08:00
parent 93bf0deb5f
commit bac35b5f38
3 changed files with 73 additions and 48 deletions

View File

@ -61,7 +61,9 @@ exports.menuRebuild = (mainWindow) => {
if (!result.canceled) {
console.log('result.filePaths', result.filePaths)
try {
mainWindow.webContents.send('openDirectory', readDirLocal(result.filePaths[0]))
readDirLocal(result.filePaths[0]).then(dirDirectory=>{
mainWindow.webContents.send('openDirectory', dirDirectory)
})
} catch (err) {
console.error(err);
}

View File

@ -2,12 +2,13 @@ import React, {useEffect, useMemo, useState} from 'react';
import {Input, Tree} from 'antd';
import {FolderOutlined, FileMarkdownOutlined, FileOutlined} from '@ant-design/icons';
import "./index.less"
const {Search} = Input;
import {useSelector, useDispatch} from "react-redux";
import {addTableBarItem} from "../../redux/tableBarItem_reducer";
import {readDir} from "../../utils/File";
import {nextDirAdd} from "../../redux/dirMessage_reducer";
const defaultData = [];
// const defaultData = [];
//
const dataList = [];
const generateList = (data) => {
@ -24,7 +25,7 @@ const generateList = (data) => {
}
}
};
generateList(defaultData);
// generateList(defaultData);
const getParentKey = (key, tree) => {
let parentKey;
for (let i = 0; i < tree.length; i++) {
@ -59,25 +60,23 @@ function generateChildList(fileList) {
return result;
}
const ItemTree = () => {
const [expandedKeys, setExpandedKeys] = useState([]);
const [searchValue, setSearchValue] = useState('');
const dispatch = useDispatch()
const [autoExpandParent, setAutoExpandParent] = useState(true);
/**
* 将文件信息改为树信息
* @param fileDirDate
* @returns {*[]}
*/
const flushTree = (fileDirDate) => {
const defaultValueStateSet = [];
let fileDirDate = useSelector(state => state.dirMessage.data);
if (fileDirDate.length>0){
if (Array.isArray(fileDirDate) && fileDirDate.length > 0) {
for (let i = 0; i < fileDirDate.length; i++) {
const node = fileDirDate[i];
console.log("node:", node)
const {fileName, filePath, childList, dirFlag} = node;
const childListM = []
if (childList.length > 0) {
if (Array.isArray(childList) && childList.length > 0) {
childListM.push(...generateChildList(childList));
}
if (defaultData.filter(fileMessage => fileMessage.key === filePath).length === 0) {
defaultData.push({
defaultValueStateSet.push({
"key": filePath,
"title": fileName,
"icon": <FolderOutlined/>,
@ -86,21 +85,39 @@ const ItemTree = () => {
});
}
}
console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData)))
defaultValueStateSet.push(...Array.from(new Set(defaultData)))
return defaultValueStateSet;
}
const [defaultValueState, setDefaultValueState] = useState(defaultValueStateSet);
const ItemTree = () => {
const [expandedKeys, setExpandedKeys] = useState([]);
const [searchValue, setSearchValue] = useState('');
const dispatch = useDispatch()
const [autoExpandParent, setAutoExpandParent] = useState(true);
let filePath = useSelector(state => state.dirMessage.data);
const [defaultValueState, setDefaultValueState] = useState(flushTree(filePath));
const onExpand = (newExpandedKeys) => {
setExpandedKeys(newExpandedKeys);
setAutoExpandParent(false);
};
const addChildNode = (valueState, fileStateList) => {
valueState.forEach(file => {
if (fileStateList[0].key.startsWith(file.key)){
if (file.key === fileStateList[0].key) {
file.children = fileStateList[0].children
} else if (Array.isArray(file.children) && file.children.length > 0) {
addChildNode(file.children, fileStateList)
}
}
})
}
const onChange = (e) => {
const {value} = e.target;
const newExpandedKeys = dataList
.map((item) => {
if (item.title.indexOf(value) > -1) {
return getParentKey(item.key, defaultData);
return getParentKey(item.key, defaultValueState);
}
return null;
})
@ -114,19 +131,30 @@ const ItemTree = () => {
console.log('onSelect.selectedKeys', selectedKeys, e)
if (e.node.dirFlag) {
//
if (!Array.isArray(e.node.children) || e.node.children.length === 0) {
readDir(e.node.key).then(fileStateList => {
if (Array.isArray(fileStateList[0].childList) && fileStateList[0].childList.length > 0) {
dispatch(nextDirAdd({selectDirKey: e.node.key, fileStateList}))
//
addChildNode(defaultValueState, flushTree(fileStateList))
const result = [...defaultValueState]
console.log("[...defaultValueState]:",result)
setDefaultValueState(result)
}
})
}
//
expandedKeys.push(e.node.key)
setExpandedKeys([...expandedKeys]);
setAutoExpandParent(false);
} else {
//
dispatch(addTableBarItem(
{
dispatch(addTableBarItem({
label: e.node.title,
children: e.node.key,
key: e.node.key,
activeKey: e.node.key
}
))
}))
}
}
}

View File

@ -36,11 +36,6 @@ export const dirMessageSlice = createSlice({
// 便利文件树找到对应的key并加入其中
// 如果包含下级目录则不更新,在刷新中更新。
state.data.forEach(file => {
console.log("file.filePath===selectDirKey && file.dirFlag && file.childList.length===0",
file.filePath,
file.dirFlag,
file.childList.length,
selectDirKey)
if (file.filePath === selectDirKey && file.dirFlag && file.childList.length === 0) {
file.childList.push(action.payload.fileStateList[0].childList)
} else if (file.childList.length > 0) {