vben
authored
|
1
|
import { toRaw, ref, nextTick } from 'vue';
|
vben
authored
|
2
|
import { RouteLocationNormalized } from 'vue-router';
|
vben
authored
|
3
|
import { useDesign } from '/@/hooks/web/useDesign';
|
vben
authored
|
4
|
import { useSortable } from '/@/hooks/web/useSortable';
|
vben
authored
|
5
|
import router from '/@/router';
|
vben
authored
|
6
7
|
import { tabStore } from '/@/store/modules/tab';
import { isNullAndUnDef } from '/@/utils/is';
|
Vben
authored
|
8
|
import projectSetting from '/@/settings/projectSetting';
|
vben
authored
|
9
|
|
vben
authored
|
10
11
|
export function initAffixTabs(): string[] {
const affixList = ref<RouteLocationNormalized[]>([]);
|
vben
authored
|
12
13
14
|
/**
* @description: Filter all fixed routes
*/
|
vben
authored
|
15
16
|
function filterAffixTabs(routes: RouteLocationNormalized[]) {
const tabs: RouteLocationNormalized[] = [];
|
vben
authored
|
17
18
19
|
routes &&
routes.forEach((route) => {
if (route.meta && route.meta.affix) {
|
vben
authored
|
20
|
tabs.push(toRaw(route));
|
vben
authored
|
21
22
23
24
25
26
27
28
29
|
}
});
return tabs;
}
/**
* @description: Set fixed tabs
*/
function addAffixTabs(): void {
|
vben
authored
|
30
|
const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as RouteLocationNormalized[]);
|
vben
authored
|
31
|
affixList.value = affixTabs;
|
vben
authored
|
32
|
for (const tab of affixTabs) {
|
vben
authored
|
33
34
35
36
37
|
tabStore.addTabAction(({
meta: tab.meta,
name: tab.name,
path: tab.path,
} as unknown) as RouteLocationNormalized);
|
vben
authored
|
38
39
|
}
}
|
vben
authored
|
40
|
|
vben
authored
|
41
42
43
44
45
|
let isAddAffix = false;
if (!isAddAffix) {
addAffixTabs();
isAddAffix = true;
}
|
Vben
authored
|
46
|
return affixList.value.map((item) => item.meta?.title).filter(Boolean) as string[];
|
vben
authored
|
47
|
}
|
vben
authored
|
48
49
|
export function useTabsDrag(affixTextList: string[]) {
|
Vben
authored
|
50
|
const { multiTabsSetting } = projectSetting;
|
vben
authored
|
51
|
|
vben
authored
|
52
|
const { prefixCls } = useDesign('multiple-tabs');
|
vben
authored
|
53
|
nextTick(() => {
|
vben
authored
|
54
|
if (!multiTabsSetting.canDrag) return;
|
vben
authored
|
55
56
57
58
59
60
61
62
63
|
const el = document.querySelectorAll(`.${prefixCls} .ant-tabs-nav > div`)?.[0] as HTMLElement;
const { initSortable } = useSortable(el, {
filter: (e: ChangeEvent) => {
const text = e?.target?.innerText;
if (!text) return false;
return affixTextList.includes(text);
},
onEnd: (evt) => {
const { oldIndex, newIndex } = evt;
|
vben
authored
|
64
|
|
vben
authored
|
65
66
67
|
if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) {
return;
}
|
vben
authored
|
68
|
|
vben
authored
|
69
70
|
tabStore.commitSortTabs({ oldIndex, newIndex });
},
|
vben
authored
|
71
|
});
|
vben
authored
|
72
|
initSortable();
|
vben
authored
|
73
74
|
});
}
|