assistant-note/src/pages/Note/index.jsx

119 lines
4.2 KiB
React
Raw Normal View History

2024-01-25 02:26:23 -05:00
import React, {useEffect, useRef, useState} from 'react';
2024-01-12 00:13:58 -05:00
import {
UserOutlined,
} from '@ant-design/icons';
2024-01-25 02:26:23 -05:00
import {Layout, Menu, Button, theme, Avatar, Tabs} from 'antd';
2024-01-12 00:13:58 -05:00
import Hlexical from './Hlexical';
import ItemTree from "../../components/ItemTree";
import './index.less'
2024-02-02 01:40:28 -05:00
import {store} from "../../redux/store";
2024-01-25 04:56:53 -05:00
import {isEmpty} from "../../utils/ObjectUtils";
2024-02-03 20:29:00 -05:00
import {useSelector, useDispatch} from "react-redux";
2024-02-03 22:08:49 -05:00
import {addTableBarItem, removeTableBarItem} from "../../redux/tableBarItem_reducer"
2024-01-26 02:41:22 -05:00
2024-01-25 02:26:23 -05:00
const {Header, Sider, Content} = Layout;
2024-01-12 00:13:58 -05:00
const Note = () => {
2024-02-03 20:29:00 -05:00
const dispatch = useDispatch()
2024-01-12 00:13:58 -05:00
const [collapsed, setCollapsed] = useState(false);
// const {
// token: { colorBgContainer },
// } = theme.useToken();
const colorBgContainer = '#800080'
2024-01-25 02:26:23 -05:00
const [activeKey, setActiveKey] = useState();
2024-01-12 00:13:58 -05:00
const newTabIndex = useRef(0);
2024-02-03 20:29:00 -05:00
const items = useSelector(state => state.tableBarItem.data)
const openFile = useSelector(state => state.clickFileMessage.data)
console.log("store.getState().clickFileMessage.data:",openFile)
if (!isEmpty(openFile) && activeKey !== openFile.filePath&&isEmpty(activeKey)) {
setActiveKey(openFile.filePath)
}
if (items.filter(fileItem => fileItem.key === openFile.filePath).length === 0 && !isEmpty(openFile)) {
console.log("items.filter(fileItem => fileItem.key === openFile.filePath)",items)
dispatch(addTableBarItem(
{
label: openFile.fileName,
children: openFile.filePath,
key: openFile.filePath,
2024-01-26 02:41:22 -05:00
}
2024-02-03 20:29:00 -05:00
))
}
2024-01-12 00:13:58 -05:00
const onChange = (newActiveKey) => {
2024-02-03 20:29:00 -05:00
console.log("setActiveKey(newActiveKey)",newActiveKey)
2024-01-12 00:13:58 -05:00
setActiveKey(newActiveKey);
};
const add = () => {
const newActiveKey = `newTab${newTabIndex.current++}`;
2024-02-03 20:29:00 -05:00
// const newPanes = [...items];
// newPanes.push({
// label: 'New Tab',
// children: <div className="HlexicalName"><Hlexical /></div>,
// key: newActiveKey,
// });
// setItems(newPanes);
dispatch(addTableBarItem(
{
label: 'New Tab',
children: "",
key: newActiveKey,
}
));
2024-01-12 00:13:58 -05:00
setActiveKey(newActiveKey);
};
const remove = (targetKey) => {
2024-02-03 22:08:49 -05:00
console.log("remove = (targetKey):",targetKey)
2024-01-12 00:13:58 -05:00
let newActiveKey = activeKey;
let lastIndex = -1;
items.forEach((item, i) => {
if (item.key === targetKey) {
lastIndex = i - 1;
}
});
2024-02-03 22:08:49 -05:00
dispatch(removeTableBarItem(targetKey));
2024-01-12 00:13:58 -05:00
const newPanes = items.filter((item) => item.key !== targetKey);
if (newPanes.length && newActiveKey === targetKey) {
if (lastIndex >= 0) {
newActiveKey = newPanes[lastIndex].key;
} else {
newActiveKey = newPanes[0].key;
}
}
2024-02-03 22:08:49 -05:00
console.log("remove = (newActiveKey):",newActiveKey)
2024-02-03 20:29:00 -05:00
// setItems(newPanes);
2024-01-12 00:13:58 -05:00
setActiveKey(newActiveKey);
};
const onEdit = (targetKey, action) => {
if (action === 'add') {
add();
} else {
remove(targetKey);
}
};
return (
<Layout>
<Sider margin='5' collapsed='true'>
<div>
2024-01-25 02:26:23 -05:00
<Avatar size={60} src='http://localhost/20231114150555.jpg' icon={<UserOutlined/>}/>
2024-01-12 00:13:58 -05:00
</div>
<div>
2024-01-25 02:26:23 -05:00
<p style={{color: colorBgContainer, 'fontSize': 60}}>上善若水</p>
2024-01-12 00:13:58 -05:00
</div>
</Sider>
2024-01-25 02:26:23 -05:00
<Sider trigger={null} collapsedWidth={0} collapsible collapsed={collapsed}>
2024-01-12 00:13:58 -05:00
<ItemTree></ItemTree>
</Sider>
<Layout>
2024-01-23 04:27:34 -05:00
<Tabs
type="editable-card"
onChange={onChange}
activeKey={activeKey}
onEdit={onEdit}
2024-02-03 20:29:00 -05:00
items={items.map(item=>{return {label:item.label,children: <div className="HlexicalName"><Hlexical filePath={item.children}/></div>,key:item.key}})}
2024-01-23 04:27:34 -05:00
/>
</Layout>
2024-01-12 00:13:58 -05:00
</Layout>
);
};
export default Note;