feat:只有点击文件时打开
This commit is contained in:
parent
34fb22536f
commit
a178134507
|
@ -1,7 +1,8 @@
|
|||
import React, {useEffect, useMemo, useState} from 'react';
|
||||
import { Input, Tree } from 'antd';
|
||||
import {FolderOutlined,FileMarkdownOutlined} from '@ant-design/icons';
|
||||
const { Search } = Input;
|
||||
import {Input, Tree} from 'antd';
|
||||
import {FolderOutlined, FileMarkdownOutlined} from '@ant-design/icons';
|
||||
|
||||
const {Search} = Input;
|
||||
import store from "../../redux/store";
|
||||
import {clickFile} from "../../redux/clickFile_reducer";
|
||||
|
||||
|
@ -11,7 +12,7 @@ const dataList = [];
|
|||
const generateList = (data) => {
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const node = data[i];
|
||||
const { key,title,icon } = node;
|
||||
const {key, title, icon} = node;
|
||||
dataList.push({
|
||||
key,
|
||||
title,
|
||||
|
@ -38,61 +39,62 @@ const getParentKey = (key, tree) => {
|
|||
return parentKey;
|
||||
};
|
||||
|
||||
function generateChildList(fileList){
|
||||
function generateChildList(fileList) {
|
||||
const result = []
|
||||
for (let i = 0; i < fileList.length; i++) {
|
||||
const {fileName,filePath,dirFlag}=fileList[i];
|
||||
const {fileName, filePath, dirFlag} = fileList[i];
|
||||
result.push({
|
||||
"key":filePath,
|
||||
"title":fileName,
|
||||
"icon":<FolderOutlined />,
|
||||
"dirFlag":dirFlag,
|
||||
"children":[]
|
||||
"key": filePath,
|
||||
"title": fileName,
|
||||
"icon": <FolderOutlined/>,
|
||||
"dirFlag": dirFlag,
|
||||
"children": []
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const ItemTree = () => {
|
||||
const [expandedKeys, setExpandedKeys] = useState([]);
|
||||
const [searchValue, setSearchValue] = useState('');
|
||||
const [defaultValueState, setDefaultValueState] = useState([]);
|
||||
const [autoExpandParent, setAutoExpandParent] = useState(true);
|
||||
|
||||
useEffect(()=>{
|
||||
store.subscribe(() =>{
|
||||
useEffect(() => {
|
||||
store.subscribe(() => {
|
||||
let fileDirDate = store.getState().dirMessage.data;
|
||||
console.log("打开目录fileDirDate:",fileDirDate)
|
||||
console.log("打开目录fileDirDate:", fileDirDate)
|
||||
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) {
|
||||
console.log("node:", node)
|
||||
const {fileName, filePath, childList, dirFlag} = node;
|
||||
const childListM = []
|
||||
if (childList.length > 0) {
|
||||
childListM.push(...generateChildList(childList));
|
||||
}
|
||||
if (defaultData.filter(fileMessage=>
|
||||
fileMessage.key===filePath
|
||||
).length===0){
|
||||
if (defaultData.filter(fileMessage =>
|
||||
fileMessage.key === filePath
|
||||
).length === 0) {
|
||||
defaultData.push({
|
||||
"key":filePath,
|
||||
"title":fileName,
|
||||
"icon":<FolderOutlined />,
|
||||
"dirFlag":dirFlag,
|
||||
"children":childListM
|
||||
"key": filePath,
|
||||
"title": fileName,
|
||||
"icon": <FolderOutlined/>,
|
||||
"dirFlag": dirFlag,
|
||||
"children": childListM
|
||||
});
|
||||
}
|
||||
}
|
||||
console.log("Array.from(new Set(defaultData)):",Array.from(new Set(defaultData)))
|
||||
console.log("Array.from(new Set(defaultData)):", Array.from(new Set(defaultData)))
|
||||
setDefaultValueState(Array.from(new Set(defaultData)))
|
||||
})
|
||||
},[])
|
||||
}, [])
|
||||
|
||||
const onExpand = (newExpandedKeys) => {
|
||||
setExpandedKeys(newExpandedKeys);
|
||||
setAutoExpandParent(false);
|
||||
};
|
||||
const onChange = (e) => {
|
||||
const { value } = e.target;
|
||||
const {value} = e.target;
|
||||
const newExpandedKeys = dataList
|
||||
.map((item) => {
|
||||
if (item.title.indexOf(value) > -1) {
|
||||
|
@ -105,10 +107,10 @@ const ItemTree = () => {
|
|||
setSearchValue(value);
|
||||
setAutoExpandParent(true);
|
||||
};
|
||||
const onSelect = (selectedKeys,e) =>{
|
||||
if (e.selected){
|
||||
console.log('onSelect.selectedKeys',selectedKeys,e)
|
||||
store.dispatch(clickFile({"fileName":e.node.title,"filePath":e.node.key}))
|
||||
const onSelect = (selectedKeys, e) => {
|
||||
if (e.selected && !e.node.dirFlag) {
|
||||
console.log('onSelect.selectedKeys', selectedKeys, e)
|
||||
store.dispatch(clickFile({"fileName": e.node.title, "filePath": e.node.key}))
|
||||
}
|
||||
}
|
||||
// const treeData = useMemo(() => {
|
||||
|
@ -155,7 +157,7 @@ const ItemTree = () => {
|
|||
expandedKeys={expandedKeys}
|
||||
// 是否自动展开父节点
|
||||
autoExpandParent={autoExpandParent}
|
||||
showIcon ={true}
|
||||
showIcon={true}
|
||||
// treeNodes 数据,如果设置则不需要手动构造 TreeNode 节点(key 在整个树范围内唯一)
|
||||
treeData={defaultValueState}
|
||||
onSelect={onSelect}
|
||||
|
|
|
@ -11,6 +11,7 @@ import Hlexical from './Hlexical';
|
|||
import ItemTree from "../../components/ItemTree";
|
||||
import './index.less'
|
||||
import store from "../../redux/store";
|
||||
import {isEmpty} from "../../utils/ObjectUtils";
|
||||
const {Header, Sider, Content} = Layout;
|
||||
const Note = () => {
|
||||
const [collapsed, setCollapsed] = useState(false);
|
||||
|
@ -24,7 +25,7 @@ const Note = () => {
|
|||
store.subscribe(() => {
|
||||
const openFile = store.getState().clickFileMessage.data;
|
||||
console.log("store.getState().clickFileMessage.data:",openFile,items)
|
||||
if (items.filter(fileItem => fileItem.key === openFile.filePath).length === 0) {
|
||||
if (items.filter(fileItem => fileItem.key === openFile.filePath).length === 0&& !isEmpty(openFile)) {
|
||||
setItems([...items,
|
||||
{
|
||||
label: openFile.fileName,
|
||||
|
|
|
@ -3,6 +3,7 @@ import { createSlice } from '@reduxjs/toolkit'
|
|||
export const clickFileSlice = createSlice({
|
||||
name: 'clickFile',
|
||||
initialState: {
|
||||
type:"clickFile",
|
||||
data: {}
|
||||
},
|
||||
reducers: {
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
const schema = {
|
||||
"fileId": {
|
||||
type: 'number',
|
||||
maximum: 100,
|
||||
minimum: 1,
|
||||
default: 50
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
export const isEmpty =(object)=>{
|
||||
for(let key in object) {
|
||||
if(object.hasOwnProperty(key))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
Loading…
Reference in New Issue