feat:点击文件夹自动打开
This commit is contained in:
parent
93bf0deb5f
commit
bac35b5f38
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
import React, {useEffect, useMemo, useState} from 'react';
|
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++) {
|
||||||
|
@ -43,15 +44,15 @@ const getParentKey = (key, tree) => {
|
||||||
function generateChildList(fileList) {
|
function generateChildList(fileList) {
|
||||||
const result = []
|
const result = []
|
||||||
for (let i = 0; i < fileList.length; i++) {
|
for (let i = 0; i < fileList.length; i++) {
|
||||||
const {fileName, filePath, dirFlag,childList} = fileList[i];
|
const {fileName, filePath, dirFlag, childList} = fileList[i];
|
||||||
const childListM = []
|
const childListM = []
|
||||||
if (Array.isArray(childList)&&childList.length > 0) {
|
if (Array.isArray(childList) && childList.length > 0) {
|
||||||
childListM.push(...generateChildList(childList));
|
childListM.push(...generateChildList(childList));
|
||||||
}
|
}
|
||||||
result.push({
|
result.push({
|
||||||
"key": filePath,
|
"key": filePath,
|
||||||
"title": fileName,
|
"title": fileName,
|
||||||
"icon": dirFlag?<FolderOutlined/>:fileName.endsWith(".md")?<FileMarkdownOutlined/>:<FileOutlined />,
|
"icon": dirFlag ? <FolderOutlined/> : fileName.endsWith(".md") ? <FileMarkdownOutlined/> : <FileOutlined/>,
|
||||||
"dirFlag": dirFlag,
|
"dirFlag": dirFlag,
|
||||||
"children": childListM
|
"children": childListM
|
||||||
});
|
});
|
||||||
|
@ -59,48 +60,64 @@ 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/>,
|
"dirFlag": dirFlag,
|
||||||
"dirFlag": dirFlag,
|
"children": childListM
|
||||||
"children": childListM
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData)))
|
|
||||||
defaultValueStateSet.push(...Array.from(new Set(defaultData)))
|
|
||||||
}
|
}
|
||||||
const [defaultValueState, setDefaultValueState] = useState(defaultValueStateSet);
|
return 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;
|
||||||
})
|
})
|
||||||
|
@ -112,21 +129,32 @@ const ItemTree = () => {
|
||||||
const onSelect = (selectedKeys, e) => {
|
const onSelect = (selectedKeys, e) => {
|
||||||
if (e.selected) {
|
if (e.selected) {
|
||||||
console.log('onSelect.selectedKeys', selectedKeys, e)
|
console.log('onSelect.selectedKeys', selectedKeys, e)
|
||||||
if (e.node.dirFlag){
|
if (e.node.dirFlag) {
|
||||||
// 加载目录下一级文件信息
|
// 加载目录下一级文件信息
|
||||||
readDir(e.node.key).then(fileStateList=>{
|
if (!Array.isArray(e.node.children) || e.node.children.length === 0) {
|
||||||
dispatch(nextDirAdd({selectDirKey:e.node.key,fileStateList}))
|
readDir(e.node.key).then(fileStateList => {
|
||||||
})
|
if (Array.isArray(fileStateList[0].childList) && fileStateList[0].childList.length > 0) {
|
||||||
}else {
|
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,
|
||||||
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
|
}))
|
||||||
}
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue