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

109 lines
3.6 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 {
MenuFoldOutlined,
MenuUnfoldOutlined,
UploadOutlined,
UserOutlined,
VideoCameraOutlined,
} 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-01-25 02:26:23 -05:00
import store from "../../redux/store";
2024-01-25 04:56:53 -05:00
import {isEmpty} from "../../utils/ObjectUtils";
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 = () => {
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();
const [items, setItems] = useState([]);
2024-01-12 00:13:58 -05:00
const newTabIndex = useRef(0);
2024-01-25 02:26:23 -05:00
store.subscribe(() => {
const openFile = store.getState().clickFileMessage.data;
console.log("store.getState().clickFileMessage.data:",openFile,items)
2024-01-26 02:41:22 -05:00
if (!isEmpty(openFile)&&activeKey!==openFile.filePath){
setActiveKey(openFile.filePath)
}
2024-01-25 04:56:53 -05:00
if (items.filter(fileItem => fileItem.key === openFile.filePath).length === 0&& !isEmpty(openFile)) {
2024-01-25 02:26:23 -05:00
setItems([...items,
{
label: openFile.fileName,
2024-01-26 02:41:22 -05:00
children: <div className="HlexicalName"><Hlexical filePath={openFile.filePath} /></div>,
2024-01-25 02:26:23 -05:00
key: openFile.filePath,
}
])
}
}
)
2024-01-12 00:13:58 -05:00
const onChange = (newActiveKey) => {
setActiveKey(newActiveKey);
};
const add = () => {
const newActiveKey = `newTab${newTabIndex.current++}`;
const newPanes = [...items];
newPanes.push({
label: 'New Tab',
2024-02-01 04:05:45 -05:00
children: <div className="HlexicalName"><Hlexical /></div>,
2024-01-12 00:13:58 -05:00
key: newActiveKey,
});
setItems(newPanes);
setActiveKey(newActiveKey);
};
const remove = (targetKey) => {
let newActiveKey = activeKey;
let lastIndex = -1;
items.forEach((item, i) => {
if (item.key === targetKey) {
lastIndex = i - 1;
}
});
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;
}
}
setItems(newPanes);
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}
items={items}
/>
</Layout>
2024-01-12 00:13:58 -05:00
</Layout>
);
};
export default Note;