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) { if (!result.canceled) {
console.log('result.filePaths', result.filePaths) console.log('result.filePaths', result.filePaths)
try { try {
mainWindow.webContents.send('openDirectory', readDirLocal(result.filePaths[0])) readDirLocal(result.filePaths[0]).then(dirDirectory=>{
mainWindow.webContents.send('openDirectory', dirDirectory)
})
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }

View File

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

View File

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