1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint-disable react/jsx-key */
import React, { useState, useEffect, useRef, memo } from 'react';
import { RouteContext } from '@ant-design/pro-layout';
import { history } from '@umijs/max';
import Tags from './Tags';
import styles from './index.less';
import { Scrollbars } from 'react-custom-scrollbars';
// tree遍历
function treeForeach(tree, func) {
tree.forEach((data) => {
func(data);
data.children && treeForeach(data.children, func); // 遍历子树
});
}
const TagView = ({ children, home }) => {
const [tagList, setTagList] = useState([]);
const [currentPath, setCurrentPath] = useState();
const routeContextRef = useRef();
useEffect(() => {
if (routeContextRef.current) {
handleOnChange(routeContextRef.current);
}
}, [routeContextRef.current]);
// 初始化 visitedViews,设置首页
const initTags = (routeContext) => {
// console.log(routeContext);
const { menuData } = routeContext;
if (tagList.length === 0 && menuData) {
const allarr = [];
treeForeach(menuData, (node) => {
allarr.push(node);
});
const firstTag = allarr.filter((el) => el.path === location?.hash?.replace('#', ''))[0];
if (firstTag) {
const title = firstTag.name;
const path = firstTag.path;
history.push({ pathname: firstTag.path, query: firstTag.query });
setTagList([
{
title,
path,
children: firstTag.element,
refresh: 0,
active: true,
key: firstTag.key,
},
]);
}
}
};
// 监听路由改变
const handleOnChange = (routeContext) => {
const { currentMenu } = routeContext;
// console.log(tagList);
// tags初始化
if (tagList.length === 0) {
return initTags(routeContext);
}
// 判断是否已打开过该页面
let hasOpen = false;
if (currentMenu.path) {
const tagsCopy = tagList.map((item) => {
if (currentMenu?.path === item.path) {
//console.log(item);
hasOpen = true;
// 刷新浏览器时,重新覆盖当前 path 的 children
// console.log(item);
return { ...item, active: true, children: item.children };
} else {
return { ...item, active: false };
}
});
// 没有该tag时追加一个,并打开这个tag页面
if (!hasOpen) {
const title = routeContext.title || '';
const path = currentMenu?.path;
tagsCopy.push({
title,
path,
children,
refresh: 0,
active: true,
});
}
return setTagList(tagsCopy);
}
};
// 关闭标签
const handleCloseTag = (tag) => {
const tagsCopy = tagList.map((el, i) => ({ ...el }));
// 判断关闭标签是否处于打开状态
tagList.forEach?.((el, i) => {
if (el.path === tag.path && tag.active) {
const next = tagList[i - 1];
next.active = true;
history.push({ pathname: next?.path, query: next?.query });
}
});
setTagList(tagsCopy.filter((el) => el.path !== tag?.path));
};
// 关闭所有标签
const handleCloseAll = () => {
const tagsCopy = tagList.filter((el) => el.path === home);
history.push(home);
setTagList(tagsCopy);
};
// 关闭其他标签
const handleCloseOther = (tag) => {
const tagsCopy = tagList.filter((el) => el.path === home || el.path === tag.path);
history.push({ pathname: tag?.path, query: tag?.query });
setTagList(tagsCopy);
};
const [refresh, setrefresh] = useState(true);
// 刷新选择的标签
const handleRefreshTag = async (tag) => {
// const tagsCopy = tagList.map((item) => {
// if (item.path === tag.path) {
// history.push({ pathname: tag?.path, query: tag?.query });
// return { ...item, refresh: item.refresh + 1, active: true };
// }
// return { ...item, active: false };
// });
// setTagList(tagsCopy);
await setrefresh(false);
setrefresh(true);
};
return (
<>
<RouteContext.Consumer>
{(value) => {
setTimeout(() => {
setCurrentPath(value.currentMenu?.path);
}, 0);
routeContextRef.current = value;
return null;
}}
</RouteContext.Consumer>
<div className={styles.tag_view}>
<div className={styles.tags_container}>
<Tags
tagList={tagList}
closeTag={handleCloseTag}
closeAllTag={handleCloseAll}
closeOtherTag={handleCloseOther}
refreshTag={handleRefreshTag}
home={home}
/>
</div>
</div>
<div className={styles.child}>
<div className={styles.contianbox}>
<Scrollbars
thumbMinSize={10}
autoHide
style={{ width: '100%', height: '100%' }}
hideTracksWhenNotNeeded={true}
>
{refresh && children}
</Scrollbars>
</div>
</div>
</>
);
};
export default TagView;